9
3

service_logger.h 3.5 KB

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