Skip to content

Commit

Permalink
refactor(log,conf): initialize log without conf file and installation…
Browse files Browse the repository at this point in the history
… directory

Especially for the case where the installation directory does not exist during CMake installation.
  • Loading branch information
Water-Melon committed May 12, 2024
1 parent dadd3b9 commit f3eba40
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 24 deletions.
3 changes: 3 additions & 0 deletions docs/Melon Developer Guide.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
15 changes: 15 additions & 0 deletions docs/book/cn/conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/book/cn/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
15 changes: 15 additions & 0 deletions docs/book/en/conf.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions docs/book/en/log.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 5 additions & 0 deletions include/mln_conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/mln_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
44 changes: 21 additions & 23 deletions src/mln_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions t/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit f3eba40

Please sign in to comment.