diff --git a/src/sf_global.c b/src/sf_global.c index 9cc450a..e97cc71 100644 --- a/src/sf_global.c +++ b/src/sf_global.c @@ -188,6 +188,35 @@ int sf_load_log_config(IniFullContext *ini_ctx, LogContext *log_ctx, return 0; } +int sf_load_slow_log_config_ex(IniFullContext *ini_ctx, LogContext *log_ctx, + SFSlowLogConfig *slow_log_cfg) +{ + int result; + char *filename_prefix; + + if ((result=sf_load_log_config(ini_ctx, log_ctx, + &slow_log_cfg->log_cfg)) != 0) + { + return result; + } + + slow_log_cfg->enabled = iniGetBoolValue(ini_ctx->section_name, + "enabled", ini_ctx->context, false); + slow_log_cfg->log_slower_than_ms = iniGetIntValue(ini_ctx->section_name, + "log_slower_than_ms", ini_ctx->context, 100); + filename_prefix = iniGetStrValue(ini_ctx->section_name, + "filename_prefix", ini_ctx->context); + if (filename_prefix == NULL || *filename_prefix == '\0') { + strcpy(slow_log_cfg->filename_prefix, "slow"); + } else { + snprintf(slow_log_cfg->filename_prefix, + sizeof(slow_log_cfg->filename_prefix), + "%s", filename_prefix); + } + + return 0; +} + int sf_load_global_config_ex(const char *server_name, IniFullContext *ini_ctx, const bool load_network_params, const int task_buffer_extra_size) @@ -437,14 +466,16 @@ void sf_context_config_to_string(const SFContext *sf_context, sf_context->accept_threads, sf_context->work_threads); } -void sf_log_config_to_string(SFLogConfig *log_cfg, - const char *caption, char *output, const int size) +void sf_log_config_to_string_ex(SFLogConfig *log_cfg, const char *caption, + const char *other_config, char *output, const int size) { snprintf(output, size, - "%s: {sync_log_buff_interval=%d, rotate_everyday=%d, " + "%s: {%s%ssync_log_buff_interval=%d, rotate_everyday=%d, " "rotate_time=%02d:%02d, rotate_on_size=%"PRId64", " "compress_old=%d, compress_days_before=%d, keep_days=%d, " "delete_old_time=%02d:%02d}", caption, + other_config != NULL ? other_config : "", + other_config != NULL ? ", " : "", log_cfg->sync_log_buff_interval, log_cfg->rotate_everyday, log_cfg->rotate_time.hour, log_cfg->rotate_time.minute, log_cfg->rotate_on_size, log_cfg->compress_old, @@ -452,6 +483,29 @@ void sf_log_config_to_string(SFLogConfig *log_cfg, log_cfg->delete_old_time.hour, log_cfg->delete_old_time.minute); } +void sf_slow_log_config_to_string(SFSlowLogConfig *slow_log_cfg, + const char *caption, char *output, const int size) +{ + int len; + char slow_log_buff[256]; + + len = snprintf(slow_log_buff, sizeof(slow_log_buff), + "enabled=%d", slow_log_cfg->enabled); + if (!slow_log_cfg->enabled) { + snprintf(output, size, "%s: {%s}", + caption, slow_log_buff); + return; + } + + snprintf(slow_log_buff + len, sizeof(slow_log_buff) - len, + ", filename_prefix=%s, log_slower_than_ms=%d", + slow_log_cfg->filename_prefix, + slow_log_cfg->log_slower_than_ms); + + sf_log_config_to_string_ex(&slow_log_cfg->log_cfg, caption, + slow_log_buff, output, size); +} + void sf_global_config_to_string(char *output, const int size) { int len; @@ -483,7 +537,7 @@ void sf_global_config_to_string(char *output, const int size) ); sf_log_config_to_string(&g_sf_global_vars.error_log, - "error_log_file", output + len, size - len); + "error_log", output + len, size - len); } void sf_log_config_ex(const char *other_config) diff --git a/src/sf_global.h b/src/sf_global.h index fd3724b..ee7ebbc 100644 --- a/src/sf_global.h +++ b/src/sf_global.h @@ -157,9 +157,28 @@ static inline int sf_load_context_from_config(SFContext *sf_context, return sf_load_context_from_config_ex(sf_context, &config); } +int sf_load_log_config(IniFullContext *ini_ctx, LogContext *log_ctx, + SFLogConfig *log_cfg); + +int sf_load_slow_log_config_ex(IniFullContext *ini_ctx, LogContext *log_ctx, + SFSlowLogConfig *slow_log_cfg); + +static inline int sf_load_slow_log_config(const char *config_file, + IniContext *ini_context, LogContext *log_ctx, + SFSlowLogConfig *slow_log_cfg) +{ + IniFullContext ini_ctx; + + FAST_INI_SET_FULL_CTX_EX(ini_ctx, config_file, "slow_log", ini_context); + return sf_load_slow_log_config_ex(&ini_ctx, log_ctx, slow_log_cfg); +} + void sf_set_log_rotate_size(LogContext *context, const int64_t log_rotate_size); -void sf_log_config_to_string(SFLogConfig *log_cfg, +void sf_log_config_to_string_ex(SFLogConfig *log_cfg, const char *caption, + const char *other_config, char *output, const int size); + +void sf_slow_log_config_to_string(SFSlowLogConfig *slow_log_cfg, const char *caption, char *output, const int size); void sf_global_config_to_string(char *output, const int size); @@ -171,6 +190,9 @@ void sf_log_config_ex(const char *other_config); #define sf_log_config() sf_log_config_ex(NULL) +#define sf_log_config_to_string(log_cfg, caption, output, size) \ + sf_log_config_to_string_ex(log_cfg, caption, NULL, output, size) + #ifdef __cplusplus } #endif diff --git a/src/sf_service.c b/src/sf_service.c index b02eefc..a525323 100644 --- a/src/sf_service.c +++ b/src/sf_service.c @@ -600,12 +600,12 @@ int sf_setup_signal_handler() return 0; } +#define LOG_SCHEDULE_ENTRIES_COUNT 3 + int sf_startup_schedule(pthread_t *schedule_tid) { -#define SCHEDULE_ENTRIES_COUNT 3 - ScheduleArray scheduleArray; - ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_COUNT]; + ScheduleEntry scheduleEntries[LOG_SCHEDULE_ENTRIES_COUNT]; scheduleArray.entries = scheduleEntries; sf_setup_schedule(&g_log_context, &g_sf_global_vars.error_log, @@ -615,6 +615,28 @@ int sf_startup_schedule(pthread_t *schedule_tid) &g_sf_global_vars.continue_flag); } +int sf_add_slow_log_schedule(LogContext *pContext, + SFSlowLogConfig *slow_log_cfg) +{ + int result; + ScheduleArray scheduleArray; + ScheduleEntry scheduleEntries[LOG_SCHEDULE_ENTRIES_COUNT]; + + if (!slow_log_cfg->enabled) { + return 0; + } + + if ((result=sf_logger_init(pContext, slow_log_cfg-> + filename_prefix)) != 0) + { + return result; + } + + scheduleArray.entries = scheduleEntries; + sf_setup_schedule(pContext, &slow_log_cfg->log_cfg, &scheduleArray); + return sched_add_entries(&scheduleArray); +} + void sf_set_current_time() { g_current_time = time(NULL); diff --git a/src/sf_service.h b/src/sf_service.h index faf91b9..ad497d2 100644 --- a/src/sf_service.h +++ b/src/sf_service.h @@ -67,7 +67,11 @@ int sf_service_destroy_ex(SFContext *sf_context); #define sf_service_destroy() sf_service_destroy_ex(&g_sf_context) int sf_setup_signal_handler(); + int sf_startup_schedule(pthread_t *schedule_tid); +int sf_add_slow_log_schedule(LogContext *pContext, + SFSlowLogConfig *slow_log_cfg); + void sf_set_current_time(); int sf_socket_server_ex(SFContext *sf_context); diff --git a/src/sf_types.h b/src/sf_types.h index 6d77112..8999572 100644 --- a/src/sf_types.h +++ b/src/sf_types.h @@ -118,4 +118,16 @@ typedef struct sf_log_config { int64_t rotate_on_size; } SFLogConfig; +typedef struct sf_slow_log_config { + bool enabled; + int log_slower_than_ms; + char filename_prefix[64]; + SFLogConfig log_cfg; +} SFSlowLogConfig; + +typedef struct sf_slow_log_context { + SFSlowLogConfig cfg; + LogContext ctx; +} SFSlowLogContext; + #endif diff --git a/src/sf_util.c b/src/sf_util.c index 04648a5..9c71efd 100644 --- a/src/sf_util.c +++ b/src/sf_util.c @@ -151,14 +151,16 @@ ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext, INIT_SCHEDULE_ENTRY_EX(*pScheduleEntry, sched_generate_next_id(), log_cfg->rotate_time, 86400, log_notify_rotate, pContext); pScheduleEntry++; + } - if (log_cfg->keep_days > 0) { - log_set_keep_days(pContext, log_cfg->keep_days); - INIT_SCHEDULE_ENTRY_EX(*pScheduleEntry, sched_generate_next_id(), - log_cfg->delete_old_time, 86400, log_delete_old_files, - pContext); - pScheduleEntry++; - } + if ((log_cfg->rotate_everyday || log_cfg->rotate_on_size > 0) && + (log_cfg->keep_days > 0)) + { + log_set_keep_days(pContext, log_cfg->keep_days); + INIT_SCHEDULE_ENTRY_EX(*pScheduleEntry, sched_generate_next_id(), + log_cfg->delete_old_time, 86400, log_delete_old_files, + pContext); + pScheduleEntry++; } return pScheduleEntry;