]> git.ipfire.org Git - thirdparty/squid.git/blame - src/log/File.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / log / File.cc
CommitLineData
82b7abe3 1/*
f70aedc4 2 * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
82b7abe3 3 *
bbc27441
AJ
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.
82b7abe3
AJ
7 */
8
bbc27441
AJ
9/* DEBUG: section 50 Log file handling */
10
582c2af2 11#include "squid.h"
ed6e9fb9 12#include "fatal.h"
82b7abe3
AJ
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"
fb0c2f17 19#include "log/TcpLogger.h"
a03343c2 20#include "sbuf/SBuf.h"
82b7abe3 21
9f4aecaf
AJ
22CBDATA_CLASS_INIT(Logfile);
23
24Logfile::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}
82b7abe3
AJ
37
38Logfile *
39logfileOpen(const char *path, size_t bufsz, int fatal_flag)
40{
41 int ret;
42 const char *patharg;
43
e0236918 44 debugs(50, DBG_IMPORTANT, "Logfile: opening log " << path);
82b7abe3 45
9f4aecaf 46 Logfile *lf = new Logfile(path);
82b7abe3
AJ
47 patharg = path;
48 /* need to call the per-logfile-type code */
49 if (strncmp(path, "stdio:", 6) == 0) {
9d65168e
A
50 patharg = path + 6;
51 ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
82b7abe3 52 } else if (strncmp(path, "daemon:", 7) == 0) {
9d65168e
A
53 patharg = path + 7;
54 ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag);
d33e847c
AJ
55 } else if (strncmp(path, "tcp:", 4) == 0) {
56 patharg = path + 4;
fb0c2f17 57 ret = Log::TcpLogger::Open(lf, patharg, bufsz, fatal_flag);
82b7abe3 58 } else if (strncmp(path, "udp:", 4) == 0) {
9d65168e
A
59 patharg = path + 4;
60 ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag);
82b7abe3
AJ
61#if HAVE_SYSLOG
62 } else if (strncmp(path, "syslog:", 7) == 0) {
9d65168e
A
63 patharg = path + 7;
64 ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag);
82b7abe3
AJ
65#endif
66 } else {
fb0c2f17 67 debugs(50, DBG_IMPORTANT, "WARNING: log name now starts with a module name. Use 'stdio:" << patharg << "'");
8030a2a0 68 snprintf(lf->path, MAXPATHLEN, "stdio:%s", patharg);
9d65168e 69 ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
82b7abe3
AJ
70 }
71 if (!ret) {
9d65168e 72 if (fatal_flag)
8030a2a0 73 fatalf("logfileOpen: %s: couldn't open!\n", path);
9d65168e 74 else
e0236918 75 debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!");
9d65168e 76 lf->f_close(lf);
9f4aecaf 77 delete lf;
9d65168e 78 return NULL;
82b7abe3
AJ
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
90void
91logfileClose(Logfile * lf)
92{
e0236918 93 debugs(50, DBG_IMPORTANT, "Logfile: closing log " << lf->path);
82b7abe3
AJ
94 lf->f_flush(lf);
95 lf->f_close(lf);
9f4aecaf 96 delete lf;
82b7abe3
AJ
97}
98
99void
efc23871 100logfileRotate(Logfile * lf, int16_t rotateCount)
82b7abe3 101{
e0236918 102 debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
efc23871 103 lf->f_rotate(lf, rotateCount);
82b7abe3
AJ
104}
105
106void
a03343c2 107logfileWrite(Logfile * lf, const char *buf, size_t len)
82b7abe3
AJ
108{
109 lf->f_linewrite(lf, buf, len);
110}
111
112void
113logfilePrintf(Logfile * lf, const char *fmt,...)
114{
115 va_list args;
82b7abe3 116 va_start(args, fmt);
a03343c2
DN
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());
82b7abe3
AJ
121 va_end(args);
122}
123
124void
125logfileLineStart(Logfile * lf)
126{
127 lf->f_linestart(lf);
128}
129
130void
131logfileLineEnd(Logfile * lf)
132{
133 lf->f_lineend(lf);
d7ae3534 134 ++ lf->sequence_number;
82b7abe3
AJ
135}
136
137void
138logfileFlush(Logfile * lf)
139{
140 lf->f_flush(lf);
141}
f53969cc 142