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