| 1 | /* Copyright (C) 2012 Monty Program Ab | 
|---|
| 2 |  | 
|---|
| 3 | This program is free software; you can redistribute it and/or modify | 
|---|
| 4 | it under the terms of the GNU General Public License as published by | 
|---|
| 5 | the Free Software Foundation; version 2 of the License. | 
|---|
| 6 |  | 
|---|
| 7 | This program is distributed in the hope that it will be useful, | 
|---|
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 10 | GNU General Public License for more details. | 
|---|
| 11 |  | 
|---|
| 12 | You should have received a copy of the GNU General Public License | 
|---|
| 13 | along with this program; if not, write to the Free Software | 
|---|
| 14 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA */ | 
|---|
| 15 |  | 
|---|
| 16 | #ifndef MYSQL_SERVICE_LOGGER_INCLUDED | 
|---|
| 17 | #define MYSQL_SERVICE_LOGGER_INCLUDED | 
|---|
| 18 |  | 
|---|
| 19 | #ifndef MYSQL_ABI_CHECK | 
|---|
| 20 | #include <stdarg.h> | 
|---|
| 21 | #endif | 
|---|
| 22 |  | 
|---|
| 23 | /** | 
|---|
| 24 | @file | 
|---|
| 25 | logger service | 
|---|
| 26 |  | 
|---|
| 27 | Log file with rotation implementation. | 
|---|
| 28 |  | 
|---|
| 29 | This service implements logging with possible rotation | 
|---|
| 30 | of the log files. Interface intentionally tries to be similar to FILE* | 
|---|
| 31 | related functions. | 
|---|
| 32 |  | 
|---|
| 33 | So that one can open the log with logger_open(), specifying | 
|---|
| 34 | the limit on the logfile size and the rotations number. | 
|---|
| 35 |  | 
|---|
| 36 | Then it's possible to write messages to the log with | 
|---|
| 37 | logger_printf or logger_vprintf functions. | 
|---|
| 38 |  | 
|---|
| 39 | As the size of the logfile grows over the specified limit, | 
|---|
| 40 | it is renamed to 'logfile.1'. The former 'logfile.1' becomes | 
|---|
| 41 | 'logfile.2', etc. The file 'logfile.rotations' is removed. | 
|---|
| 42 | That's how the rotation works. | 
|---|
| 43 |  | 
|---|
| 44 | The rotation can be forced with the logger_rotate() call. | 
|---|
| 45 |  | 
|---|
| 46 | Finally the log should be closed with logger_close(). | 
|---|
| 47 |  | 
|---|
| 48 | @notes: | 
|---|
| 49 | Implementation checks the size of the log file before it starts new | 
|---|
| 50 | printf into it. So the size of the file gets over the limit when it rotates. | 
|---|
| 51 |  | 
|---|
| 52 | The access is secured with the mutex, so the log is threadsafe. | 
|---|
| 53 | */ | 
|---|
| 54 |  | 
|---|
| 55 |  | 
|---|
| 56 | #ifdef __cplusplus | 
|---|
| 57 | extern "C"{ | 
|---|
| 58 | #endif | 
|---|
| 59 |  | 
|---|
| 60 | typedef struct logger_handle_st LOGGER_HANDLE; | 
|---|
| 61 |  | 
|---|
| 62 | extern struct logger_service_st { | 
|---|
| 63 | void (*logger_init_mutexes)(); | 
|---|
| 64 | LOGGER_HANDLE* (*open)(const char *path, | 
|---|
| 65 | unsigned long long size_limit, | 
|---|
| 66 | unsigned int rotations); | 
|---|
| 67 | int (*close)(LOGGER_HANDLE *log); | 
|---|
| 68 | int (*vprintf)(LOGGER_HANDLE *log, const char *fmt, va_list argptr); | 
|---|
| 69 | int (*printf)(LOGGER_HANDLE *log, const char *fmt, ...); | 
|---|
| 70 | int (*write)(LOGGER_HANDLE *log, const char *buffer, size_t size); | 
|---|
| 71 | int (*rotate)(LOGGER_HANDLE *log); | 
|---|
| 72 | } *logger_service; | 
|---|
| 73 |  | 
|---|
| 74 | #ifdef MYSQL_DYNAMIC_PLUGIN | 
|---|
| 75 |  | 
|---|
| 76 | #define logger_init_mutexes logger_service->logger_init_mutexes | 
|---|
| 77 | #define logger_open(path, size_limit, rotations) \ | 
|---|
| 78 | (logger_service->open(path, size_limit, rotations)) | 
|---|
| 79 | #define logger_close(log) (logger_service->close(log)) | 
|---|
| 80 | #define logger_rotate(log) (logger_service->rotate(log)) | 
|---|
| 81 | #define logger_vprintf(log, fmt, argptr) (logger_service->\ | 
|---|
| 82 | vprintf(log, fmt, argptr)) | 
|---|
| 83 | #define logger_printf (*logger_service->printf) | 
|---|
| 84 | #define logger_write(log, buffer, size) \ | 
|---|
| 85 | (logger_service->write(log, buffer, size)) | 
|---|
| 86 | #else | 
|---|
| 87 |  | 
|---|
| 88 | void logger_init_mutexes(); | 
|---|
| 89 | LOGGER_HANDLE *logger_open(const char *path, | 
|---|
| 90 | unsigned long long size_limit, | 
|---|
| 91 | unsigned int rotations); | 
|---|
| 92 | int logger_close(LOGGER_HANDLE *log); | 
|---|
| 93 | int logger_vprintf(LOGGER_HANDLE *log, const char *fmt, va_list argptr); | 
|---|
| 94 | int logger_printf(LOGGER_HANDLE *log, const char *fmt, ...); | 
|---|
| 95 | int logger_write(LOGGER_HANDLE *log, const char *buffer, size_t size); | 
|---|
| 96 | int logger_rotate(LOGGER_HANDLE *log); | 
|---|
| 97 | #endif | 
|---|
| 98 |  | 
|---|
| 99 |  | 
|---|
| 100 | #ifdef __cplusplus | 
|---|
| 101 | } | 
|---|
| 102 | #endif | 
|---|
| 103 |  | 
|---|
| 104 | #endif /*MYSQL_SERVICE_LOGGER_INCLUDED*/ | 
|---|
| 105 |  | 
|---|
| 106 |  | 
|---|