syslog.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #pragma once
  27. #include <stdarg.h>
  28. __BEGIN_DECLS
  29. struct syslog_data {
  30. const char* ident;
  31. int logopt;
  32. int facility;
  33. int maskpri;
  34. };
  35. /* The severity of the message. This is ordered. */
  36. #define LOG_EMERG 0
  37. #define LOG_ALERT 1
  38. #define LOG_CRIT 2
  39. #define LOG_ERR 3
  40. #define LOG_WARNING 4
  41. #define LOG_NOTICE 5
  42. #define LOG_INFO 6
  43. #define LOG_DEBUG 7
  44. /* Macros for masking out the priority of a combined priority */
  45. #define LOG_PRIMASK (7)
  46. #define LOG_PRI(priority) ((priority) & LOG_PRIMASK)
  47. /*
  48. * Many of these facilities don't really make sense anymore, but we keep them
  49. * for compatibility purposes.
  50. */
  51. #define LOG_KERN ( 0 << 3)
  52. #define LOG_USER ( 1 << 3)
  53. #define LOG_MAIL ( 2 << 3)
  54. #define LOG_DAEMON ( 3 << 3)
  55. #define LOG_AUTH ( 4 << 3)
  56. #define LOG_SYSLOG ( 5 << 3)
  57. #define LOG_LPR ( 6 << 3)
  58. #define LOG_NEWS ( 7 << 3)
  59. #define LOG_UUCP ( 8 << 3)
  60. #define LOG_CRON ( 9 << 3)
  61. #define LOG_AUTHPRIV (10 << 3)
  62. #define LOG_FTP (11 << 3)
  63. /* glibc and OpenBSD reserve 12..15 for future system usage, we will too */
  64. #define LOG_LOCAL0 (16 << 3)
  65. #define LOG_LOCAL1 (17 << 3)
  66. #define LOG_LOCAL2 (18 << 3)
  67. #define LOG_LOCAL3 (19 << 3)
  68. #define LOG_LOCAL4 (20 << 3)
  69. #define LOG_LOCAL5 (21 << 3)
  70. #define LOG_LOCAL6 (22 << 3)
  71. #define LOG_LOCAL7 (23 << 3)
  72. #define LOG_NFACILITIES 24
  73. /* Macros to get the facility from a combined priority. */
  74. #define LOG_FACMASK (~7)
  75. #define LOG_FAC(priority) (((priority) & LOG_FACMASK) >> 3)
  76. /* For masking logs, we use these macros with just the priority. */
  77. #define LOG_MASK(priority) (1 << (priority))
  78. #define LOG_UPTO(priority) (LOG_MASK(priority) + (LOG_MASK(priority) - 1))
  79. /* Macro to make a combined priority. */
  80. #define LOG_MAKEPRI(facility, priority) ((facility) | (priority))
  81. /* Include a PID with the message. */
  82. #define LOG_PID (1 << 0)
  83. /* Log on the console. */
  84. #define LOG_CONS (1 << 1)
  85. /* Open the syslogd connection at the first call. (not implemented, default) */
  86. #define LOG_ODELAY (1 << 2)
  87. /* Open the syslogd connection immediately. (not implemented) */
  88. #define LOG_NDELAY (1 << 3)
  89. /* Log to stderr as well. */
  90. #define LOG_PERROR (1 << 4)
  91. /* This is useful to have, but has to be stored weirdly for compatibility. */
  92. #ifdef SYSLOG_NAMES
  93. /* Used for marking the fallback; some applications check for these defines. */
  94. # define INTERNAL_NOPRI 0x10
  95. # define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES << 3, 0)
  96. typedef struct _code {
  97. /*
  98. * Most Unices define this as char*, but in C++, we have to define it as a
  99. * const char* if we want to use string constants.
  100. */
  101. const char* c_name;
  102. int c_val;
  103. } CODE;
  104. /*
  105. * The names we use are the same as what glibc and OpenBSD use. We omit
  106. * deprecated values in the hope that no one uses them. Sorted, as well.
  107. */
  108. CODE prioritynames[] = {
  109. { "alert", LOG_ALERT },
  110. { "crit", LOG_CRIT },
  111. { "debug", LOG_DEBUG },
  112. { "emerg", LOG_EMERG },
  113. { "err", LOG_ERR },
  114. { "info", LOG_INFO },
  115. /* Fallback */
  116. { "none", INTERNAL_NOPRI },
  117. { "notice", LOG_NOTICE },
  118. { "warning", LOG_WARNING },
  119. { NULL, -1 },
  120. };
  121. CODE facilitynames[] = {
  122. { "auth", LOG_AUTH },
  123. { "authpriv", LOG_AUTHPRIV },
  124. { "cron", LOG_CRON },
  125. { "daemon", LOG_DAEMON },
  126. { "ftp", LOG_FTP },
  127. { "kern", LOG_KERN },
  128. { "local0", LOG_LOCAL0 },
  129. { "local1", LOG_LOCAL1 },
  130. { "local2", LOG_LOCAL2 },
  131. { "local3", LOG_LOCAL3 },
  132. { "local4", LOG_LOCAL4 },
  133. { "local5", LOG_LOCAL5 },
  134. { "local6", LOG_LOCAL6 },
  135. { "local7", LOG_LOCAL7 },
  136. { "lpr", LOG_LPR },
  137. { "mail", LOG_MAIL },
  138. /* Fallback */
  139. { "mark", INTERNAL_MARK },
  140. { "news", LOG_NEWS },
  141. { "syslog", LOG_SYSLOG },
  142. { "user", LOG_USER },
  143. { "uucp", LOG_UUCP },
  144. { NULL, -1 },
  145. };
  146. #endif
  147. /* The re-entrant versions are an OpenBSD extension we also implement. */
  148. void syslog(int, const char*, ...);
  149. void syslog_r(int, struct syslog_data*, const char*, ...);
  150. void vsyslog(int, const char* message, va_list);
  151. void vsyslog_r(int, struct syslog_data* data, const char* message, va_list);
  152. void openlog(const char*, int, int);
  153. void openlog_r(const char*, int, int, struct syslog_data*);
  154. void closelog(void);
  155. void closelog_r(struct syslog_data*);
  156. int setlogmask(int);
  157. int setlogmask_r(int, struct syslog_data*);
  158. __END_DECLS