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