Discord library in plain C
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. * Copyright (c) 2017 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 <stdio.h>
  23. #include <stdlib.h>
  24. #include <stdarg.h>
  25. #include <string.h>
  26. #include <time.h>
  27. #include <log.h>
  28. static struct {
  29. void *udata;
  30. log_LockFn lock;
  31. FILE *fp;
  32. int level;
  33. int quiet;
  34. } L;
  35. static const char *level_names[] = {
  36. "TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"
  37. };
  38. #ifdef LOG_USE_COLOR
  39. static const char *level_colors[] = {
  40. "\x1b[94m", "\x1b[36m", "\x1b[32m", "\x1b[33m", "\x1b[31m", "\x1b[35m"
  41. };
  42. #endif
  43. static void lock(void) {
  44. if (L.lock) {
  45. L.lock(L.udata, 1);
  46. }
  47. }
  48. static void unlock(void) {
  49. if (L.lock) {
  50. L.lock(L.udata, 0);
  51. }
  52. }
  53. void log_set_udata(void *udata) {
  54. L.udata = udata;
  55. }
  56. void log_set_lock(log_LockFn fn) {
  57. L.lock = fn;
  58. }
  59. void log_set_fp(FILE *fp) {
  60. L.fp = fp;
  61. }
  62. void log_set_level(int level) {
  63. L.level = level;
  64. }
  65. void log_set_quiet(int enable) {
  66. L.quiet = enable ? 1 : 0;
  67. }
  68. void log_log(int level, const char *file, int line, const char *fmt, ...) {
  69. if (level < L.level) {
  70. return;
  71. }
  72. /* Acquire lock */
  73. lock();
  74. /* Get current time */
  75. time_t t = time(NULL);
  76. struct tm *lt = localtime(&t);
  77. /* Log to stderr */
  78. if (!L.quiet) {
  79. va_list args;
  80. char buf[16];
  81. buf[strftime(buf, sizeof(buf), "%H:%M:%S", lt)] = '\0';
  82. #ifdef LOG_USE_COLOR
  83. fprintf(
  84. stderr, "%s %s%-5s\x1b[0m \x1b[90m%s:%d:\x1b[0m ",
  85. buf, level_colors[level], level_names[level], file, line);
  86. #else
  87. fprintf(stderr, "%s %-5s %s:%d: ", buf, level_names[level], file, line);
  88. #endif
  89. va_start(args, fmt);
  90. vfprintf(stderr, fmt, args);
  91. va_end(args);
  92. fprintf(stderr, "\n");
  93. fflush(stderr);
  94. }
  95. /* Log to file */
  96. if (L.fp) {
  97. va_list args;
  98. char buf[32];
  99. buf[strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", lt)] = '\0';
  100. fprintf(L.fp, "%s %-5s %s:%d: ", buf, level_names[level], file, line);
  101. va_start(args, fmt);
  102. vfprintf(L.fp, fmt, args);
  103. va_end(args);
  104. fprintf(L.fp, "\n");
  105. fflush(L.fp);
  106. }
  107. /* Release lock */
  108. unlock();
  109. }