diff --git a/doc/cetus-sqllog-usage.md b/doc/cetus-sqllog-usage.md index 37e6ea1..ead7601 100644 --- a/doc/cetus-sqllog-usage.md +++ b/doc/cetus-sqllog-usage.md @@ -60,11 +60,11 @@ Cetus增加了全量日志的功能,即可以按需要输出经由Cetus的所 - sql-log-path -该参数可以指定全量日志输出的路径,该值默认与basedir路径相同,该参数不能动态配置。 +该参数可以指定全量日志输出的路径,该值默认与basedir/logs/路径相同,如果路径不存在,尝试创建该目录,该参数不能动态配置。 - sql-log-maxsize -该值控制每个全量日志的最大容量,默认值为0,表示不限制文件大小,单位是B,该参数不能动态配置。如果当前日志量超过该值,则会rotate成历史日志文件。 +该值控制每个全量日志的最大容量,默认值为1024,0表示不限制文件大小,单位是M,该参数不能动态配置。如果当前日志量超过该值,则会rotate成历史日志文件。 - sql-log-mode @@ -76,7 +76,7 @@ Cetus增加了全量日志的功能,即可以按需要输出经由Cetus的所 - sql-log-maxnum -保留的历史文件的个数,默认为0,表示不限制文件个数。 +保留的历史文件的个数,默认为3,0表示不限制文件个数。 #### 2.3 统计信息 diff --git a/src/chassis-options-utils.c b/src/chassis-options-utils.c index b67b4a5..c9e2990 100644 --- a/src/chassis-options-utils.c +++ b/src/chassis-options-utils.c @@ -1296,7 +1296,7 @@ gchar* show_sql_log_maxsize(gpointer param) { chassis *srv = opt_param->chas; gint opt_type = opt_param->opt_type; if (CAN_SHOW_OPTS_PROPERTY(opt_type) || CAN_SAVE_OPTS_PROPERTY(opt_type)) { - return g_strdup_printf("%u", srv->sql_mgr->sql_log_maxsize); + return g_strdup_printf("%u M", srv->sql_mgr->sql_log_maxsize); } return NULL; } @@ -1394,7 +1394,7 @@ show_sql_log_maxnum(gpointer param) { return g_strdup_printf("%u", srv->sql_mgr->sql_log_maxnum); } if (CAN_SAVE_OPTS_PROPERTY(opt_type)) { - if (srv->sql_mgr->sql_log_idletime == 0) return NULL; + if (srv->sql_mgr->sql_log_maxnum == 3) return NULL; return g_strdup_printf("%u", srv->sql_mgr->sql_log_maxnum); } return NULL; @@ -1408,10 +1408,14 @@ assign_sql_log_maxnum(const gchar *newval, gpointer param) { gint opt_type = opt_param->opt_type; if (CAN_ASSIGN_OPTS_PROPERTY(opt_type)) { if (NULL != newval) { - guint value = 0; + gint value = 0; if (try_get_int_value(newval, &value)) { - srv->sql_mgr->sql_log_maxnum = value; - ret = ASSIGN_OK; + if (value < 0) { + ret = ASSIGN_VALUE_INVALID; + } else { + srv->sql_mgr->sql_log_maxnum = value; + ret = ASSIGN_OK; + } } else { ret = ASSIGN_VALUE_INVALID; } diff --git a/src/chassis-sql-log.c b/src/chassis-sql-log.c index a01d4cc..cbfdc56 100644 --- a/src/chassis-sql-log.c +++ b/src/chassis-sql-log.c @@ -2,6 +2,7 @@ #include "network-mysqld-packet.h" #include #include +#include const COM_STRING com_command_name[]={ { C("Sleep") }, @@ -162,13 +163,13 @@ struct sql_log_mgr *sql_log_alloc() { mgr->sql_log_mode = BACKEND; mgr->sql_log_switch = OFF; mgr->sql_log_cursize = 0; - mgr->sql_log_maxsize = 0; + mgr->sql_log_maxsize = 1024; mgr->sql_log_fullname = NULL; mgr->sql_log_idletime = SQL_LOG_DEF_IDLETIME; mgr->sql_log_action = SQL_LOG_STOP; mgr->fifo = NULL; mgr->sql_log_filelist = NULL; - mgr->sql_log_maxnum = 0; + mgr->sql_log_maxnum = 3; return mgr; } @@ -236,7 +237,7 @@ static void sql_log_check_filenum(struct sql_log_mgr *mgr, gchar *filename) { static void sql_log_check_rotate(struct sql_log_mgr *mgr) { if (!mgr) return ; if (mgr->sql_log_maxsize == 0) return; - if (mgr->sql_log_cursize < mgr->sql_log_maxsize) return ; + if (mgr->sql_log_cursize < mgr->sql_log_maxsize * MEGABYTES) return ; time_t t = time(NULL); struct tm cur_tm; @@ -341,6 +342,14 @@ sql_log_thread_start(struct sql_log_mgr *mgr) { if (mgr->sql_log_path == NULL) { mgr->sql_log_path = g_strdup(SQL_LOG_DEF_PATH); } + int result = access(mgr->sql_log_path, F_OK); + if (result != 0) { + g_message("sql log path is not exist, try to mkdir"); + result = mkdir(mgr->sql_log_path, 0660); + if (result != 0) { + g_message("mkdir(%s) failed", mgr->sql_log_path); + } + } if (mgr->sql_log_fullname == NULL) { mgr->sql_log_fullname = g_strdup_printf("%s/%s.%s", mgr->sql_log_path, mgr->sql_log_filename, SQL_LOG_DEF_SUFFIX); } diff --git a/src/chassis-sql-log.h b/src/chassis-sql-log.h index cac9f89..5b224b6 100644 --- a/src/chassis-sql-log.h +++ b/src/chassis-sql-log.h @@ -12,6 +12,7 @@ #define SQL_LOG_DEF_SUFFIX "sql" #define SQL_LOG_DEF_PATH "/var/log/" #define SQL_LOG_DEF_IDLETIME 500 +#define MEGABYTES 1024*1024 #define min(a, b) ((a) < (b) ? (a) : (b)) @@ -57,13 +58,13 @@ struct sql_log_mgr { SQL_LOG_SWITCH sql_log_switch; SQL_LOG_MODE sql_log_mode; gchar *sql_log_path; - guint sql_log_maxsize; + gulong sql_log_maxsize; volatile SQL_LOG_ACTION sql_log_idletime; volatile guint sql_log_maxnum; GThread *thread; FILE *sql_log_fp; - guint sql_log_cursize; + gulong sql_log_cursize; gchar *sql_log_fullname; volatile guint sql_log_action; struct rfifo *fifo; diff --git a/src/mysql-proxy-cli.c b/src/mysql-proxy-cli.c index 0d236d1..d79f3ce 100644 --- a/src/mysql-proxy-cli.c +++ b/src/mysql-proxy-cli.c @@ -154,10 +154,10 @@ struct chassis_frontend_t { gchar *sql_log_switch; gchar *sql_log_filename; gchar *sql_log_path; - guint sql_log_maxsize; + gint sql_log_maxsize; gchar *sql_log_mode; guint sql_log_idletime; - guint sql_log_maxnum; + gint sql_log_maxnum; }; /** @@ -196,9 +196,10 @@ chassis_frontend_new(void) frontend->sql_log_switch = NULL; frontend->sql_log_filename = NULL; frontend->sql_log_path = NULL; - frontend->sql_log_maxsize = 0; + frontend->sql_log_maxsize = -1; frontend->sql_log_mode = NULL; frontend->sql_log_idletime = 0; + frontend->sql_log_maxnum = -1; return frontend; } @@ -515,7 +516,7 @@ chassis_frontend_set_chassis_options(struct chassis_frontend_t *frontend, chassi chassis_options_add(opts, "sql-log-maxsize", 0, 0, OPTION_ARG_INT, &(frontend->sql_log_maxsize), - "the maxsize of sql file","", + "the maxsize of sql file, units is M","", NULL, show_sql_log_maxsize, SHOW_OPTS_PROPERTY|SAVE_OPTS_PROPERTY); chassis_options_add(opts, "sql-log-mode", @@ -1161,9 +1162,12 @@ main_cmdline(int argc, char **argv) if (frontend->sql_log_path) { srv->sql_mgr->sql_log_path = g_strdup(frontend->sql_log_path); } else if(frontend->base_dir) { - srv->sql_mgr->sql_log_path = g_strdup(frontend->base_dir); + srv->sql_mgr->sql_log_path = g_strdup_printf("%s/logs", frontend->base_dir); } - srv->sql_mgr->sql_log_maxsize = frontend->sql_log_maxsize; + if (frontend->sql_log_maxsize >= 0) { + srv->sql_mgr->sql_log_maxsize = frontend->sql_log_maxsize; + } + if (frontend->sql_log_mode) { if (strcasecmp(frontend->sql_log_mode, "CLIENT") == 0) { srv->sql_mgr->sql_log_mode = CLIENT; @@ -1183,7 +1187,7 @@ main_cmdline(int argc, char **argv) if (frontend->sql_log_idletime) { srv->sql_mgr->sql_log_idletime = frontend->sql_log_idletime; } - if (frontend->sql_log_maxnum) { + if (frontend->sql_log_maxnum >= 0) { srv->sql_mgr->sql_log_maxnum = frontend->sql_log_maxnum; } }