log.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2020 rxi
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to
  6. * deal in the Software without restriction, including without limitation the
  7. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. * sell copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. * IN THE SOFTWARE.
  21. */
  22. #include "log.h"
  23. #define MAX_CALLBACKS 32
  24. typedef struct {
  25. log_LogFn fn;
  26. void *udata;
  27. int level;
  28. } Callback;
  29. static struct {
  30. void *udata;
  31. log_LockFn lock;
  32. int level;
  33. bool quiet;
  34. Callback callbacks[MAX_CALLBACKS];
  35. } L;
  36. static const char *level_strings[] = {
  37. "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
  38. };
  39. #ifdef LOG_USE_COLOR
  40. static const char *level_colors[] = {
  41. "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
  42. };
  43. #endif
  44. static void stdout_callback(log_Event *ev) {
  45. char buf[16];
  46. buf[strftime(buf, sizeof(buf), "%H:%M:%S", ev->time)] = '\0';
  47. #ifdef LOG_USE_COLOR
  48. fprintf(
  49. ev->udata, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
  50. buf, level_colors[ev->level], level_strings[ev->level],
  51. ev->file, ev->line);
  52. #else
  53. fprintf(
  54. ev->udata, "%s %-5s %s:%d: ",
  55. buf, level_strings[ev->level], ev->file, ev->line);
  56. #endif
  57. vfprintf(ev->udata, ev->fmt, ev->ap);
  58. fprintf(ev->udata, "\n");
  59. fflush(ev->udata);
  60. }
  61. static void file_callback(log_Event *ev) {
  62. char buf[64];
  63. buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", ev->time)] = '\0';
  64. fprintf(ev->udata, "%s,",buf);
  65. // fprintf(
  66. // ev->udata, "%s %-5s %s:%d: ",
  67. // buf, level_strings[ev->level], ev->file, ev->line);
  68. vfprintf(ev->udata, ev->fmt, ev->ap);
  69. fprintf(ev->udata, "\n");
  70. fflush(ev->udata);
  71. fdatasync(fileno(ev->udata));
  72. }
  73. static void lock(void) {
  74. if (L.lock) { L.lock(true, L.udata); }
  75. }
  76. static void unlock(void) {
  77. if (L.lock) { L.lock(false, L.udata); }
  78. }
  79. const char* log_level_string(int level) {
  80. return level_strings[level];
  81. }
  82. void log_set_lock(log_LockFn fn, void *udata) {
  83. L.lock = fn;
  84. L.udata = udata;
  85. }
  86. void log_set_level(int level) {
  87. L.level = level;
  88. }
  89. void log_set_quiet(bool enable) {
  90. L.quiet = enable;
  91. }
  92. int log_add_callback(log_LogFn fn, void *udata, int level) {
  93. for (int i = 0; i < MAX_CALLBACKS; i++) {
  94. if (!L.callbacks[i].fn) {
  95. L.callbacks[i] = (Callback) { fn, udata, level };
  96. return 0;
  97. }
  98. }
  99. return -1;
  100. }
  101. int log_add_fp(FILE *fp, int level) {
  102. return log_add_callback(file_callback, fp, level);
  103. }
  104. static void init_event(log_Event *ev, void *udata) {
  105. if (!ev->time) {
  106. time_t t = time(NULL);
  107. ev->time = localtime(&t);
  108. }
  109. ev->udata = udata;
  110. }
  111. void log_log(int level, const char *file, int line, const char *fmt, ...) {
  112. log_Event ev = {
  113. .fmt = fmt,
  114. .file = file,
  115. .line = line,
  116. .level = level,
  117. };
  118. lock();
  119. if (!L.quiet && level >= L.level) {
  120. init_event(&ev, stderr);
  121. va_start(ev.ap, fmt);
  122. stdout_callback(&ev);
  123. va_end(ev.ap);
  124. }
  125. for (int i = 0; i < MAX_CALLBACKS && L.callbacks[i].fn; i++) {
  126. Callback *cb = &L.callbacks[i];
  127. if (level >= cb->level) {
  128. init_event(&ev, cb->udata);
  129. va_start(ev.ap, fmt);
  130. cb->fn(&ev);
  131. va_end(ev.ap);
  132. }
  133. }
  134. unlock();
  135. }