From 03d52d4eb99f0daded2d28956c1fd1381972386d Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Wed, 28 May 2025 04:05:12 +0200 Subject: [PATCH] Add support for building engine without logging --- Source/Editor/Analytics/EditorAnalytics.cpp | 4 ++ Source/Editor/Managed/ManagedEditor.cpp | 4 ++ Source/Engine/Content/Assets/VisualScript.cpp | 2 + Source/Engine/Core/Log.cpp | 3 ++ Source/Engine/Core/Log.h | 52 +++++++++++-------- Source/Engine/Core/LogContext.cpp | 2 + Source/Engine/Debug/Exception.cpp | 2 + Source/Engine/Engine/Engine.cpp | 8 +++ Source/Engine/Graphics/Graphics.cpp | 4 ++ .../Vulkan/RenderToolsVulkan.cpp | 2 + Source/Engine/Platform/Base/PlatformBase.cpp | 2 + Source/Engine/Platform/Mac/MacPlatform.cpp | 4 ++ Source/Engine/Profiler/ProfilerMemory.cpp | 2 + .../Internal/EngineInternalCalls.cpp | 4 ++ Source/Engine/Scripting/ManagedCLR/MCore.cpp | 2 + 15 files changed, 76 insertions(+), 21 deletions(-) diff --git a/Source/Editor/Analytics/EditorAnalytics.cpp b/Source/Editor/Analytics/EditorAnalytics.cpp index 385492789..ab4bf7f33 100644 --- a/Source/Editor/Analytics/EditorAnalytics.cpp +++ b/Source/Editor/Analytics/EditorAnalytics.cpp @@ -174,7 +174,9 @@ void EditorAnalytics::StartSession() // Bind events GameCooker::OnEvent.Bind(); ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Bind(); +#if LOG_ENABLE Log::Logger::OnError.Bind(); +#endif } void EditorAnalytics::EndSession() @@ -187,7 +189,9 @@ void EditorAnalytics::EndSession() // Unbind events GameCooker::OnEvent.Unbind(); ShadowsOfMordor::Builder::Instance()->OnBuildStarted.Unbind(); +#if LOG_ENABLE Log::Logger::OnError.Unbind(); +#endif // End session { diff --git a/Source/Editor/Managed/ManagedEditor.cpp b/Source/Editor/Managed/ManagedEditor.cpp index d65ae6e0a..e270d08f8 100644 --- a/Source/Editor/Managed/ManagedEditor.cpp +++ b/Source/Editor/Managed/ManagedEditor.cpp @@ -156,7 +156,9 @@ ManagedEditor::ManagedEditor() lightmapsBuilder->OnBuildProgress.Bind(); lightmapsBuilder->OnBuildFinished.Bind(); CSG::Builder::OnBrushModified.Bind(); +#if LOG_ENABLE Log::Logger::OnMessage.Bind(); +#endif VisualScripting::DebugFlow.Bind(); } @@ -172,7 +174,9 @@ ManagedEditor::~ManagedEditor() lightmapsBuilder->OnBuildProgress.Unbind(); lightmapsBuilder->OnBuildFinished.Unbind(); CSG::Builder::OnBrushModified.Unbind(); +#if LOG_ENABLE Log::Logger::OnMessage.Unbind(); +#endif VisualScripting::DebugFlow.Unbind(); } diff --git a/Source/Engine/Content/Assets/VisualScript.cpp b/Source/Engine/Content/Assets/VisualScript.cpp index 2e6ffb735..329696dea 100644 --- a/Source/Engine/Content/Assets/VisualScript.cpp +++ b/Source/Engine/Content/Assets/VisualScript.cpp @@ -38,10 +38,12 @@ namespace void PrintStack(LogType type) { +#if LOG_ENABLE const String stack = VisualScripting::GetStackTrace(); Log::Logger::Write(type, TEXT("Visual Script stack trace:")); Log::Logger::Write(type, stack); Log::Logger::Write(type, TEXT("")); +#endif } bool SerializeValue(const Variant& a, const Variant& b) diff --git a/Source/Engine/Core/Log.cpp b/Source/Engine/Core/Log.cpp index 85cc3cc02..013031ced 100644 --- a/Source/Engine/Core/Log.cpp +++ b/Source/Engine/Core/Log.cpp @@ -1,6 +1,7 @@ // Copyright (c) Wojciech Figat. All rights reserved. #include "Log.h" +#if LOG_ENABLE #include "Engine/Engine/CommandLine.h" #include "Engine/Core/Types/DateTime.h" #include "Engine/Core/Collections/Array.h" @@ -310,3 +311,5 @@ const Char* ToString(LogType e) } return result; } + +#endif diff --git a/Source/Engine/Core/Log.h b/Source/Engine/Core/Log.h index 2db769e45..f09099d11 100644 --- a/Source/Engine/Core/Log.h +++ b/Source/Engine/Core/Log.h @@ -7,27 +7,6 @@ #include "Engine/Core/Types/String.h" #include "Engine/Core/Types/StringView.h" -// Enable/disable auto flush function -#define LOG_ENABLE_AUTO_FLUSH 1 - -/// -/// Sends a formatted message to the log file (message type - describes level of the log (see LogType enum)) -/// -#define LOG(messageType, format, ...) Log::Logger::Write(LogType::messageType, ::String::Format(TEXT(format), ##__VA_ARGS__)) - -/// -/// Sends a string message to the log file (message type - describes level of the log (see LogType enum)) -/// -#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 - /// /// The log message types. /// @@ -54,6 +33,29 @@ API_ENUM() enum class LogType Fatal = 8, }; +#if LOG_ENABLE + +// Enable/disable auto flush function +#define LOG_ENABLE_AUTO_FLUSH 1 + +/// +/// Sends a formatted message to the log file (message type - describes level of the log (see LogType enum)) +/// +#define LOG(messageType, format, ...) Log::Logger::Write(LogType::messageType, ::String::Format(TEXT(format), ##__VA_ARGS__)) + +/// +/// Sends a string message to the log file (message type - describes level of the log (see LogType enum)) +/// +#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); namespace Log @@ -186,3 +188,11 @@ namespace Log 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 diff --git a/Source/Engine/Core/LogContext.cpp b/Source/Engine/Core/LogContext.cpp index 8a14ad48c..5eeec5738 100644 --- a/Source/Engine/Core/LogContext.cpp +++ b/Source/Engine/Core/LogContext.cpp @@ -47,6 +47,7 @@ ThreadLocal GlobalLogContexts; void LogContext::Print(LogType verbosity) { +#if LOG_ENABLE auto& stack = GlobalLogContexts.Get(); if (stack.Count == 0) return; @@ -102,6 +103,7 @@ void LogContext::Print(LogType verbosity) // Print message Log::Logger::Write(verbosity, msg.ToStringView()); } +#endif } void LogContext::Push(const Guid& id) diff --git a/Source/Engine/Debug/Exception.cpp b/Source/Engine/Debug/Exception.cpp index d866d4867..7eb112aa1 100644 --- a/Source/Engine/Debug/Exception.cpp +++ b/Source/Engine/Debug/Exception.cpp @@ -4,6 +4,8 @@ Log::Exception::~Exception() { +#if LOG_ENABLE // Always write exception to the log Logger::Write(_level, ToString()); +#endif } diff --git a/Source/Engine/Engine/Engine.cpp b/Source/Engine/Engine/Engine.cpp index 17a2f377b..a20c1780b 100644 --- a/Source/Engine/Engine/Engine.cpp +++ b/Source/Engine/Engine/Engine.cpp @@ -150,7 +150,9 @@ int32 Engine::Main(const Char* cmdLine) { // End LOG(Warning, "Loading project cancelled. Closing..."); +#if LOG_ENABLE Log::Logger::Dispose(); +#endif return 0; } #endif @@ -168,8 +170,10 @@ int32 Engine::Main(const Char* cmdLine) #if !USE_EDITOR && (PLATFORM_WINDOWS || PLATFORM_LINUX || PLATFORM_MAC) EngineImpl::RunInBackground = PlatformSettings::Get()->RunInBackground; #endif +#if LOG_ENABLE Log::Logger::WriteFloor(); LOG_FLUSH(); +#endif Time::Synchronize(); EngineImpl::IsReady = true; PROFILE_MEM_END(); @@ -546,14 +550,17 @@ void Engine::OnExit() ProfilerGPU::Dispose(); #endif +#if LOG_ENABLE // Close logging service Log::Logger::Dispose(); +#endif Platform::Exit(); } void EngineImpl::InitLog() { +#if LOG_ENABLE // Initialize logger Log::Logger::Init(); @@ -607,6 +614,7 @@ void EngineImpl::InitLog() Platform::LogInfo(); LOG_FLUSH(); +#endif } void EngineImpl::InitPaths() diff --git a/Source/Engine/Graphics/Graphics.cpp b/Source/Engine/Graphics/Graphics.cpp index 43bd1a76d..733e8c222 100644 --- a/Source/Engine/Graphics/Graphics.cpp +++ b/Source/Engine/Graphics/Graphics.cpp @@ -101,8 +101,10 @@ bool GraphicsService::Init() PROFILE_MEM(Graphics); // Create and initialize graphics device +#if LOG_ENABLE Log::Logger::WriteFloor(); LOG(Info, "Creating Graphics Device..."); +#endif PixelFormatExtensions::Init(); GPUDevice* device = nullptr; @@ -216,7 +218,9 @@ bool GraphicsService::Init() { return true; } +#if LOG_ENABLE Log::Logger::WriteFloor(); +#endif return false; } diff --git a/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp b/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp index 961799a42..d535f6ea1 100644 --- a/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp +++ b/Source/Engine/GraphicsDevice/Vulkan/RenderToolsVulkan.cpp @@ -250,8 +250,10 @@ void RenderToolsVulkan::LogVkResult(VkResult result, const char* file, uint32 li errorType = FatalErrorType::GPUCrash; if (errorType != FatalErrorType::None) Platform::Fatal(msg, nullptr, errorType); +#if LOG_ENABLE else Log::Logger::Write(fatal ? LogType::Fatal : LogType::Error, msg); +#endif } bool RenderToolsVulkan::HasExtension(const Array& extensions, const char* name) diff --git a/Source/Engine/Platform/Base/PlatformBase.cpp b/Source/Engine/Platform/Base/PlatformBase.cpp index 9ba6b7bd6..7e8428e64 100644 --- a/Source/Engine/Platform/Base/PlatformBase.cpp +++ b/Source/Engine/Platform/Base/PlatformBase.cpp @@ -310,6 +310,7 @@ void PlatformBase::Fatal(const StringView& msg, void* context, FatalErrorType er Engine::RequestingExit(); // 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_FLUSH(); @@ -406,6 +407,7 @@ void PlatformBase::Fatal(const StringView& msg, void* context, FatalErrorType er LOG(Error, "Crash info collected."); Log::Logger::WriteFloor(); } +#endif // Show error message if (Engine::ReportCrash.IsBinded()) diff --git a/Source/Engine/Platform/Mac/MacPlatform.cpp b/Source/Engine/Platform/Mac/MacPlatform.cpp index fca66cf42..6cb8b1153 100644 --- a/Source/Engine/Platform/Mac/MacPlatform.cpp +++ b/Source/Engine/Platform/Mac/MacPlatform.cpp @@ -490,7 +490,9 @@ int32 MacPlatform::CreateProcess(CreateProcessSettings& settings) StringView lineView(line); if (line[line.Length() - 1] == '\n') lineView = StringView(line.Get(), line.Length() - 1); +#if LOG_ENABLE Log::Logger::Write(LogType::Info, lineView); +#endif } [[stdoutPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; } @@ -517,7 +519,9 @@ int32 MacPlatform::CreateProcess(CreateProcessSettings& settings) StringView lineView(line); if (line[line.Length() - 1] == '\n') lineView = StringView(line.Get(), line.Length() - 1); +#if LOG_ENABLE Log::Logger::Write(LogType::Error, lineView); +#endif } [[stderrPipe fileHandleForReading] waitForDataInBackgroundAndNotify]; } diff --git a/Source/Engine/Profiler/ProfilerMemory.cpp b/Source/Engine/Profiler/ProfilerMemory.cpp index 61433c0e7..0aa2ba053 100644 --- a/Source/Engine/Profiler/ProfilerMemory.cpp +++ b/Source/Engine/Profiler/ProfilerMemory.cpp @@ -394,6 +394,7 @@ ProfilerMemory::GroupsArray ProfilerMemory::GetGroups(int32 mode) void ProfilerMemory::Dump(const StringView& options) { +#if LOG_ENABLE bool file = options.Contains(TEXT("file")); StringBuilder output; int32 maxCount = 20; @@ -408,6 +409,7 @@ void ProfilerMemory::Dump(const StringView& options) return; } LOG_STR(Info, output.ToStringView()); +#endif } void ProfilerMemory::OnMemoryAlloc(void* ptr, uint64 size) diff --git a/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp b/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp index 21323d2bd..a310409c0 100644 --- a/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp +++ b/Source/Engine/Scripting/Internal/EngineInternalCalls.cpp @@ -52,13 +52,16 @@ DEFINE_INTERNAL_CALL(int32) PlatformInternal_MemoryCompare(const void* buf1, con DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_LogWrite(LogType level, MString* msgObj) { +#if LOG_ENABLE StringView msg; MUtils::ToString(msgObj, msg); Log::Logger::Write(level, msg); +#endif } DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_Log(LogType level, MString* msgObj, ScriptingObject* obj, MString* stackTrace) { +#if LOG_ENABLE if (msgObj == nullptr) 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? //const String logMessage = String::Format(TEXT("Debug:{1} {2}"), objName, *msg); Log::Logger::Write(level, msg); +#endif } DEFINE_INTERNAL_CALL(void) DebugLogHandlerInternal_LogException(MObject* exception, ScriptingObject* obj) diff --git a/Source/Engine/Scripting/ManagedCLR/MCore.cpp b/Source/Engine/Scripting/ManagedCLR/MCore.cpp index 0b38730e8..4300434e3 100644 --- a/Source/Engine/Scripting/ManagedCLR/MCore.cpp +++ b/Source/Engine/Scripting/ManagedCLR/MCore.cpp @@ -246,6 +246,7 @@ MType* MEvent::GetType() const void MException::Log(const LogType type, const Char* target) { +#if LOG_ENABLE // Log inner exceptions chain MException* inner = InnerException; 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."); 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)); +#endif } MType* MProperty::GetType() const