]> git.ipfire.org Git - thirdparty/squid.git/blob - src/log/File.cc
Author: Dhaval Varia <dhavalkvaria@gmail.com>
[thirdparty/squid.git] / src / log / File.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 50 Log file handling
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 */
34
35 #include "squid.h"
36 #include "fde.h"
37 #include "log/File.h"
38 #include "log/ModDaemon.h"
39 #include "log/ModStdio.h"
40 #include "log/ModSyslog.h"
41 #include "log/ModUdp.h"
42 #include "log/ModTcp.h"
43
44 CBDATA_TYPE(Logfile);
45
46 Logfile *
47 logfileOpen(const char *path, size_t bufsz, int fatal_flag)
48 {
49 int ret;
50 const char *patharg;
51
52 debugs(50, 1, "Logfile: opening log " << path);
53 CBDATA_INIT_TYPE(Logfile);
54
55 Logfile *lf = cbdataAlloc(Logfile);
56 xstrncpy(lf->path, path, MAXPATHLEN);
57 patharg = path;
58 /* need to call the per-logfile-type code */
59 if (strncmp(path, "stdio:", 6) == 0) {
60 patharg = path + 6;
61 ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
62 } else if (strncmp(path, "daemon:", 7) == 0) {
63 patharg = path + 7;
64 ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag);
65 } else if (strncmp(path, "tcp:", 4) == 0) {
66 patharg = path + 4;
67 ret = logfile_mod_tcp_open(lf, patharg, bufsz, fatal_flag);
68 } else if (strncmp(path, "udp:", 4) == 0) {
69 patharg = path + 4;
70 ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag);
71 #if HAVE_SYSLOG
72 } else if (strncmp(path, "syslog:", 7) == 0) {
73 patharg = path + 7;
74 ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag);
75 #endif
76 } else {
77 ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
78 }
79 if (!ret) {
80 if (fatal_flag)
81 fatalf("logfileOpen: path %s: couldn't open!\n", path);
82 else
83 debugs(50, 1, "logfileOpen: path " << 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, 1, "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, 1, "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 }