Add support for building engine without logging

This commit is contained in:
2025-05-28 04:05:12 +02:00
parent ab61ed5a37
commit 03d52d4eb9
15 changed files with 76 additions and 21 deletions
@@ -174,7 +174,9 @@ void EditorAnalytics::StartSession()
// Bind events // Bind events
GameCooker::OnEvent.Bind<RegisterGameCookingStart>(); GameCooker::OnEvent.Bind<RegisterGameCookingStart>();
ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Bind<RegisterLightmapsBuildingStart>(); ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Bind<RegisterLightmapsBuildingStart>();
#if LOG_ENABLE
Log::Logger::OnError.Bind<RegisterError>(); Log::Logger::OnError.Bind<RegisterError>();
#endif
} }
void EditorAnalytics::EndSession() void EditorAnalytics::EndSession()
@@ -187,7 +189,9 @@ void EditorAnalytics::EndSession()
// Unbind events // Unbind events
GameCooker::OnEvent.Unbind<RegisterGameCookingStart>(); GameCooker::OnEvent.Unbind<RegisterGameCookingStart>();
ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Unbind<RegisterLightmapsBuildingStart>(); ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Unbind<RegisterLightmapsBuildingStart>();
#if LOG_ENABLE
Log::Logger::OnError.Unbind<RegisterError>(); Log::Logger::OnError.Unbind<RegisterError>();
#endif
// End session // End session
{ {
+4
View File
@@ -156,7 +156,9 @@ ManagedEditor::ManagedEditor()
lightmapsBuilder->OnBuildProgress.Bind<OnLightmapsBuildProgress>(); lightmapsBuilder->OnBuildProgress.Bind<OnLightmapsBuildProgress>();
lightmapsBuilder->OnBuildFinished.Bind<OnLightmapsBuildFinished>(); lightmapsBuilder->OnBuildFinished.Bind<OnLightmapsBuildFinished>();
CSG::Builder::OnBrushModified.Bind<OnBrushModified>(); CSG::Builder::OnBrushModified.Bind<OnBrushModified>();
#if LOG_ENABLE
Log::Logger::OnMessage.Bind<OnLogMessage>(); Log::Logger::OnMessage.Bind<OnLogMessage>();
#endif
VisualScripting::DebugFlow.Bind<OnVisualScriptingDebugFlow>(); VisualScripting::DebugFlow.Bind<OnVisualScriptingDebugFlow>();
} }
@@ -172,7 +174,9 @@ ManagedEditor::~ManagedEditor()
lightmapsBuilder->OnBuildProgress.Unbind<OnLightmapsBuildProgress>(); lightmapsBuilder->OnBuildProgress.Unbind<OnLightmapsBuildProgress>();
lightmapsBuilder->OnBuildFinished.Unbind<OnLightmapsBuildFinished>(); lightmapsBuilder->OnBuildFinished.Unbind<OnLightmapsBuildFinished>();
CSG::Builder::OnBrushModified.Unbind<OnBrushModified>(); CSG::Builder::OnBrushModified.Unbind<OnBrushModified>();
#if LOG_ENABLE
Log::Logger::OnMessage.Unbind<OnLogMessage>(); Log::Logger::OnMessage.Unbind<OnLogMessage>();
#endif
VisualScripting::DebugFlow.Unbind<OnVisualScriptingDebugFlow>(); VisualScripting::DebugFlow.Unbind<OnVisualScriptingDebugFlow>();
} }
@@ -38,10 +38,12 @@ namespace
void PrintStack(LogType type) void PrintStack(LogType type)
{ {
#if LOG_ENABLE
const String stack = VisualScripting::GetStackTrace(); const String stack = VisualScripting::GetStackTrace();
Log::Logger::Write(type, TEXT("Visual Script stack trace:")); Log::Logger::Write(type, TEXT("Visual Script stack trace:"));
Log::Logger::Write(type, stack); Log::Logger::Write(type, stack);
Log::Logger::Write(type, TEXT("")); Log::Logger::Write(type, TEXT(""));
#endif
} }
bool SerializeValue(const Variant& a, const Variant& b) bool SerializeValue(const Variant& a, const Variant& b)
+3
View File
@@ -1,6 +1,7 @@
// Copyright (c) Wojciech Figat. All rights reserved. // Copyright (c) Wojciech Figat. All rights reserved.
#include "Log.h" #include "Log.h"
#if LOG_ENABLE
#include "Engine/Engine/CommandLine.h" #include "Engine/Engine/CommandLine.h"
#include "Engine/Core/Types/DateTime.h" #include "Engine/Core/Types/DateTime.h"
#include "Engine/Core/Collections/Array.h" #include "Engine/Core/Collections/Array.h"
@@ -310,3 +311,5 @@ const Char* ToString(LogType e)
} }
return result; return result;
} }
#endif
+31 -21
View File
@@ -7,27 +7,6 @@
#include "Engine/Core/Types/String.h" #include "Engine/Core/Types/String.h"
#include "Engine/Core/Types/StringView.h" #include "Engine/Core/Types/StringView.h"
// Enable/disable auto flush function
#define LOG_ENABLE_AUTO_FLUSH 1
/// <summary>
/// Sends a formatted message to the log file (message type - describes level of the log (see LogType enum))
/// </summary>
#define LOG(messageType, format, ...) Log::Logger::Write(LogType::messageType, ::String::Format(TEXT(format), ##__VA_ARGS__))
/// <summary>
/// Sends a string message to the log file (message type - describes level of the log (see LogType enum))
/// </summary>
#define LOG_STR(messageType, str) Log::Logger::Write(LogType::messageType, str)
#if LOG_ENABLE_AUTO_FLUSH
// Noop as log is auto-flushed on write
#define LOG_FLUSH()
#else
// Flushes the log file buffer
#define LOG_FLUSH() Log::Logger::Flush()
#endif
/// <summary> /// <summary>
/// The log message types. /// The log message types.
/// </summary> /// </summary>
@@ -54,6 +33,29 @@ API_ENUM() enum class LogType
Fatal = 8, Fatal = 8,
}; };
#if LOG_ENABLE
// Enable/disable auto flush function
#define LOG_ENABLE_AUTO_FLUSH 1
/// <summary>
/// Sends a formatted message to the log file (message type - describes level of the log (see LogType enum))
/// </summary>
#define LOG(messageType, format, ...) Log::Logger::Write(LogType::messageType, ::String::Format(TEXT(format), ##__VA_ARGS__))
/// <summary>
/// Sends a string message to the log file (message type - describes level of the log (see LogType enum))
/// </summary>
#define LOG_STR(messageType, str) Log::Logger::Write(LogType::messageType, str)
#if LOG_ENABLE_AUTO_FLUSH
// Noop as log is auto-flushed on write
#define LOG_FLUSH()
#else
// Flushes the log file buffer
#define LOG_FLUSH() Log::Logger::Flush()
#endif
extern const Char* ToString(LogType e); extern const Char* ToString(LogType e);
namespace Log namespace Log
@@ -186,3 +188,11 @@ namespace Log
static void ProcessLogMessage(LogType type, const StringView& msg, fmt_flax::memory_buffer& w); static void ProcessLogMessage(LogType type, const StringView& msg, fmt_flax::memory_buffer& w);
}; };
} }
#else
#define LOG(messageType, format, ...)
#define LOG_STR(messageType, str)
#define LOG_FLUSH()
#endif
+2
View File
@@ -47,6 +47,7 @@ ThreadLocal<LogContextThreadData> GlobalLogContexts;
void LogContext::Print(LogType verbosity) void LogContext::Print(LogType verbosity)
{ {
#if LOG_ENABLE
auto& stack = GlobalLogContexts.Get(); auto& stack = GlobalLogContexts.Get();
if (stack.Count == 0) if (stack.Count == 0)
return; return;
@@ -102,6 +103,7 @@ void LogContext::Print(LogType verbosity)
// Print message // Print message
Log::Logger::Write(verbosity, msg.ToStringView()); Log::Logger::Write(verbosity, msg.ToStringView());
} }
#endif
} }
void LogContext::Push(const Guid& id) void LogContext::Push(const Guid& id)
+2
View File
@@ -4,6 +4,8 @@
Log::Exception::~Exception() Log::Exception::~Exception()
{ {
#if LOG_ENABLE
// Always write exception to the log // Always write exception to the log
Logger::Write(_level, ToString()); Logger::Write(_level, ToString());
#endif
} }
+8
View File
@@ -150,7 +150,9 @@ int32 Engine::Main(const Char* cmdLine)
{ {
// End // End
LOG(Warning, "Loading project cancelled. Closing..."); LOG(Warning, "Loading project cancelled. Closing...");
#if LOG_ENABLE
Log::Logger::Dispose(); Log::Logger::Dispose();
#endif
return 0; return 0;
} }
#endif #endif
@@ -168,8 +170,10 @@ int32 Engine::Main(const Char* cmdLine)
#if !USE_EDITOR && (PLATFORM_WINDOWS || PLATFORM_LINUX || PLATFORM_MAC) #if !USE_EDITOR && (PLATFORM_WINDOWS || PLATFORM_LINUX || PLATFORM_MAC)
EngineImpl::RunInBackground = PlatformSettings::Get()->RunInBackground; EngineImpl::RunInBackground = PlatformSettings::Get()->RunInBackground;
#endif #endif
#if LOG_ENABLE
Log::Logger::WriteFloor(); Log::Logger::WriteFloor();
LOG_FLUSH(); LOG_FLUSH();
#endif
Time::Synchronize(); Time::Synchronize();
EngineImpl::IsReady = true; EngineImpl::IsReady = true;
PROFILE_MEM_END(); PROFILE_MEM_END();
@@ -546,14 +550,17 @@ void Engine::OnExit()
ProfilerGPU::Dispose(); ProfilerGPU::Dispose();
#endif #endif
#if LOG_ENABLE
// Close logging service // Close logging service
Log::Logger::Dispose(); Log::Logger::Dispose();
#endif
Platform::Exit(); Platform::Exit();
} }
void EngineImpl::InitLog() void EngineImpl::InitLog()
{ {
#if LOG_ENABLE
// Initialize logger // Initialize logger
Log::Logger::Init(); Log::Logger::Init();
@@ -607,6 +614,7 @@ void EngineImpl::InitLog()
Platform::LogInfo(); Platform::LogInfo();
LOG_FLUSH(); LOG_FLUSH();
#endif
} }
void EngineImpl::InitPaths() void EngineImpl::InitPaths()
+4
View File
@@ -101,8 +101,10 @@ bool GraphicsService::Init()
PROFILE_MEM(Graphics); PROFILE_MEM(Graphics);
// Create and initialize graphics device // Create and initialize graphics device
#if LOG_ENABLE
Log::Logger::WriteFloor(); Log::Logger::WriteFloor();
LOG(Info, "Creating Graphics Device..."); LOG(Info, "Creating Graphics Device...");
#endif
PixelFormatExtensions::Init(); PixelFormatExtensions::Init();
GPUDevice* device = nullptr; GPUDevice* device = nullptr;
@@ -216,7 +218,9 @@ bool GraphicsService::Init()
{ {
return true; return true;
} }
#if LOG_ENABLE
Log::Logger::WriteFloor(); Log::Logger::WriteFloor();
#endif
return false; return false;
} }
@@ -250,8 +250,10 @@ void RenderToolsVulkan::LogVkResult(VkResult result, const char* file, uint32 li
errorType = FatalErrorType::GPUCrash; errorType = FatalErrorType::GPUCrash;
if (errorType != FatalErrorType::None) if (errorType != FatalErrorType::None)
Platform::Fatal(msg, nullptr, errorType); Platform::Fatal(msg, nullptr, errorType);
#if LOG_ENABLE
else else
Log::Logger::Write(fatal ? LogType::Fatal : LogType::Error, msg); Log::Logger::Write(fatal ? LogType::Fatal : LogType::Error, msg);
#endif
} }
bool RenderToolsVulkan::HasExtension(const Array<const char*>& extensions, const char* name) bool RenderToolsVulkan::HasExtension(const Array<const char*>& extensions, const char* name)
@@ -310,6 +310,7 @@ void PlatformBase::Fatal(const StringView& msg, void* context, FatalErrorType er
Engine::RequestingExit(); Engine::RequestingExit();
// Collect crash info (platform-dependant implementation that might collect stack trace and/or create memory dump) // Collect crash info (platform-dependant implementation that might collect stack trace and/or create memory dump)
#if LOG_ENABLE
{ {
// Log separation for crash info // Log separation for crash info
LOG_FLUSH(); LOG_FLUSH();
@@ -406,6 +407,7 @@ void PlatformBase::Fatal(const StringView& msg, void* context, FatalErrorType er
LOG(Error, "Crash info collected."); LOG(Error, "Crash info collected.");
Log::Logger::WriteFloor(); Log::Logger::WriteFloor();
} }
#endif
// Show error message // Show error message
if (Engine::ReportCrash.IsBinded()) if (Engine::ReportCrash.IsBinded())
@@ -490,7 +490,9 @@ int32 MacPlatform::CreateProcess(CreateProcessSettings& settings)
StringView lineView(line); StringView lineView(line);
if (line[line.Length() - 1] == '\n') if (line[line.Length() - 1] == '\n')
lineView = StringView(line.Get(), line.Length() - 1); lineView = StringView(line.Get(), line.Length() - 1);
#if LOG_ENABLE
Log::Logger::Write(LogType::Info, lineView); Log::Logger::Write(LogType::Info, lineView);
#endif
} }
[[stdoutPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; [[stdoutPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
} }
@@ -517,7 +519,9 @@ int32 MacPlatform::CreateProcess(CreateProcessSettings& settings)
StringView lineView(line); StringView lineView(line);
if (line[line.Length() - 1] == '\n') if (line[line.Length() - 1] == '\n')
lineView = StringView(line.Get(), line.Length() - 1); lineView = StringView(line.Get(), line.Length() - 1);
#if LOG_ENABLE
Log::Logger::Write(LogType::Error, lineView); Log::Logger::Write(LogType::Error, lineView);
#endif
} }
[[stderrPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; [[stderrPipe fileHandleForReading] waitForDataInBackgroundAndNotify];
} }
@@ -394,6 +394,7 @@ ProfilerMemory::GroupsArray ProfilerMemory::GetGroups(int32 mode)
void ProfilerMemory::Dump(const StringView& options) void ProfilerMemory::Dump(const StringView& options)
{ {
#if LOG_ENABLE
bool file = options.Contains(TEXT("file")); bool file = options.Contains(TEXT("file"));
StringBuilder output; StringBuilder output;
int32 maxCount = 20; int32 maxCount = 20;
@@ -408,6 +409,7 @@ void ProfilerMemory::Dump(const StringView& options)
return; return;
} }
LOG_STR(Info, output.ToStringView()); LOG_STR(Info, output.ToStringView());
#endif
} }
void ProfilerMemory::OnMemoryAlloc(void* ptr, uint64 size) void ProfilerMemory::OnMemoryAlloc(void* ptr, uint64 size)
@@ -52,13 +52,16 @@ DEFINE_INTERNAL_CALL(int32) PlatformInternal_MemoryCompare(const void* buf1, con
DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_LogWrite(LogType level, MString* msgObj) DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_LogWrite(LogType level, MString* msgObj)
{ {
#if LOG_ENABLE
StringView msg; StringView msg;
MUtils::ToString(msgObj, msg); MUtils::ToString(msgObj, msg);
Log::Logger::Write(level, msg); Log::Logger::Write(level, msg);
#endif
} }
DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_Log(LogType level, MString* msgObj, ScriptingObject* obj, MString* stackTrace) DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_Log(LogType level, MString* msgObj, ScriptingObject* obj, MString* stackTrace)
{ {
#if LOG_ENABLE
if (msgObj == nullptr) if (msgObj == nullptr)
return; return;
@@ -71,6 +74,7 @@ DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_Log(LogType level, MString* m
// TODO: maybe option for build to threat warnings and errors as fatal errors? // TODO: maybe option for build to threat warnings and errors as fatal errors?
//const String logMessage = String::Format(TEXT("Debug:{1} {2}"), objName, *msg); //const String logMessage = String::Format(TEXT("Debug:{1} {2}"), objName, *msg);
Log::Logger::Write(level, msg); Log::Logger::Write(level, msg);
#endif
} }
DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_LogException(MObject* exception, ScriptingObject* obj) DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_LogException(MObject* exception, ScriptingObject* obj)
@@ -246,6 +246,7 @@ MType* MEvent::GetType() const
void MException::Log(const LogType type, const Char* target) void MException::Log(const LogType type, const Char* target)
{ {
#if LOG_ENABLE
// Log inner exceptions chain // Log inner exceptions chain
MException* inner = InnerException; MException* inner = InnerException;
while (inner) while (inner)
@@ -260,6 +261,7 @@ void MException::Log(const LogType type, const Char* target)
const String info = target && *target ? String::Format(TEXT("Exception has been thrown during {0}."), target) : TEXT("Exception has been thrown."); const String info = target && *target ? String::Format(TEXT("Exception has been thrown during {0}."), target) : TEXT("Exception has been thrown.");
Log::Logger::Write(LogType::Warning, String::Format(TEXT("{0} {1}\nStack strace:\n{2}"), info, Message, stackTrace)); Log::Logger::Write(LogType::Warning, String::Format(TEXT("{0} {1}\nStack strace:\n{2}"), info, Message, stackTrace));
Log::Logger::Write(type, String::Format(TEXT("{0}\n{1}"), info, Message)); Log::Logger::Write(type, String::Format(TEXT("{0}\n{1}"), info, Message));
#endif
} }
MType* MProperty::GetType() const MType* MProperty::GetType() const