-
Notifications
You must be signed in to change notification settings - Fork 494
/
File.cc
142 lines (124 loc) · 3.4 KB
/
File.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
* Copyright (C) 1996-2020 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
* Please see the COPYING and CONTRIBUTORS files for details.
*/
/* DEBUG: section 50 Log file handling */
#include "squid.h"
#include "fatal.h"
#include "fde.h"
#include "log/File.h"
#include "log/ModDaemon.h"
#include "log/ModStdio.h"
#include "log/ModSyslog.h"
#include "log/ModUdp.h"
#include "log/TcpLogger.h"
#include "sbuf/SBuf.h"
CBDATA_CLASS_INIT(Logfile);
Logfile::Logfile(const char *aPath) :
sequence_number(0),
data(NULL),
f_linestart(NULL),
f_linewrite(NULL),
f_lineend(NULL),
f_flush(NULL),
f_rotate(NULL),
f_close(NULL)
{
xstrncpy(path, aPath, sizeof(path));
flags.fatal = 0;
}
Logfile *
logfileOpen(const char *path, size_t bufsz, int fatal_flag)
{
int ret;
const char *patharg;
debugs(50, DBG_IMPORTANT, "Logfile: opening log " << path);
Logfile *lf = new Logfile(path);
patharg = path;
/* need to call the per-logfile-type code */
if (strncmp(path, "stdio:", 6) == 0) {
patharg = path + 6;
ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
} else if (strncmp(path, "daemon:", 7) == 0) {
patharg = path + 7;
ret = logfile_mod_daemon_open(lf, patharg, bufsz, fatal_flag);
} else if (strncmp(path, "tcp:", 4) == 0) {
patharg = path + 4;
ret = Log::TcpLogger::Open(lf, patharg, bufsz, fatal_flag);
} else if (strncmp(path, "udp:", 4) == 0) {
patharg = path + 4;
ret = logfile_mod_udp_open(lf, patharg, bufsz, fatal_flag);
#if HAVE_SYSLOG
} else if (strncmp(path, "syslog:", 7) == 0) {
patharg = path + 7;
ret = logfile_mod_syslog_open(lf, patharg, bufsz, fatal_flag);
#endif
} else {
debugs(50, DBG_IMPORTANT, "WARNING: log name now starts with a module name. Use 'stdio:" << patharg << "'");
snprintf(lf->path, MAXPATHLEN, "stdio:%s", patharg);
ret = logfile_mod_stdio_open(lf, patharg, bufsz, fatal_flag);
}
if (!ret) {
if (fatal_flag)
fatalf("logfileOpen: %s: couldn't open!\n", path);
else
debugs(50, DBG_IMPORTANT, "logfileOpen: " << path << ": couldn't open!");
lf->f_close(lf);
delete lf;
return NULL;
}
assert(lf->data != NULL);
if (fatal_flag)
lf->flags.fatal = 1;
lf->sequence_number = 0;
return lf;
}
void
logfileClose(Logfile * lf)
{
debugs(50, DBG_IMPORTANT, "Logfile: closing log " << lf->path);
lf->f_flush(lf);
lf->f_close(lf);
delete lf;
}
void
logfileRotate(Logfile * lf, int16_t rotateCount)
{
debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
lf->f_rotate(lf, rotateCount);
}
void
logfileWrite(Logfile * lf, const char *buf, size_t len)
{
lf->f_linewrite(lf, buf, len);
}
void
logfilePrintf(Logfile * lf, const char *fmt,...)
{
va_list args;
va_start(args, fmt);
static SBuf sbuf;
sbuf.clear();
sbuf.vappendf(fmt, args); // Throws on overflow. TODO: handle that better
logfileWrite(lf, sbuf.c_str(), sbuf.length());
va_end(args);
}
void
logfileLineStart(Logfile * lf)
{
lf->f_linestart(lf);
}
void
logfileLineEnd(Logfile * lf)
{
lf->f_lineend(lf);
++ lf->sequence_number;
}
void
logfileFlush(Logfile * lf)
{
lf->f_flush(lf);
}