FormattedLog.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 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#include "squid.h"
10#include "acl/Gadgets.h"
11#include "base/TextException.h"
12#include "cache_cf.h"
13#include "debug/Stream.h"
14#include "log/Config.h"
15#include "log/File.h"
16#include "log/FormattedLog.h"
17#include "Parsing.h"
18#include "sbuf/Stream.h"
19#include "SquidConfig.h"
20
22{
23 close(); // TODO: destructing a Logfile object should be enough
26 // leave logFormat alone -- we do not own that object
27}
28
29bool
31{
32 return (filename && strncmp(filename, "daemon:", 7) == 0);
33}
34
35void
36FormattedLog::parseOptions(ConfigParser &parser, const char *defaultFormatName)
37{
38 const char *explicitFormatName = nullptr;
39 char *key = nullptr;
40 char *value = nullptr;
41 while (parser.optionalKvPair(key, value)) {
42
43 if (strcmp(key, "on-error") == 0) {
44 if (strcmp(value, "die") == 0) {
45 fatal = true;
46 } else if (strcmp(value, "drop") == 0) {
47 fatal = false;
48 } else {
49 throw TextException(ToSBuf("unsupported ", cfg_directive, " on-error value: ", value,
50 Debug::Extra, "expected 'drop' or 'die'"), Here());
51 }
52 continue;
53 }
54
55 if (strcmp(key, "buffer-size") == 0) {
56 parseBytesOptionValue(&bufferSize, "bytes", value);
57 continue;
58 }
59
60 if (strcmp(key, "rotate") == 0) {
61 rotationsToKeep = std::optional<unsigned int>(xatoui(value));
62 continue;
63 }
64
65 if (strcmp(key, "logformat") == 0 && defaultFormatName) {
66 if (explicitFormatName)
67 throw TextException(ToSBuf("duplicated ", cfg_directive, " option: ", key), Here());
68
69 explicitFormatName = value;
70 continue;
71 }
72
73 throw TextException(ToSBuf("unsupported ", cfg_directive, " option: ", key, "=", value), Here());
74 }
75
76 if (const auto formatName = explicitFormatName ? explicitFormatName : defaultFormatName) {
77 assert(defaultFormatName); // this log supports logformat=name
78 setLogformat(formatName);
79 } // else OK: this log does not support logformat=name and none was given
80}
81
82void
83FormattedLog::dumpOptions(std::ostream &os) const
84{
85 /* do not report defaults */
86
87 // TODO: Here and elsewhere, report both explicitly configured settings and
88 // various defaults. Properly excluding defaults requires wrapping most
89 // non-pointer members in std::optional and adding methods to compute the final
90 // option value after accounting for defaults (and those may change with
91 // reconfiguration!). And all that effort may still not result in a faithful
92 // reproduction of the original squid.conf because of size unit changes,
93 // order changes, duplicates removal, etc. More importantly, these reports
94 // are much more useful for determining complete Squid state (especially
95 // when triaging older Squids with some difficult-to-figure-out defaults).
96
97 switch (type) {
99 break; // do not report a format when it was not configured
100
102 break; // the special "none" case has no format to report
103
105 break; // do not report default format (XXX: icap_log default differs)
106
108 if (logFormat) // paranoid; the format should be set
109 os << " logformat=" << logFormat->name;
110 break;
111
112 default:
113 os << " logformat=" << Log::LogConfig::BuiltInFormatName(type);
114 }
115
116 if (!fatal)
117 os << " on-error=drop";
118
119 if (bufferSize != 8*MAX_URL)
120 os << " buffer-size=" << bufferSize << "bytes";
121
122 if (rotationsToKeep)
123 os << " rotate=" << rotationsToKeep.value();
124}
125
126void
127FormattedLog::setLogformat(const char *logformatName)
128{
129 assert(logformatName);
130 assert(type == Log::Format::CLF_UNKNOWN); // set only once
131 assert(!logFormat); // set only once
132
133 debugs(3, 7, "possible " << filename << " logformat: " << logformatName);
134
135 if (const auto lf = Log::TheConfig.findCustomFormat(logformatName)) {
137 logFormat = lf;
138 return;
139 }
140
141 if (const auto id = Log::LogConfig::FindBuiltInFormat(logformatName)) {
142 type = id;
143 return;
144 }
145
146 throw TextException(ToSBuf("unknown logformat name in ", cfg_directive, ": ", logformatName), Here());
147}
148
149void
151{
152 Must(!logfile);
153 Must(filename);
155 // the opening code reports failures and returns nil if they are non-fatal
156}
157
158void
160{
161 if (logfile)
163}
164
165void
167{
168 if (logfile) {
170 logfile = nullptr; // deleted by the closing code
171 }
172}
173
#define Here()
source code location of the caller
Definition: Here.h:15
unsigned int xatoui(const char *token, char eov)
Definition: Parsing.cc:58
class SquidConfig Config
Definition: SquidConfig.cc:12
#define Must(condition)
Definition: TextException.h:75
#define assert(EX)
Definition: assert.h:17
const char * cfg_directive
During parsing, the name of the current squid.conf directive being parsed.
Definition: cache_cf.cc:271
void parseBytesOptionValue(size_t *bptr, const char *units, char const *value)
Parse bytes number from a string.
Definition: cache_cf.cc:1391
bool optionalKvPair(char *&key, char *&value)
static std::ostream & Extra(std::ostream &)
Definition: debug.cc:1313
char * name
Definition: Format.h:59
Log::Format::log_type type
log record template ID
Definition: FormattedLog.h:67
char * filename
logging destination
Definition: FormattedLog.h:58
void close()
stop recording entries
void dumpOptions(std::ostream &os) const
reports explicitly-configured key=value options, in squid.conf format
Definition: FormattedLog.cc:83
void setLogformat(const char *logformatName)
configures formatting-related settings for the given logformat name
bool fatal
whether unrecoverable errors (e.g., dropping a log record) kill worker
Definition: FormattedLog.h:76
void open()
prepare for recording entries
bool usesDaemon() const
Definition: FormattedLog.cc:30
Format::Format * logFormat
custom log record template for type == Log::Format::CLF_CUSTOM
Definition: FormattedLog.h:64
size_t bufferSize
how much to buffer before dropping or dying (buffer-size=N)
Definition: FormattedLog.h:70
void rotate()
handle the log rotation request
Logfile * logfile
records writer
Definition: FormattedLog.h:55
std::optional< unsigned int > rotationsToKeep
how many log files to retain when rotating. Default: obey logfile_rotate
Definition: FormattedLog.h:73
void parseOptions(ConfigParser &, const char *defaultFormat)
Definition: FormattedLog.cc:36
ACLList * aclList
restrict logging to matching transactions
Definition: FormattedLog.h:61
static const char * BuiltInFormatName(Format::log_type type)
Definition: Config.cc:18
static Format::log_type FindBuiltInFormat(const char *logformatName)
Definition: Config.cc:52
struct SquidConfig::@98 Log
int rotateNumber
Definition: SquidConfig.h:191
an std::runtime_error with thrower location info
Definition: TextException.h:21
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define MAX_URL
Definition: defines.h:78
void aclDestroyAclList(ACLList **list)
Definition: Gadgets.cc:267
void logfileRotate(Logfile *lf, int16_t rotateCount)
Definition: File.cc:101
void logfileClose(Logfile *lf)
Definition: File.cc:92
Logfile * logfileOpen(const char *path, size_t bufsz, int fatal_flag)
Definition: File.cc:40
@ CLF_SQUID
Definition: Formats.h:35
@ CLF_NONE
Definition: Formats.h:37
@ CLF_UNKNOWN
Definition: Formats.h:27
@ CLF_CUSTOM
Definition: Formats.h:30
LogConfig TheConfig
Definition: Config.cc:15
SBuf ToSBuf(Args &&... args)
slowly stream-prints all arguments into a freshly allocated SBuf
Definition: Stream.h:63
#define safe_free(x)
Definition: xalloc.h:73

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors