source: squid-ssl/trunk/fuentes/src/Debug.h @ 5495

Last change on this file since 5495 was 5495, checked in by Juanma, 2 years ago

Initial release

File size: 6.3 KB
Line 
1/*
2 * Copyright (C) 1996-2015 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_DEBUG_H
12#define SQUID_DEBUG_H
13
14#include <iostream>
15#undef assert
16#include <sstream>
17#include <iomanip>
18#if defined(assert)
19#undef assert
20#endif
21
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
31
32/* context-based debugging, the actual type is subject to change */
33typedef int Ctx;
34Ctx ctx_enter(const char *descr);
35void ctx_exit(Ctx ctx);
36
37/* defined debug section limits */
38#define MAX_DEBUG_SECTIONS 100
39
40/* defined names for Debug Levels */
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 */
43/* levels 2-8 are still being discussed amongst the developers */
44#define DBG_DATA    9   /**< output is a large data dump only necessary for advanced debugging */
45
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
48class Debug
49{
50
51public:
52    static char *debugOptions;
53    static char *cache_log;
54    static int rotateNumber;
55    static int Levels[MAX_DEBUG_SECTIONS];
56    static int level; ///< minimum debugging level required by debugs() call
57    static int sectionLevel; ///< maximum debugging level allowed now
58    static int override_X;
59    static int log_stderr;
60    static bool log_syslog;
61
62    static std::ostream &getDebugOut();
63    static void finishDebug();
64    static void parseOptions(char const *);
65
66private:
67    // Hack: replaces global ::xassert() to debug debugging assertions
68    static void xassert(const char *msg, const char *file, int line);
69
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;
83    static int TheDepth; // level of nested debugging calls
84};
85
86extern FILE *debug_log;
87
88size_t BuildPrefixInit();
89const char * SkipBuildPrefix(const char* path);
90
91/* Debug stream */
92#define debugs(SECTION, LEVEL, CONTENT) \
93   do { \
94        if ((Debug::level = (LEVEL)) <= Debug::Levels[SECTION]) { \
95            Debug::sectionLevel = Debug::Levels[SECTION]; \
96            std::ostream &_dbo=Debug::getDebugOut(); \
97            if (Debug::level > DBG_IMPORTANT) { \
98                _dbo << (SECTION) << ',' << (LEVEL) << "| " \
99                     << SkipBuildPrefix(__FILE__)<<"("<<__LINE__<<") "<<__FUNCTION__<<": "; \
100            } \
101            _dbo << CONTENT; \
102            Debug::finishDebug(); \
103        } \
104   } while (/*CONSTCOND*/ 0)
105
106/** stream manipulator which does nothing.
107 * \deprecated Do not add to new code, and remove when editing old code
108 *
109 * Its purpose is to inactivate calls made following previous debugs()
110 * guidelines such as
111 * debugs(1,2, HERE << "some message");
112 *
113 * His former objective is now absorbed in the debugs call itself
114 */
115inline std::ostream&
116HERE(std::ostream& s)
117{
118    return s;
119}
120
121/*
122 * MYNAME is for use at debug levels 0 and 1 where HERE is too messy.
123 *
124 * debugs(1,1, MYNAME << "WARNING: some message");
125 */
126#ifdef __PRETTY_FUNCTION__
127#define MYNAME __PRETTY_FUNCTION__ << " "
128#else
129#define MYNAME __FUNCTION__ << " "
130#endif
131
132/* some uint8_t do not like streaming control-chars (values 0-31, 127+) */
133inline std::ostream& operator <<(std::ostream &os, const uint8_t d)
134{
135    return (os << (int)d);
136}
137
138/* Legacy debug style. Still used in some places. needs to die... */
139#define do_debug(SECTION, LEVEL)   ((Debug::level = (LEVEL)) <= Debug::Levels[SECTION])
140#define old_debug(SECTION, LEVEL)  if do_debug((SECTION), (LEVEL)) _db_print
141
142/* Legacy debug function definitions */
143void _db_init(const char *logfile, const char *options);
144void _db_print(const char *,...) PRINTF_FORMAT_ARG1;
145void _db_set_syslog(const char *facility);
146void _db_rotate_log(void);
147
148/// Prints raw and/or non-terminated data safely, efficiently, and beautifully.
149/// Allows raw data debugging in debugs() statements with low debugging levels
150/// by printing only if higher section debugging levels are configured:
151///   debugs(11, DBG_IMPORTANT, "always printed" << Raw(may be printed...));
152class Raw
153{
154public:
155    Raw(const char *label, const char *data, const size_t size):
156        level(-1), label_(label), data_(data), size_(size) {}
157
158    /// limit data printing to at least the given debugging level
159    Raw &minLevel(const int aLevel) { level = aLevel; return *this; }
160
161    /// If debugging is prohibited by the current debugs() or section level,
162    /// prints nothing. Otherwise, dumps data using one of these formats:
163    ///   " label[size]=data" if label was set and data size is positive
164    ///   " label[0]" if label was set and data size is zero
165    ///   " data" if label was not set and data size is positive
166    ///   "" (i.e., prints nothing) if label was not set and data size is zero
167    std::ostream &print(std::ostream &os) const;
168
169    /// Minimum section debugging level necessary for printing. By default,
170    /// small strings are always printed while large strings are only printed
171    /// if DBG_DATA debugging level is enabled.
172    int level;
173
174private:
175    const char *label_; ///< optional data name or ID; triggers size printing
176    const char *data_; ///< raw data to be printed
177    size_t size_; ///< data length
178};
179
180inline
181std::ostream &operator <<(std::ostream &os, const Raw &raw)
182{
183    return raw.print(os);
184}
185
186#endif /* SQUID_DEBUG_H */
187
Note: See TracBrowser for help on using the repository browser.