1
0

Log.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. Log_printf(0, Log_Level_KEYS, __VA_ARGS__)
  75. #endif
  76. #ifdef Log_DEBUG
  77. #define Log_debug(log, ...) \
  78. Log_printf(log, Log_Level_DEBUG, __VA_ARGS__)
  79. #else
  80. #define Log_debug(log, ...) \
  81. Log_printf(0, Log_Level_DEBUG, __VA_ARGS__)
  82. #endif
  83. #ifdef Log_INFO
  84. #define Log_info(log, ...) \
  85. Log_printf(log, Log_Level_INFO, __VA_ARGS__)
  86. #else
  87. #define Log_info(log, ...) \
  88. Log_printf(0, Log_Level_INFO, __VA_ARGS__)
  89. #endif
  90. #ifdef Log_WARN
  91. #define Log_warn(log, ...) \
  92. Log_printf(log, Log_Level_WARN, __VA_ARGS__)
  93. #else
  94. #define Log_warn(log, ...) \
  95. Log_printf(0, Log_Level_WARN, __VA_ARGS__)
  96. #endif
  97. #ifdef Log_ERROR
  98. #define Log_error(log, ...) \
  99. Log_printf(log, Log_Level_ERROR, __VA_ARGS__)
  100. #else
  101. #define Log_error(log, ...) \
  102. Log_printf(0, Log_Level_ERROR, __VA_ARGS__)
  103. #endif
  104. #ifdef Log_CRITICAL
  105. #define Log_critical(log, ...) \
  106. Log_printf(log, Log_Level_CRITICAL, __VA_ARGS__)
  107. #else
  108. #define Log_critical(log, ...) \
  109. Log_printf(0, Log_Level_CRITICAL, __VA_ARGS__)
  110. #endif
  111. #endif