BadVPN – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /** |
2 | * @file BLog_syslog.c |
||
3 | * @author Ambroz Bizjak <ambrop7@gmail.com> |
||
4 | * |
||
5 | * @section LICENSE |
||
6 | * |
||
7 | * Redistribution and use in source and binary forms, with or without |
||
8 | * modification, are permitted provided that the following conditions are met: |
||
9 | * 1. Redistributions of source code must retain the above copyright |
||
10 | * notice, this list of conditions and the following disclaimer. |
||
11 | * 2. Redistributions in binary form must reproduce the above copyright |
||
12 | * notice, this list of conditions and the following disclaimer in the |
||
13 | * documentation and/or other materials provided with the distribution. |
||
14 | * 3. Neither the name of the author nor the |
||
15 | * names of its contributors may be used to endorse or promote products |
||
16 | * derived from this software without specific prior written permission. |
||
17 | * |
||
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
||
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
||
20 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
||
21 | * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
||
22 | * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
||
23 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
24 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
||
25 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||
27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||
28 | */ |
||
29 | |||
30 | #include <string.h> |
||
31 | #include <stdio.h> |
||
32 | #include <syslog.h> |
||
33 | |||
34 | #include <misc/debug.h> |
||
35 | |||
36 | #include "BLog_syslog.h" |
||
37 | |||
38 | static int resolve_facility (char *str, int *out) |
||
39 | { |
||
40 | if (!strcmp(str, "authpriv")) { |
||
41 | *out = LOG_AUTHPRIV; |
||
42 | } |
||
43 | else if (!strcmp(str, "cron")) { |
||
44 | *out = LOG_CRON; |
||
45 | } |
||
46 | else if (!strcmp(str, "daemon")) { |
||
47 | *out = LOG_DAEMON; |
||
48 | } |
||
49 | else if (!strcmp(str, "ftp")) { |
||
50 | *out = LOG_FTP; |
||
51 | } |
||
52 | else if (!strcmp(str, "local0")) { |
||
53 | *out = LOG_LOCAL0; |
||
54 | } |
||
55 | else if (!strcmp(str, "local1")) { |
||
56 | *out = LOG_LOCAL1; |
||
57 | } |
||
58 | else if (!strcmp(str, "local2")) { |
||
59 | *out = LOG_LOCAL2; |
||
60 | } |
||
61 | else if (!strcmp(str, "local3")) { |
||
62 | *out = LOG_LOCAL3; |
||
63 | } |
||
64 | else if (!strcmp(str, "local4")) { |
||
65 | *out = LOG_LOCAL4; |
||
66 | } |
||
67 | else if (!strcmp(str, "local5")) { |
||
68 | *out = LOG_LOCAL5; |
||
69 | } |
||
70 | else if (!strcmp(str, "local6")) { |
||
71 | *out = LOG_LOCAL6; |
||
72 | } |
||
73 | else if (!strcmp(str, "local7")) { |
||
74 | *out = LOG_LOCAL7; |
||
75 | } |
||
76 | else if (!strcmp(str, "lpr")) { |
||
77 | *out = LOG_LPR; |
||
78 | } |
||
79 | else if (!strcmp(str, "mail")) { |
||
80 | *out = LOG_MAIL; |
||
81 | } |
||
82 | else if (!strcmp(str, "news")) { |
||
83 | *out = LOG_NEWS; |
||
84 | } |
||
85 | else if (!strcmp(str, "syslog")) { |
||
86 | *out = LOG_SYSLOG; |
||
87 | } |
||
88 | else if (!strcmp(str, "user")) { |
||
89 | *out = LOG_USER; |
||
90 | } |
||
91 | else if (!strcmp(str, "uucp")) { |
||
92 | *out = LOG_UUCP; |
||
93 | } |
||
94 | else { |
||
95 | return 0; |
||
96 | } |
||
97 | |||
98 | return 1; |
||
99 | } |
||
100 | |||
101 | static int convert_level (int level) |
||
102 | { |
||
103 | ASSERT(level >= BLOG_ERROR && level <= BLOG_DEBUG) |
||
104 | |||
105 | switch (level) { |
||
106 | case BLOG_ERROR: |
||
107 | return LOG_ERR; |
||
108 | case BLOG_WARNING: |
||
109 | return LOG_WARNING; |
||
110 | case BLOG_NOTICE: |
||
111 | return LOG_NOTICE; |
||
112 | case BLOG_INFO: |
||
113 | return LOG_INFO; |
||
114 | case BLOG_DEBUG: |
||
115 | return LOG_DEBUG; |
||
116 | default: |
||
117 | ASSERT(0) |
||
118 | return 0; |
||
119 | } |
||
120 | } |
||
121 | |||
122 | static struct { |
||
123 | char ident[200]; |
||
124 | } syslog_global; |
||
125 | |||
126 | static void syslog_log (int channel, int level, const char *msg) |
||
127 | { |
||
128 | syslog(convert_level(level), "%s: %s", blog_global.channels[channel].name, msg); |
||
129 | } |
||
130 | |||
131 | static void syslog_free (void) |
||
132 | { |
||
133 | closelog(); |
||
134 | } |
||
135 | |||
136 | int BLog_InitSyslog (char *ident, char *facility_str) |
||
137 | { |
||
138 | int facility; |
||
139 | if (!resolve_facility(facility_str, &facility)) { |
||
140 | return 0; |
||
141 | } |
||
142 | |||
143 | snprintf(syslog_global.ident, sizeof(syslog_global.ident), "%s", ident); |
||
144 | |||
145 | openlog(syslog_global.ident, 0, facility); |
||
146 | |||
147 | BLog_Init(syslog_log, syslog_free); |
||
148 | |||
149 | return 1; |
||
150 | } |