-
Notifications
You must be signed in to change notification settings - Fork 111
/
nvprint.cpp
308 lines (274 loc) · 7.65 KB
/
nvprint.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Copyright (c) 2014-2023, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-FileCopyrightText: Copyright (c) 2014-2021 NVIDIA CORPORATION
* SPDX-License-Identifier: Apache-2.0
*/
#include "nvprint.hpp"
#include <limits.h>
#include <mutex>
#include <vector>
#ifdef _WIN32
#include <io.h>
#include <windows.h>
#else
#include <signal.h>
#include <unistd.h>
#endif
enum class TriState
{
eUnknown,
eFalse,
eTrue
};
static std::string s_logFileName = "log_nvprosample.txt";
static std::vector<char> s_strBuffer; // Persistent allocation for formatted text.
static FILE* s_fd = nullptr;
static bool s_bLogReady = false;
static bool s_bPrintLogging = true;
static uint32_t s_bPrintFileLogging = LOGBITS_ALL;
static uint32_t s_bPrintConsoleLogging = LOGBITS_ALL;
static uint32_t s_bPrintBreakpoints = 0;
static int s_printLevel = -1; // <0 mean no level prefix
static PFN_NVPRINTCALLBACK s_printCallback = nullptr;
static TriState s_consoleSupportsColor = TriState::eUnknown;
// Lock this when modifying any static variables.
// Because it is a recursive mutex, its owner can lock it multiple times.
static std::recursive_mutex s_mutex;
void nvprintSetLogFileName(const char* name) noexcept
{
std::lock_guard<std::recursive_mutex> lockGuard(s_mutex);
if(name == NULL || s_logFileName == name)
return;
try
{
s_logFileName = name;
}
catch(const std::exception& e)
{
nvprintLevel(LOGLEVEL_ERROR, "nvprintfSetLogFileName could not allocate space for new file name. Additional info below:");
nvprintLevel(LOGLEVEL_ERROR, e.what());
}
if(s_fd)
{
fclose(s_fd);
s_fd = nullptr;
s_bLogReady = false;
}
}
void nvprintSetCallback(PFN_NVPRINTCALLBACK callback)
{
s_printCallback = callback;
}
void nvprintSetLevel(int l)
{
s_printLevel = l;
}
int nvprintGetLevel()
{
return s_printLevel;
}
void nvprintSetLogging(bool b)
{
s_bPrintLogging = b;
}
void nvprintSetFileLogging(bool state, uint32_t mask)
{
std::lock_guard<std::recursive_mutex> lockGuard(s_mutex);
if(state)
{
s_bPrintFileLogging |= mask;
}
else
{
s_bPrintFileLogging &= ~mask;
}
}
void nvprintSetConsoleLogging(bool state, uint32_t mask)
{
std::lock_guard<std::recursive_mutex> lockGuard(s_mutex);
if(state)
{
s_bPrintConsoleLogging |= mask;
}
else
{
s_bPrintConsoleLogging &= ~mask;
}
}
void nvprintSetBreakpoints(bool state, uint32_t mask)
{
std::lock_guard<std::recursive_mutex> lockGuard(s_mutex);
if(state)
{
s_bPrintBreakpoints |= mask;
}
else
{
s_bPrintBreakpoints &= ~mask;
}
}
void nvprintfV(va_list& vlist, const char* fmt, int level) noexcept
{
if(s_bPrintLogging == false)
{
return;
}
// Format the inputs into s_strBuffer.
std::lock_guard<std::recursive_mutex> lockGuard(s_mutex);
{
// Copy vlist as it may be modified by vsnprintf.
va_list vlistCopy;
va_copy(vlistCopy, vlist);
const int charactersNeeded = vsnprintf(s_strBuffer.data(), s_strBuffer.size(), fmt, vlistCopy);
va_end(vlistCopy);
// Check that:
// * vsnprintf did not return an error;
// * The string (plus null terminator) could fit in a vector.
if((charactersNeeded < 0) || (size_t(charactersNeeded) > s_strBuffer.max_size() - 1))
{
// Formatting error
nvprintLevel(LOGLEVEL_ERROR, "nvprintfV: Internal message formatting error.");
return;
}
// Increase the size of s_strBuffer as needed if there wasn't enough space.
if(size_t(charactersNeeded) >= s_strBuffer.size())
{
try
{
// Make sure to add 1, because vsnprintf doesn't count the terminating
// null character. This can potentially throw an exception.
s_strBuffer.resize(size_t(charactersNeeded) + 1, '\0');
}
catch(const std::exception& e)
{
nvprintLevel(LOGLEVEL_ERROR, "nvprintfV: Error resizing buffer to hold message. Additional info below:");
nvprintLevel(LOGLEVEL_ERROR, e.what());
return;
}
// Now format it; we know this will succeed.
(void)vsnprintf(s_strBuffer.data(), s_strBuffer.size(), fmt, vlist);
}
}
nvprintLevel(level, s_strBuffer.data());
}
void nvprintLevel(int level, const std::string& msg) noexcept
{
nvprintLevel(level, msg.c_str());
}
void nvprintLevel(int level, const char* msg) noexcept
{
std::lock_guard<std::recursive_mutex> lockGuard(s_mutex);
#ifdef WIN32
// Note: Maybe we could consider changing to a text encoding of UTF-8 in
// the future, bring in calls to Windows' MultiByteToWideChar, and call
// OutputDebugStringW.
OutputDebugStringA(msg);
#endif
if(s_bPrintFileLogging & (1 << level))
{
if(s_bLogReady == false)
{
s_fd = fopen(s_logFileName.c_str(), "wt");
s_bLogReady = true;
}
if(s_fd)
{
fputs(msg, s_fd);
}
}
if(s_printCallback)
{
s_printCallback(level, msg);
}
if(s_bPrintConsoleLogging & (1 << level))
{
// Determine if the output supports ANSI color sequences only once to avoid
// many calls to isatty.
if(TriState::eUnknown == s_consoleSupportsColor)
{
// Determining this perfectly is difficult; terminfo does it by storing
// a large table of all consoles it knows about. For now, we assume
// all consoles support colors, and all pipes do not.
#ifdef WIN32
const bool supportsColor = _isatty(_fileno(stderr)) && _isatty(_fileno(stdout));
#else
const bool supportsColor = isatty(fileno(stderr)) && isatty(fileno(stdout));
#endif
s_consoleSupportsColor = (supportsColor ? TriState::eTrue : TriState::eFalse);
}
FILE* outStream = (((1 << level) & LOGBITS_ERRORS) ? stderr : stdout);
if(TriState::eTrue == s_consoleSupportsColor)
{
// Set the foreground color depending on level:
// https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters
if(level == LOGLEVEL_OK)
{
fputs("\033[32m", outStream); // Green
}
else if(level == LOGLEVEL_ERROR)
{
fputs("\033[31m", outStream); // Red
}
else if(level == LOGLEVEL_WARNING)
{
fputs("\033[33m", outStream); // Yellow
}
else if(level == LOGLEVEL_DEBUG)
{
fputs("\033[36m", outStream); // Cyan
}
}
fputs(msg, outStream);
if(TriState::eTrue == s_consoleSupportsColor)
{
// Reset all attributes
fputs("\033[0m", outStream);
}
}
if(s_bPrintBreakpoints & (1 << level))
{
#ifdef WIN32
DebugBreak();
#else
raise(SIGTRAP);
#endif
}
}
void nvprintf(
#ifdef _MSC_VER
_Printf_format_string_
#endif
const char* fmt,
...) noexcept
{
// int r = 0;
va_list vlist;
va_start(vlist, fmt);
nvprintfV(vlist, fmt, s_printLevel);
va_end(vlist);
}
void nvprintfLevel(int level,
#ifdef _MSC_VER
_Printf_format_string_
#endif
const char* fmt,
...) noexcept
{
va_list vlist;
va_start(vlist, fmt);
nvprintfV(vlist, fmt, level);
va_end(vlist);
}