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