diff --git a/lib/log.c b/lib/log.c index 515cafd7d..1d69c1bcc 100644 --- a/lib/log.c +++ b/lib/log.c @@ -24,14 +24,13 @@ /** The global log instance. */ static struct log *log; - -__attribute__((constructor)) static void setup_default_logger() -{ - static struct log l; - - log_init(&l, V, LOG_ALL); - log_start(&l); -} +static struct log default_log = { + .level = V, + .facilities = LOG_ALL, + .file = NULL, + .path = NULL, + .epoch = { -1 , -1 } +}; /** List of debug facilities as strings */ static const char *facilities_strs[] = { @@ -116,10 +115,10 @@ int log_start(struct log *l) int log_stop(struct log *l) { - assert(l->state == STATE_STARTED); - - if (l->file != stderr && l->file != stdout) - fclose(l->file); + if (l->state == STATE_STARTED) { + if (l->file != stderr && l->file != stdout) + fclose(l->file); + } l->state = STATE_STOPPED; @@ -128,10 +127,12 @@ int log_stop(struct log *l) int log_destroy(struct log *l) { - assert(l->state != STATE_STARTED); + default_log.epoch = l->epoch; + log = NULL; + l->state = STATE_DESTROYED; - + return 0; } @@ -233,7 +234,7 @@ void log_vprint(struct log *l, const char *lvl, const char *fmt, va_list ap) #ifdef ENABLE_OPAL_ASYNC OpalPrint("VILLASnode: %s\n", buf); #endif - fprintf(l->file, "%s\n", buf); + fprintf(l->file ? l->file : stderr, "%s\n", buf); free(buf); } @@ -249,14 +250,14 @@ void debug(long class, const char *fmt, ...) { va_list ap; - int lvl = class & 0xFF; - int fac = class & ~0xFF; + struct log *l = log ? log : &default_log; - assert(log && log->state != STATE_DESTROYED); + int lvl = class & 0xFF; + int fac = class & ~0xFF; - if (((fac == 0) || (fac & log->facilities)) && (lvl <= log->level)) { + if (((fac == 0) || (fac & l->facilities)) && (lvl <= l->level)) { va_start(ap, fmt); - log_vprint(log, LOG_LVL_DEBUG, fmt, ap); + log_vprint(l, LOG_LVL_DEBUG, fmt, ap); va_end(ap); } } @@ -265,10 +266,10 @@ void info(const char *fmt, ...) { va_list ap; - assert(log && log->state != STATE_DESTROYED); - + struct log *l = log ? log : &default_log; + va_start(ap, fmt); - log_vprint(log, LOG_LVL_INFO, fmt, ap); + log_vprint(l, LOG_LVL_INFO, fmt, ap); va_end(ap); } @@ -276,21 +277,21 @@ void warn(const char *fmt, ...) { va_list ap; - assert(log && log->state != STATE_DESTROYED); - + struct log *l = log ? log : &default_log; + va_start(ap, fmt); - log_vprint(log, LOG_LVL_WARN, fmt, ap); + log_vprint(l, LOG_LVL_WARN, fmt, ap); va_end(ap); } void stats(const char *fmt, ...) { va_list ap; - - assert(log && log->state != STATE_DESTROYED); + + struct log *l = log ? log : &default_log; va_start(ap, fmt); - log_vprint(log, LOG_LVL_STATS, fmt, ap); + log_vprint(l, LOG_LVL_STATS, fmt, ap); va_end(ap); } @@ -298,10 +299,10 @@ void error(const char *fmt, ...) { va_list ap; - assert(log && log->state != STATE_DESTROYED); - + struct log *l = log ? log : &default_log; + va_start(ap, fmt); - log_vprint(log, LOG_LVL_ERROR, fmt, ap); + log_vprint(l, LOG_LVL_ERROR, fmt, ap); va_end(ap); die(); @@ -311,14 +312,14 @@ void serror(const char *fmt, ...) { va_list ap; char *buf = NULL; - - assert(log && log->state != STATE_DESTROYED); + + struct log *l = log ? log : &default_log; va_start(ap, fmt); vstrcatf(&buf, fmt, ap); va_end(ap); - log_print(log, LOG_LVL_ERROR, "%s: %m (%u)", buf, errno); + log_print(l, LOG_LVL_ERROR, "%s: %m (%u)", buf, errno); free(buf); die(); @@ -330,8 +331,8 @@ void cerror(config_setting_t *cfg, const char *fmt, ...) char *buf = NULL; const char *file; int line; - - assert(log && log->state != STATE_DESTROYED); + + struct log *l = log ? log : &default_log; va_start(ap, fmt); vstrcatf(&buf, fmt, ap); @@ -342,7 +343,7 @@ void cerror(config_setting_t *cfg, const char *fmt, ...) if (!file) file = config_setting_get_hook(config_root_setting(cfg->config)); - log_print(log, LOG_LVL_ERROR, "%s in %s:%u", buf, file, line); + log_print(l, LOG_LVL_ERROR, "%s in %s:%u", buf, file, line); free(buf); die(); diff --git a/lib/path.c b/lib/path.c index 53065fc97..e6807eb0c 100644 --- a/lib/path.c +++ b/lib/path.c @@ -357,6 +357,9 @@ int path_stop(struct path *p) int path_destroy(struct path *p) { + if (p->state == STATE_DESTROYED) + return 0; + list_destroy(&p->hooks, (dtor_cb_t) hook_destroy, true); list_destroy(&p->destinations, (dtor_cb_t) path_destination_destroy, true); diff --git a/lib/pool.c b/lib/pool.c index 5a68257b5..5deaf09b5 100644 --- a/lib/pool.c +++ b/lib/pool.c @@ -44,7 +44,8 @@ int pool_destroy(struct pool *p) { int ret; - assert(p->state == STATE_INITIALIZED); + if (p->state == STATE_DESTROYED) + return 0; queue_destroy(&p->queue); diff --git a/lib/queue.c b/lib/queue.c index 65b0590ec..5a28ef4fc 100644 --- a/lib/queue.c +++ b/lib/queue.c @@ -68,7 +68,8 @@ int queue_destroy(struct queue *q) { int ret = 0; - assert(q->state == STATE_INITIALIZED); + if (q->state == STATE_DESTROYED) + return 0; ret = memory_free(q->mem, q->buffer, (q->buffer_mask + 1) * sizeof(q->buffer[0])); diff --git a/lib/super_node.c b/lib/super_node.c index e24ba057d..828289236 100644 --- a/lib/super_node.c +++ b/lib/super_node.c @@ -364,7 +364,8 @@ int super_node_start(struct super_node *sn) int super_node_stop(struct super_node *sn) { - assert(sn->state == STATE_STARTED); + if (sn->state != STATE_STARTED) + return 0; info("Stopping paths"); for (size_t i = 0; i < list_length(&sn->paths); i++) { INDENT @@ -380,7 +381,7 @@ int super_node_stop(struct super_node *sn) node_stop(n); } - info("De-initializing node types"); + info("Stopping node types"); for (size_t i = 0; i < list_length(&plugins); i++) { INDENT struct plugin *p = list_at(&plugins, i); if (p->type == PLUGIN_TYPE_NODE) @@ -400,16 +401,16 @@ int super_node_destroy(struct super_node *sn) { assert(sn->state != STATE_DESTROYED); - config_destroy(&sn->cfg); - - web_destroy(&sn->web); - log_destroy(&sn->log); - api_destroy(&sn->api); - list_destroy(&sn->plugins, (dtor_cb_t) plugin_destroy, false); list_destroy(&sn->paths, (dtor_cb_t) path_destroy, true); list_destroy(&sn->nodes, (dtor_cb_t) node_destroy, true); - + + web_destroy(&sn->web); + api_destroy(&sn->api); + log_destroy(&sn->log); + + config_destroy(&sn->cfg); + sn->state = STATE_DESTROYED; return 0; diff --git a/lib/web.c b/lib/web.c index d4f16e7a4..c5e547df7 100644 --- a/lib/web.c +++ b/lib/web.c @@ -193,9 +193,8 @@ int web_start(struct web *w) int web_stop(struct web *w) { - assert(w->state == STATE_STARTED); - - lws_cancel_service(w->context); + if (w->state == STATE_STARTED) + lws_cancel_service(w->context); w->state = STATE_STOPPED; @@ -204,9 +203,11 @@ int web_stop(struct web *w) int web_destroy(struct web *w) { - assert(w->state != STATE_DESTROYED && w->state != STATE_STARTED); + if (w->state == STATE_DESTROYED) + return 0; - lws_context_destroy(w->context); + if (w->context) + lws_context_destroy(w->context); w->state = STATE_DESTROYED;