diff --git a/src/sf_util.c b/src/sf_util.c index b726f1c..f5fea2a 100644 --- a/src/sf_util.c +++ b/src/sf_util.c @@ -7,6 +7,7 @@ #include #include #include +#include "sf_global.h" #include "sf_util.h" int64_t getticks() @@ -103,3 +104,57 @@ void sf_parse_daemon_mode_and_action(int argc, char *argv[], *action = NULL; } } + +int sf_logger_init(LogContext *pContext, const char *filename_prefix) +{ + int result; + if ((result=log_init_ex(pContext)) != 0) { + return result; + } + + if ((result=log_set_prefix_ex(pContext, g_sf_global_vars.base_path, + filename_prefix)) != 0) + { + return result; + } + + log_set_rotate_time_format(pContext, "%Y%m%d"); + log_set_cache_ex(pContext, true); + return 0; +} + +ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext, + ScheduleEntry *pScheduleEntry) +{ + pScheduleEntry->id = sched_generate_next_id(); + pScheduleEntry->time_base.hour = TIME_NONE; + pScheduleEntry->time_base.minute = TIME_NONE; + pScheduleEntry->interval = g_sf_global_vars.sync_log_buff_interval; + pScheduleEntry->task_func = log_sync_func; + pScheduleEntry->func_args = pContext; + pScheduleEntry++; + + pScheduleEntry->id = sched_generate_next_id(); + pScheduleEntry->time_base.hour = 0; + pScheduleEntry->time_base.minute = 0; + pScheduleEntry->time_base.second = 0; + pScheduleEntry->interval = 86400; + pScheduleEntry->task_func = log_notify_rotate; + pScheduleEntry->func_args = pContext; + pScheduleEntry++; + + if (g_sf_global_vars.log_file_keep_days > 0) { + log_set_keep_days(pContext, g_sf_global_vars.log_file_keep_days); + + pScheduleEntry->id = sched_generate_next_id(); + pScheduleEntry->time_base.hour = 1; + pScheduleEntry->time_base.minute = 30; + pScheduleEntry->time_base.second = 0; + pScheduleEntry->interval = 86400; + pScheduleEntry->task_func = log_delete_old_files; + pScheduleEntry->func_args = pContext; + pScheduleEntry++; + } + + return pScheduleEntry; +} diff --git a/src/sf_util.h b/src/sf_util.h index 341d10e..d967cd2 100644 --- a/src/sf_util.h +++ b/src/sf_util.h @@ -4,6 +4,8 @@ #define _SF_UTIL_H_ #include "fastcommon/logger.h" +#include "fastcommon/sched_thread.h" + #ifdef DEBUG_FLAG /*only for format check*/ #define lemerg(...) snprintf(0,0,__VA_ARGS__), log_plus(LOG_EMERG, __FILE__, __LINE__, __VA_ARGS__) @@ -54,9 +56,13 @@ void sf_usage(const char *program); void sf_parse_daemon_mode_and_action(int argc, char *argv[], bool *daemon_mode, char **action); +int sf_logger_init(LogContext *pContext, const char *filename_prefix); + +ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext, + ScheduleEntry *pScheduleEntry); + #ifdef __cplusplus } #endif #endif -