monolog.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #ifndef monolog_H_INCLUDED
  2. #define monolog_H_INCLUDED
  3. #include <stdarg.h>
  4. #include <stdio.h>
  5. #ifndef FALSE
  6. #define FALSE 0
  7. #endif
  8. #ifndef TRUE
  9. #define TRUE 1
  10. #endif
  11. /* Finer grained logging. */
  12. /** The interface for a logger */
  13. struct MonologLoggerInterface {
  14. /* Logger function. */
  15. int (*log)(char * file, int line, char * level, void * data, char * format, va_list args);
  16. /* Optional destructor. */
  17. void (*free)(void * data);
  18. };
  19. int monolog_add_logger(void * data, struct MonologLoggerInterface * logger);
  20. int monolog_remove_logger(int index);
  21. int monolog_enable_level(char * name);
  22. int monolog_disable_level(char * name);
  23. int monolog_log_va(char * file, int line, char * level, char * format, va_list args);
  24. int monolog_log(char * file, int line, char * level, char * format, ...);
  25. int monolog_init(void);
  26. void monolog_done();
  27. int monolog_stdout_logger(char * file, int line, char * level, void * data, char * format, va_list args);
  28. int monolog_stderr_logger(char * file, int line, char * level, void * data, char * format, va_list args);
  29. /** Log function for logger that logs to a FILE. */
  30. int monolog_file_logger(char * file, int line, char * level, void * data, char * format, va_list args);
  31. /** Free function for logger that logs to a FILE. */
  32. void monolog_file_free(void * data);
  33. /* Logger helper macros */
  34. #define DEFINE_LOGGER(NAME, LOGF, FREEF) \
  35. static struct MonologLoggerInterface NAME = { LOGF, FREEF }
  36. #define DEFINE_STDERR_LOGGER(NAME) DEFINE_LOGGER(NAME, monolog_stderr_logger, NULL)
  37. #define DEFINE_STDOUT_LOGGER(NAME) DEFINE_LOGGER(NAME, monolog_stdout_logger, NULL)
  38. #define DEFINE_FILE_LOGGER(NAME) DEFINE_LOGGER(NAME, monolog_file_logger, monolog_file_free)
  39. /* Logging helper macros */
  40. #define LOG_LEVEL(LEVEL, ...) \
  41. monolog_log(__FILE__, __LINE__, LEVEL, __VA_ARGS__)
  42. #define LOG(...) LOG_LEVEL(__FILE__, __VA_ARGS__)
  43. #define LOG_ERROR(...) LOG_LEVEL("ERROR", __VA_ARGS__)
  44. #define LOG_WARNING(...) LOG_LEVEL("WARNING", __VA_ARGS__)
  45. #define LOG_NOTE(...) LOG_LEVEL("NOTE", __VA_ARGS__)
  46. #define LOG_DEBUG(...) LOG_LEVEL("DEBUG", __VA_ARGS__)
  47. #define LOG_ENABLE(FORMAT) \
  48. monolog_enable_level(#FORMAT)
  49. #define LOG_ENABLE_ERROR() LOG_ENABLE(ERROR)
  50. #define LOG_ENABLE_WARNING() LOG_ENABLE(WARNING)
  51. #define LOG_ENABLE_NOTE() LOG_ENABLE(NOTE)
  52. #define LOG_ENABLE_DEBUG() LOG_ENABLE(DEBUG)
  53. #define LOG_DISABLE(FORMAT) \
  54. monolog_disable_level(#FORMAT)
  55. #define LOG_DISABLE_ERROR() LOG_DISABLE(error)
  56. #define LOG_DISABLE_WARNING() LOG_DISABLE(warning)
  57. #define LOG_DISABLE_NOTE() LOG_DISABLE(note)
  58. #define LOG_DISABLE_DEBUG() LOG_DISABLE(debug)
  59. #endif