chiark / gitweb /
basic: modernize conf-files.c a bit
[elogind.git] / src / basic / syslog-util.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <syslog.h>
23
24 #include "assert.h"
25 #include "hexdecoct.h"
26 #include "string-table.h"
27 #include "syslog-util.h"
28
29 #if 0 /// UNNEEDED by elogind
30 int syslog_parse_priority(const char **p, int *priority, bool with_facility) {
31         int a = 0, b = 0, c = 0;
32         int k;
33
34         assert(p);
35         assert(*p);
36         assert(priority);
37
38         if ((*p)[0] != '<')
39                 return 0;
40
41         if (!strchr(*p, '>'))
42                 return 0;
43
44         if ((*p)[2] == '>') {
45                 c = undecchar((*p)[1]);
46                 k = 3;
47         } else if ((*p)[3] == '>') {
48                 b = undecchar((*p)[1]);
49                 c = undecchar((*p)[2]);
50                 k = 4;
51         } else if ((*p)[4] == '>') {
52                 a = undecchar((*p)[1]);
53                 b = undecchar((*p)[2]);
54                 c = undecchar((*p)[3]);
55                 k = 5;
56         } else
57                 return 0;
58
59         if (a < 0 || b < 0 || c < 0 ||
60             (!with_facility && (a || b || c > 7)))
61                 return 0;
62
63         if (with_facility)
64                 *priority = a*100 + b*10 + c;
65         else
66                 *priority = (*priority & LOG_FACMASK) | c;
67
68         *p += k;
69         return 1;
70 }
71 #endif // 0
72
73 static const char *const log_facility_unshifted_table[LOG_NFACILITIES] = {
74         [LOG_FAC(LOG_KERN)] = "kern",
75         [LOG_FAC(LOG_USER)] = "user",
76         [LOG_FAC(LOG_MAIL)] = "mail",
77         [LOG_FAC(LOG_DAEMON)] = "daemon",
78         [LOG_FAC(LOG_AUTH)] = "auth",
79         [LOG_FAC(LOG_SYSLOG)] = "syslog",
80         [LOG_FAC(LOG_LPR)] = "lpr",
81         [LOG_FAC(LOG_NEWS)] = "news",
82         [LOG_FAC(LOG_UUCP)] = "uucp",
83         [LOG_FAC(LOG_CRON)] = "cron",
84         [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
85         [LOG_FAC(LOG_FTP)] = "ftp",
86         [LOG_FAC(LOG_LOCAL0)] = "local0",
87         [LOG_FAC(LOG_LOCAL1)] = "local1",
88         [LOG_FAC(LOG_LOCAL2)] = "local2",
89         [LOG_FAC(LOG_LOCAL3)] = "local3",
90         [LOG_FAC(LOG_LOCAL4)] = "local4",
91         [LOG_FAC(LOG_LOCAL5)] = "local5",
92         [LOG_FAC(LOG_LOCAL6)] = "local6",
93         [LOG_FAC(LOG_LOCAL7)] = "local7"
94 };
95
96 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_facility_unshifted, int, LOG_FAC(~0));
97
98 #if 0 /// UNNEEDED by elogind
99 bool log_facility_unshifted_is_valid(int facility) {
100         return facility >= 0 && facility <= LOG_FAC(~0);
101 }
102 #endif // 0
103
104 static const char *const log_level_table[] = {
105         [LOG_EMERG] = "emerg",
106         [LOG_ALERT] = "alert",
107         [LOG_CRIT] = "crit",
108         [LOG_ERR] = "err",
109         [LOG_WARNING] = "warning",
110         [LOG_NOTICE] = "notice",
111         [LOG_INFO] = "info",
112         [LOG_DEBUG] = "debug"
113 };
114
115 DEFINE_STRING_TABLE_LOOKUP_WITH_FALLBACK(log_level, int, LOG_DEBUG);
116
117 #if 0 /// UNNEEDED by elogind
118 bool log_level_is_valid(int level) {
119         return level >= 0 && level <= LOG_DEBUG;
120 }
121 #endif // 0