1 /*
2  * Copyright (C) 1996-2021 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 /*
10  * A stub implementation of the Debug.h API.
11  * For use by test binaries which do not need the full context debugging
12  *
13  * Note: it doesn't use the STUB API as the functions defined here must
14  * not abort the unit test.
15  */
16 #include "squid.h"
17 #include "Debug.h"
18 
19 #define STUB_API "debug.cc"
20 #include "tests/STUB.h"
21 
22 char *Debug::debugOptions;
23 char *Debug::cache_log= NULL;
24 int Debug::rotateNumber = 0;
25 int Debug::Levels[MAX_DEBUG_SECTIONS];
26 int Debug::override_X = 0;
27 int Debug::log_stderr = 1;
28 bool Debug::log_syslog = false;
ForceAlert()29 void Debug::ForceAlert() STUB
30 
31 void StopUsingDebugLog() STUB
32 void ResyncDebugLog(FILE *) STUB
33 
34 FILE *
35 DebugStream()
36 {
37     return stderr;
38 }
39 
40 Ctx
ctx_enter(const char *)41 ctx_enter(const char *)
42 {
43     return -1;
44 }
45 
46 void
ctx_exit(Ctx)47 ctx_exit(Ctx)
48 {}
49 
50 void
_db_init(const char *,const char *)51 _db_init(const char *, const char *)
52 {}
53 
54 void
_db_set_syslog(const char *)55 _db_set_syslog(const char *)
56 {}
57 
58 void
_db_rotate_log(void)59 _db_rotate_log(void)
60 {}
61 
62 static void
63 _db_print_stderr(const char *format, va_list args);
64 
65 void
_db_print(const char * format,...)66 _db_print(const char *format,...)
67 {
68     static char f[BUFSIZ];
69     va_list args1;
70     va_list args2;
71     va_list args3;
72 
73     va_start(args1, format);
74     va_start(args2, format);
75     va_start(args3, format);
76 
77     snprintf(f, BUFSIZ, "%s| %s",
78              "stub time", //debugLogTime(squid_curtime),
79              format);
80 
81     _db_print_stderr(f, args2);
82 
83     va_end(args1);
84     va_end(args2);
85     va_end(args3);
86 }
87 
88 static void
_db_print_stderr(const char * format,va_list args)89 _db_print_stderr(const char *format, va_list args)
90 {
91     if (1 < Debug::Level())
92         return;
93 
94     vfprintf(stderr, format, args);
95 }
96 
97 void
parseOptions(char const *)98 Debug::parseOptions(char const *)
99 {}
100 
101 Debug::Context *Debug::Current = nullptr;
102 
Context(const int aSection,const int aLevel)103 Debug::Context::Context(const int aSection, const int aLevel):
104     level(aLevel),
105     sectionLevel(Levels[aSection]),
106     upper(Current),
107     forceAlert(false)
108 {
109     buf.setf(std::ios::fixed);
110     buf.precision(2);
111 }
112 
113 std::ostringstream &
Start(const int section,const int level)114 Debug::Start(const int section, const int level)
115 {
116     Current = new Context(section, level);
117     return Current->buf;
118 }
119 
120 void
Finish()121 Debug::Finish()
122 {
123     if (Current) {
124         _db_print("%s\n", Current->buf.str().c_str());
125         delete Current;
126         Current = nullptr;
127     }
128 }
129 
130 std::ostream&
ForceAlert(std::ostream & s)131 ForceAlert(std::ostream& s)
132 {
133     return s;
134 }
135 
136 std::ostream &
print(std::ostream & os) const137 Raw::print(std::ostream &os) const
138 {
139     if (label_)
140         os << ' ' << label_ << '[' << size_ << ']';
141 
142     if (!size_)
143         return os;
144 
145     // finalize debugging level if no level was set explicitly via minLevel()
146     const int finalLevel = (level >= 0) ? level :
147                            (size_ > 40 ? DBG_DATA : Debug::SectionLevel());
148     if (finalLevel <= Debug::SectionLevel()) {
149         os << (label_ ? '=' : ' ');
150         if (data_)
151             os.write(data_, size_);
152         else
153             os << "[null]";
154     }
155 
156     return os;
157 }
158 
159