]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Debug.h
SourceFormat Enforcement
[thirdparty/squid.git] / src / Debug.h
CommitLineData
c772f001 1/*
bde978a6 2 * Copyright (C) 1996-2015 The Squid Software Foundation and contributors
c772f001 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.
c772f001 7 */
bbc27441
AJ
8
9/* DEBUG: section 00 Debug Routines */
10
e1f7507e
AJ
11#ifndef SQUID_DEBUG_H
12#define SQUID_DEBUG_H
c772f001 13
f95fe6ed 14#include <iostream>
a8d99c60 15#undef assert
f95fe6ed 16#include <sstream>
bf8fe701 17#include <iomanip>
27e059d4 18#if defined(assert)
a8d99c60 19#undef assert
20#endif
27e059d4 21
a8d99c60 22#if PURIFY
23#define assert(EX) ((void)0)
24#elif defined(NODEBUG)
25#define assert(EX) ((void)0)
26#elif STDC_HEADERS
27#define assert(EX) ((EX)?((void)0):xassert( # EX , __FILE__, __LINE__))
28#else
29#define assert(EX) ((EX)?((void)0):xassert("EX", __FILE__, __LINE__))
30#endif
f95fe6ed 31
62493678
AJ
32/* context-based debugging, the actual type is subject to change */
33typedef int Ctx;
8a648e8d
FC
34Ctx ctx_enter(const char *descr);
35void ctx_exit(Ctx ctx);
62493678 36
e1f7507e
AJ
37/* defined debug section limits */
38#define MAX_DEBUG_SECTIONS 100
39
feefade1 40/* defined names for Debug Levels */
f53969cc
SM
41#define DBG_CRITICAL 0 /**< critical messages always shown when they occur */
42#define DBG_IMPORTANT 1 /**< important messages always shown when their section is being checked */
feefade1 43/* levels 2-8 are still being discussed amongst the developers */
f53969cc 44#define DBG_DATA 9 /**< output is a large data dump only necessary for advanced debugging */
feefade1 45
bbbea8ad
AJ
46#define DBG_PARSE_NOTE(x) (opt_parse_cfg_only?0:(x)) /**< output is always to be displayed on '-k parse' but at level-x normally. */
47
62e76326 48class Debug
c772f001 49{
62e76326 50
c772f001 51public:
62493678
AJ
52 static char *debugOptions;
53 static char *cache_log;
54 static int rotateNumber;
62e76326 55 static int Levels[MAX_DEBUG_SECTIONS];
be039a68
AR
56 static int level; ///< minimum debugging level required by debugs() call
57 static int sectionLevel; ///< maximum debugging level allowed now
62493678
AJ
58 static int override_X;
59 static int log_stderr;
60 static bool log_syslog;
61
f95fe6ed 62 static std::ostream &getDebugOut();
63 static void finishDebug();
d9e04dc7 64 static void parseOptions(char const *);
f95fe6ed 65
66private:
9eab365d 67 // Hack: replaces global ::xassert() to debug debugging assertions
68 static void xassert(const char *msg, const char *file, int line);
26ac0430 69
bfa09779
AJ
70 /// Wrapper class to prevent SquidNew.h overrides getting confused
71 /// with the libc++6 std::ostringstream definitions
72 class OutStream : public std::ostringstream
73 {
74 // XXX: use MEMPROXY_CLASS() once that no longer pulls in typedefs.h and enums.h and globals.h
75 public:
76 void *operator new(size_t size) throw(std::bad_alloc) {return xmalloc(size);}
77 void operator delete(void *address) throw() {xfree(address);}
78 void *operator new[] (size_t size) throw(std::bad_alloc) ; //{return xmalloc(size);}
79 void operator delete[] (void *address) throw() ; // {xfree(address);}
80 };
81
82 static OutStream *CurrentDebug;
9eab365d 83 static int TheDepth; // level of nested debugging calls
c772f001 84};
85
f76d2f97
AJ
86extern FILE *debug_log;
87
881c4733 88size_t BuildPrefixInit();
af28bfbd
AJ
89const char * SkipBuildPrefix(const char* path);
90
f95fe6ed 91/* Debug stream */
92#define debugs(SECTION, LEVEL, CONTENT) \
93 do { \
299b2ed1 94 if ((Debug::level = (LEVEL)) <= Debug::Levels[SECTION]) { \
be039a68 95 Debug::sectionLevel = Debug::Levels[SECTION]; \
bfd38d03 96 std::ostream &_dbo=Debug::getDebugOut(); \
299b2ed1 97 if (Debug::level > DBG_IMPORTANT) \
bfd38d03
FC
98 _dbo << SkipBuildPrefix(__FILE__)<<"("<<__LINE__<<") "<<__FUNCTION__<<": "; \
99 _dbo << CONTENT; \
100 Debug::finishDebug(); \
f95fe6ed 101 } \
102 } while (/*CONSTCOND*/ 0)
103
fc9d2eb0
FC
104/** stream manipulator which does nothing.
105 * \deprecated Do not add to new code, and remove when editing old code
def17b6a 106 *
fc9d2eb0
FC
107 * Its purpose is to inactivate calls made following previous debugs()
108 * guidelines such as
23d6095a 109 * debugs(1,2, HERE << "some message");
fc9d2eb0
FC
110 *
111 * His former objective is now absorbed in the debugs call itself
def17b6a 112 */
fc9d2eb0
FC
113inline std::ostream&
114HERE(std::ostream& s)
115{
116 return s;
117}
def17b6a 118
23d6095a
AJ
119/*
120 * MYNAME is for use at debug levels 0 and 1 where HERE is too messy.
121 *
122 * debugs(1,1, MYNAME << "WARNING: some message");
123 */
124#ifdef __PRETTY_FUNCTION__
125#define MYNAME __PRETTY_FUNCTION__ << " "
126#else
127#define MYNAME __FUNCTION__ << " "
128#endif
129
cc192b50 130/* some uint8_t do not like streaming control-chars (values 0-31, 127+) */
26ac0430
AJ
131inline std::ostream& operator <<(std::ostream &os, const uint8_t d)
132{
066c9540 133 return (os << (int)d);
134}
135
96e03dd8 136/* Legacy debug style. Still used in some places. needs to die... */
2c87d96a
AJ
137#define do_debug(SECTION, LEVEL) ((Debug::level = (LEVEL)) <= Debug::Levels[SECTION])
138#define old_debug(SECTION, LEVEL) if do_debug((SECTION), (LEVEL)) _db_print
96e03dd8 139
ec85ebda 140/* Legacy debug function definitions */
8a648e8d
FC
141void _db_init(const char *logfile, const char *options);
142void _db_print(const char *,...) PRINTF_FORMAT_ARG1;
143void _db_set_syslog(const char *facility);
144void _db_rotate_log(void);
96e03dd8 145
be039a68
AR
146/// Prints raw and/or non-terminated data safely, efficiently, and beautifully.
147/// Allows raw data debugging in debugs() statements with low debugging levels
148/// by printing only if higher section debugging levels are configured:
149/// debugs(11, DBG_IMPORTANT, "always printed" << Raw(may be printed...));
2113e038
A
150class Raw
151{
be039a68
AR
152public:
153 Raw(const char *label, const char *data, const size_t size):
f53969cc 154 level(-1), label_(label), data_(data), size_(size) {}
be039a68
AR
155
156 /// limit data printing to at least the given debugging level
157 Raw &minLevel(const int aLevel) { level = aLevel; return *this; }
158
159 /// If debugging is prohibited by the current debugs() or section level,
160 /// prints nothing. Otherwise, dumps data using one of these formats:
161 /// " label[size]=data" if label was set and data size is positive
162 /// " label[0]" if label was set and data size is zero
163 /// " data" if label was not set and data size is positive
164 /// "" (i.e., prints nothing) if label was not set and data size is zero
165 std::ostream &print(std::ostream &os) const;
166
167 /// Minimum section debugging level necessary for printing. By default,
168 /// small strings are always printed while large strings are only printed
169 /// if DBG_DATA debugging level is enabled.
170 int level;
171
172private:
173 const char *label_; ///< optional data name or ID; triggers size printing
174 const char *data_; ///< raw data to be printed
175 size_t size_; ///< data length
176};
177
178inline
179std::ostream &operator <<(std::ostream &os, const Raw &raw)
180{
181 return raw.print(os);
182}
183
e1f7507e 184#endif /* SQUID_DEBUG_H */
f53969cc 185