syslog.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <stdarg.h>
  8. #include <sys/cdefs.h>
  9. __BEGIN_DECLS
  10. struct syslog_data {
  11. char const* ident;
  12. int logopt;
  13. int facility;
  14. int maskpri;
  15. };
  16. /* The severity of the message. This is ordered. */
  17. #define LOG_EMERG 0
  18. #define LOG_ALERT 1
  19. #define LOG_CRIT 2
  20. #define LOG_ERR 3
  21. #define LOG_WARNING 4
  22. #define LOG_NOTICE 5
  23. #define LOG_INFO 6
  24. #define LOG_DEBUG 7
  25. /* Macros for masking out the priority of a combined priority */
  26. #define LOG_PRIMASK (7)
  27. #define LOG_PRI(priority) ((priority)&LOG_PRIMASK)
  28. /*
  29. * Many of these facilities don't really make sense anymore, but we keep them
  30. * for compatibility purposes.
  31. */
  32. #define LOG_KERN (0 << 3)
  33. #define LOG_USER (1 << 3)
  34. #define LOG_MAIL (2 << 3)
  35. #define LOG_DAEMON (3 << 3)
  36. #define LOG_AUTH (4 << 3)
  37. #define LOG_SYSLOG (5 << 3)
  38. #define LOG_LPR (6 << 3)
  39. #define LOG_NEWS (7 << 3)
  40. #define LOG_UUCP (8 << 3)
  41. #define LOG_CRON (9 << 3)
  42. #define LOG_AUTHPRIV (10 << 3)
  43. #define LOG_FTP (11 << 3)
  44. /* glibc and OpenBSD reserve 12..15 for future system usage, we will too */
  45. #define LOG_LOCAL0 (16 << 3)
  46. #define LOG_LOCAL1 (17 << 3)
  47. #define LOG_LOCAL2 (18 << 3)
  48. #define LOG_LOCAL3 (19 << 3)
  49. #define LOG_LOCAL4 (20 << 3)
  50. #define LOG_LOCAL5 (21 << 3)
  51. #define LOG_LOCAL6 (22 << 3)
  52. #define LOG_LOCAL7 (23 << 3)
  53. #define LOG_NFACILITIES 24
  54. /* Macros to get the facility from a combined priority. */
  55. #define LOG_FACMASK (~7)
  56. #define LOG_FAC(priority) (((priority)&LOG_FACMASK) >> 3)
  57. /* For masking logs, we use these macros with just the priority. */
  58. #define LOG_MASK(priority) (1 << (priority))
  59. #define LOG_UPTO(priority) (LOG_MASK(priority) + (LOG_MASK(priority) - 1))
  60. /* Macro to make a combined priority. */
  61. #define LOG_MAKEPRI(facility, priority) ((facility) | (priority))
  62. /* Include a PID with the message. */
  63. #define LOG_PID (1 << 0)
  64. /* Log on the console. */
  65. #define LOG_CONS (1 << 1)
  66. /* Open the syslogd connection at the first call. (not implemented, default) */
  67. #define LOG_ODELAY (1 << 2)
  68. /* Open the syslogd connection immediately. (not implemented) */
  69. #define LOG_NDELAY (1 << 3)
  70. /* Log to stderr as well. */
  71. #define LOG_PERROR (1 << 4)
  72. /* Don't wait for child processes created while logging the message. */
  73. #define LOG_NOWAIT (1 << 5)
  74. /* This is useful to have, but has to be stored weirdly for compatibility. */
  75. #ifdef SYSLOG_NAMES
  76. /* Used for marking the fallback; some applications check for these defines. */
  77. # define INTERNAL_NOPRI 0x10
  78. # define INTERNAL_MARK LOG_MAKEPRI(LOG_NFACILITIES << 3, 0)
  79. typedef struct _code {
  80. /*
  81. * Most Unices define this as char*, but in C++, we have to define it as a
  82. * const char* if we want to use string constants.
  83. */
  84. char const* c_name;
  85. int c_val;
  86. } CODE;
  87. /*
  88. * The names we use are the same as what glibc and OpenBSD use. We omit
  89. * deprecated values in the hope that no one uses them. Sorted, as well.
  90. */
  91. CODE prioritynames[] = {
  92. { "alert", LOG_ALERT },
  93. { "crit", LOG_CRIT },
  94. { "debug", LOG_DEBUG },
  95. { "emerg", LOG_EMERG },
  96. { "err", LOG_ERR },
  97. { "info", LOG_INFO },
  98. /* Fallback */
  99. { "none", INTERNAL_NOPRI },
  100. { "notice", LOG_NOTICE },
  101. { "warning", LOG_WARNING },
  102. { NULL, -1 },
  103. };
  104. CODE facilitynames[] = {
  105. { "auth", LOG_AUTH },
  106. { "authpriv", LOG_AUTHPRIV },
  107. { "cron", LOG_CRON },
  108. { "daemon", LOG_DAEMON },
  109. { "ftp", LOG_FTP },
  110. { "kern", LOG_KERN },
  111. { "local0", LOG_LOCAL0 },
  112. { "local1", LOG_LOCAL1 },
  113. { "local2", LOG_LOCAL2 },
  114. { "local3", LOG_LOCAL3 },
  115. { "local4", LOG_LOCAL4 },
  116. { "local5", LOG_LOCAL5 },
  117. { "local6", LOG_LOCAL6 },
  118. { "local7", LOG_LOCAL7 },
  119. { "lpr", LOG_LPR },
  120. { "mail", LOG_MAIL },
  121. /* Fallback */
  122. { "mark", INTERNAL_MARK },
  123. { "news", LOG_NEWS },
  124. { "syslog", LOG_SYSLOG },
  125. { "user", LOG_USER },
  126. { "uucp", LOG_UUCP },
  127. { NULL, -1 },
  128. };
  129. #endif
  130. /* The re-entrant versions are an OpenBSD extension we also implement. */
  131. void syslog(int, char const*, ...);
  132. void syslog_r(int, struct syslog_data*, char const*, ...);
  133. void vsyslog(int, char const* message, va_list);
  134. void vsyslog_r(int, struct syslog_data* data, char const* message, va_list);
  135. void openlog(char const*, int, int);
  136. void openlog_r(char const*, int, int, struct syslog_data*);
  137. void closelog(void);
  138. void closelog_r(struct syslog_data*);
  139. int setlogmask(int);
  140. int setlogmask_r(int, struct syslog_data*);
  141. __END_DECLS