summaryrefslogtreecommitdiff
path: root/mysys
diff options
context:
space:
mode:
authorAlexey Botchkov <holyfoot@askmonty.org>2012-03-14 00:55:56 +0400
committerAlexey Botchkov <holyfoot@askmonty.org>2012-03-14 00:55:56 +0400
commit07a82c58a7306bd8ef889dd912cdc437a9f83a89 (patch)
treeedd7edc02e96ad32d8f7d8d5137db548562be16e /mysys
parent92f31d8070738206c5b64d3ab5cc9a0cb05c56fa (diff)
downloadmariadb-git-07a82c58a7306bd8ef889dd912cdc437a9f83a89.tar.gz
MDEV-15 Log all SQL errors.
Added the logger service that provides us with the rotating logs. The plugin SQL_ERROR_LOG added. It logs the errors using the 'logger service' for the rotating log files. the example record from the log: 2012-03-09 15:07:29 root[root] @ localhost [] ERROR 1146: Table 'test.xyz' doesn't exist : select * from test.xyz
Diffstat (limited to 'mysys')
-rw-r--r--mysys/CMakeLists.txt2
-rw-r--r--mysys/my_logger.c168
2 files changed, 169 insertions, 1 deletions
diff --git a/mysys/CMakeLists.txt b/mysys/CMakeLists.txt
index 7397990eddb..5204f7e873c 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_rdtsc.c my_context.c my_logger.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/mysys/my_logger.c b/mysys/my_logger.c
new file mode 100644
index 00000000000..292c98f4ad1
--- /dev/null
+++ b/mysys/my_logger.c
@@ -0,0 +1,168 @@
+/* Copyright (C) 2012 Monty Program Ab
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; version 2 of the License.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
+
+
+#include "my_global.h"
+#include <my_sys.h>
+#include <mysql/service_logger.h>
+
+extern char *mysql_data_home;
+extern PSI_mutex_key key_LOCK_logger_service;
+
+typedef struct logger_handle_st {
+ FILE *file;
+ char path[FN_REFLEN];
+ long size_limit;
+ unsigned int rotations;
+ size_t path_len;
+ mysql_mutex_t lock;
+} LSFS;
+
+
+static unsigned int n_dig(unsigned int i)
+{
+ return (i == 0) ? 0 : ((i < 10) ? 1 : ((i < 100) ? 2 : 3));
+}
+
+
+LOGGER_HANDLE *logger_open(const char *path,
+ unsigned long size_limit,
+ unsigned int rotations)
+{
+ LOGGER_HANDLE new_log, *l_perm;
+
+ /*
+ I don't think we ever need more rotations,
+ but if it's so, the rotation procedure should be adapted to it.
+ */
+ if (rotations > 999)
+ return 0;
+
+ new_log.rotations= rotations;
+ new_log.size_limit= size_limit;
+ new_log.path_len= strlen(fn_format(new_log.path, path,
+ mysql_data_home, "", MY_UNPACK_FILENAME));
+
+ if (new_log.path_len+n_dig(rotations)+1 > FN_REFLEN)
+ {
+ errno= ENAMETOOLONG;
+ /* File path too long */
+ return 0;
+ }
+ if (!(new_log.file= fopen(new_log.path, "a+")))
+ {
+ /* 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))))
+ {
+ fclose(new_log.file);
+ new_log.file= NULL;
+ return 0; /* End of memory */
+ }
+ *l_perm= new_log;
+ mysql_mutex_init(key_LOCK_logger_service, &l_perm->lock, MY_MUTEX_INIT_FAST);
+ return l_perm;
+}
+
+int logger_close(LOGGER_HANDLE *log)
+{
+ int result;
+ mysql_mutex_destroy(&log->lock);
+ result= fclose(log->file);
+ my_free(log);
+ return result;
+}
+
+
+static char *logname(LOGGER_HANDLE *log, char *buf, unsigned int n_log)
+{
+ sprintf(buf+log->path_len, ".%0*u", n_dig(log->rotations), n_log);
+ return buf;
+}
+
+
+static int do_rotate(LOGGER_HANDLE *log)
+{
+ char namebuf[FN_REFLEN];
+ int result;
+ unsigned int i;
+ char *buf_old, *buf_new, *tmp;
+
+ memcpy(namebuf, log->path, log->path_len);
+
+ buf_new= logname(log, namebuf, log->rotations);
+ buf_old= log->path;
+ for (i=log->rotations-1; i>0; i--)
+ {
+ logname(log, buf_old, i);
+ if (!access(buf_old, F_OK) &&
+ (result= my_rename(buf_old, buf_new, MYF(0))))
+ return result;
+ tmp= buf_old;
+ buf_old= buf_new;
+ buf_new= tmp;
+ }
+ if ((result= fclose(log->file)))
+ return result;
+ 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;
+}
+
+
+int logger_vprintf(LOGGER_HANDLE *log, const char* fmt, va_list ap)
+{
+ int result;
+ mysql_mutex_lock(&log->lock);
+ if (ftell(log->file) >= log->size_limit &&
+ do_rotate(log))
+ {
+ result= -1;
+ goto exit; /* Log rotation needed but failed */
+ }
+
+ result= my_vfprintf(log->file, fmt, ap);
+exit:
+ mysql_mutex_unlock(&log->lock);
+ return result;
+}
+
+
+int logger_rotate(LOGGER_HANDLE *log)
+{
+ int result;
+ mysql_mutex_lock(&log->lock);
+ result= do_rotate(log);
+ mysql_mutex_unlock(&log->lock);
+ return result;
+}
+
+
+int logger_printf(LOGGER_HANDLE *log, const char *fmt, ...)
+{
+ int result;
+ va_list args;
+ va_start(args,fmt);
+ result= logger_vprintf(log, fmt, args);
+ va_end(args);
+ return result;
+}
+