Changeset 1648bed76d308479cdd5f492ca2a697c4df97812
- Timestamp:
- 02/19/08 05:01:25 (5 years ago)
- git-parent:
- Files:
-
- src/noit_conf.c (modified) (1 diff)
- src/noit_conf.h (modified) (1 diff)
- src/noitd.c (modified) (2 diffs)
- src/sample.conf (modified) (1 diff)
- src/utils/noit_log.c (modified) (9 diffs)
- src/utils/noit_log.h (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/noit_conf.c
r3b3b432 r1648bed 1257 1257 ADD_CMD(tl, "reload", noit_conf_reload, NULL, NULL); 1258 1258 } 1259 1260 void 1261 noit_conf_log_init() { 1262 int i, cnt = 0, o, ocnt = 0; 1263 noit_conf_section_t *log_configs, *outlets; 1264 1265 log_configs = noit_conf_get_sections(NULL, "/noit/logs/log", &cnt); 1266 noitL(noit_stderr, "Found %d /noit/logs/log stanzas\n", cnt); 1267 for(i=0; i<cnt; i++) { 1268 noit_log_stream_t ls; 1269 char name[256], type[256], path[256]; 1270 noit_hash_table *config; 1271 noit_conf_boolean disabled; 1272 1273 if(!noit_conf_get_stringbuf(log_configs[i], "@name", name, sizeof(name))) { 1274 noitL(noit_error, "log section %d does not have a name attribute\n", i+1); 1275 exit(-1); 1276 } 1277 if(!noit_conf_get_stringbuf(log_configs[i], "@type", type, sizeof(type))) { 1278 type[0] = '\0'; 1279 } 1280 if(!noit_conf_get_stringbuf(log_configs[i], "@path", path, sizeof(path))) { 1281 path[0] = '\0'; 1282 } 1283 config = noit_conf_get_hash(log_configs[i], "config/*"); 1284 ls = noit_log_stream_new(name, type[0] ? type : NULL, 1285 path[0] ? path : NULL, config); 1286 if(!ls) { 1287 fprintf(stderr, "Error configuring log: %s[%s:%s]\n", name, type, path); 1288 exit(-1); 1289 } 1290 if(noit_conf_get_boolean(log_configs[i], "@disabled", &disabled)) 1291 if(disabled) 1292 ls->enabled = 0; 1293 1294 outlets = noit_conf_get_sections(log_configs[i], "outlet", &ocnt); 1295 for(o=0; o<ocnt; o++) { 1296 noit_log_stream_t outlet; 1297 char oname[256]; 1298 noit_conf_get_stringbuf(outlets[i], "@name", 1299 oname, sizeof(oname)); 1300 outlet = noit_log_stream_find(oname); 1301 if(!outlet) { 1302 fprintf(stderr, "Cannot find outlet '%s' for %s[%s:%s]\n", oname, 1303 name, type, path); 1304 exit(-1); 1305 } 1306 noit_log_stream_add_stream(ls, outlet); 1307 } 1308 if(outlets) free(outlets); 1309 } 1310 if(log_configs) free(log_configs); 1311 } src/noit_conf.h
re781d1e r1648bed 59 59 noit_console_state_t *state, void *closure); 60 60 61 API_EXPORT(void) noit_conf_log_init(); 62 61 63 #endif src/noitd.c
r08cc6f6 r1648bed 62 62 /* First initialize logging, so we can log errors */ 63 63 noit_log_init(); 64 if(debug) { 65 noit_log_stream_add_stream(noit_debug, noit_stderr); 66 noit_debug->enabled = 1; 67 } 68 else { 69 noit_debug->enabled = 0; 70 } 64 noit_log_stream_add_stream(noit_debug, noit_stderr); 71 65 noit_log_stream_add_stream(noit_error, noit_stderr); 72 66 … … 76 70 fprintf(stderr, "Cannot load config: '%s'\n", config_file); 77 71 } 72 73 /* Reinitialize the logging system now that we have a config */ 74 noit_conf_log_init(); 75 if(debug) 76 noit_debug->enabled = 1; 78 77 79 78 /* Lastly, run through all other system inits */ src/sample.conf
r3b3b432 r1648bed 7 7 </config> 8 8 </eventer> 9 <logs> 10 <log name="error"> 11 <outlet name="stderr" /> 12 </log> 13 <log name="debug" disabled="true"> 14 <outlet name="stderr" /> 15 </log> 16 </logs> 9 17 <modules> 10 18 <directory>./modules</directory> src/utils/noit_log.c
re9e34e9 r1648bed 11 11 #include "utils/noit_log.h" 12 12 #include "utils/noit_hash.h" 13 #include "jlog/jlog.h" 13 14 14 15 static noit_hash_table noit_loggers = NOIT_HASH_EMPTY; 16 static noit_hash_table noit_logops = NOIT_HASH_EMPTY; 15 17 noit_log_stream_t noit_stderr = NULL; 16 18 noit_log_stream_t noit_error = NULL; … … 62 64 }; 63 65 66 static int 67 jlog_logio_open(noit_log_stream_t ls) { 68 jlog_ctx *log = NULL; 69 if(!ls->path) return -1; 70 log = jlog_new(ls->path); 71 if(!log) return -1; 72 if(jlog_ctx_open_writer(log)) { 73 jlog_ctx_close(log); 74 return -1; 75 } 76 ls->op_ctx = log; 77 return 0; 78 } 79 static int 80 jlog_logio_reopen(noit_log_stream_t ls) { 81 return 0; 82 } 83 static int 84 jlog_logio_write(noit_log_stream_t ls, const void *buf, size_t len) { 85 if(!ls->op_ctx) return -1; 86 if(jlog_ctx_write((jlog_ctx *)ls->op_ctx, buf, len) != 0) 87 return -1; 88 return len; 89 } 90 static int 91 jlog_logio_close(noit_log_stream_t ls) { 92 if(ls->op_ctx) { 93 jlog_ctx_close((jlog_ctx *)ls->op_ctx); 94 ls->op_ctx = NULL; 95 } 96 return 0; 97 } 98 static logops_t jlog_logio_ops = { 99 jlog_logio_open, 100 jlog_logio_reopen, 101 jlog_logio_write, 102 jlog_logio_close, 103 }; 104 64 105 void 65 106 noit_log_init() { 66 107 noit_hash_init(&noit_loggers); 67 noit_stderr = noit_log_stream_new_on_fd("stderr", 2); 68 noit_error = noit_log_stream_new("error", NULL); 69 noit_debug = noit_log_stream_new("debug", NULL); 70 } 71 72 noit_log_stream_t 73 noit_log_stream_new_on_fd(const char *name, int fd) { 108 noit_hash_init(&noit_logops); 109 noit_register_logops("file", &posix_logio_ops); 110 noit_register_logops("jlog", &jlog_logio_ops); 111 noit_stderr = noit_log_stream_new_on_fd("stderr", 2, NULL); 112 noit_error = noit_log_stream_new("error", NULL, NULL, NULL); 113 noit_debug = noit_log_stream_new("debug", NULL, NULL, NULL); 114 } 115 116 void 117 noit_register_logops(const char *name, logops_t *ops) { 118 noit_hash_store(&noit_logops, strdup(name), strlen(name), ops); 119 } 120 121 noit_log_stream_t 122 noit_log_stream_new_on_fd(const char *name, int fd, noit_hash_table *config) { 74 123 noit_log_stream_t ls; 75 124 ls = calloc(1, sizeof(*ls)); … … 78 127 ls->op_ctx = (void *)fd; 79 128 ls->enabled = 1; 129 ls->config = config; 80 130 if(noit_hash_store(&noit_loggers, ls->name, strlen(ls->name), ls) == 0) { 81 131 free(ls->name); … … 87 137 88 138 noit_log_stream_t 89 noit_log_stream_new_on_file(const char *path) { 90 noit_log_stream_t ls; 139 noit_log_stream_new_on_file(const char *path, noit_hash_table *config) { 140 return noit_log_stream_new(path, "file", path, config); 141 } 142 143 noit_log_stream_t 144 noit_log_stream_new(const char *name, const char *type, const char *path, 145 noit_hash_table *config) { 146 noit_log_stream_t ls, saved; 147 struct _noit_log_stream tmpbuf; 91 148 ls = calloc(1, sizeof(*ls)); 92 ls->path = strdup(path); 93 ls->ops = &posix_logio_ops; 94 if(ls->ops->openop(ls)) { 95 free(ls->path); 149 ls->name = strdup(name); 150 ls->path = path ? strdup(path) : NULL; 151 ls->enabled = 1; 152 ls->config = config; 153 if(!type) 154 ls->ops = NULL; 155 else if(!noit_hash_retrieve(&noit_logops, type, strlen(type), 156 (void **)&ls->ops)) 157 goto freebail; 158 159 if(ls->ops && ls->ops->openop(ls)) goto freebail; 160 161 saved = noit_log_stream_find(name); 162 if(saved) { 163 memcpy(&tmpbuf, saved, sizeof(*saved)); 164 memcpy(saved, ls, sizeof(*saved)); 165 memcpy(ls, &tmpbuf, sizeof(*saved)); 166 noit_log_stream_free(ls); 167 ls = saved; 168 } 169 else 170 if(noit_hash_store(&noit_loggers, ls->name, strlen(ls->name), ls) == 0) 171 goto freebail; 172 173 return ls; 174 175 freebail: 176 free(ls->name); 177 if(ls->path) free(ls->path); 96 178 free(ls); 97 179 return NULL; 98 }99 ls->name = strdup(ls->path);100 ls->enabled = 1;101 if(noit_hash_store(&noit_loggers, ls->name, strlen(ls->name), ls) == 0) {102 free(ls->path);103 free(ls->name);104 free(ls);105 return NULL;106 }107 return ls;108 }109 110 noit_log_stream_t111 noit_log_stream_new(const char *name, logops_t *ops) {112 noit_log_stream_t ls;113 ls = calloc(1, sizeof(*ls));114 ls->name = strdup(name);115 ls->enabled = 1;116 ls->ops = ops ? ops : &posix_logio_ops;117 if(!ops) ls->op_ctx = (void *)-1;118 if(noit_hash_store(&noit_loggers, ls->name, strlen(ls->name), ls) == 0) {119 free(ls->name);120 free(ls);121 return NULL;122 }123 return ls;124 180 } 125 181 … … 131 187 } 132 188 return NULL; 189 } 190 191 void 192 noit_log_stream_remove(const char *name) { 193 noit_hash_delete(&noit_loggers, name, strlen(name), NULL, NULL); 133 194 } 134 195 … … 172 233 void noit_log_stream_reopen(noit_log_stream_t ls) { 173 234 struct _noit_log_stream_outlet_list *node; 174 ls->ops->reopenop(ls);235 if(ls->ops) ls->ops->reopenop(ls); 175 236 for(node = ls->outlets; node; node = node->next) { 176 237 noit_log_stream_reopen(node->outlet); … … 180 241 void 181 242 noit_log_stream_close(noit_log_stream_t ls) { 182 struct _noit_log_stream_outlet_list *node; 183 ls->ops->closeop(ls); 184 for(node = ls->outlets; node; node = node->next) { 185 noit_log_stream_close(node->outlet); 186 } 243 if(ls->ops) ls->ops->closeop(ls); 187 244 } 188 245 … … 198 255 ls->outlets = node; 199 256 } 257 if(ls->config) { 258 noit_hash_destroy(ls->config, free, free); 259 free(ls->config); 260 } 200 261 free(ls); 201 262 } … … 214 275 if(ls->enabled) { 215 276 int len; 277 if(ls->ops) { 216 278 #ifdef va_copy 217 va_copy(copy, arg);218 len = vsnprintf(buffer, sizeof(buffer), format, copy);219 va_end(copy);279 va_copy(copy, arg); 280 len = vsnprintf(buffer, sizeof(buffer), format, copy); 281 va_end(copy); 220 282 #else 221 len = vsnprintf(buffer, sizeof(buffer), format, arg);283 len = vsnprintf(buffer, sizeof(buffer), format, arg); 222 284 #endif 223 ls->ops->writeop(ls, buffer, len); /* Not much one can do about errors */ 285 ls->ops->writeop(ls, buffer, len); /* Not much one can do about errors */ 286 } 224 287 225 288 for(node = ls->outlets; node; node = node->next) { src/utils/noit_log.h
rcdad681 r1648bed 10 10 #include <pthread.h> 11 11 #include <stdarg.h> 12 #include "utils/noit_hash.h" 12 13 13 14 struct _noit_log_stream_outlet_list { … … 29 30 logops_t *ops; 30 31 void *op_ctx; 32 noit_hash_table *config; 31 33 struct _noit_log_stream_outlet_list *outlets; 32 34 } * noit_log_stream_t; … … 37 39 38 40 API_EXPORT(void) noit_log_init(); 39 API_EXPORT(noit_log_stream_t) noit_log_stream_new(const char *, logops_t *); 40 API_EXPORT(noit_log_stream_t) noit_log_stream_new_on_fd(const char *, int); 41 API_EXPORT(noit_log_stream_t) noit_log_stream_new_on_file(const char *); 41 API_EXPORT(void) noit_register_logops(const char *name, logops_t *ops); 42 API_EXPORT(noit_log_stream_t) 43 noit_log_stream_new(const char *, const char *, const char *, 44 noit_hash_table *); 45 API_EXPORT(noit_log_stream_t) 46 noit_log_stream_new_on_fd(const char *, int, noit_hash_table *); 47 API_EXPORT(noit_log_stream_t) 48 noit_log_stream_new_on_file(const char *, noit_hash_table *); 42 49 API_EXPORT(noit_log_stream_t) noit_log_stream_find(const char *); 50 API_EXPORT(void) noit_log_stream_remove(const char *name); 43 51 API_EXPORT(void) noit_log_stream_add_stream(noit_log_stream_t ls, 44 52 noit_log_stream_t outlet);
