diff --git a/docs/Melon Developer Guide.txt b/docs/Melon Developer Guide.txt index 89dc897..c4244b9 100644 --- a/docs/Melon Developer Guide.txt +++ b/docs/Melon Developer Guide.txt @@ -906,6 +906,9 @@ Their definitions can be found in melon/include/mln_types.h. f) void mln_conf_hook_unset(mln_conf_hook_t *hook); This interface is used to unset a callback function which has been set in the global reload chain. + g) mln_conf_is_empty(cf); + Check if the configuration is empty. + 11) Event Just like libevent, Melon's event module also integrates epoll, kqueue and select. It supports three kinds of event: timer event, signal event and file descriptor event. diff --git a/docs/book/cn/conf.md b/docs/book/cn/conf.md index 66124b1..3d7ab1b 100644 --- a/docs/book/cn/conf.md +++ b/docs/book/cn/conf.md @@ -293,6 +293,21 @@ mln_u32_t mln_conf_arg_num(mln_conf_cmd_t *cc); +#### mln_conf_is_empty + +```c +mln_conf_is_empty(cf); +``` + +描述:检查配置是否为空。 + +返回值: + +- `0` - 非空 +- `非0` - 空 + + + ### 示例 ```c diff --git a/docs/book/cn/log.md b/docs/book/cn/log.md index 51c3466..340f667 100644 --- a/docs/book/cn/log.md +++ b/docs/book/cn/log.md @@ -140,6 +140,7 @@ mln_logger_t mln_log_logger_get(void); int main(int argc, char *argv[]) { mln_log(debug, "This will be outputted to stderr\n"); + mln_conf_load(); mln_log_init(NULL); mln_log(debug, "This will be outputted to stderr and log file\n"); return 0; diff --git a/docs/book/en/conf.md b/docs/book/en/conf.md index bf22b3a..c682d51 100644 --- a/docs/book/en/conf.md +++ b/docs/book/en/conf.md @@ -291,6 +291,21 @@ Return value: the number of command item parameters +#### mln_conf_is_empty + +```c +mln_conf_is_empty(cf); +``` + +Description: Check if the configuration is empty. + +Return values: + +- `0` - Not empty +- `Non-zero` - Empty + + + ### Example ```c diff --git a/docs/book/en/log.md b/docs/book/en/log.md index 2d602c2..a37521d 100644 --- a/docs/book/en/log.md +++ b/docs/book/en/log.md @@ -140,6 +140,7 @@ Return value: Returns the current log processing function pointer int main(int argc, char *argv[]) { mln_log(debug, "This will be outputted to stderr\n"); + mln_conf_load(); mln_log_init(NULL); mln_log(debug, "This will be outputted to stderr and log file\n"); return 0; diff --git a/include/mln_conf.h b/include/mln_conf.h index 1ed81cb..1d64468 100644 --- a/include/mln_conf.h +++ b/include/mln_conf.h @@ -86,6 +86,11 @@ typedef struct mln_conf_hook_s { /* * mln_conf_load should be called before any pthread_create */ + +#define mln_conf_is_empty(cf) ((cf) == NULL || \ + (mln_rbtree_node_num((cf)->domain) <= 1 && \ + mln_rbtree_node_num(((mln_conf_domain_t *)(mln_rbtree_node_data_get(mln_rbtree_root((cf)->domain))))->cmd) == 0)) + extern int mln_conf_load(void); extern int mln_conf_reload(void); extern void mln_conf_free(void); diff --git a/src/mln_conf.c b/src/mln_conf.c index 35bea5d..19a77fb 100644 --- a/src/mln_conf.c +++ b/src/mln_conf.c @@ -377,7 +377,7 @@ static inline mln_conf_t *mln_conf_init(void) return NULL; } } else { - fprintf(stderr, "[Warn] Configuration file [%s] not found.\n", conf_file_path); + fprintf(stderr, "[Warn] Configuration file [%s] not found, default configuration will be used.\n", conf_file_path); cf->lex = NULL; } diff --git a/src/mln_log.c b/src/mln_log.c index 827f796..449a748 100644 --- a/src/mln_log.c +++ b/src/mln_log.c @@ -102,39 +102,37 @@ mln_logger_t mln_log_logger_get(void) */ int mln_log_init(mln_conf_t *cf) { - mln_u32_t in_daemon = 0; + mln_u32_t in_daemon = 0, init = 0; mln_conf_domain_t *cd; mln_conf_cmd_t *cc; mln_conf_item_t *ci; mln_log_t *log = &g_log; if (log->init) return 0; - if (cf == NULL) { - if (mln_conf_load() < 0) { - fprintf(stderr, "load configuration failed.\n"); + if (cf == NULL) cf = mln_conf(); + if (mln_conf_is_empty(cf)) + fprintf(stdout, "[WARN] Load configuration failed. Logger won't be initialized.\n"); + else { + if ((cd = cf->search(cf, "main")) == NULL) { + fprintf(stderr, "No such domain named 'main'\n"); return -1; } - cf = mln_conf(); - } - - if ((cd = cf->search(cf, "main")) == NULL) { - fprintf(stderr, "No such domain named 'main'\n"); - return -1; - } - if ((cc = cd->search(cd, "daemon")) != NULL) { - if ((ci = cc->search(cc, 1)) == NULL) { - fprintf(stderr, "Command 'daemon' need a parameter.\n"); - return -1; - } - if (ci->type != CONF_BOOL) { - fprintf(stderr, "Parameter type of command 'daemon' error.\n"); - return -1; + if ((cc = cd->search(cd, "daemon")) != NULL) { + if ((ci = cc->search(cc, 1)) == NULL) { + fprintf(stderr, "Command 'daemon' need a parameter.\n"); + return -1; + } + if (ci->type != CONF_BOOL) { + fprintf(stderr, "Parameter type of command 'daemon' error.\n"); + return -1; + } + if (ci->val.b) in_daemon = 1; } - if (ci->val.b) in_daemon = 1; + init = 1; } log->in_daemon = in_daemon; - log->init = 1; + log->init = init; log->level = none; int ret = 0; #if !defined(MSVC) @@ -169,7 +167,7 @@ int mln_log_init(mln_conf_t *cf) static int mln_log_get_log(mln_log_t *log, mln_conf_t *cf, int is_init) { - if (cf == NULL) return -1; + if (mln_conf_is_empty(cf)) return 0; mln_conf_domain_t *cd; mln_conf_cmd_t *cc; @@ -314,7 +312,7 @@ void mln_log_destroy(void) */ static int mln_log_set_level(mln_log_t *log, mln_conf_t *cf, int is_init) { - if (cf == NULL) return 0; + if (mln_conf_is_empty(cf)) return 0; mln_conf_domain_t *cd = cf->search(cf, "main"); if (cd == NULL) { diff --git a/t/log.c b/t/log.c index 88453fd..75d3743 100644 --- a/t/log.c +++ b/t/log.c @@ -4,6 +4,7 @@ int main(int argc, char *argv[]) { mln_log(debug, "This will be outputted to stderr\n"); + mln_conf_load(); assert(mln_log_init(NULL) == 0); mln_log(debug, "This will be outputted to stderr and log file\n"); mln_log_destroy();