BLog_syslog.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /**
  2. * @file BLog_syslog.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. */
  22. #include <string.h>
  23. #include <stdio.h>
  24. #include <syslog.h>
  25. #include <misc/debug.h>
  26. #include "BLog_syslog.h"
  27. static int resolve_facility (char *str, int *out)
  28. {
  29. if (!strcmp(str, "authpriv")) {
  30. *out = LOG_AUTHPRIV;
  31. }
  32. else if (!strcmp(str, "cron")) {
  33. *out = LOG_CRON;
  34. }
  35. else if (!strcmp(str, "daemon")) {
  36. *out = LOG_DAEMON;
  37. }
  38. else if (!strcmp(str, "ftp")) {
  39. *out = LOG_FTP;
  40. }
  41. else if (!strcmp(str, "local0")) {
  42. *out = LOG_LOCAL0;
  43. }
  44. else if (!strcmp(str, "local1")) {
  45. *out = LOG_LOCAL1;
  46. }
  47. else if (!strcmp(str, "local2")) {
  48. *out = LOG_LOCAL2;
  49. }
  50. else if (!strcmp(str, "local3")) {
  51. *out = LOG_LOCAL3;
  52. }
  53. else if (!strcmp(str, "local4")) {
  54. *out = LOG_LOCAL4;
  55. }
  56. else if (!strcmp(str, "local5")) {
  57. *out = LOG_LOCAL5;
  58. }
  59. else if (!strcmp(str, "local6")) {
  60. *out = LOG_LOCAL6;
  61. }
  62. else if (!strcmp(str, "local7")) {
  63. *out = LOG_LOCAL7;
  64. }
  65. else if (!strcmp(str, "lpr")) {
  66. *out = LOG_LPR;
  67. }
  68. else if (!strcmp(str, "mail")) {
  69. *out = LOG_MAIL;
  70. }
  71. else if (!strcmp(str, "news")) {
  72. *out = LOG_NEWS;
  73. }
  74. else if (!strcmp(str, "syslog")) {
  75. *out = LOG_SYSLOG;
  76. }
  77. else if (!strcmp(str, "user")) {
  78. *out = LOG_USER;
  79. }
  80. else if (!strcmp(str, "uucp")) {
  81. *out = LOG_UUCP;
  82. }
  83. else {
  84. return 0;
  85. }
  86. return 1;
  87. }
  88. static int convert_level (int level)
  89. {
  90. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  91. switch (level) {
  92. case BLOG_ERROR:
  93. return LOG_ERR;
  94. case BLOG_WARNING:
  95. return LOG_WARNING;
  96. case BLOG_NOTICE:
  97. return LOG_NOTICE;
  98. case BLOG_INFO:
  99. return LOG_INFO;
  100. case BLOG_DEBUG:
  101. return LOG_DEBUG;
  102. default:
  103. ASSERT(0)
  104. return 0;
  105. }
  106. }
  107. struct {
  108. char ident[200];
  109. } syslog_global;
  110. static void syslog_log (int channel, int level, const char *msg)
  111. {
  112. syslog(convert_level(level), "%s: %s", blog_global.channels[channel].name, msg);
  113. }
  114. static void syslog_free (void)
  115. {
  116. closelog();
  117. }
  118. int BLog_InitSyslog (char *ident, char *facility_str)
  119. {
  120. int facility;
  121. if (!resolve_facility(facility_str, &facility)) {
  122. return 0;
  123. }
  124. snprintf(syslog_global.ident, sizeof(syslog_global.ident), "%s", ident);
  125. openlog(syslog_global.ident, 0, facility);
  126. BLog_Init(syslog_log, syslog_free);
  127. return 1;
  128. }