From d263acfaa206740cea4bbc6100b39a3dbcb440d5 Mon Sep 17 00:00:00 2001 From: yuqing Date: Sat, 9 Aug 2014 12:01:18 +0800 Subject: [PATCH] use newest logger from libfastcommon --- HISTORY | 4 +++- client/Makefile.in | 15 ++++++++++--- common/logger.c | 47 +++++++++++++++++++++++++++++++++++++++-- common/logger.h | 24 +++++++++++++++++++++ storage/fdfs_storaged.c | 14 ++---------- tracker/fdfs_trackerd.c | 14 ++---------- 6 files changed, 88 insertions(+), 30 deletions(-) diff --git a/HISTORY b/HISTORY index 62b3ea2..2aaa28e 100644 --- a/HISTORY +++ b/HISTORY @@ -1,6 +1,8 @@ -Version 5.03 2014-08-02 +Version 5.03 2014-08-11 * network send and recv retry when error EINTR happen + * support mac OS Darwin + * use newest logger from libfastcommon Version 5.02 2014-07-20 * corect README spell mistake diff --git a/client/Makefile.in b/client/Makefile.in index a27abb8..1a9310c 100644 --- a/client/Makefile.in +++ b/client/Makefile.in @@ -16,7 +16,10 @@ FAST_STATIC_OBJS = ../common/hash.o ../common/chain.o \ ../common/base64.o ../common/sched_thread.o \ ../common/http_func.o ../common/md5.o \ ../common/pthread_func.o ../common/local_ip_func.o \ - ../common/avl_tree.o ../common/connection_pool.o + ../common/avl_tree.o ../common/connection_pool.o \ + ../common/ioevent.o ../common/ioevent_loop.o \ + ../common/fast_task_queue.o ../common/fast_timer.o \ + ../common/process_ctrl.o ../common/fast_mblock.o FDFS_STATIC_OBJS = ../common/fdfs_global.o ../common/fdfs_http_shared.o \ ../common/mime_file_parser.o ../tracker/tracker_proto.o \ @@ -33,7 +36,10 @@ FAST_SHARED_OBJS = ../common/hash.lo ../common/chain.lo \ ../common/base64.lo ../common/sched_thread.lo \ ../common/http_func.lo ../common/md5.lo \ ../common/pthread_func.lo ../common/local_ip_func.lo \ - ../common/avl_tree.lo ../common/connection_pool.lo + ../common/avl_tree.lo ../common/connection_pool.lo \ + ../common/ioevent.lo ../common/ioevent_loop.lo \ + ../common/fast_task_queue.lo ../common/fast_timer.lo \ + ../common/process_ctrl.lo ../common/fast_mblock.lo FDFS_SHARED_OBJS = ../common/fdfs_global.lo ../common/fdfs_http_shared.lo \ ../common/mime_file_parser.lo ../tracker/tracker_proto.lo \ @@ -49,7 +55,10 @@ FAST_HEADER_FILES = ../common/common_define.h ../common/hash.h \ ../common/sockopt.h ../common/sched_thread.h \ ../common/http_func.h ../common/md5.h ../common/_os_bits.h \ ../common/local_ip_func.h ../common/avl_tree.h \ - ../common/connection_pool.h + ../common/connection_pool.h ../common/ioevent.h \ + ../common/ioevent_loop.h ../common/fast_task_queue.h \ + ../common/fast_timer.h ../common/process_ctrl.h \ + ../common/fast_mblock.h FDFS_HEADER_FILES = ../common/fdfs_define.h ../common/fdfs_global.h \ ../common/mime_file_parser.h ../common/fdfs_http_shared.h \ diff --git a/common/logger.c b/common/logger.c index 9b4a963..38a5480 100644 --- a/common/logger.c +++ b/common/logger.c @@ -63,6 +63,18 @@ int log_init() return log_init_ex(&g_log_context); } +int log_init2() +{ + int result; + if ((result=log_init()) != 0) { + return result; + } + + log_take_over_stderr(); + log_take_over_stdout(); + return 0; +} + int log_init_ex(LogContext *pContext) { int result; @@ -97,17 +109,33 @@ static int log_open(LogContext *pContext) O_CREAT | O_APPEND, 0644)) < 0) { fprintf(stderr, "open log file \"%s\" to write fail, " \ - "errno: %d, error info: %s", \ + "errno: %d, error info: %s\n", \ pContext->log_filename, errno, STRERROR(errno)); pContext->log_fd = STDERR_FILENO; return errno != 0 ? errno : EACCES; } + if (pContext->take_over_stderr) { + if (dup2(pContext->log_fd, STDERR_FILENO) < 0) { + fprintf(stderr, "file: "__FILE__", line: %d, " + "call dup2 fail, errno: %d, error info: %s\n", + __LINE__, errno, STRERROR(errno)); + } + } + + if (pContext->take_over_stdout) { + if (dup2(pContext->log_fd, STDOUT_FILENO) < 0) { + fprintf(stderr, "file: "__FILE__", line: %d, " + "call dup2 fail, errno: %d, error info: %s\n", + __LINE__, errno, STRERROR(errno)); + } + } + pContext->current_size = lseek(pContext->log_fd, 0, SEEK_END); if (pContext->current_size < 0) { fprintf(stderr, "lseek file \"%s\" fail, " \ - "errno: %d, error info: %s", \ + "errno: %d, error info: %s\n", \ pContext->log_filename, errno, STRERROR(errno)); return errno != 0 ? errno : EACCES; } @@ -137,6 +165,11 @@ int log_set_prefix_ex(LogContext *pContext, const char *base_path, \ int log_set_filename_ex(LogContext *pContext, const char *log_filename) { + if (log_filename == NULL) { + fprintf(stderr, "file: "__FILE__", line: %d, " \ + "log_filename is NULL!\n", __LINE__); + return EINVAL; + } snprintf(pContext->log_filename, MAX_PATH_SIZE, "%s", log_filename); return log_open(pContext); } @@ -177,6 +210,16 @@ void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_call } } +void log_take_over_stderr_ex(LogContext *pContext) +{ + pContext->take_over_stderr = true; +} + +void log_take_over_stdout_ex(LogContext *pContext) +{ + pContext->take_over_stdout = true; +} + void log_destroy_ex(LogContext *pContext) { if (pContext->log_fd >= 0 && pContext->log_fd != STDERR_FILENO) diff --git a/common/logger.h b/common/logger.h index 48050e0..cf5ed8e 100644 --- a/common/logger.h +++ b/common/logger.h @@ -61,6 +61,12 @@ typedef struct log_context /* if rotate the access log */ bool rotate_immediately; + /* if stderr to the log file */ + bool take_over_stderr; + + /* if stdout to the log file */ + bool take_over_stdout; + /* time precision */ char time_precision; @@ -88,6 +94,11 @@ extern LogContext g_log_context; */ int log_init(); +/** init function using global log context, take over stderr and stdout + * return: 0 for success, != 0 fail +*/ +int log_init2(); + #define log_set_prefix(base_path, filename_prefix) \ log_set_prefix_ex(&g_log_context, base_path, filename_prefix) @@ -96,6 +107,9 @@ int log_init(); #define log_set_cache(bLogCache) log_set_cache_ex(&g_log_context, bLogCache) +#define log_take_over_stderr() log_take_over_stderr_ex(&g_log_context) +#define log_take_over_stdout() log_take_over_stdout_ex(&g_log_context) + #define log_destroy() log_destroy_ex(&g_log_context) /** init function, use stderr for output by default @@ -164,6 +178,16 @@ void log_set_keep_days(LogContext *pContext, const int keep_days); */ void log_set_header_callback(LogContext *pContext, LogHeaderCallback header_callback); +/** set take_over_stderr to true + * return: none +*/ +void log_take_over_stderr_ex(LogContext *pContext); + +/** set take_over_stdout to true + * return: none +*/ +void log_take_over_stdout_ex(LogContext *pContext); + /** destroy function * parameters: * pContext: the log context diff --git a/storage/fdfs_storaged.c b/storage/fdfs_storaged.c index da508e8..143551f 100644 --- a/storage/fdfs_storaged.c +++ b/storage/fdfs_storaged.c @@ -106,7 +106,7 @@ int main(int argc, char *argv[]) g_current_time = time(NULL); g_up_time = g_current_time; - log_init(); + log_init2(); trunk_shared_init(); conf_filename = argv[1]; @@ -168,7 +168,7 @@ int main(int argc, char *argv[]) return result; } - daemon_init(true); + daemon_init(false); umask(0); if ((result=write_to_pid_file(pidFilename)) != 0) { @@ -176,16 +176,6 @@ int main(int argc, char *argv[]) return result; } - if (dup2(g_log_context.log_fd, STDOUT_FILENO) < 0 || \ - dup2(g_log_context.log_fd, STDERR_FILENO) < 0) - { - logCrit("file: "__FILE__", line: %d, " \ - "call dup2 fail, errno: %d, error info: %s, " \ - "program exit!", __LINE__, errno, STRERROR(errno)); - g_continue_flag = false; - return errno; - } - if ((result=storage_sync_init()) != 0) { logCrit("file: "__FILE__", line: %d, " \ diff --git a/tracker/fdfs_trackerd.c b/tracker/fdfs_trackerd.c index 07dfed2..a117fe9 100644 --- a/tracker/fdfs_trackerd.c +++ b/tracker/fdfs_trackerd.c @@ -105,7 +105,7 @@ int main(int argc, char *argv[]) g_up_time = g_current_time; srand(g_up_time); - log_init(); + log_init2(); conf_filename = argv[1]; if ((result=get_base_path_from_conf_file(conf_filename, @@ -188,7 +188,7 @@ int main(int argc, char *argv[]) return result; } - daemon_init(true); + daemon_init(false); umask(0); if ((result=write_to_pid_file(pidFilename)) != 0) @@ -197,16 +197,6 @@ int main(int argc, char *argv[]) return result; } - if (dup2(g_log_context.log_fd, STDOUT_FILENO) < 0 || \ - dup2(g_log_context.log_fd, STDERR_FILENO) < 0) - { - logCrit("file: "__FILE__", line: %d, " \ - "call dup2 fail, errno: %d, error info: %s, " \ - "program exit!", __LINE__, errno, STRERROR(errno)); - g_continue_flag = false; - return errno; - } - if ((result=tracker_service_init()) != 0) { logCrit("exit abnormally!\n");