BLog_syslog.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /**
  2. * @file BLog_syslog.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #include <string.h>
  30. #include <stdio.h>
  31. #include <syslog.h>
  32. #include <misc/debug.h>
  33. #include "BLog_syslog.h"
  34. static int resolve_facility (char *str, int *out)
  35. {
  36. if (!strcmp(str, "authpriv")) {
  37. *out = LOG_AUTHPRIV;
  38. }
  39. else if (!strcmp(str, "cron")) {
  40. *out = LOG_CRON;
  41. }
  42. else if (!strcmp(str, "daemon")) {
  43. *out = LOG_DAEMON;
  44. }
  45. else if (!strcmp(str, "ftp")) {
  46. *out = LOG_FTP;
  47. }
  48. else if (!strcmp(str, "local0")) {
  49. *out = LOG_LOCAL0;
  50. }
  51. else if (!strcmp(str, "local1")) {
  52. *out = LOG_LOCAL1;
  53. }
  54. else if (!strcmp(str, "local2")) {
  55. *out = LOG_LOCAL2;
  56. }
  57. else if (!strcmp(str, "local3")) {
  58. *out = LOG_LOCAL3;
  59. }
  60. else if (!strcmp(str, "local4")) {
  61. *out = LOG_LOCAL4;
  62. }
  63. else if (!strcmp(str, "local5")) {
  64. *out = LOG_LOCAL5;
  65. }
  66. else if (!strcmp(str, "local6")) {
  67. *out = LOG_LOCAL6;
  68. }
  69. else if (!strcmp(str, "local7")) {
  70. *out = LOG_LOCAL7;
  71. }
  72. else if (!strcmp(str, "lpr")) {
  73. *out = LOG_LPR;
  74. }
  75. else if (!strcmp(str, "mail")) {
  76. *out = LOG_MAIL;
  77. }
  78. else if (!strcmp(str, "news")) {
  79. *out = LOG_NEWS;
  80. }
  81. else if (!strcmp(str, "syslog")) {
  82. *out = LOG_SYSLOG;
  83. }
  84. else if (!strcmp(str, "user")) {
  85. *out = LOG_USER;
  86. }
  87. else if (!strcmp(str, "uucp")) {
  88. *out = LOG_UUCP;
  89. }
  90. else {
  91. return 0;
  92. }
  93. return 1;
  94. }
  95. static int convert_level (int level)
  96. {
  97. ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG)
  98. switch (level) {
  99. case BLOG_ERROR:
  100. return LOG_ERR;
  101. case BLOG_WARNING:
  102. return LOG_WARNING;
  103. case BLOG_NOTICE:
  104. return LOG_NOTICE;
  105. case BLOG_INFO:
  106. return LOG_INFO;
  107. case BLOG_DEBUG:
  108. return LOG_DEBUG;
  109. default:
  110. ASSERT(0)
  111. return 0;
  112. }
  113. }
  114. struct {
  115. char ident[200];
  116. } syslog_global;
  117. static void syslog_log (int channel, int level, const char *msg)
  118. {
  119. syslog(convert_level(level), "%s: %s", blog_global.channels[channel].name, msg);
  120. }
  121. static void syslog_free (void)
  122. {
  123. closelog();
  124. }
  125. int BLog_InitSyslog (char *ident, char *facility_str)
  126. {
  127. int facility;
  128. if (!resolve_facility(facility_str, &facility)) {
  129. return 0;
  130. }
  131. snprintf(syslog_global.ident, sizeof(syslog_global.ident), "%s", ident);
  132. openlog(syslog_global.ident, 0, facility);
  133. BLog_Init(syslog_log, syslog_free);
  134. return 1;
  135. }