summaryrefslogtreecommitdiff
path: root/src/lib/spy/spy_main.c
blob: 1e512a0cf27ec71e6a5f71d79d4f258e430efd12 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include "spy_private.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

static int _spy_init_count = 0;
int _spy_log_dom_global = -1;

int SPY_EVENT_LINE = 0;

Spy *
spy_new(void)
{
   Spy *spy;

   spy = calloc(1, sizeof(Spy));
   DBG("spy[%p]", spy);
   return spy;
}

void
spy_free(Spy *spy)
{
   Eina_Inlist *l;
   Spy_File *sf;
   EINA_SAFETY_ON_NULL_RETURN(spy);

   EINA_INLIST_FOREACH_SAFE(spy->files, l, sf)
     spy_file_free(sf);
   free(spy);
}

void
spy_file_free(Spy_File *sf)
{
   Spy_File *tmp;

   EINA_SAFETY_ON_NULL_RETURN(sf);

   EINA_INLIST_FOREACH(sf->spy->files, tmp)
     {
        if (sf != tmp)
          continue;

        sf->spy->files = eina_inlist_remove(sf->spy->files, sf->spy->files);
        break;
     }

   free((char *)sf->name);
   ecore_timer_del(sf->poll.timer);
   eina_strbuf_free(sf->read.buf);
   free(sf);
}

void
spy_file_pause(Spy_File *sf)
{
   EINA_SAFETY_ON_NULL_RETURN(sf);

   sf->poll.pause = EINA_TRUE;
}

void
spy_file_resume(Spy_File *sf)
{
   EINA_SAFETY_ON_NULL_RETURN(sf);

   sf->poll.pause = EINA_FALSE;
}

Spy_File *
spy_file_get(Spy *spy, const char *file)
{
   Spy_File *sf;

   EINA_INLIST_FOREACH(spy->files, sf)
     {
        if (!strcmp(sf->name, file))
          return sf;
     }

   return NULL;
}

Spy_File *
spy_file_new(Spy *spy, const char *file)
{
   Spy_File *sf;
   struct stat st;

   DBG("spy[%p] file[%s]", spy, file);

   if (access(file, R_OK))
     {
        ERR("Failed to access %s : %s", file, strerror(errno));
        return EINA_FALSE;
     }

   sf = calloc(1, sizeof(Spy_File));
   if (!sf)
     {
        ERR("Failed to allocate Spy_File structure");
        return EINA_FALSE;
     }

   sf->name = (const char *)strdup(file);
   if (!sf->name)
     {
        ERR("Failed to dupe string \"%s\"", file);
        goto free_sf;
     }

   if (stat(file, &st))
     {
        ERR("Failed to get size of %s : %s", file, strerror(errno));
        goto free_name;
     }

   sf->read.buf = eina_strbuf_new();
   if (!sf->read.buf)
     {
        ERR("Failed to create stringbuffer");
        goto free_name;
     }

   sf->poll.size = st.st_size;
   sf->poll.timer = ecore_timer_loop_add(0.3, spy_file_poll, sf);
   sf->spy = spy;

   spy->files = eina_inlist_append(spy->files, EINA_INLIST_GET(sf));
   DBG("spy_file[%p] size[%zd]", sf, st.st_size);
   return sf;

free_name:
   free((char *)sf->name);
free_sf:
   free(sf);
   return NULL;
}


int
spy_init(void)
{
   if (++_spy_init_count != 1)
     return _spy_init_count;

   if (!eina_init())
     {
        fprintf(stderr, "Spy can not initialize Eina\n");
        return --_spy_init_count;
     }

   _spy_log_dom_global = eina_log_domain_register("spy", EINA_COLOR_RED);
   if (_spy_log_dom_global < 0)
     {
        EINA_LOG_ERR("Spy can not create a general log domain");
        goto shutdown_eina;
     }

   if (!ecore_init())
     {
        ERR("Can not initialize Ecore");
        goto unregister_log_domain;
     }

   SPY_EVENT_LINE = ecore_event_type_new();

   return _spy_init_count;

unregister_log_domain:
   eina_log_domain_unregister(_spy_log_dom_global);
   _spy_log_dom_global = -1;
shutdown_eina:
   eina_shutdown();
   return --_spy_init_count;
}


int
spy_shutdown(void)
{
   if (_spy_init_count <= 0)
     {
        fprintf(stderr, "Spy init count not greater than 0 in shutdown.");
        return 0;
     }

   if (--_spy_init_count != 0)
     return _spy_init_count;

   ecore_shutdown();
   eina_log_domain_unregister(_spy_log_dom_global);
   _spy_log_dom_global = -1;
   eina_shutdown();
   return _spy_init_count;
}