aboutsummaryrefslogtreecommitdiff
path: root/src/zenutil/logging/logging.cpp
blob: 06e8f920e8bd75bc53234ecf5d94c13b497f15bc (plain) (blame)
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
// Copyright Epic Games, Inc. All Rights Reserved.

#include "zenutil/logging.h"

#include <zencore/callstack.h>
#include <zencore/compactbinary.h>
#include <zencore/filesystem.h>
#include <zencore/logging.h>
#include <zencore/logging/ansicolorsink.h>
#include <zencore/logging/asyncsink.h>
#include <zencore/logging/backlogsink.h>
#include <zencore/logging/broadcastsink.h>
#include <zencore/logging/logger.h>
#include <zencore/logging/msvcsink.h>
#include <zencore/logging/registry.h>
#include <zencore/memory/llm.h>
#include <zencore/string.h>
#include <zencore/timer.h>
#include <zenutil/logging/fullformatter.h>
#include <zenutil/logging/jsonformatter.h>
#include <zenutil/logging/rotatingfilesink.h>

#include <chrono>
#include <memory>

namespace zen {

static bool					g_IsLoggingInitialized;
logging::SinkPtr			g_FileSink;
Ref<logging::BroadcastSink> g_BroadcastSink;
Ref<logging::BacklogSink>	g_BacklogSink;

logging::SinkPtr
GetFileSink()
{
	return g_FileSink;
}

Ref<logging::BroadcastSink>
GetDefaultBroadcastSink()
{
	return g_BroadcastSink;
}

Ref<logging::BacklogSink>
GetLogBacklogSink()
{
	return g_BacklogSink;
}

void
AttachSinkWithBacklogReplay(logging::SinkPtr Sink)
{
	if (!Sink)
	{
		return;
	}
	// Drain any AsyncSink queue first so messages emitted just before this
	// call have a chance to land in the backlog before we snapshot its
	// cursor. Without this we'd still be correct (AsyncSink would deliver
	// the queued lines to the new sink directly via the broadcast after
	// AddSink runs), but the new sink would observe them out of timestamp
	// order — replayed history first, then late drains interleaved with
	// fresh post-attach messages.
	zen::logging::FlushLogging();

	if (!g_BroadcastSink)
	{
		return;
	}

	// Subscribe Sink to the broadcast and snapshot the backlog cursor
	// atomically. The broadcast's exclusive lock blocks fanout while it's
	// held, so during the callback no thread can be inside backlog.Log()
	// and the cursor we capture is exact w.r.t. the moment Sink became
	// visible to fanout.
	//
	// After the callback returns and the lock is released, two things are
	// true:
	//  - Any backlog entry at index < Cursor was added before Sink became
	//    a fanout target, so Sink did NOT receive it via Log() — Replay
	//    must deliver it.
	//  - Any backlog entry at index >= Cursor was added after Sink became
	//    a fanout target, so Sink already received it via Log() — Replay
	//    must skip it.
	// This closes the prior race where a message arriving between Replay
	// and AddSink would land in the backlog only and be lost on
	// DisableLogBacklog.
	size_t Cursor = 0;
	g_BroadcastSink->AddSinkAtomic(Sink, [&]() {
		if (g_BacklogSink && g_BacklogSink->IsEnabled())
		{
			Cursor = g_BacklogSink->Size();
		}
	});

	if (Cursor > 0)
	{
		g_BacklogSink->Replay(*Sink, Cursor);
	}
}

void
DisableLogBacklog()
{
	if (!g_BacklogSink)
	{
		return;
	}
	// Remove from the broadcast first so subsequent log lines don't even
	// fan out to a now-no-op sink — one fewer per-log overhead for the
	// rest of the process lifetime. Disable() then frees the arena, and
	// dropping our own Ref releases the object.
	if (g_BroadcastSink)
	{
		g_BroadcastSink->RemoveSink(logging::SinkPtr(g_BacklogSink.Get()));
	}
	g_BacklogSink->Disable();
	g_BacklogSink = nullptr;
}

void
InitializeLogging(const LoggingOptions& LogOptions)
{
	BeginInitializeLogging(LogOptions);
	FinishInitializeLogging(LogOptions);
}

static std::terminate_handler OldTerminateHandler = nullptr;

void
BeginInitializeLogging(const LoggingOptions& LogOptions)
{
	ZEN_MEMSCOPE(ELLMTag::Logging);

	zen::logging::InitializeLogging();

	// Sinks

	logging::SinkPtr FileSink;

	if (!LogOptions.AbsLogFile.empty())
	{
		if (LogOptions.AbsLogFile.has_parent_path())
		{
			zen::CreateDirectories(LogOptions.AbsLogFile.parent_path());
		}

		FileSink = logging::SinkPtr(new zen::logging::RotatingFileSink(LogOptions.AbsLogFile,
																	   /* max size */ 128 * 1024 * 1024,
																	   /* max files */ 16,
																	   /* rotate on open */ true));
		if (LogOptions.AbsLogFile.extension() == ".json")
		{
			FileSink->SetFormatter(std::make_unique<logging::JsonFormatter>(LogOptions.LogId));
		}
		else
		{
			FileSink->SetFormatter(std::make_unique<logging::FullFormatter>(LogOptions.LogId));	 // this will have a date prefix
		}
	}

	OldTerminateHandler = std::set_terminate([]() {
		try
		{
			constexpr int	 SkipFrameCount = 4;
			constexpr int	 FrameCount		= 8;
			uint8_t			 CallstackBuffer[CallstackRawMemorySize(SkipFrameCount, FrameCount)];
			CallstackFrames* Callstack = GetCallstackRaw(&CallstackBuffer[0], SkipFrameCount, FrameCount);

			fmt::basic_memory_buffer<char, 2048> Message;
			auto								 Appender = fmt::appender(Message);
			fmt::format_to(Appender, "Program exited abnormally via std::terminate()");

			if (Callstack->FrameCount > 0)
			{
				fmt::format_to(Appender, "\n");

				CallstackToStringRaw(Callstack, &Message, [](void* UserData, uint32_t FrameIndex, const char* FrameText) {
					ZEN_UNUSED(FrameIndex);
					fmt::basic_memory_buffer<char, 2048>* Message  = (fmt::basic_memory_buffer<char, 2048>*)UserData;
					auto								  Appender = fmt::appender(*Message);
					fmt::format_to(Appender, "  {}\n", FrameText);
				});
			}
			Message.push_back('\0');

			// We use direct ZEN_LOG here instead of ZEN_ERROR as we don't care about *this* code location in the log
			ZEN_LOG(Log(), zen::logging::Critical, "{}", Message.data());
			zen::logging::FlushLogging();
		}
		catch (const std::exception&)
		{
			// Ignore any exceptions in terminate callback
		}
		if (OldTerminateHandler)
		{
			OldTerminateHandler();
		}
	});

	// Default

	LoggerRef DefaultLogger = zen::logging::Default();

	// Build the broadcast sink - a shared indirection point that all
	// loggers cloned from the default will share.  Adding or removing
	// a child sink later is immediately visible to every logger.
	std::vector<logging::SinkPtr> BroadcastChildren;

	// Install the backlog sink as the first broadcast child so it sees
	// every line emitted from this point until the bootstrap window
	// closes (DisableLogBacklog at server-run-loop / CLI-dispatch). Sinks
	// attached later via AttachSinkWithBacklogReplay can replay the
	// captured window into themselves so they don't miss the early logs.
	g_BacklogSink = Ref<logging::BacklogSink>(new logging::BacklogSink());
	BroadcastChildren.push_back(logging::SinkPtr(g_BacklogSink.Get()));

	if (LogOptions.NoConsoleOutput)
	{
		zen::logging::SuppressConsoleLog();
	}
	else
	{
		logging::SinkPtr ConsoleSink(
			new logging::AnsiColorStdoutSink(LogOptions.ForceColor ? logging::ColorMode::On : logging::ColorMode::Auto));
		if (LogOptions.QuietConsole)
		{
			ConsoleSink->SetLevel(logging::Warn);
		}
		BroadcastChildren.push_back(ConsoleSink);
	}

	if (FileSink)
	{
		BroadcastChildren.push_back(FileSink);
	}

#if ZEN_PLATFORM_WINDOWS
	if (zen::IsDebuggerPresent() && LogOptions.IsDebug)
	{
		logging::SinkPtr DebugSink(new logging::MsvcSink());
		DebugSink->SetLevel(logging::Debug);
		BroadcastChildren.push_back(DebugSink);
	}
#endif

	// Trace forwarding is installed at a lower level (in Logger::Log itself)
	// so it can see the typed fmt argument pack before vformat collapses it
	// to a string - see src/zencore/logging/tracelog.cpp.

	g_BroadcastSink = Ref<logging::BroadcastSink>(new logging::BroadcastSink(std::move(BroadcastChildren)));

	bool IsAsync = LogOptions.AllowAsync && !LogOptions.IsDebug && !LogOptions.IsTest;

	if (IsAsync)
	{
		DefaultLogger->SetSink(logging::SinkPtr(new logging::AsyncSink({logging::SinkPtr(g_BroadcastSink.Get())})));
	}
	else
	{
		DefaultLogger->SetSink(logging::SinkPtr(g_BroadcastSink.Get()));
	}

	static struct : logging::ErrorHandler
	{
		void HandleError(const std::string_view& ErrorMsg) override
		{
			if (ErrorMsg == std::bad_alloc().what())
			{
				return;
			}
			static constinit logging::LogPoint ErrorPoint{0, 0, logging::Err, "{}"};
			if (auto ErrLogger = zen::logging::ErrorLog())
			{
				try
				{
					ErrLogger->Log(ErrorPoint, fmt::make_format_args(ErrorMsg));
				}
				catch (const std::exception&)
				{
				}
			}
			try
			{
				Log()->Log(ErrorPoint, fmt::make_format_args(ErrorMsg));
			}
			catch (const std::exception&)
			{
			}
		}
	} s_ErrorHandler;
	logging::Registry::Instance().SetErrorHandler(&s_ErrorHandler);

	g_FileSink = std::move(FileSink);
}

void
FinishInitializeLogging(const LoggingOptions& LogOptions)
{
	ZEN_MEMSCOPE(ELLMTag::Logging);

	logging::LogLevel LogLevel = logging::Info;

	if (LogOptions.IsDebug)
	{
		LogLevel = logging::Debug;
	}

	if (LogOptions.IsTest || LogOptions.IsVerbose)
	{
		LogLevel = logging::Trace;
	}

	// Configure all registered loggers according to settings

	logging::RefreshLogLevels(LogLevel);
	logging::Registry::Instance().FlushOn(logging::Err);
	logging::Registry::Instance().FlushEvery(std::chrono::seconds{2});
	logging::Registry::Instance().SetFormatter(std::make_unique<logging::FullFormatter>(
		LogOptions.LogId,
		std::chrono::system_clock::now() - std::chrono::milliseconds(GetTimeSinceProcessStart())));	 // default to duration prefix

	// If the console logger was initialized before, the above will change the output format
	// so we need to reset it

	logging::ResetConsoleLog();

	if (g_FileSink)
	{
		if (LogOptions.AbsLogFile.extension() == ".json")
		{
			g_FileSink->SetFormatter(std::make_unique<logging::JsonFormatter>(LogOptions.LogId));
		}
		else
		{
			g_FileSink->SetFormatter(std::make_unique<logging::FullFormatter>(LogOptions.LogId));  // this will have a date prefix
		}

		const std::string StartLogTime = zen::DateTime::Now().ToIso8601();

		logging::Registry::Instance().ApplyAll([&](auto Logger) {
			static constinit logging::LogPoint LogStartPoint{0, 0, logging::Info, "log starting at {}"};
			Logger->Log(LogStartPoint, fmt::make_format_args(StartLogTime));
		});
	}

	g_IsLoggingInitialized = true;
}

void
ShutdownLogging()
{
	if (g_IsLoggingInitialized && g_FileSink)
	{
		auto DefaultLogger = zen::logging::Default();
		ZEN_LOG_INFO(DefaultLogger, "log ending at {}", zen::DateTime::Now().ToIso8601());
	}

	zen::logging::ShutdownLogging();

	g_FileSink		= nullptr;
	g_BroadcastSink = nullptr;
	g_BacklogSink	= nullptr;
}

}  // namespace zen