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