ModSyslog.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 50 Log file handling */
10
11#include "squid.h"
12#include "debug/Stream.h"
13
14#if HAVE_SYSLOG
15
16#include "log/File.h"
17#include "log/ModSyslog.h"
18
19/* Define LOG_AUTHPRIV as LOG_AUTH on systems still using the old deprecated LOG_AUTH */
20#if !defined(LOG_AUTHPRIV) && defined(LOG_AUTH)
21#define LOG_AUTHPRIV LOG_AUTH
22#endif
23
24typedef struct {
25 const char *name;
26 int value;
27} syslog_symbol_t;
28
29static int
30syslog_ntoa(const char *s)
31{
32#define syslog_symbol(a) #a, a
33 static syslog_symbol_t symbols[] = {
34#ifdef LOG_AUTHPRIV
35 {syslog_symbol(LOG_AUTHPRIV)},
36#endif
37#ifdef LOG_DAEMON
38 {syslog_symbol(LOG_DAEMON)},
39#endif
40#ifdef LOG_LOCAL0
41 {syslog_symbol(LOG_LOCAL0)},
42#endif
43#ifdef LOG_LOCAL1
44 {syslog_symbol(LOG_LOCAL1)},
45#endif
46#ifdef LOG_LOCAL2
47 {syslog_symbol(LOG_LOCAL2)},
48#endif
49#ifdef LOG_LOCAL3
50 {syslog_symbol(LOG_LOCAL3)},
51#endif
52#ifdef LOG_LOCAL4
53 {syslog_symbol(LOG_LOCAL4)},
54#endif
55#ifdef LOG_LOCAL5
56 {syslog_symbol(LOG_LOCAL5)},
57#endif
58#ifdef LOG_LOCAL6
59 {syslog_symbol(LOG_LOCAL6)},
60#endif
61#ifdef LOG_LOCAL7
62 {syslog_symbol(LOG_LOCAL7)},
63#endif
64#ifdef LOG_USER
65 {syslog_symbol(LOG_USER)},
66#endif
67#ifdef LOG_ERR
68 {syslog_symbol(LOG_ERR)},
69#endif
70#ifdef LOG_WARNING
71 {syslog_symbol(LOG_WARNING)},
72#endif
73#ifdef LOG_NOTICE
74 {syslog_symbol(LOG_NOTICE)},
75#endif
76#ifdef LOG_INFO
77 {syslog_symbol(LOG_INFO)},
78#endif
79#ifdef LOG_DEBUG
80 {syslog_symbol(LOG_DEBUG)},
81#endif
82 {nullptr, 0}
83 };
84 syslog_symbol_t *p;
85
86 for (p = symbols; p->name != nullptr; ++p)
87 if (!strcmp(s, p->name) || !strcasecmp(s, p->name + 4))
88 return p->value;
89
90 debugs(1, DBG_IMPORTANT, "ERROR: Unknown syslog facility/priority '" << s << "'");
91 return 0;
92}
93
94typedef struct {
95 int syslog_priority;
96} l_syslog_t;
97
98#define PRIORITY_MASK (LOG_ERR | LOG_WARNING | LOG_NOTICE | LOG_INFO | LOG_DEBUG)
99
100static void
101logfile_mod_syslog_writeline(Logfile * lf, const char *buf, size_t)
102{
103 l_syslog_t *ll = (l_syslog_t *) lf->data;
104 syslog(ll->syslog_priority, "%s", (char *) buf);
105}
106
107static void
108logfile_mod_syslog_linestart(Logfile *)
109{
110}
111
112static void
113logfile_mod_syslog_lineend(Logfile *)
114{
115}
116
117static void
118logfile_mod_syslog_flush(Logfile *)
119{
120}
121
122static void
123logfile_mod_syslog_rotate(Logfile *, const int16_t)
124{
125}
126
127static void
128logfile_mod_syslog_close(Logfile *lf)
129{
130 xfree(lf->data);
131 lf->data = nullptr;
132}
133
134/*
135 * This code expects the path to be syslog:<priority>
136 */
137int
138logfile_mod_syslog_open(Logfile * lf, const char *path, size_t, int)
139{
140 lf->f_close = logfile_mod_syslog_close;
141 lf->f_linewrite = logfile_mod_syslog_writeline;
142 lf->f_linestart = logfile_mod_syslog_linestart;
143 lf->f_lineend = logfile_mod_syslog_lineend;
144 lf->f_flush = logfile_mod_syslog_flush;
145 lf->f_rotate = logfile_mod_syslog_rotate;
146
147 l_syslog_t *ll = static_cast<l_syslog_t*>(xcalloc(1, sizeof(*ll)));
148 lf->data = ll;
149
150 ll->syslog_priority = LOG_INFO;
151
152 if (path[6] != '\0') {
153 char *priority = xstrdup(path);
154 char *facility = (char *) strchr(priority, '.');
155 if (!facility)
156 facility = (char *) strchr(priority, '|');
157 if (facility) {
158 *facility = '\0';
159 ++facility;
160 ll->syslog_priority |= syslog_ntoa(facility);
161 }
162 ll->syslog_priority |= syslog_ntoa(priority);
163 xfree(priority);
164 if ((ll->syslog_priority & PRIORITY_MASK) == 0)
165 ll->syslog_priority |= LOG_INFO;
166 }
167
168 return 1;
169}
170#endif
171
int logfile_mod_syslog_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
Definition: File.h:39
LOGLINEEND * f_lineend
Definition: File.h:59
LOGFLUSH * f_flush
Definition: File.h:60
void * data
Definition: File.h:55
LOGCLOSE * f_close
Definition: File.h:62
LOGLINESTART * f_linestart
Definition: File.h:57
LOGWRITE * f_linewrite
Definition: File.h:58
LOGROTATE * f_rotate
Definition: File.h:61
#define DBG_IMPORTANT
Definition: Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define xfree
#define xstrdup
void * xcalloc(size_t n, size_t sz)
Definition: xalloc.cc:71

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors