monolog.h 2.6 KB

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