File.cc
Go to the documentation of this file.
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
24
25Logfile::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
39Logfile *
40logfileOpen(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
91void
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
100void
101logfileRotate(Logfile * lf, int16_t rotateCount)
102{
103 debugs(50, DBG_IMPORTANT, "logfileRotate: " << lf->path);
104 lf->f_rotate(lf, rotateCount);
105}
106
107void
108logfileWrite(Logfile * lf, const char *buf, size_t len)
109{
110 lf->f_linewrite(lf, buf, len);
111}
112
113void
114logfilePrintf(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
125void
127{
128 lf->f_linestart(lf);
129}
130
131void
133{
134 lf->f_lineend(lf);
135 ++ lf->sequence_number;
136}
137
138void
140{
141 lf->f_flush(lf);
142}
143
int logfile_mod_daemon_open(Logfile *lf, const char *path, size_t, int)
Definition: ModDaemon.cc:210
int logfile_mod_stdio_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
Definition: ModStdio.cc:177
int logfile_mod_syslog_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
int logfile_mod_udp_open(Logfile *lf, const char *path, size_t bufsz, int fatal_flag)
Definition: ModUdp.cc:133
#define assert(EX)
Definition: assert.h:19
static int Open(Logfile *lf, const char *path, size_t bufSz, int fatalFlag)
Definition: TcpLogger.cc:452
Definition: File.h:39
LOGLINEEND * f_lineend
Definition: File.h:59
struct Logfile::@84 flags
LOGFLUSH * f_flush
Definition: File.h:60
int64_t sequence_number
Unique sequence number per log line.
Definition: File.h:52
void * data
Definition: File.h:55
LOGCLOSE * f_close
Definition: File.h:62
char path[MAXPATHLEN]
Definition: File.h:46
LOGLINESTART * f_linestart
Definition: File.h:57
LOGWRITE * f_linewrite
Definition: File.h:58
unsigned int fatal
Definition: File.h:49
Logfile(const char *aPath)
Definition: File.cc:25
LOGROTATE * f_rotate
Definition: File.h:61
Definition: SBuf.h:94
SBuf & vappendf(const char *fmt, va_list vargs)
Definition: SBuf.cc:239
const char * c_str()
Definition: SBuf.cc:516
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
void clear()
Definition: SBuf.cc:175
#define Important(id)
Definition: Messages.h:91
#define DBG_IMPORTANT
Definition: Stream.h:41
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
void fatalf(const char *fmt,...)
Definition: fatal.cc:68
void logfileLineEnd(Logfile *lf)
Definition: File.cc:132
void logfileWrite(Logfile *lf, const char *buf, size_t len)
Definition: File.cc:108
void logfileFlush(Logfile *lf)
Definition: File.cc:139
CBDATA_CLASS_INIT(Logfile)
void logfileRotate(Logfile *lf, int16_t rotateCount)
Definition: File.cc:101
void logfileLineStart(Logfile *lf)
Definition: File.cc:126
void logfileClose(Logfile *lf)
Definition: File.cc:92
void logfilePrintf(Logfile *lf, const char *fmt,...)
Definition: File.cc:114
Logfile * logfileOpen(const char *path, size_t bufsz, int fatal_flag)
Definition: File.cc:40
#define MAXPATHLEN
Definition: stdio.h:62
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors