Log.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <http://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. struct Log;
  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. #define Log_printf(log, level, ...) \
  63. do { \
  64. if (log) { \
  65. Log_print(log, level, Gcc_SHORT_FILE, Gcc_LINE, __VA_ARGS__); \
  66. } \
  67. } while (0)
  68. // CHECKFILES_IGNORE missing ;
  69. #ifdef Log_KEYS
  70. #define Log_keys(log, ...) \
  71. Log_printf(log, Log_Level_KEYS, __VA_ARGS__)
  72. #else
  73. #define Log_keys(log, ...)
  74. #endif
  75. #ifdef Log_DEBUG
  76. #define Log_debug(log, ...) \
  77. Log_printf(log, Log_Level_DEBUG, __VA_ARGS__)
  78. #else
  79. #define Log_debug(log, ...)
  80. #endif
  81. #ifdef Log_INFO
  82. #define Log_info(log, ...) \
  83. Log_printf(log, Log_Level_INFO, __VA_ARGS__)
  84. #else
  85. #define Log_info(log, ...)
  86. #endif
  87. #ifdef Log_WARN
  88. #define Log_warn(log, ...) \
  89. Log_printf(log, Log_Level_WARN, __VA_ARGS__)
  90. #else
  91. #define Log_warn(log, ...)
  92. #endif
  93. #ifdef Log_ERROR
  94. #define Log_error(log, ...) \
  95. Log_printf(log, Log_Level_ERROR, __VA_ARGS__)
  96. #else
  97. #define Log_error(log, ...)
  98. #endif
  99. #ifdef Log_CRITICAL
  100. #define Log_critical(log, ...) \
  101. Log_printf(log, Log_Level_CRITICAL, __VA_ARGS__)
  102. #else
  103. #define Log_critical(log, ...)
  104. #endif
  105. #endif