Log.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* vim: set expandtab ts=4 sw=4: */
  2. /*
  3. * You may redistribute this program and/or modify it under the terms of
  4. * the GNU General Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  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, see <https://www.gnu.org/licenses/>.
  14. */
  15. #ifndef Log_H
  16. #define Log_H
  17. #include "util/Gcc.h"
  18. #include "util/Linker.h"
  19. Linker_require("util/log/Log.c")
  20. enum Log_Level
  21. {
  22. Log_Level_KEYS,
  23. Log_Level_DEBUG,
  24. Log_Level_INFO,
  25. Log_Level_WARN,
  26. Log_Level_ERROR,
  27. Log_Level_CRITICAL,
  28. Log_Level_INVALID
  29. };
  30. typedef struct Log Log_t;
  31. char* Log_nameForLevel(enum Log_Level logLevel);
  32. enum Log_Level Log_levelForName(char* name);
  33. #ifdef Log_KEYS
  34. #define Log_DEBUG
  35. #endif
  36. #ifdef Log_DEBUG
  37. #define Log_INFO
  38. #endif
  39. #ifdef Log_INFO
  40. #define Log_WARN
  41. #endif
  42. #ifdef Log_WARN
  43. #define Log_ERROR
  44. #endif
  45. #ifdef Log_ERROR
  46. #define Log_CRITICAL
  47. #endif
  48. // Default
  49. #ifndef Log_CRITICAL
  50. #define Log_INFO
  51. #define Log_WARN
  52. #define Log_ERROR
  53. #define Log_CRITICAL
  54. #endif
  55. Gcc_PRINTF(5,6)
  56. void Log_print(struct Log* log,
  57. enum Log_Level logLevel,
  58. const char* file,
  59. int line,
  60. const char* format,
  61. ...);
  62. void Log_print0(struct Log* log, enum Log_Level lvl, const char* file, int line, const char* msg);
  63. #define Log_printf(log, level, ...) \
  64. do { \
  65. if (log && level >= Log_MIN_LEVEL) { \
  66. Log_print(log, level, Gcc_SHORT_FILE, Gcc_LINE, __VA_ARGS__); \
  67. } \
  68. } while (0)
  69. // CHECKFILES_IGNORE missing ;
  70. #if defined(Log_KEYS)
  71. #define Log_MIN_LEVEL Log_Level_KEYS
  72. #elif defined(Log_DEBUG)
  73. #define Log_MIN_LEVEL Log_Level_DEBUG
  74. #elif defined(Log_INFO)
  75. #define Log_MIN_LEVEL Log_Level_INFO
  76. #elif defined(Log_WARN)
  77. #define Log_MIN_LEVEL Log_Level_WARN
  78. #elif defined(Log_ERROR)
  79. #define Log_MIN_LEVEL Log_Level_ERROR
  80. #elif defined(Log_CRITICAL)
  81. #define Log_MIN_LEVEL Log_Level_CRITICAL
  82. #else
  83. #error
  84. #endif
  85. #define Log_keys(log, ...) Log_printf(log, Log_Level_KEYS, __VA_ARGS__)
  86. #define Log_debug(log, ...) Log_printf(log, Log_Level_DEBUG, __VA_ARGS__)
  87. #define Log_info(log, ...) Log_printf(log, Log_Level_INFO, __VA_ARGS__)
  88. #define Log_warn(log, ...) Log_printf(log, Log_Level_WARN, __VA_ARGS__)
  89. #define Log_error(log, ...) Log_printf(log, Log_Level_ERROR, __VA_ARGS__)
  90. #define Log_critical(log, ...) Log_printf(log, Log_Level_CRITICAL, __VA_ARGS__)
  91. #endif