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
|
// 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
# include <zencore/trace.h>
# include <zencore/memory/memorytrace.h>
namespace zen {
void
TraceConfigure()
{
// Configure channels based on command line options
using namespace std::literals;
constexpr std::string_view TraceOption = "--trace="sv;
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); };
bool TraceOptionPresent = false;
std::function<void(const std::string_view&)> ProcessArg = [&](const std::string_view& Arg) {
if (Arg.starts_with(TraceOption))
{
const std::string_view OptionArgs = Arg.substr(TraceOption.size());
TraceOptionPresent = true;
ProcessChannelList(OptionArgs);
}
};
IterateCommandlineArgs(ProcessArg);
if (!TraceOptionPresent)
{
ProcessTraceArg("default"sv);
}
}
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();
});
TraceConfigure();
}
void
TraceShutdown()
{
(void)TraceStop();
trace::Shutdown();
}
bool
IsTracing()
{
return trace::IsTracing();
}
void
TraceStart(std::string_view ProgramName, const char* HostOrPath, TraceType Type)
{
TraceInit(ProgramName);
switch (Type)
{
case TraceType::Network:
trace::SendTo(HostOrPath);
break;
case TraceType::File:
trace::WriteTo(HostOrPath);
break;
case TraceType::None:
break;
}
}
bool
TraceStop()
{
if (trace::Stop())
{
return true;
}
return false;
}
} // namespace zen
#endif // ZEN_WITH_TRACE
|