Squid Web Cache master
Loading...
Searching...
No Matches
Stream.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 00 Debug Routines */
10
11#ifndef SQUID_SRC_DEBUG_STREAM_H
12#define SQUID_SRC_DEBUG_STREAM_H
13
14#include "base/Here.h"
15// XXX should be mem/forward.h once it removes dependencies on typedefs.h
16#include "mem/AllocatorProxy.h"
17
18#include <iostream>
19#undef assert
20#include <sstream>
21#include <iomanip>
22#if defined(assert)
23#undef assert
24#endif
25#if PURIFY
26#define assert(EX) ((void)0)
27#elif defined(NODEBUG)
28#define assert(EX) ((void)0)
29#else
30#define assert(EX) ((EX)?((void)0):xassert( # EX , __FILE__, __LINE__))
31#endif
32
33/* defined debug section limits */
34#define MAX_DEBUG_SECTIONS 100
35
36/* defined names for Debug Levels */
37#define DBG_CRITICAL 0
38#define DBG_IMPORTANT 1
39/* levels 2-8 are still being discussed amongst the developers */
40#define DBG_DATA 9
42#define DBG_PARSE_NOTE(x) (opt_parse_cfg_only?0:(x))
45
46class Debug
47{
48
49public:
51 class Context
52 {
53 public:
54 Context(const int aSectionLevel, const int aLevel);
55
56 int section;
57 int level;
59
60 private:
61 friend class Debug;
62 friend class DebugMessageHeader;
63
64 void rewind(const int aSection, const int aLevel);
66 std::ostringstream buf;
68
72 };
73
75 static bool Enabled(const int section, const int level)
76 {
77 return level <= Debug::Levels[section];
78 }
79
80 static char *debugOptions;
81 static char *cache_log;
82 static int rotateNumber;
84 static int override_X;
85 static bool log_syslog;
86
87 // TODO: Convert all helpers to use debugs() and NameThisHelper() APIs.
91 static void NameThisHelper(const char *name);
92
95 static void NameThisKid(int kidIdentifier);
96
97 static void parseOptions(char const *);
98
100 static int Level() { return Current ? Current->level : 1; }
102 static int SectionLevel() { return Current ? Current->sectionLevel : 1; }
103
105 static std::ostringstream &Start(const int section, const int level);
107 static void Finish();
108
110 static void ForceAlert();
111
114 static std::ostream& Extra(std::ostream &);
115
117 static void ForgetSaved();
118
122 static void PrepareToDie();
123
125 static void LogWaitingForIdle();
126
127 /* cache_log channel */
128
132 static void BanCacheLogUse();
133
137 static void UseCacheLog();
138
141 static void StopCacheLogUse();
142
143 /* stderr channel */
144
149 static void EnsureDefaultStderrLevel(int maxDefault);
150
153 static void ResetStderrLevel(int maxLevel);
154
157 static void SettleStderr();
158
161 static bool StderrEnabled();
162
163 /* syslog channel */
164
166 static void ConfigureSyslog(const char *facility);
167
170 static void SettleSyslog();
171
172private:
173 static void FormatStream(std::ostream &);
174 static void LogMessage(const Context &);
175
176 static Context *Current;
177};
178
181FILE *DebugStream();
182
184void ResyncDebugLog(FILE *newDestination);
185
186/* Debug stream
187 *
188 * Unit tests can enable full debugging to stderr for one
189 * debug section; to enable this, #define ENABLE_DEBUG_SECTION to the
190 * section number before any header
191 */
192#define debugs(SECTION, LEVEL, CONTENT) \
193 do { \
194 const int _dbg_level = (LEVEL); \
195 if (Debug::Enabled((SECTION), _dbg_level)) { \
196 std::ostream &_dbo = Debug::Start((SECTION), _dbg_level); \
197 if (_dbg_level > DBG_IMPORTANT) { \
198 _dbo << (SECTION) << ',' << _dbg_level << "| " \
199 << Here() << ": "; \
200 } \
201 _dbo << CONTENT; \
202 Debug::Finish(); \
203 } \
204 } while (/*CONSTCOND*/ 0)
205
209std::ostream& ForceAlert(std::ostream& s);
210
211/*
212 * MYNAME is for use (in rare/special cases) at debug levels 0 and 1
213 *
214 * debugs(1,1, MYNAME << "WARNING: some message");
215 */
216#ifdef __PRETTY_FUNCTION__
217#define MYNAME __PRETTY_FUNCTION__ << " "
218#else
219#define MYNAME __func__ << " "
220#endif
221
222/* some uint8_t do not like streaming control-chars (values 0-31, 127+) */
223inline std::ostream& operator <<(std::ostream &os, const uint8_t d)
224{
225 return (os << (int)d);
226}
227
228/* Legacy debug function definitions */
229void _db_rotate_log(void);
230
231#endif /* SQUID_SRC_DEBUG_STREAM_H */
232
meta-information of a Finish()ed debugs() message
Definition debug.cc:109
meta-information for debugs() or a similar debugging call
Definition Stream.h:52
int section
the debug section of the debugs() call
Definition Stream.h:56
bool waitingForIdle
Definition Stream.h:71
Context * upper
previous or parent record in nested debugging calls
Definition Stream.h:65
int sectionLevel
maximum debugging level allowed during the call
Definition Stream.h:58
std::ostringstream buf
debugs() output sink
Definition Stream.h:66
int level
minimum debugging level required by the debugs() call
Definition Stream.h:57
bool forceAlert
the current debugs() will be a syslog ALERT
Definition Stream.h:67
void rewind(const int aSection, const int aLevel)
Optimization: avoids new Context creation for every debugs().
Definition debug.cc:1290
Definition Stream.h:47
static void ResetStderrLevel(int maxLevel)
Definition debug.cc:701
static bool StderrEnabled()
Definition debug.cc:727
static bool log_syslog
Definition Stream.h:85
static void parseOptions(char const *)
Definition debug.cc:1095
static void PrepareToDie()
Definition debug.cc:563
static void ForgetSaved()
silently erases saved early debugs() messages (if any)
Definition debug.cc:554
static bool Enabled(const int section, const int level)
whether debugging the given section and the given level produces output
Definition Stream.h:75
static Context * Current
deepest active context; nil outside debugs()
Definition Stream.h:176
static std::ostream & Extra(std::ostream &)
Definition debug.cc:1316
static void NameThisKid(int kidIdentifier)
Definition debug.cc:407
static int SectionLevel()
maximum level currently allowed
Definition Stream.h:102
static void LogMessage(const Context &)
broadcasts debugs() message to the logging channels
Definition debug.cc:781
static int override_X
Definition Stream.h:84
static void SettleSyslog()
Definition debug.cc:1145
static int Levels[MAX_DEBUG_SECTIONS]
Definition Stream.h:83
static void Finish()
logs output buffer created in Start() and closes debugging context
Definition debug.cc:1366
static void NameThisHelper(const char *name)
Definition debug.cc:384
static char * debugOptions
Definition Stream.h:80
static void UseCacheLog()
Definition debug.cc:1125
static char * cache_log
Definition Stream.h:81
static void FormatStream(std::ostream &)
configures default formatting for the debugging stream
Definition debug.cc:1305
static std::ostringstream & Start(const int section, const int level)
opens debugging context and returns output buffer
Definition debug.cc:1342
static void BanCacheLogUse()
Definition debug.cc:1118
static void ConfigureSyslog(const char *facility)
enables logging to syslog (using the specified facility, when not nil)
Definition debug.cc:1083
static void StopCacheLogUse()
Definition debug.cc:1132
static int Level()
minimum level required by the current debugs() call
Definition Stream.h:100
static int rotateNumber
Definition Stream.h:82
static void EnsureDefaultStderrLevel(int maxDefault)
Definition debug.cc:693
static void SettleStderr()
Definition debug.cc:707
static void LogWaitingForIdle()
Logs messages of Finish()ed debugs() calls that were queued earlier.
Definition debug.cc:1327
static void ForceAlert()
configures the active debugging context to write syslog ALERT
Definition debug.cc:1403
FILE * DebugStream()
Definition debug.cc:355
void ResyncDebugLog(FILE *newDestination)
a hack for low-level file descriptor manipulations in ipcCreate()
Definition debug.cc:515
#define MAX_DEBUG_SECTIONS
Definition Stream.h:34
void _db_rotate_log(void)
Definition debug.cc:1160
std::ostream & operator<<(std::ostream &os, const uint8_t d)
Definition Stream.h:223
std::ostream & ForceAlert(std::ostream &s)
Definition debug.cc:1411