]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/File.cc
Merged from trunk r12948.
[thirdparty/squid.git] / src / log / File.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 "fde.h"
35 #include "log/File.h"
36 #include "log/ModDaemon.h"
37 #include "log/ModStdio.h"
38 #include "log/ModSyslog.h"
39 #include "log/ModUdp.h"
40 #include "log/TcpLogger.h"
41
42 CBDATA_TYPE(Logfile);
43
44 Logfile *
45 logfileOpen(const char *path, size_t bufsz, int fatal_flag)
46 {
47 int ret;
48 const char *patharg;
49
50 debugs(50, DBG_IMPORTANT, "Logfile: opening log " << path);
51 CBDATA_INIT_TYPE(Logfile);
52
53 Logfile *lf = cbdataAlloc(Logfile);
54 xstrncpy(lf->path, path, MAXPATHLEN);
55 patharg = path;
56 /* need to call the per-logfile-type code */
57 if (strncmp(path, "stdio:", 6) == 0) {
58 patharg = path + 6;
59 ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
60 } else if (strncmp(path, "daemon:", 7) == 0) {
61 patharg = path + 7;
62 ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag);
63 } else if (strncmp(path, "tcp:", 4) == 0) {
64 patharg = path + 4;
65 ret = Log::TcpLogger::Open(lf, patharg, bufsz, fatal_flag);
66 } else if (strncmp(path, "udp:", 4) == 0) {
67 patharg = path + 4;
68 ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag);
69 #if HAVE_SYSLOG
70 } else if (strncmp(path, "syslog:", 7) == 0) {
71 patharg = path + 7;
72 ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag);
73 #endif
74 } else {
75 debugs(50, DBG_IMPORTANT, "WARNING: log name now starts with a module name. Use 'stdio:" << patharg << "'");
76 snprintf(lf->path, MAXPATHLEN, "stdio:%s", patharg);
77 ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
78 }
79 if (!ret) {
80 if (fatal_flag)
81 fatalf("logfileOpen: %s: couldn't open!\n", path);
82 else
83 debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!");
84 lf->f_close(lf);
85 cbdataFree(lf);
86 return NULL;
87 }
88 assert(lf->data != NULL);
89
90 if (fatal_flag)
91 lf->flags.fatal = 1;
92
93 lf->sequence_number = 0;
94
95 return lf;
96 }
97
98 void
99 logfileClose(Logfile * lf)
100 {
101 debugs(50, DBG_IMPORTANT, "Logfile: closing log " << lf->path);
102 lf->f_flush(lf);
103 lf->f_close(lf);
104 cbdataFree(lf);
105 }
106
107 void
108 logfileRotate(Logfile * lf)
109 {
110 debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
111 lf->f_rotate(lf);
112 }
113
114 void
115 logfileWrite(Logfile * lf, char *buf, size_t len)
116 {
117 lf->f_linewrite(lf, buf, len);
118 }
119
120 void
121 logfilePrintf(Logfile * lf, const char *fmt,...)
122 {
123 va_list args;
124 char buf[8192];
125 int s;
126
127 va_start(args, fmt);
128
129 s = vsnprintf(buf, 8192, fmt, args);
130
131 if (s > 8192) {
132 s = 8192;
133
134 if (fmt[strlen(fmt) - 1] == '\n')
135 buf[8191] = '\n';
136 }
137
138 logfileWrite(lf, buf, (size_t) s);
139 va_end(args);
140 }
141
142 void
143 logfileLineStart(Logfile * lf)
144 {
145 lf->f_linestart(lf);
146 }
147
148 void
149 logfileLineEnd(Logfile * lf)
150 {
151 lf->f_lineend(lf);
152 ++ lf->sequence_number;
153 }
154
155 void
156 logfileFlush(Logfile * lf)
157 {
158 lf->f_flush(lf);
159 }