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
|
// Copyright Epic Games, Inc. All Rights Reserved.
#if ZEN_WITH_TRACE
# include <zencore/config.h>
# include <zencore/zencore.h>
# include <zencore/commandline.h>
# include <zencore/string.h>
# include <zencore/logging.h>
# define TRACE_IMPLEMENT 1
# undef _WINSOCK_DEPRECATED_NO_WARNINGS
# include <zencore/trace.h>
# include <zencore/memory/fmalloc.h>
# include <zencore/memory/memorytrace.h>
namespace zen {
void
TraceConfigure(const TraceOptions& Options)
{
// Configure channels based on command line options
using namespace std::literals;
std::function<void(const std::string_view&)> ProcessChannelList;
auto ProcessTraceArg = [&](const std::string_view& Arg) {
if (Arg == "default"sv)
{
ProcessChannelList("cpu,log"sv);
}
else if (Arg == "memory"sv)
{
ProcessChannelList("memtag,memalloc,callstack,module"sv);
}
else if (Arg == "memory_light"sv)
{
ProcessChannelList("memtag,memalloc"sv);
}
else if (Arg == "memtag"sv)
{
// memtag actually traces to the memalloc channel
ProcessChannelList("memalloc"sv);
}
else
{
// Presume that the argument is a trace channel name
StringBuilder<128> AnsiChannel;
AnsiChannel << Arg;
const bool IsEnabled = trace::ToggleChannel(AnsiChannel.c_str(), true);
if (IsEnabled == false)
{
// Logging here could be iffy, but we might want some other feedback mechanism here
// to indicate to users that they're not getting what they might be expecting
}
}
};
ProcessChannelList = [&](const std::string_view& OptionArgs) { IterateCommaSeparatedValue(OptionArgs, ProcessTraceArg); };
if (Options.Channels.empty())
{
ProcessTraceArg("default"sv);
}
else
{
ProcessChannelList(Options.Channels);
}
if (Options.Host.size())
{
trace::SendTo(Options.Host.c_str());
}
else if (Options.File.size())
{
trace::WriteTo(Options.File.c_str());
}
# if ZEN_WITH_MEMTRACK
FMalloc* TraceMalloc = MemoryTrace_Create(GMalloc, Options);
if (TraceMalloc != GMalloc)
{
GMalloc = TraceMalloc;
MemoryTrace_Initialize();
}
# endif
}
void
TraceInit(std::string_view ProgramName)
{
static std::atomic_bool gInited = false;
bool Expected = false;
if (!gInited.compare_exchange_strong(Expected, true))
{
return;
}
trace::FInitializeDesc Desc = {
.bUseImportantCache = true,
};
trace::Initialize(Desc);
# if ZEN_PLATFORM_WINDOWS
const char* CommandLineString = GetCommandLineA();
# else
const char* CommandLineString = "";
# endif
trace::ThreadRegister("main", /* system id */ 0, /* sort id */ 0);
trace::DescribeSession(ProgramName,
# if ZEN_BUILD_DEBUG
trace::Build::Debug,
# else
trace::Build::Development,
# endif
CommandLineString,
ZEN_CFG_VERSION_BUILD_STRING);
atexit([] {
# if ZEN_WITH_MEMTRACK
zen::MemoryTrace_Shutdown();
# endif
trace::Update();
TraceShutdown();
});
}
void
TraceShutdown()
{
(void)TraceStop();
trace::Shutdown();
}
bool
IsTracing()
{
return trace::IsTracing();
}
bool
TraceStop()
{
if (trace::Stop())
{
return true;
}
return false;
}
bool
GetTraceOptionsFromCommandline(TraceOptions& OutOptions)
{
bool HasOptions = false;
# if ZEN_WITH_TRACE
using namespace std::literals;
auto MatchesArg = [](std::string_view Option, std::string_view Arg) -> std::optional<std::string_view> {
if (Arg.starts_with(Option))
{
std::string_view::value_type DelimChar = Arg[Option.length()];
if (DelimChar == ' ' || DelimChar == '=')
{
return Arg.substr(Option.size() + 1);
}
}
return {};
};
constexpr std::string_view TraceOption = "--trace"sv;
constexpr std::string_view TraceHostOption = "--tracehost"sv;
constexpr std::string_view TraceFileOption = "--tracefile"sv;
std::function<void(const std::string_view&)> ProcessArg = [&](const std::string_view& Arg) {
if (auto Host = MatchesArg(TraceHostOption, Arg); Host.has_value())
{
OutOptions.Host = Host.value();
HasOptions = true;
}
else if (auto File = MatchesArg(TraceFileOption, Arg); File.has_value())
{
OutOptions.File = File.value();
HasOptions = true;
}
else if (auto Channels = MatchesArg(TraceOption, Arg); Channels.has_value())
{
if (!OutOptions.Channels.empty())
{
OutOptions.Channels = ","sv;
}
OutOptions.Channels += Channels.value();
HasOptions = true;
}
};
IterateCommandlineArgs(ProcessArg);
# endif // ZEN_WITH_TRACE
return HasOptions;
}
} // namespace zen
#endif // ZEN_WITH_TRACE
|