unify log relative configs

connection_manager
YuQing 2020-12-29 16:25:19 +08:00
parent c55b5cf5d6
commit fc5480214a
6 changed files with 136 additions and 79 deletions

View File

@ -37,7 +37,7 @@ SFGlobalVariables g_sf_global_vars = {
{'/', 't', 'm', 'p', '\0'}, true, true, DEFAULT_MAX_CONNECTONS,
SF_DEF_MAX_PACKAGE_SIZE, SF_DEF_MIN_BUFF_SIZE,
SF_DEF_MAX_BUFF_SIZE, 0, SF_DEF_THREAD_STACK_SIZE,
SYNC_LOG_BUFF_DEF_INTERVAL, 0, 0, 0, {'\0'}, {'\0'}, false, 0, {0, 0}
0, 0, 0, {'\0'}, {'\0'}, {SYNC_LOG_BUFF_DEF_INTERVAL, false}, {0, 0}
};
SFContext g_sf_context = {
@ -131,11 +131,69 @@ static int load_network_parameters(IniFullContext *ini_ctx,
return 0;
}
void sf_set_log_rotate_size(LogContext *log_ctx, const int64_t rotate_on_size)
{
if (rotate_on_size > 0) {
log_ctx->rotate_size = rotate_on_size;
log_set_rotate_time_format(log_ctx, "%Y%m%d_%H%M%S");
} else {
log_ctx->rotate_size = 0;
log_set_rotate_time_format(log_ctx, "%Y%m%d");
}
}
int sf_load_log_config(IniFullContext *ini_ctx, LogContext *log_ctx,
SFLogConfig *log_cfg)
{
int result;
log_cfg->sync_log_buff_interval = iniGetIntValueEx(
ini_ctx->section_name, "sync_log_buff_interval",
ini_ctx->context, SYNC_LOG_BUFF_DEF_INTERVAL, true);
if (log_cfg->sync_log_buff_interval <= 0) {
log_cfg->sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL;
}
log_cfg->rotate_everyday = iniGetBoolValueEx(ini_ctx->section_name,
"log_file_rotate_everyday", ini_ctx->context, false, true);
log_cfg->keep_days = iniGetIntValueEx(ini_ctx->section_name,
"log_file_keep_days", ini_ctx->context, 0, true);
log_cfg->compress_old = iniGetBoolValueEx(ini_ctx->section_name,
"log_file_compress_old", ini_ctx->context, false, true);
log_cfg->compress_days_before = iniGetIntValueEx(ini_ctx->section_name,
"log_file_compress_days_before", ini_ctx->context, 1, true);
if (log_cfg->compress_old) {
log_set_compress_log_flags_ex(log_ctx, LOG_COMPRESS_FLAGS_ENABLED |
LOG_COMPRESS_FLAGS_NEW_THREAD);
log_set_compress_log_days_before_ex(log_ctx,
log_cfg->compress_days_before);
}
if ((result=get_time_item_from_conf_ex(ini_ctx, "log_file_rotate_time",
&log_cfg->rotate_time, 0, 0, true)) != 0)
{
return result;
}
if ((result=get_time_item_from_conf_ex(ini_ctx, "log_file_delete_old_time",
&log_cfg->delete_old_time, 1, 30, true)) != 0)
{
return result;
}
log_cfg->rotate_on_size = iniGetByteCorrectValueEx(ini_ctx,
"log_file_rotate_on_size", 0, 1, 0,
64 * 1024 * 1024 * 1024LL, true);
sf_set_log_rotate_size(log_ctx, log_cfg->rotate_on_size);
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)
{
int result;
const char *old_section_name;
char *pBasePath;
char *pRunByGroup;
char *pRunByUser;
@ -238,21 +296,18 @@ int sf_load_global_config_ex(const char *server_name,
return result;
}
g_sf_global_vars.sync_log_buff_interval = iniGetIntValue(NULL,
"sync_log_buff_interval", ini_ctx->context,
SYNC_LOG_BUFF_DEF_INTERVAL);
if (g_sf_global_vars.sync_log_buff_interval <= 0) {
g_sf_global_vars.sync_log_buff_interval = SYNC_LOG_BUFF_DEF_INTERVAL;
}
g_sf_global_vars.thread_stack_size = iniGetByteCorrectValueEx(ini_ctx,
"thread_stack_size", SF_DEF_THREAD_STACK_SIZE, 1,
SF_MIN_THREAD_STACK_SIZE, SF_MAX_THREAD_STACK_SIZE, true);
g_sf_global_vars.rotate_error_log = iniGetBoolValue(NULL,
"rotate_error_log", ini_ctx->context, false);
g_sf_global_vars.log_file_keep_days = iniGetIntValue(NULL,
"log_file_keep_days", ini_ctx->context, 0);
old_section_name = ini_ctx->section_name;
ini_ctx->section_name = "error_log";
if ((result=sf_load_log_config(ini_ctx, &g_log_context,
&g_sf_global_vars.error_log)) != 0)
{
return result;
}
ini_ctx->section_name = old_section_name;
load_log_level(ini_ctx->context);
if ((result=log_set_prefix(g_sf_global_vars.base_path, server_name)) != 0) {
@ -382,20 +437,35 @@ 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)
{
snprintf(output, size,
"%s: {sync_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,
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,
log_cfg->compress_days_before, log_cfg->keep_days,
log_cfg->delete_old_time.hour, log_cfg->delete_old_time.minute);
}
void sf_global_config_to_string(char *output, const int size)
{
int len;
char sz_thread_stack_size[32];
char sz_max_pkg_size[32];
char sz_min_buff_size[32];
char sz_max_buff_size[32];
snprintf(output, size,
len = snprintf(output, size,
"base_path=%s, max_connections=%d, connect_timeout=%d, "
"network_timeout=%d, thread_stack_size=%s, max_pkg_size=%s, "
"min_buff_size=%s, max_buff_size=%s, task_buffer_extra_size=%d, "
"tcp_quick_ack=%d, log_level=%s, sync_log_buff_interval=%d, "
"rotate_error_log=%d, log_file_keep_days=%d, "
"run_by_group=%s, run_by_user=%s",
"tcp_quick_ack=%d, log_level=%s, "
"run_by_group=%s, run_by_user=%s, ",
g_sf_global_vars.base_path,
g_sf_global_vars.max_connections,
g_sf_global_vars.connect_timeout,
@ -408,12 +478,12 @@ void sf_global_config_to_string(char *output, const int size)
g_sf_global_vars.task_buffer_extra_size,
g_sf_global_vars.tcp_quick_ack,
log_get_level_caption(),
g_sf_global_vars.sync_log_buff_interval,
g_sf_global_vars.rotate_error_log,
g_sf_global_vars.log_file_keep_days,
g_sf_global_vars.run_by_group,
g_sf_global_vars.run_by_user
);
sf_log_config_to_string(&g_sf_global_vars.error_log,
"error_log_file", output + len, size - len);
}
void sf_log_config_ex(const char *other_config)

View File

@ -42,7 +42,6 @@ typedef struct sf_global_variables {
int max_buff_size;
int task_buffer_extra_size;
int thread_stack_size;
int sync_log_buff_interval; //sync log buff to disk every interval seconds
time_t up_time;
gid_t run_by_gid;
@ -50,9 +49,7 @@ typedef struct sf_global_variables {
char run_by_group[32];
char run_by_user[32];
bool rotate_error_log;
int log_file_keep_days;
SFLogConfig error_log;
SFConnectionStat connection_stat;
} SFGlobalVariables;
@ -160,6 +157,11 @@ static inline int sf_load_context_from_config(SFContext *sf_context,
return sf_load_context_from_config_ex(sf_context, &config);
}
void sf_set_log_rotate_size(LogContext *context, const int64_t log_rotate_size);
void sf_log_config_to_string(SFLogConfig *log_cfg,
const char *caption, char *output, const int size);
void sf_global_config_to_string(char *output, const int size);
void sf_context_config_to_string(const SFContext *sf_context,

View File

@ -32,8 +32,9 @@
#include "fastcommon/sched_thread.h"
#include "fastcommon/ioevent_loop.h"
#include "fastcommon/fc_memory.h"
#include "sf_global.h"
#include "sf_nio.h"
#include "sf_util.h"
#include "sf_global.h"
#include "sf_service.h"
#if defined(OS_LINUX)
@ -605,36 +606,10 @@ int sf_startup_schedule(pthread_t *schedule_tid)
ScheduleArray scheduleArray;
ScheduleEntry scheduleEntries[SCHEDULE_ENTRIES_COUNT];
int index;
scheduleArray.entries = scheduleEntries;
scheduleArray.count = 0;
memset(scheduleEntries, 0, sizeof(scheduleEntries));
index = scheduleArray.count++;
INIT_SCHEDULE_ENTRY(scheduleEntries[index], sched_generate_next_id(),
TIME_NONE, TIME_NONE, 0,
g_sf_global_vars.sync_log_buff_interval,
log_sync_func, &g_log_context);
if (g_sf_global_vars.rotate_error_log) {
log_set_rotate_time_format(&g_log_context, "%Y%m%d");
index = scheduleArray.count++;
INIT_SCHEDULE_ENTRY(scheduleEntries[index], sched_generate_next_id(),
0, 0, 0, 86400, log_notify_rotate, &g_log_context);
if (g_sf_global_vars.log_file_keep_days > 0) {
log_set_keep_days(&g_log_context,
g_sf_global_vars.log_file_keep_days);
index = scheduleArray.count++;
INIT_SCHEDULE_ENTRY(scheduleEntries[index], sched_generate_next_id(),
1, 0, 0, 86400, log_delete_old_files, &g_log_context);
}
}
sf_setup_schedule(&g_log_context, &g_sf_global_vars.error_log,
&scheduleArray);
return sched_start(&scheduleArray, schedule_tid,
g_sf_global_vars.thread_stack_size, (bool * volatile)
&g_sf_global_vars.continue_flag);

View File

@ -107,4 +107,15 @@ typedef struct sf_version_range {
int64_t last; //including
} SFVersionRange;
typedef struct sf_log_config {
int sync_log_buff_interval; //sync log buff to disk every interval seconds
bool rotate_everyday;
bool compress_old;
int compress_days_before;
TimeInfo rotate_time;
TimeInfo delete_old_time;
int keep_days;
int64_t rotate_on_size;
} SFLogConfig;
#endif

View File

@ -140,36 +140,25 @@ int sf_logger_init(LogContext *pContext, const char *filename_prefix)
}
ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext,
ScheduleEntry *pScheduleEntry)
SFLogConfig *log_cfg, 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;
INIT_SCHEDULE_ENTRY(*pScheduleEntry, sched_generate_next_id(),
TIME_NONE, TIME_NONE, 0, log_cfg->sync_log_buff_interval,
log_sync_func, 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;
if (log_cfg->rotate_everyday) {
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++;
}
}
return pScheduleEntry;

View File

@ -20,6 +20,7 @@
#include "fastcommon/logger.h"
#include "fastcommon/sched_thread.h"
#include "sf_types.h"
#ifdef DEBUG_FLAG /*only for format check*/
@ -77,7 +78,16 @@ void sf_parse_daemon_mode_and_action_ex(int argc, char *argv[],
int sf_logger_init(LogContext *pContext, const char *filename_prefix);
ScheduleEntry *sf_logger_set_schedule_entry(struct log_context *pContext,
ScheduleEntry *pScheduleEntry);
SFLogConfig *log_cfg, ScheduleEntry *pScheduleEntry);
static inline void sf_setup_schedule(struct log_context *pContext,
SFLogConfig *log_cfg, ScheduleArray *scheduleArray)
{
ScheduleEntry *scheduleEntry;
scheduleEntry = sf_logger_set_schedule_entry(pContext,
log_cfg, scheduleArray->entries);
scheduleArray->count = scheduleEntry - scheduleArray->entries;
}
const char *sf_strerror(const int errnum);