]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModSyslog.cc
c79d9dc7a57b58a59f0fe381e91c4aba42ea5cde
[thirdparty/squid.git] / src / log / ModSyslog.cc
1 /*
2 * DEBUG: section 50 Log file handling
3 * AUTHOR: Duane Wessels
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32
33 #include "squid.h"
34
35 #if HAVE_SYSLOG
36
37 #include "log/File.h"
38 #include "log/ModSyslog.h"
39
40 /* Define LOG_AUTHPRIV as LOG_AUTH on systems still using the old deprecated LOG_AUTH */
41 #if !defined(LOG_AUTHPRIV) && defined(LOG_AUTH)
42 #define LOG_AUTHPRIV LOG_AUTH
43 #endif
44
45 typedef struct {
46 const char *name;
47 int value;
48 } syslog_symbol_t;
49
50 static int
51 syslog_ntoa(const char *s)
52 {
53 #define syslog_symbol(a) #a, a
54 static syslog_symbol_t symbols[] = {
55 #ifdef LOG_AUTHPRIV
56 {syslog_symbol(LOG_AUTHPRIV)},
57 #endif
58 #ifdef LOG_DAEMON
59 {syslog_symbol(LOG_DAEMON)},
60 #endif
61 #ifdef LOG_LOCAL0
62 {syslog_symbol(LOG_LOCAL0)},
63 #endif
64 #ifdef LOG_LOCAL1
65 {syslog_symbol(LOG_LOCAL1)},
66 #endif
67 #ifdef LOG_LOCAL2
68 {syslog_symbol(LOG_LOCAL2)},
69 #endif
70 #ifdef LOG_LOCAL3
71 {syslog_symbol(LOG_LOCAL3)},
72 #endif
73 #ifdef LOG_LOCAL4
74 {syslog_symbol(LOG_LOCAL4)},
75 #endif
76 #ifdef LOG_LOCAL5
77 {syslog_symbol(LOG_LOCAL5)},
78 #endif
79 #ifdef LOG_LOCAL6
80 {syslog_symbol(LOG_LOCAL6)},
81 #endif
82 #ifdef LOG_LOCAL7
83 {syslog_symbol(LOG_LOCAL7)},
84 #endif
85 #ifdef LOG_USER
86 {syslog_symbol(LOG_USER)},
87 #endif
88 #ifdef LOG_ERR
89 {syslog_symbol(LOG_ERR)},
90 #endif
91 #ifdef LOG_WARNING
92 {syslog_symbol(LOG_WARNING)},
93 #endif
94 #ifdef LOG_NOTICE
95 {syslog_symbol(LOG_NOTICE)},
96 #endif
97 #ifdef LOG_INFO
98 {syslog_symbol(LOG_INFO)},
99 #endif
100 #ifdef LOG_DEBUG
101 {syslog_symbol(LOG_DEBUG)},
102 #endif
103 {NULL, 0}
104 };
105 syslog_symbol_t *p;
106
107 for (p = symbols; p->name != NULL; ++p)
108 if (!strcmp(s, p->name) || !strcasecmp(s, p->name + 4))
109 return p->value;
110
111 debugs(1, 1, "Unknown syslog facility/priority '" << s << "'");
112 return 0;
113 }
114
115 typedef struct {
116 int syslog_priority;
117 } l_syslog_t;
118
119 #define PRIORITY_MASK (LOG_ERR | LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG)
120
121 static void
122 logfile_mod_syslog_writeline(Logfile * lf, const char *buf, size_t len)
123 {
124 l_syslog_t *ll = (l_syslog_t *) lf->data;
125 syslog(ll->syslog_priority, "%s", (char *) buf);
126 }
127
128 static void
129 logfile_mod_syslog_linestart(Logfile * lf)
130 {
131 }
132
133 static void
134 logfile_mod_syslog_lineend(Logfile * lf)
135 {
136 }
137
138 static void
139 logfile_mod_syslog_flush(Logfile * lf)
140 {
141 }
142
143 static void
144 logfile_mod_syslog_rotate(Logfile * lf)
145 {
146 }
147
148 static void
149 logfile_mod_syslog_close(Logfile * lf)
150 {
151 xfree(lf->data);
152 lf->data = NULL;
153 }
154
155
156
157 /*
158 * This code expects the path to be syslog:<priority>
159 */
160 int
161 logfile_mod_syslog_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
162 {
163 lf->f_close = logfile_mod_syslog_close;
164 lf->f_linewrite = logfile_mod_syslog_writeline;
165 lf->f_linestart = logfile_mod_syslog_linestart;
166 lf->f_lineend = logfile_mod_syslog_lineend;
167 lf->f_flush = logfile_mod_syslog_flush;
168 lf->f_rotate = logfile_mod_syslog_rotate;
169
170 l_syslog_t *ll = static_cast<l_syslog_t*>(xcalloc(1, sizeof(*ll)));
171 lf->data = ll;
172
173 ll->syslog_priority = LOG_INFO;
174
175 if (path[6] != '\0') {
176 char *priority = xstrdup(path);
177 char *facility = (char *) strchr(priority, '.');
178 if (!facility)
179 facility = (char *) strchr(priority, '|');
180 if (facility) {
181 *facility++ = '\0';
182 ll->syslog_priority |= syslog_ntoa(facility);
183 }
184 ll->syslog_priority |= syslog_ntoa(priority);
185 xfree(priority);
186 if ((ll->syslog_priority & PRIORITY_MASK) == 0)
187 ll->syslog_priority |= LOG_INFO;
188 }
189
190 return 1;
191 }
192 #endif