-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathDebug.cpp
227 lines (180 loc) · 5.62 KB
/
Debug.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
//==============================================================================
//
// Debug.cpp
//
// Copyright (C) 2013-2025 Greg Utas
//
// This file is part of the Robust Services Core (RSC).
//
// RSC is free software: you can redistribute it and/or modify it under the
// terms of the Lesser GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// RSC is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
// details.
//
// You should have received a copy of the Lesser GNU General Public License
// along with RSC. If not, see <http://www.gnu.org/licenses/>.
//
#include "Debug.h"
#include <cstdint>
#include <ios>
#include <new>
#include <sstream>
#include "AssertionException.h"
#include "CoutThread.h"
#include "Duration.h"
#include "Element.h"
#include "Formatters.h"
#include "InitFlags.h"
#include "Log.h"
#include "NbAppIds.h"
#include "NbLogs.h"
#include "RootThread.h"
#include "Singleton.h"
#include "SysStackTrace.h"
#include "SysThread.h"
#include "ThisThread.h"
using std::string;
//------------------------------------------------------------------------------
namespace NodeBase
{
fixed_string UnexpectedInvocation = "unexpected invocation";
// SwFlags_ controls the behavior of software during testing.
//
static Flags SwFlags_ = Flags();
// Set to ExitCode_ to suppress logs when the system is exiting. A simple
// bool is not used in case this gets trampled.
//
static uint32_t ExitStatus_ = 0;
// ExitCode_ is the magic value which indicates that the system is exiting.
//
static const uint32_t ExitCode_ = 0xDEADC0DE;
Flags Debug::FcFlags_ = Flags(InitFlags::TraceInit() ? 1 << TracingActive : 0);
//------------------------------------------------------------------------------
void Debug::Assert(bool condition, debug64_t errval)
{
if(!condition)
{
throw AssertionException(errval);
}
}
//------------------------------------------------------------------------------
void Debug::Exiting()
{
Debug::ft("Debug.Exiting");
// Disable function tracing and logs while destructors are invoked
// during shutdown.
//
FcFlags_.reset();
ExitStatus_ = ExitCode_;
}
//------------------------------------------------------------------------------
//
// The cost of function tracing was assessed by running POTS traffic while
// tracing threads in the Payload faction only. The results, determined by
// when the system entered overload, were
// o tracing off: 17000 calls/minute
// o tracing on: 4000 calls/minute
//
void Debug::ft(fn_name_arg func) NO_FT
{
if(FcFlags_.none()) return;
Thread::FunctionInvoked(func);
}
//------------------------------------------------------------------------------
void Debug::ftnt(fn_name_arg func) NO_FT
{
if(FcFlags_.none()) return;
Thread::FunctionInvoked(func, std::nothrow);
}
//------------------------------------------------------------------------------
Flags Debug::GetSwFlags()
{
Debug::ft("Debug.GetSwFlags");
if(Element::RunningInLab()) return SwFlags_;
return NoFlags;
}
//------------------------------------------------------------------------------
void Debug::noop(debug64_t info)
{
Debug::ft("Debug.noop");
}
//------------------------------------------------------------------------------
void Debug::Progress(const string& s)
{
Debug::ft("Debug.Progress");
CoutThread::Spool(s.c_str());
ThisThread::Pause(msecs_t(10));
}
//------------------------------------------------------------------------------
void Debug::ResetSwFlags()
{
Debug::ft("Debug.ResetSwFlags");
SwFlags_.reset();
}
//------------------------------------------------------------------------------
void Debug::SetSwFlag(FlagId fid, bool value)
{
Debug::ftnt("Debug.SetSwFlag");
if(Element::RunningInLab() && (fid <= MaxFlagId))
{
SwFlags_.set(fid, value);
// To be reenabled, RootThread has to be signalled.
//
if((fid == DisableRootThread) && !value)
{
Singleton<RootThread>::Extant()->systhrd_->Proceed();
}
}
}
//------------------------------------------------------------------------------
bool Debug::SwFlagOn(FlagId fid)
{
Debug::ftnt("Debug.SwFlagOn");
if(Element::RunningInLab() && (fid <= MaxFlagId))
{
return SwFlags_.test(fid);
}
return false;
}
//------------------------------------------------------------------------------
fn_name Debug_SwLog = "Debug.SwLog";
void Debug::SwLog(fn_name_arg func,
const string& errstr, debug64_t errval, bool stack)
{
Debug::ftnt(Debug_SwLog);
if(ExitStatus_ == ExitCode_) return;
if(!Thread::EnterSwLog()) return;
Debug::ftnt(Debug_SwLog);
auto log = Log::Create(SoftwareLogGroup, SoftwareError);
if(log != nullptr)
{
*log << Log::Tab << "in ";
if(func != nullptr)
*log << func;
else
*log << "Unknown Function";
*log << CRLF;
*log << Log::Tab << "expl=" << errstr;
if(!errstr.empty() && (errstr.back() == CRLF))
*log << Log::Tab;
else
*log << spaces(2);
*log << "errval=" << HexPrefixStr;
*log << std::hex << errval << std::dec << CRLF;
if(stack) SysStackTrace::Display(*log);
Log::Submit(log);
}
Thread::ExitSwLog(false);
}
//------------------------------------------------------------------------------
string strOver(const Base* obj, bool ns)
{
string result = "override not found in " + strClass(obj, ns);
return result;
}
}