You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

147 lines
3.9 KiB

  1. #include "log.h"
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <stdarg.h>
  5. #include <string.h>
  6. #include <stdbool.h>
  7. #include <errno.h>
  8. #include <assert.h>
  9. #include <unistd.h>
  10. #include <syslog.h>
  11. static bool colorize = false;
  12. static bool do_syslog = true;
  13. void
  14. log_init(enum log_colorize _colorize, bool _do_syslog,
  15. enum log_facility syslog_facility, enum log_class syslog_level)
  16. {
  17. static const int facility_map[] = {
  18. [LOG_FACILITY_USER] = LOG_USER,
  19. [LOG_FACILITY_DAEMON] = LOG_DAEMON,
  20. };
  21. static const int level_map[] = {
  22. [LOG_CLASS_ERROR] = LOG_ERR,
  23. [LOG_CLASS_WARNING] = LOG_WARNING,
  24. [LOG_CLASS_INFO] = LOG_INFO,
  25. [LOG_CLASS_DEBUG] = LOG_DEBUG,
  26. };
  27. colorize = _colorize == LOG_COLORIZE_NEVER ? false : _colorize == LOG_COLORIZE_ALWAYS ? true : isatty(STDERR_FILENO);
  28. do_syslog = _do_syslog;
  29. if (do_syslog) {
  30. openlog(NULL, /*LOG_PID*/0, facility_map[syslog_facility]);
  31. setlogmask(LOG_UPTO(level_map[syslog_level]));
  32. }
  33. }
  34. void
  35. log_deinit(void)
  36. {
  37. if (do_syslog)
  38. closelog();
  39. }
  40. static void
  41. _log(enum log_class log_class, const char *module, const char *file, int lineno,
  42. const char *fmt, int sys_errno, va_list va)
  43. {
  44. const char *class = "abcd";
  45. int class_clr = 0;
  46. switch (log_class) {
  47. case LOG_CLASS_ERROR: class = " err"; class_clr = 31; break;
  48. case LOG_CLASS_WARNING: class = "warn"; class_clr = 33; break;
  49. case LOG_CLASS_INFO: class = "info"; class_clr = 97; break;
  50. case LOG_CLASS_DEBUG: class = " dbg"; class_clr = 36; break;
  51. }
  52. char clr[16];
  53. snprintf(clr, sizeof(clr), "\033[%dm", class_clr);
  54. fprintf(stderr, "%s%s%s: ", colorize ? clr : "", class, colorize ? "\033[0m" : "");
  55. if (colorize)
  56. fprintf(stderr, "\033[2m");
  57. fprintf(stderr, "%s:%d: ", file, lineno);
  58. if (colorize)
  59. fprintf(stderr, "\033[0m");
  60. vfprintf(stderr, fmt, va);
  61. if (sys_errno != 0)
  62. fprintf(stderr, ": %s", strerror(sys_errno));
  63. fprintf(stderr, "\n");
  64. }
  65. static void
  66. _sys_log(enum log_class log_class, const char *module,
  67. const char UNUSED *file, int UNUSED lineno,
  68. const char *fmt, int sys_errno, va_list va)
  69. {
  70. if (!do_syslog)
  71. return;
  72. /* Map our log level to syslog's level */
  73. int level = -1;
  74. switch (log_class) {
  75. case LOG_CLASS_ERROR: level = LOG_ERR; break;
  76. case LOG_CLASS_WARNING: level = LOG_WARNING; break;
  77. case LOG_CLASS_INFO: level = LOG_INFO; break;
  78. case LOG_CLASS_DEBUG: level = LOG_DEBUG; break;
  79. }
  80. assert(level != -1);
  81. char msg[4096];
  82. int n = vsnprintf(msg, sizeof(msg), fmt, va);
  83. assert(n >= 0);
  84. if (sys_errno != 0 && (size_t)n < sizeof(msg))
  85. snprintf(msg + n, sizeof(msg) - n, ": %s", strerror(sys_errno));
  86. syslog(level, "%s: %s", module, msg);
  87. }
  88. void
  89. log_msg(enum log_class log_class, const char *module,
  90. const char *file, int lineno, const char *fmt, ...)
  91. {
  92. va_list ap1, ap2;
  93. va_start(ap1, fmt);
  94. va_copy(ap2, ap1);
  95. _log(log_class, module, file, lineno, fmt, 0, ap1);
  96. _sys_log(log_class, module, file, lineno, fmt, 0, ap2);
  97. va_end(ap1);
  98. va_end(ap2);
  99. }
  100. void log_errno(enum log_class log_class, const char *module,
  101. const char *file, int lineno,
  102. const char *fmt, ...)
  103. {
  104. va_list ap1, ap2;
  105. va_start(ap1, fmt);
  106. va_copy(ap2, ap1);
  107. _log(log_class, module, file, lineno, fmt, errno, ap1);
  108. _sys_log(log_class, module, file, lineno, fmt, errno, ap2);
  109. va_end(ap1);
  110. va_end(ap2);
  111. }
  112. void log_errno_provided(enum log_class log_class, const char *module,
  113. const char *file, int lineno, int _errno,
  114. const char *fmt, ...)
  115. {
  116. va_list ap1, ap2;
  117. va_start(ap1, fmt);
  118. va_copy(ap2, ap1);
  119. _log(log_class, module, file, lineno, fmt, _errno, ap1);
  120. _sys_log(log_class, module, file, lineno, fmt, _errno, ap2);
  121. va_end(ap1);
  122. va_end(ap2);
  123. }