forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrace.cpp
More file actions
206 lines (175 loc) · 5.42 KB
/
trace.cpp
File metadata and controls
206 lines (175 loc) · 5.42 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include "trace.h"
#include <mutex>
// g_trace_verbosity is used to encode COREHOST_TRACE and COREHOST_TRACE_VERBOSITY to selectively control output of
// trace::warn(), trace::info(), and trace::verbose()
// COREHOST_TRACE=0 COREHOST_TRACE_VERBOSITY=N/A implies g_trace_verbosity = 0. // Trace "disabled". error() messages will be produced.
// COREHOST_TRACE=1 COREHOST_TRACE_VERBOSITY=4 or unset implies g_trace_verbosity = 4. // Trace "enabled". verbose(), info(), warn() and error() messages will be produced
// COREHOST_TRACE=1 COREHOST_TRACE_VERBOSITY=3 implies g_trace_verbosity = 3. // Trace "enabled". info(), warn() and error() messages will be produced
// COREHOST_TRACE=1 COREHOST_TRACE_VERBOSITY=2 implies g_trace_verbosity = 2. // Trace "enabled". warn() and error() messages will be produced
// COREHOST_TRACE=1 COREHOST_TRACE_VERBOSITY=1 implies g_trace_verbosity = 1. // Trace "enabled". error() messages will be produced
static int g_trace_verbosity = 0;
static FILE * g_trace_file = stderr;
static pal::mutex_t g_trace_mutex;
thread_local static trace::error_writer_fn g_error_writer = nullptr;
//
// Turn on tracing for the corehost based on "COREHOST_TRACE" & "COREHOST_TRACEFILE" env.
//
void trace::setup()
{
// Read trace environment variable
pal::string_t trace_str;
if (!pal::getenv(_X("COREHOST_TRACE"), &trace_str))
{
return;
}
auto trace_val = pal::xtoi(trace_str.c_str());
if (trace_val > 0)
{
if (trace::enable())
{
auto ts = pal::get_timestamp();
trace::info(_X("Tracing enabled @ %s"), ts.c_str());
}
}
}
bool trace::enable()
{
bool file_open_error = false;
pal::string_t tracefile_str;
if (g_trace_verbosity)
{
return false;
}
else
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
g_trace_file = stderr;
if (pal::getenv(_X("COREHOST_TRACEFILE"), &tracefile_str))
{
FILE *tracefile = pal::file_open(tracefile_str, _X("a"));
if (tracefile)
{
setvbuf(tracefile, nullptr, _IONBF, 0);
g_trace_file = tracefile;
}
else
{
file_open_error = true;
}
}
pal::string_t trace_str;
if (!pal::getenv(_X("COREHOST_TRACE_VERBOSITY"), &trace_str))
{
g_trace_verbosity = 4; // Verbose trace by default
}
else
{
g_trace_verbosity = pal::xtoi(trace_str.c_str());
}
}
if (file_open_error)
{
trace::error(_X("Unable to open COREHOST_TRACEFILE=%s for writing"), tracefile_str.c_str());
}
return true;
}
bool trace::is_enabled()
{
return g_trace_verbosity;
}
void trace::verbose(const pal::char_t* format, ...)
{
if (g_trace_verbosity > 3)
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
va_list args;
va_start(args, format);
pal::file_vprintf(g_trace_file, format, args);
va_end(args);
}
}
void trace::info(const pal::char_t* format, ...)
{
if (g_trace_verbosity > 2)
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
va_list args;
va_start(args, format);
pal::file_vprintf(g_trace_file, format, args);
va_end(args);
}
}
void trace::error(const pal::char_t* format, ...)
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
// Always print errors
va_list args;
va_start(args, format);
va_list trace_args;
va_copy(trace_args, args);
va_list dup_args;
va_copy(dup_args, args);
int count = pal::str_vprintf(nullptr, 0, format, args) + 1;
std::vector<pal::char_t> buffer(count);
pal::str_vprintf(&buffer[0], count, format, dup_args);
if (g_error_writer == nullptr)
{
pal::err_fputs(buffer.data());
}
else
{
g_error_writer(buffer.data());
}
#if defined(_WIN32)
::OutputDebugStringW(buffer.data());
#endif
if (g_trace_verbosity && ((g_trace_file != stderr) || g_error_writer != nullptr))
{
pal::file_vprintf(g_trace_file, format, trace_args);
}
va_end(args);
}
void trace::println(const pal::char_t* format, ...)
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
va_list args;
va_start(args, format);
pal::out_vprintf(format, args);
va_end(args);
}
void trace::println()
{
println(_X(""));
}
void trace::warning(const pal::char_t* format, ...)
{
if (g_trace_verbosity > 1)
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
va_list args;
va_start(args, format);
pal::file_vprintf(g_trace_file, format, args);
va_end(args);
}
}
void trace::flush()
{
std::lock_guard<pal::mutex_t> lock(g_trace_mutex);
pal::file_flush(g_trace_file);
pal::err_flush();
pal::out_flush();
}
trace::error_writer_fn trace::set_error_writer(trace::error_writer_fn error_writer)
{
// No need for locking since g_error_writer is thread local.
error_writer_fn previous_writer = g_error_writer;
g_error_writer = error_writer;
return previous_writer;
}
trace::error_writer_fn trace::get_error_writer()
{
// No need for locking since g_error_writer is thread local.
return g_error_writer;
}