]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/ModSyslog.cc
SourceFormat Enforcement
[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 #include "Debug.h"
35
36 #if HAVE_SYSLOG
37
38 #include "log/File.h"
39 #include "log/ModSyslog.h"
40
41 /* Define LOG_AUTHPRIV as LOG_AUTH on systems still using the old deprecated LOG_AUTH */
42 #if !defined(LOG_AUTHPRIV) && defined(LOG_AUTH)
43 #define LOG_AUTHPRIV LOG_AUTH
44 #endif
45
46 typedef struct {
47 const char *name;
48 int value;
49 } syslog_symbol_t;
50
51 static int
52 syslog_ntoa(const char *s)
53 {
54 #define syslog_symbol(a) #a, a
55 static syslog_symbol_t symbols[] = {
56 #ifdef LOG_AUTHPRIV
57 {syslog_symbol(LOG_AUTHPRIV)},
58 #endif
59 #ifdef LOG_DAEMON
60 {syslog_symbol(LOG_DAEMON)},
61 #endif
62 #ifdef LOG_LOCAL0
63 {syslog_symbol(LOG_LOCAL0)},
64 #endif
65 #ifdef LOG_LOCAL1
66 {syslog_symbol(LOG_LOCAL1)},
67 #endif
68 #ifdef LOG_LOCAL2
69 {syslog_symbol(LOG_LOCAL2)},
70 #endif
71 #ifdef LOG_LOCAL3
72 {syslog_symbol(LOG_LOCAL3)},
73 #endif
74 #ifdef LOG_LOCAL4
75 {syslog_symbol(LOG_LOCAL4)},
76 #endif
77 #ifdef LOG_LOCAL5
78 {syslog_symbol(LOG_LOCAL5)},
79 #endif
80 #ifdef LOG_LOCAL6
81 {syslog_symbol(LOG_LOCAL6)},
82 #endif
83 #ifdef LOG_LOCAL7
84 {syslog_symbol(LOG_LOCAL7)},
85 #endif
86 #ifdef LOG_USER
87 {syslog_symbol(LOG_USER)},
88 #endif
89 #ifdef LOG_ERR
90 {syslog_symbol(LOG_ERR)},
91 #endif
92 #ifdef LOG_WARNING
93 {syslog_symbol(LOG_WARNING)},
94 #endif
95 #ifdef LOG_NOTICE
96 {syslog_symbol(LOG_NOTICE)},
97 #endif
98 #ifdef LOG_INFO
99 {syslog_symbol(LOG_INFO)},
100 #endif
101 #ifdef LOG_DEBUG
102 {syslog_symbol(LOG_DEBUG)},
103 #endif
104 {NULL, 0}
105 };
106 syslog_symbol_t *p;
107
108 for (p = symbols; p->name != NULL; ++p)
109 if (!strcmp(s, p->name) || !strcasecmp(s, p->name + 4))
110 return p->value;
111
112 debugs(1, DBG_IMPORTANT, "Unknown syslog facility/priority '" << s << "'");
113 return 0;
114 }
115
116 typedef struct {
117 int syslog_priority;
118 } l_syslog_t;
119
120 #define PRIORITY_MASK (LOG_ERR | LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG)
121
122 static void
123 logfile_mod_syslog_writeline(Logfile * lf, const char *buf, size_t len)
124 {
125 l_syslog_t *ll = (l_syslog_t *) lf->data;
126 syslog(ll->syslog_priority, "%s", (char *) buf);
127 }
128
129 static void
130 logfile_mod_syslog_linestart(Logfile * lf)
131 {
132 }
133
134 static void
135 logfile_mod_syslog_lineend(Logfile * lf)
136 {
137 }
138
139 static void
140 logfile_mod_syslog_flush(Logfile * lf)
141 {
142 }
143
144 static void
145 logfile_mod_syslog_rotate(Logfile * lf)
146 {
147 }
148
149 static void
150 logfile_mod_syslog_close(Logfile * lf)
151 {
152 xfree(lf->data);
153 lf->data = NULL;
154 }
155
156 /*
157 * This code expects the path to be syslog:<priority>
158 */
159 int
160 logfile_mod_syslog_open(Logfile * lf, const char *path, size_t bufsz, int fatal_flag)
161 {
162 lf->f_close = logfile_mod_syslog_close;
163 lf->f_linewrite = logfile_mod_syslog_writeline;
164 lf->f_linestart = logfile_mod_syslog_linestart;
165 lf->f_lineend = logfile_mod_syslog_lineend;
166 lf->f_flush = logfile_mod_syslog_flush;
167 lf->f_rotate = logfile_mod_syslog_rotate;
168
169 l_syslog_t *ll = static_cast<l_syslog_t*>(xcalloc(1, sizeof(*ll)));
170 lf->data = ll;
171
172 ll->syslog_priority = LOG_INFO;
173
174 if (path[6] != '\0') {
175 char *priority = xstrdup(path);
176 char *facility = (char *) strchr(priority, '.');
177 if (!facility)
178 facility = (char *) strchr(priority, '|');
179 if (facility) {
180 *facility = '\0';
181 ++facility;
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