fix the logger bug
This commit is contained in:
parent
fae9cad5dc
commit
484b4ff024
|
@ -22,27 +22,6 @@ static file_t *g_file = NULL;
|
|||
|
||||
static int request_stop = 0;
|
||||
|
||||
void check_signal(int signum)
|
||||
{
|
||||
switch (signum)
|
||||
{
|
||||
case SIGTERM:
|
||||
case SIGINT:
|
||||
{
|
||||
d_info("%s received",
|
||||
signum == SIGTERM ? "SIGTERM" : "SIGINT");
|
||||
|
||||
logger_stop();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
d_error("Unknown signal number = %d\n", signum);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
status_t log_file_backup()
|
||||
{
|
||||
status_t rv;
|
||||
|
@ -110,7 +89,7 @@ status_t log_file_backup()
|
|||
return CORE_OK;
|
||||
}
|
||||
|
||||
int logger_start_internal(const char *path)
|
||||
int logger_start(const char *path)
|
||||
{
|
||||
status_t rv;
|
||||
int ret, count = 0;
|
||||
|
@ -239,38 +218,6 @@ int logger_start_internal(const char *path)
|
|||
return 0;
|
||||
}
|
||||
|
||||
int logger_start(const char *path)
|
||||
{
|
||||
pid_t pid;
|
||||
int ret;
|
||||
pid = fork();
|
||||
|
||||
d_assert(pid >= 0, _exit(EXIT_FAILURE), "fork() failed");
|
||||
|
||||
if (pid == 0)
|
||||
{
|
||||
|
||||
/* Child */
|
||||
umask(027);
|
||||
|
||||
signal_unblock(SIGINT);
|
||||
signal_unblock(SIGTERM);
|
||||
core_signal(SIGINT, check_signal);
|
||||
core_signal(SIGTERM, check_signal);
|
||||
|
||||
d_print(" Logging '%s'\n", path);
|
||||
ret = logger_start_internal(path);
|
||||
d_assert(ret == 0, _exit(EXIT_FAILURE),
|
||||
"logger_start() failed\n"
|
||||
"You may need root privileges.");
|
||||
|
||||
_exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/* Parent */
|
||||
return pid;
|
||||
}
|
||||
|
||||
void logger_stop()
|
||||
{
|
||||
request_stop = 1;
|
||||
|
|
86
src/init.c
86
src/init.c
|
@ -1,24 +1,27 @@
|
|||
#define TRACE_MODULE _app
|
||||
#define TRACE_MODULE _app_init
|
||||
|
||||
#include "core_debug.h"
|
||||
#include "core_thread.h"
|
||||
#include "core_net.h"
|
||||
#include "core_signal.h"
|
||||
#include "core_proc.h"
|
||||
#include "core_semaphore.h"
|
||||
|
||||
#include "context.h"
|
||||
#include "logger.h"
|
||||
|
||||
#include "app.h"
|
||||
|
||||
static pid_t logger_pid = 0;
|
||||
static proc_id logger_proc;
|
||||
static semaphore_id logger_sem;
|
||||
static void *PROC_FUNC logger_main(proc_id id, void *data);
|
||||
static void check_signal(int signum);
|
||||
|
||||
status_t app_will_initialize(char *config_path, char *log_path)
|
||||
{
|
||||
status_t rv;
|
||||
int others = 0;
|
||||
|
||||
#if 0
|
||||
core_initialize();
|
||||
#endif
|
||||
context_init();
|
||||
|
||||
rv = context_read_file(config_path);
|
||||
|
@ -27,13 +30,21 @@ status_t app_will_initialize(char *config_path, char *log_path)
|
|||
rv = context_parse_config();
|
||||
if (rv != CORE_OK) return rv;
|
||||
|
||||
others = context_self()->trace_level.others;
|
||||
if (others)
|
||||
{
|
||||
d_trace_level(&_app_init, others);
|
||||
}
|
||||
|
||||
if (log_path)
|
||||
context_self()->log_path = log_path;
|
||||
|
||||
if (context_self()->log_path)
|
||||
{
|
||||
logger_pid = logger_start(context_self()->log_path);
|
||||
d_assert(logger_pid > 0, return -1, "logger_start() failed");
|
||||
d_assert(semaphore_create(&logger_sem, 0) == CORE_OK,
|
||||
return CORE_ERROR, "semaphore_create() failed");
|
||||
rv = proc_create(&logger_proc, logger_main, context_self()->log_path);
|
||||
if (rv != CORE_OK) return rv;
|
||||
}
|
||||
|
||||
if (context_self()->db_uri)
|
||||
|
@ -52,14 +63,63 @@ status_t app_did_initialize(char *config_path, char *log_path)
|
|||
|
||||
void app_will_terminate(void)
|
||||
{
|
||||
if (logger_pid)
|
||||
core_kill(logger_pid, SIGTERM);
|
||||
}
|
||||
|
||||
void app_did_terminate(void)
|
||||
{
|
||||
context_final();
|
||||
#if 0
|
||||
core_terminate();
|
||||
#endif
|
||||
if (context_self()->log_path)
|
||||
{
|
||||
d_assert(semaphore_post(logger_sem) == CORE_OK,,
|
||||
"semaphore_post() failed");
|
||||
proc_delete(logger_proc);
|
||||
}
|
||||
|
||||
context_final();
|
||||
}
|
||||
|
||||
static void *PROC_FUNC logger_main(proc_id id, void *data)
|
||||
{
|
||||
status_t rv;
|
||||
char *path = data;
|
||||
|
||||
umask(027);
|
||||
|
||||
signal_unblock(SIGINT);
|
||||
signal_unblock(SIGTERM);
|
||||
core_signal(SIGINT, check_signal);
|
||||
core_signal(SIGTERM, check_signal);
|
||||
|
||||
d_print(" Logging '%s'\n", path);
|
||||
rv = logger_start(path);
|
||||
if (rv != CORE_OK) return NULL;
|
||||
|
||||
d_assert(semaphore_wait(logger_sem) == CORE_OK, return NULL,
|
||||
"semaphore_wait() failed");
|
||||
d_assert(semaphore_delete(logger_sem) == CORE_OK, return NULL,
|
||||
"semaphore_delete() failed");
|
||||
|
||||
d_trace(1, "LOGGER terminate...done\n");
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static void check_signal(int signum)
|
||||
{
|
||||
switch (signum)
|
||||
{
|
||||
case SIGTERM:
|
||||
case SIGINT:
|
||||
{
|
||||
d_info("%s received",
|
||||
signum == SIGTERM ? "SIGTERM" : "SIGINT");
|
||||
|
||||
logger_stop();
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
d_error("Unknown signal number = %d\n", signum);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue