@cryptotaxi247 / netdata-1 / commits / 603dc5b22

netdata way of logging in procfile

box@home root committed Mar 19, 2015 at 10:21 UTC 603dc5b22e32440f4e46cc107eb37299908dff99
2 files changed +30 -29
src/log.h
+1
@@ -20,6 +20,7 @@
20 #define D_EXIT 0x00002000
21 #define D_CHECKS 0x00004000
22 #define D_NFACCT_LOOP 0x00008000
23 +#define D_PROCFILE 0x00010000
24
25 #define DEBUG (D_WEB_CLIENT_ACCESS|D_LISTENER|D_RRD_STATS)
26 //#define DEBUG 0xffffffff
src/procfile.c
+29 -29
@@ -15,10 +15,10 @@
15 #include <sys/stat.h>
16 #include <sys/mman.h>
17
18 +#include "log.h"
19 #include "procfile.h"
20
21 #define PF_PREFIX "PROCFILE"
21 -int pfdebug = 0;
22
23 // ----------------------------------------------------------------------------
24 // An array of words
@@ -26,14 +26,14 @@ int pfdebug = 0;
26 #define PFWORDS_INCREASE_STEP 200
27
28 pfwords *pfwords_add(pfwords *fw, char *str) {
29 - if(pfdebug) fprintf(stderr, PF_PREFIX ": adding word No %d: '%s'\n", fw->len, str);
29 + debug(D_PROCFILE, PF_PREFIX ": adding word No %d: '%s'\n", fw->len, str);
30
31 if(fw->len == fw->size) {
32 - if(pfdebug) fprintf(stderr, PF_PREFIX ": expanding words\n");
32 + debug(D_PROCFILE, PF_PREFIX ": expanding words\n");
33
34 pfwords *new = realloc(fw, sizeof(pfwords) + (fw->size + PFWORDS_INCREASE_STEP) * sizeof(char *));
35 if(!new) {
36 - fprintf(stderr, PF_PREFIX ": failed to expand words\n");
36 + error(PF_PREFIX ": failed to expand words\n");
37 free(fw);
38 return NULL;
39 }
@@ -47,7 +47,7 @@ pfwords *pfwords_add(pfwords *fw, char *str) {
47 }
48
49 pfwords *pfwords_new(void) {
50 - if(pfdebug) fprintf(stderr, PF_PREFIX ": initializing words\n");
50 + debug(D_PROCFILE, PF_PREFIX ": initializing words\n");
51
52 pfwords *new = malloc(sizeof(pfwords) + PFWORDS_INCREASE_STEP * sizeof(char *));
53 if(!new) return NULL;
@@ -58,12 +58,12 @@ pfwords *pfwords_new(void) {
58 }
59
60 void pfwords_reset(pfwords *fw) {
61 - if(pfdebug) fprintf(stderr, PF_PREFIX ": reseting words\n");
61 + debug(D_PROCFILE, PF_PREFIX ": reseting words\n");
62 fw->len = 0;
63 }
64
65 void pfwords_free(pfwords *fw) {
66 - if(pfdebug) fprintf(stderr, PF_PREFIX ": freeing words\n");
66 + debug(D_PROCFILE, PF_PREFIX ": freeing words\n");
67
68 free(fw);
69 }
@@ -75,14 +75,14 @@ void pfwords_free(pfwords *fw) {
75 #define PFLINES_INCREASE_STEP 10
76
77 pflines *pflines_add(pflines *fl, uint32_t first_word) {
78 - if(pfdebug) fprintf(stderr, PF_PREFIX ": adding line %d at word %d\n", fl->len, first_word);
78 + debug(D_PROCFILE, PF_PREFIX ": adding line %d at word %d\n", fl->len, first_word);
79
80 if(fl->len == fl->size) {
81 - if(pfdebug) fprintf(stderr, PF_PREFIX ": expanding lines\n");
81 + debug(D_PROCFILE, PF_PREFIX ": expanding lines\n");
82
83 pflines *new = realloc(fl, sizeof(pflines) + (fl->size + PFLINES_INCREASE_STEP) * sizeof(ffline));
84 if(!new) {
85 - fprintf(stderr, PF_PREFIX ": failed to expand lines\n");
85 + error(PF_PREFIX ": failed to expand lines\n");
86 free(fl);
87 return NULL;
88 }
@@ -97,7 +97,7 @@ pflines *pflines_add(pflines *fl, uint32_t first_word) {
97 }
98
99 pflines *pflines_new(void) {
100 - if(pfdebug) fprintf(stderr, PF_PREFIX ": initializing lines\n");
100 + debug(D_PROCFILE, PF_PREFIX ": initializing lines\n");
101
102 pflines *new = malloc(sizeof(pflines) + PFLINES_INCREASE_STEP * sizeof(ffline));
103 if(!new) return NULL;
@@ -108,13 +108,13 @@ pflines *pflines_new(void) {
108 }
109
110 void pflines_reset(pflines *fl) {
111 - if(pfdebug) fprintf(stderr, PF_PREFIX ": reseting lines\n");
111 + debug(D_PROCFILE, PF_PREFIX ": reseting lines\n");
112
113 fl->len = 0;
114 }
115
116 void pflines_free(pflines *fl) {
117 - if(pfdebug) fprintf(stderr, PF_PREFIX ": freeing lines\n");
117 + debug(D_PROCFILE, PF_PREFIX ": freeing lines\n");
118
119 free(fl);
120 }
@@ -131,7 +131,7 @@ void pflines_free(pflines *fl) {
131 #define PF_CHAR_IS_WORD 2
132
133 void procfile_close(procfile *ff) {
134 - if(pfdebug) fprintf(stderr, PF_PREFIX ": Closing file '%s'. Reason: %s\n", ff->filename, strerror(errno));
134 + debug(D_PROCFILE, PF_PREFIX ": Closing file '%s'. Reason: %s\n", ff->filename, strerror(errno));
135
136 if(ff->lines) pflines_free(ff->lines);
137 if(ff->words) pfwords_free(ff->words);
@@ -141,7 +141,7 @@ void procfile_close(procfile *ff) {
141 }
142
143 procfile *procfile_parser(procfile *ff) {
144 - if(pfdebug) fprintf(stderr, PF_PREFIX ": Parsing file '%s'\n", ff->filename);
144 + debug(D_PROCFILE, PF_PREFIX ": Parsing file '%s'\n", ff->filename);
145
146 char *s = ff->data, *e = ff->data, *t = ff->data;
147 uint32_t l = 0, w = 0;
@@ -181,7 +181,7 @@ procfile *procfile_parser(procfile *ff) {
181 ff->lines->lines[l].words++;
182 w++;
183
184 - if(pfdebug) fprintf(stderr, PF_PREFIX ": ended line %d with %d words\n", l, ff->lines->lines[l].words);
184 + debug(D_PROCFILE, PF_PREFIX ": ended line %d with %d words\n", l, ff->lines->lines[l].words);
185
186 ff->lines = pflines_add(ff->lines, w);
187 if(!ff->lines) goto cleanup;
@@ -214,24 +214,24 @@ procfile *procfile_parser(procfile *ff) {
214 return ff;
215
216 cleanup:
217 - fprintf(stderr, PF_PREFIX ": Failed to parse file '%s'. Reason: %s\n", ff->filename, strerror(errno));
217 + error(PF_PREFIX ": Failed to parse file '%s'. Reason: %s\n", ff->filename, strerror(errno));
218 procfile_close(ff);
219 return NULL;
220 }
221
222 procfile *procfile_readall(procfile *ff) {
223 - if(pfdebug) fprintf(stderr, PF_PREFIX ": Reading file '%s'.\n", ff->filename);
223 + debug(D_PROCFILE, PF_PREFIX ": Reading file '%s'.\n", ff->filename);
224
225 ssize_t s = 0, r = ff->size, x = ff->size;
226 ff->len = 0;
227
228 while(r == x) {
229 if(s) {
230 - if(pfdebug) fprintf(stderr, PF_PREFIX ": Expanding data buffer for file '%s'.\n", ff->filename);
230 + debug(D_PROCFILE, PF_PREFIX ": Expanding data buffer for file '%s'.\n", ff->filename);
231
232 procfile *new = realloc(ff, sizeof(procfile) + ff->size + PROCFILE_INCREMENT_BUFFER);
233 if(!new) {
234 - fprintf(stderr, PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
234 + error(PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
235 procfile_close(ff);
236 return NULL;
237 }
@@ -242,7 +242,7 @@ procfile *procfile_readall(procfile *ff) {
242
243 r = read(ff->fd, &ff->data[s], ff->size - s);
244 if(r == -1) {
245 - fprintf(stderr, PF_PREFIX ": Cannot read from file '%s'. Reason: %s\n", ff->filename, strerror(errno));
245 + error(PF_PREFIX ": Cannot read from file '%s'. Reason: %s\n", ff->filename, strerror(errno));
246 procfile_close(ff);
247 return NULL;
248 }
@@ -252,7 +252,7 @@ procfile *procfile_readall(procfile *ff) {
252 }
253
254 if(lseek(ff->fd, 0, SEEK_SET) == -1) {
255 - fprintf(stderr, PF_PREFIX ": Cannot rewind on file '%s'.\n", ff->filename);
255 + error(PF_PREFIX ": Cannot rewind on file '%s'.\n", ff->filename);
256 procfile_close(ff);
257 return NULL;
258 }
@@ -266,17 +266,17 @@ procfile *procfile_readall(procfile *ff) {
266 }
267
268 procfile *procfile_open(const char *filename, const char *separators) {
269 - if(pfdebug) fprintf(stderr, PF_PREFIX ": Opening file '%s'\n", filename);
269 + debug(D_PROCFILE, PF_PREFIX ": Opening file '%s'\n", filename);
270
271 int fd = open(filename, O_RDONLY, 0666);
272 if(fd == -1) {
273 - fprintf(stderr, PF_PREFIX ": Cannot open file '%s'. Reason: %s\n", filename, strerror(errno));
273 + error(PF_PREFIX ": Cannot open file '%s'. Reason: %s\n", filename, strerror(errno));
274 return NULL;
275 }
276
277 procfile *ff = malloc(sizeof(procfile) + PROCFILE_INITIAL_BUFFER);
278 if(!ff) {
279 - fprintf(stderr, PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
279 + error(PF_PREFIX ": Cannot allocate memory for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
280 close(fd);
281 return NULL;
282 }
@@ -292,7 +292,7 @@ procfile *procfile_open(const char *filename, const char *separators) {
292 ff->words = pfwords_new();
293
294 if(!ff->lines || !ff->words) {
295 - fprintf(stderr, PF_PREFIX ": Cannot initialize parser for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
295 + error(PF_PREFIX ": Cannot initialize parser for file '%s'. Reason: %s\n", ff->filename, strerror(errno));
296 procfile_close(ff);
297 return NULL;
298 }
@@ -320,16 +320,16 @@ void procfile_print(procfile *ff) {
320 uint32_t words, w;
321 char *s;
322
323 - fprintf(stderr, "File '%s' with %d lines and %d words\n", ff->filename, ff->lines->len, ff->words->len);
323 + debug(D_PROCFILE, "File '%s' with %d lines and %d words\n", ff->filename, ff->lines->len, ff->words->len);
324
325 for(l = 0; l < lines ;l++) {
326 words = procfile_linewords(ff, l);
327
328 - fprintf(stderr, " line %d starts at word %d and has %d words\n", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
328 + debug(D_PROCFILE, " line %d starts at word %d and has %d words\n", l, ff->lines->lines[l].first, ff->lines->lines[l].words);
329
330 for(w = 0; w < words ;w++) {
331 s = procfile_lineword(ff, l, w);
332 - fprintf(stderr, " [%d.%d] '%s'\n", l, w, s);
332 + debug(D_PROCFILE, " [%d.%d] '%s'\n", l, w, s);
333 }
334 }
335 }