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