diff options
-rw-r--r-- | include/mysql/plugin_audit.h.pp | 4 | ||||
-rw-r--r-- | include/mysql/plugin_auth.h.pp | 4 | ||||
-rw-r--r-- | include/mysql/plugin_ftparser.h.pp | 4 | ||||
-rw-r--r-- | include/mysql/service_logger.h | 4 | ||||
-rw-r--r-- | libmysqld/CMakeLists.txt | 1 | ||||
-rw-r--r-- | mysys/CMakeLists.txt | 2 | ||||
-rw-r--r-- | plugin/sql_errlog/sql_errlog.c | 19 | ||||
-rw-r--r-- | sql/CMakeLists.txt | 1 | ||||
-rw-r--r-- | sql/sql_logger.c (renamed from mysys/my_logger.c) | 53 |
9 files changed, 60 insertions, 32 deletions
diff --git a/include/mysql/plugin_audit.h.pp b/include/mysql/plugin_audit.h.pp index 42ad957e556..dd7dde7973c 100644 --- a/include/mysql/plugin_audit.h.pp +++ b/include/mysql/plugin_audit.h.pp @@ -84,7 +84,7 @@ const char *set_thd_proc_info(void*, const char * info, const char *func, typedef struct logger_handle_st LOGGER_HANDLE; extern struct logger_service_st { LOGGER_HANDLE* (*open)(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int (*close)(LOGGER_HANDLE *log); int (*vprintf)(LOGGER_HANDLE *log, const char *fmt, va_list argptr); @@ -92,7 +92,7 @@ extern struct logger_service_st { int (*rotate)(LOGGER_HANDLE *log); } *logger_service; LOGGER_HANDLE *logger_open(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int logger_close(LOGGER_HANDLE *log); int logger_vprintf(LOGGER_HANDLE *log, const char *fmt, va_list argptr); diff --git a/include/mysql/plugin_auth.h.pp b/include/mysql/plugin_auth.h.pp index 146768f9575..cf3b60d128b 100644 --- a/include/mysql/plugin_auth.h.pp +++ b/include/mysql/plugin_auth.h.pp @@ -84,7 +84,7 @@ const char *set_thd_proc_info(void*, const char * info, const char *func, typedef struct logger_handle_st LOGGER_HANDLE; extern struct logger_service_st { LOGGER_HANDLE* (*open)(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int (*close)(LOGGER_HANDLE *log); int (*vprintf)(LOGGER_HANDLE *log, const char *fmt, va_list argptr); @@ -92,7 +92,7 @@ extern struct logger_service_st { int (*rotate)(LOGGER_HANDLE *log); } *logger_service; LOGGER_HANDLE *logger_open(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int logger_close(LOGGER_HANDLE *log); int logger_vprintf(LOGGER_HANDLE *log, const char *fmt, va_list argptr); diff --git a/include/mysql/plugin_ftparser.h.pp b/include/mysql/plugin_ftparser.h.pp index 59afe4276b4..14129aec85e 100644 --- a/include/mysql/plugin_ftparser.h.pp +++ b/include/mysql/plugin_ftparser.h.pp @@ -84,7 +84,7 @@ const char *set_thd_proc_info(void*, const char * info, const char *func, typedef struct logger_handle_st LOGGER_HANDLE; extern struct logger_service_st { LOGGER_HANDLE* (*open)(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int (*close)(LOGGER_HANDLE *log); int (*vprintf)(LOGGER_HANDLE *log, const char *fmt, va_list argptr); @@ -92,7 +92,7 @@ extern struct logger_service_st { int (*rotate)(LOGGER_HANDLE *log); } *logger_service; LOGGER_HANDLE *logger_open(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int logger_close(LOGGER_HANDLE *log); int logger_vprintf(LOGGER_HANDLE *log, const char *fmt, va_list argptr); diff --git a/include/mysql/service_logger.h b/include/mysql/service_logger.h index 315c382791e..47a478ef5b4 100644 --- a/include/mysql/service_logger.h +++ b/include/mysql/service_logger.h @@ -61,7 +61,7 @@ typedef struct logger_handle_st LOGGER_HANDLE; extern struct logger_service_st { LOGGER_HANDLE* (*open)(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int (*close)(LOGGER_HANDLE *log); int (*vprintf)(LOGGER_HANDLE *log, const char *fmt, va_list argptr); @@ -81,7 +81,7 @@ extern struct logger_service_st { #else LOGGER_HANDLE *logger_open(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations); int logger_close(LOGGER_HANDLE *log); int logger_vprintf(LOGGER_HANDLE *log, const char *fmt, va_list argptr); diff --git a/libmysqld/CMakeLists.txt b/libmysqld/CMakeLists.txt index 14db8327897..993d54f8aae 100644 --- a/libmysqld/CMakeLists.txt +++ b/libmysqld/CMakeLists.txt @@ -95,6 +95,7 @@ SET(SQL_EMBEDDED_SOURCES emb_qcache.cc libmysqld.c lib_sql.cc ../sql/create_options.cc ../sql/rpl_utility.cc ../sql/rpl_reporting.cc ../sql/sql_expression_cache.cc + ../sql/sql_logger.c ${GEN_SOURCES} ${MYSYS_LIBWRAP_SOURCE} ) diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt index 5204f7e873c..7397990eddb 100644 --- a/mysys/CMakeLists.txt +++ b/mysys/CMakeLists.txt @@ -37,7 +37,7 @@ SET(MYSYS_SOURCES array.c charset-def.c charset.c checksum.c default.c safemalloc.c my_new.cc my_atomic.c my_getncpus.c my_safehash.c my_chmod.c my_rnd.c my_uuid.c wqueue.c waiting_threads.c ma_dyncol.c - my_rdtsc.c my_context.c my_logger.c) + my_rdtsc.c my_context.c) IF (WIN32) SET (MYSYS_SOURCES ${MYSYS_SOURCES} my_winthread.c my_wincond.c my_winerr.c my_winfile.c my_windac.c my_conio.c) diff --git a/plugin/sql_errlog/sql_errlog.c b/plugin/sql_errlog/sql_errlog.c index 53d804feeb3..ee2765a7143 100644 --- a/plugin/sql_errlog/sql_errlog.c +++ b/plugin/sql_errlog/sql_errlog.c @@ -18,12 +18,23 @@ #include <time.h> /* + Disable __attribute__() on non-gcc compilers. +*/ +#if !defined(__attribute__) && !defined(__GNUC__) +#define __attribute__(A) +#endif + +#ifdef _WIN32 +#define localtime_r(a, b) localtime_s(b, a) +#endif /*WIN32*/ + +/* rate 0 means the logging was disabled. */ static char *filename; static unsigned int rate; -static unsigned int size_limit; +static unsigned long long size_limit; static unsigned int rotations; static char rotate; @@ -37,9 +48,9 @@ static MYSQL_SYSVAR_UINT(rate, rate, PLUGIN_VAR_RQCMDARG, "Sampling rate. If set to 0(zero), the logging is disabled.", NULL, NULL, 1, 0, 1000000, 1); -static MYSQL_SYSVAR_UINT(size_limit, size_limit, +static MYSQL_SYSVAR_ULONGLONG(size_limit, size_limit, PLUGIN_VAR_READONLY, "Log file size limit", NULL, NULL, - 1000000, 100, 1024*1024L*1024L, 1); + 1000000, 100, 0, 1); static MYSQL_SYSVAR_UINT(rotations, rotations, PLUGIN_VAR_READONLY, "Number of rotations before log is removed.", @@ -79,7 +90,7 @@ static void log_sql_errors(MYSQL_THD thd __attribute__((unused)), time_t event_time = event->general_time; count = 0; - localtime_r(&event_time, &t); + (void) localtime_r(&event_time, &t); logger_printf(logfile, "%04d-%02d-%02d %2d:%02d:%02d " "%s ERROR %d: %s : %s\n", t.tm_year + 1900, t.tm_mon + 1, diff --git a/sql/CMakeLists.txt b/sql/CMakeLists.txt index 446db5d128a..4eb2b2197ef 100644 --- a/sql/CMakeLists.txt +++ b/sql/CMakeLists.txt @@ -85,6 +85,7 @@ SET (SQL_SOURCE gcalc_slicescan.cc gcalc_tools.cc threadpool_common.cc ../sql-common/mysql_async.c + sql_logger.c ${GEN_SOURCES} ${MYSYS_LIBWRAP_SOURCE} ) diff --git a/mysys/my_logger.c b/sql/sql_logger.c index 292c98f4ad1..fe3c9a87d20 100644 --- a/mysys/my_logger.c +++ b/sql/sql_logger.c @@ -22,15 +22,17 @@ extern char *mysql_data_home; extern PSI_mutex_key key_LOCK_logger_service; typedef struct logger_handle_st { - FILE *file; + File file; char path[FN_REFLEN]; - long size_limit; + unsigned long long size_limit; unsigned int rotations; size_t path_len; mysql_mutex_t lock; } LSFS; +#define LOG_FLAGS (O_APPEND | O_CREAT | O_WRONLY) + static unsigned int n_dig(unsigned int i) { return (i == 0) ? 0 : ((i < 10) ? 1 : ((i < 100) ? 2 : 3)); @@ -38,7 +40,7 @@ static unsigned int n_dig(unsigned int i) LOGGER_HANDLE *logger_open(const char *path, - unsigned long size_limit, + unsigned long long size_limit, unsigned int rotations) { LOGGER_HANDLE new_log, *l_perm; @@ -61,19 +63,17 @@ LOGGER_HANDLE *logger_open(const char *path, /* File path too long */ return 0; } - if (!(new_log.file= fopen(new_log.path, "a+"))) + if ((new_log.file= my_open(new_log.path, LOG_FLAGS, MYF(0))) < 0) { + errno= my_errno; /* Check errno for the cause */ return 0; } - setbuf(new_log.file, 0); - fseek(new_log.file, 0, SEEK_END); - if (!(l_perm= (LOGGER_HANDLE *) - my_malloc(sizeof(LOGGER_HANDLE), MYF(0)))) + if (!(l_perm= (LOGGER_HANDLE *) my_malloc(sizeof(LOGGER_HANDLE), MYF(0)))) { - fclose(new_log.file); - new_log.file= NULL; + my_close(new_log.file, MYF(0)); + new_log.file= -1; return 0; /* End of memory */ } *l_perm= new_log; @@ -84,9 +84,11 @@ LOGGER_HANDLE *logger_open(const char *path, int logger_close(LOGGER_HANDLE *log) { int result; + File file= log->file; mysql_mutex_destroy(&log->lock); - result= fclose(log->file); my_free(log); + if ((result= my_close(file, MYF(0)))) + errno= my_errno; return result; } @@ -114,32 +116,45 @@ static int do_rotate(LOGGER_HANDLE *log) logname(log, buf_old, i); if (!access(buf_old, F_OK) && (result= my_rename(buf_old, buf_new, MYF(0)))) - return result; + goto exit; tmp= buf_old; buf_old= buf_new; buf_new= tmp; } - if ((result= fclose(log->file))) - return result; + if ((result= my_close(log->file, MYF(0)))) + goto exit; namebuf[log->path_len]= 0; result= my_rename(namebuf, logname(log, log->path, 1), MYF(0)); - log->file= fopen(namebuf, "a+"); - return log->file==NULL || result; + log->file= my_open(namebuf, LOG_FLAGS, MYF(0)); +exit: + errno= my_errno; + return log->file < 0 || result; } int logger_vprintf(LOGGER_HANDLE *log, const char* fmt, va_list ap) { int result; + my_off_t filesize; + char cvtbuf[1024]; + size_t n_bytes; + mysql_mutex_lock(&log->lock); - if (ftell(log->file) >= log->size_limit && - do_rotate(log)) + if ((filesize= my_tell(log->file, MYF(0))) == (my_off_t) -1 || + ((unsigned long long)filesize >= log->size_limit && + do_rotate(log))) { result= -1; + errno= my_errno; goto exit; /* Log rotation needed but failed */ } - result= my_vfprintf(log->file, fmt, ap); + n_bytes= my_vsnprintf(cvtbuf, sizeof(cvtbuf), fmt, ap); + if (n_bytes >= sizeof(cvtbuf)) + n_bytes= sizeof(cvtbuf) - 1; + + result= my_write(log->file, (uchar *) cvtbuf, n_bytes, MYF(0)); + exit: mysql_mutex_unlock(&log->lock); return result; |