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