]>
Commit | Line | Data |
---|---|---|
82b7abe3 AJ |
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 | ||
582c2af2 | 35 | #include "squid.h" |
82b7abe3 AJ |
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" | |
d33e847c | 42 | #include "log/ModTcp.h" |
82b7abe3 AJ |
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 | ||
e0236918 | 52 | debugs(50, DBG_IMPORTANT, "Logfile: opening log " << path); |
82b7abe3 AJ |
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) { | |
9d65168e A |
60 | patharg = path + 6; |
61 | ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 | 62 | } else if (strncmp(path, "daemon:", 7) == 0) { |
9d65168e A |
63 | patharg = path + 7; |
64 | ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag); | |
d33e847c AJ |
65 | } else if (strncmp(path, "tcp:", 4) == 0) { |
66 | patharg = path + 4; | |
67 | ret = logfile_mod_tcp_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 | 68 | } else if (strncmp(path, "udp:", 4) == 0) { |
9d65168e A |
69 | patharg = path + 4; |
70 | ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 AJ |
71 | #if HAVE_SYSLOG |
72 | } else if (strncmp(path, "syslog:", 7) == 0) { | |
9d65168e A |
73 | patharg = path + 7; |
74 | ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag); | |
82b7abe3 AJ |
75 | #endif |
76 | } else { | |
8030a2a0 AJ |
77 | debugs(50, DBG_IMPORTANT, "WARNING: log parameters now start with a module name. Use 'stdio:" << patharg << "'"); |
78 | snprintf(lf->path, MAXPATHLEN, "stdio:%s", patharg); | |
9d65168e | 79 | ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag); |
82b7abe3 AJ |
80 | } |
81 | if (!ret) { | |
9d65168e | 82 | if (fatal_flag) |
8030a2a0 | 83 | fatalf("logfileOpen: %s: couldn't open!\n", path); |
9d65168e | 84 | else |
e0236918 | 85 | debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!"); |
9d65168e A |
86 | lf->f_close(lf); |
87 | cbdataFree(lf); | |
88 | return NULL; | |
82b7abe3 AJ |
89 | } |
90 | assert(lf->data != NULL); | |
91 | ||
92 | if (fatal_flag) | |
93 | lf->flags.fatal = 1; | |
94 | ||
95 | lf->sequence_number = 0; | |
96 | ||
97 | return lf; | |
98 | } | |
99 | ||
100 | void | |
101 | logfileClose(Logfile * lf) | |
102 | { | |
e0236918 | 103 | debugs(50, DBG_IMPORTANT, "Logfile: closing log " << lf->path); |
82b7abe3 AJ |
104 | lf->f_flush(lf); |
105 | lf->f_close(lf); | |
106 | cbdataFree(lf); | |
107 | } | |
108 | ||
109 | void | |
110 | logfileRotate(Logfile * lf) | |
111 | { | |
e0236918 | 112 | debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path); |
82b7abe3 AJ |
113 | lf->f_rotate(lf); |
114 | } | |
115 | ||
116 | void | |
117 | logfileWrite(Logfile * lf, char *buf, size_t len) | |
118 | { | |
119 | lf->f_linewrite(lf, buf, len); | |
120 | } | |
121 | ||
122 | void | |
123 | logfilePrintf(Logfile * lf, const char *fmt,...) | |
124 | { | |
125 | va_list args; | |
126 | char buf[8192]; | |
127 | int s; | |
128 | ||
129 | va_start(args, fmt); | |
130 | ||
131 | s = vsnprintf(buf, 8192, fmt, args); | |
132 | ||
133 | if (s > 8192) { | |
134 | s = 8192; | |
135 | ||
136 | if (fmt[strlen(fmt) - 1] == '\n') | |
137 | buf[8191] = '\n'; | |
138 | } | |
139 | ||
140 | logfileWrite(lf, buf, (size_t) s); | |
141 | va_end(args); | |
142 | } | |
143 | ||
144 | void | |
145 | logfileLineStart(Logfile * lf) | |
146 | { | |
147 | lf->f_linestart(lf); | |
148 | } | |
149 | ||
150 | void | |
151 | logfileLineEnd(Logfile * lf) | |
152 | { | |
153 | lf->f_lineend(lf); | |
d7ae3534 | 154 | ++ lf->sequence_number; |
82b7abe3 AJ |
155 | } |
156 | ||
157 | void | |
158 | logfileFlush(Logfile * lf) | |
159 | { | |
160 | lf->f_flush(lf); | |
161 | } |