Skip to content

Commit

Permalink
Converted logging to spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
adepke committed Sep 23, 2021
1 parent 29823a7 commit 359ed8e
Show file tree
Hide file tree
Showing 24 changed files with 201 additions and 419 deletions.
11 changes: 6 additions & 5 deletions VanguardEngine/Source/Asset/AssetLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Asset/AssetLoader.h>
#include <Asset/TextureLoader.h>
#include <Rendering/RenderComponents.h>
#include <Utility/StringTools.h>

#define TINYGLTF_IMPLEMENTATION
#define STB_IMAGE_WRITE_IMPLEMENTATION
Expand Down Expand Up @@ -60,28 +61,28 @@ namespace AssetLoader

else
{
VGLogError(Asset) << "Unknown asset load file extension '" << (path.has_extension() ? path.extension() : "[ No extension ]") << "'.";
VGLogError(logAsset, "Unknown asset load file extension '{}'.", (path.has_extension() ? path.extension().generic_wstring() : VGText("[ No extension ]")));
}
}

if (!warning.empty())
{
VGLogWarning(Asset) << "GLTF load: " << warning;
VGLogWarning(logAsset, "GLTF load: {}", Str2WideStr(warning));
}

if (!error.empty())
{
VGLogError(Asset) << "GLTF load: " << error;
VGLogError(logAsset, "GLTF load: {}", Str2WideStr(error));
}

if (!result)
{
VGLogError(Asset) << "Failed to load asset '" << path.filename().generic_string() << "'.";
VGLogError(logAsset, "Failed to load asset '{}'.", path.filename().generic_wstring());
}

else
{
VGLog(Asset) << "Loaded asset '" << path.filename().generic_string() << "'.";
VGLog(logAsset, "Loaded asset '{}'.", path.filename().generic_wstring());
}

std::vector<MeshComponent::Subset> subsets;
Expand Down
2 changes: 1 addition & 1 deletion VanguardEngine/Source/Asset/TextureLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace AssetLoader

if (!data)
{
VGLogError(Asset) << "Failed to load texture at '" << path.generic_wstring() << "'.";
VGLogError(logAsset, "Failed to load texture at '{}'.", path.generic_wstring());
return {};
}

Expand Down
2 changes: 1 addition & 1 deletion VanguardEngine/Source/Core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ namespace Config

else
{
VGLogFatal(Core) << "Failed to find engine root.";
VGLogCritical(logCore, "Failed to find engine root.");
}

std::ifstream engineConfigStream{ engineRootPath / engineConfigPath };
Expand Down
5 changes: 5 additions & 0 deletions VanguardEngine/Source/Core/CrashHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

#include <Core/Misc.h>

#include <spdlog/spdlog.h>

#include <string>
#include <atomic>

Expand All @@ -23,12 +25,15 @@ VGForceInline void RequestCrash(const std::wstring& reason, bool printToLog = fa
{
ReportCrashEvent(reason, printToLog);

spdlog::shutdown(); // Flushes all sinks.

if (HasDebuggerAttached())
{
VGBreak();
}

// Use exit instead of std::terminate to avoid a message box.
// #TODO: Consider proper thread shutdown instead of hard killing the process.
exit(-1);
}

Expand Down
79 changes: 45 additions & 34 deletions VanguardEngine/Source/Core/Engine.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) 2019-2021 Andrew Depke

#include <Core/Engine.h>
#include <Core/LogOutputs.h>
#include <Core/Base.h>
#include <Core/Config.h>
#include <Rendering/Device.h>
#include <Rendering/Renderer.h>
Expand All @@ -11,6 +11,10 @@
#include <Core/CoreSystems.h>
#include <Core/CrashHandler.h>

#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <spdlog/sinks/msvc_sink.h>

#include <string>
#include <memory>
#include <chrono>
Expand All @@ -19,6 +23,7 @@
#include <Rendering/RenderComponents.h>
#include <Rendering/RenderSystems.h>
#include <Asset/AssetLoader.h>
#include <Utility/Random.h>
//

entt::registry registry;
Expand All @@ -27,7 +32,7 @@ void OnFocusChanged(bool focus)
{
VGScopedCPUStat("Focus Changed");

VGLog(Window) << (focus ? "Acquired focus." : "Released focus.");
VGLog(logWindow, "{}", (focus ? VGText("Acquired focus.") : VGText("Released focus.")));

// #TODO: Limit render FPS, disable audio.
}
Expand All @@ -36,7 +41,7 @@ void OnSizeChanged(uint32_t width, uint32_t height, bool fullscreen)
{
VGScopedCPUStat("Size Changed");

VGLog(Window) << "Window size changed (" << width << ", " << height << ").";
VGLog(logWindow, "Window size changed ({}, {}).", width, height);

Renderer::Get().device->SetResolution(width, height, fullscreen);
Renderer::Get().OnBackBufferSizeChanged(registry);
Expand All @@ -46,9 +51,28 @@ void EngineBoot()
{
VGScopedCPUStat("Engine Boot");

// Send the output to the profiler and to the VS debugger.
Logger::Get().AddOutput<LogWindowsOutput>();
Logger::Get().AddOutput<LogProfilerOutput>();
auto fileSink = std::make_shared<spdlog::sinks::basic_file_sink_mt>("Log.txt", true);
auto msvcSink = std::make_shared<spdlog::sinks::msvc_sink_mt>();

logCore = std::make_shared<spdlog::logger>("core", spdlog::sinks_init_list{ fileSink, msvcSink });
logAsset = logCore->clone("asset");
logRendering = logCore->clone("rendering");
logThreading = logCore->clone("threading");
logUtility = logCore->clone("utility");
logWindow = logCore->clone("window");

spdlog::set_default_logger(logCore);
spdlog::set_pattern("[%H:%M:%S.%e][tid:%t][%n.%l] %v");
spdlog::flush_on(spdlog::level::critical);
spdlog::flush_every(1s);

// Not useful to set an error handler, this isn't invoked unless exceptions are enabled.
// With exceptions disabled, spdlog just writes to stderr.
// #TODO: Consider changing the behavior of error handling with exceptions disabled.
//spdlog::set_error_handler([](const std::string& msg)
//{
// VGLogError(logCore, "Logger: {}", msg);
//});

Config::Initialize();

Expand Down Expand Up @@ -97,34 +121,21 @@ void EngineLoop()
registry.emplace<TransformComponent>(sponza, std::move(sponzaTransform));
registry.emplace<MeshComponent>(sponza, AssetLoader::LoadMesh(*Renderer::Get().device, Config::shadersPath / "../Assets/Models/Sponza/glTF/Sponza.gltf"));

LightComponent pointLight1{};
pointLight1.color = { 1.f, 1.f, 1.f };
LightComponent pointLight2{};
pointLight2.color = { 1.f, 1.f, 1.f };
LightComponent pointLight3{};
pointLight3.color = { 1.f, 1.f, 1.f };

TransformComponent light1Transform{};
light1Transform.translation = { -60, 4.f, 10.f };
TransformComponent light2Transform{};
light2Transform.translation = { 0.f, 4.f, 10.f };
TransformComponent light3Transform{};
light3Transform.translation = { 60.f, 4.f, 10.f };

const auto light1 = registry.create();
registry.emplace<NameComponent>(light1, "Light1");
registry.emplace<TransformComponent>(light1, std::move(light1Transform));
registry.emplace<LightComponent>(light1, pointLight1);

const auto light2 = registry.create();
registry.emplace<NameComponent>(light2, "Light2");
registry.emplace<TransformComponent>(light2, std::move(light2Transform));
registry.emplace<LightComponent>(light2, pointLight2);
int lightCount = 10000;

const auto light3 = registry.create();
registry.emplace<NameComponent>(light3, "Light3");
registry.emplace<TransformComponent>(light3, std::move(light3Transform));
registry.emplace<LightComponent>(light3, pointLight3);
for (int i = 0; i < lightCount; ++i)
{
LightComponent pointLight{ .color = { (float)Rand(0.2f, 1.f), (float)Rand(0.2f, 1.f), (float)Rand(0.2f, 1.f) } };
TransformComponent transform{
.scale = { 1.f, 1.f, 1.f },
.rotation = { 0.f, 0.f, 0.f },
.translation = { (float)Rand(-150.0, 150.0), (float)Rand(-65.0, 65.0), (float)Rand(0.0, 120.0) }
};

const auto light = registry.create();
registry.emplace<LightComponent>(light, pointLight);
registry.emplace<TransformComponent>(light, transform);
}

auto frameBegin = std::chrono::high_resolution_clock::now();
float lastDeltaTime = 0.f;
Expand Down Expand Up @@ -169,7 +180,7 @@ void EngineShutdown()
{
VGScopedCPUStat("Engine Shutdown");

VGLog(Core) << "Engine shutting down.";
VGLog(logCore, "Engine shutting down.");
}

int32_t EngineMain()
Expand Down
2 changes: 1 addition & 1 deletion VanguardEngine/Source/Core/Engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma once

#include <Core/Base.h>
#include <cstdint>

int32_t EngineMain();
void EngineShutdown();
10 changes: 5 additions & 5 deletions VanguardEngine/Source/Core/Input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ namespace Input

if (FAILED(::GetDpiForMonitor(static_cast<HMONITOR>(monitor), MDT_EFFECTIVE_DPI, &dpiX, &dpiY)))
{
VGLogError(Core) << "Failed to get monitor DPI.";
VGLogError(logCore, "Failed to get monitor DPI.");

return 1.f;
}
Expand Down Expand Up @@ -106,7 +106,7 @@ namespace Input
POINT mousePosition;
if (!::GetCursorPos(&mousePosition))
{
VGLogWarning(Core) << "Failed to get mouse cursor position: " << GetPlatformError();
VGLogWarning(logCore, "Failed to get mouse cursor position: {}", GetPlatformError());

return;
}
Expand All @@ -131,7 +131,7 @@ namespace Input
if (foregroundWindow == window)
{
if (!::ScreenToClient(static_cast<HWND>(window), &mousePosition))
VGLogWarning(Core) << "Failed to convert mouse position from screen space to window space: " << GetPlatformError();
VGLogWarning(logCore, "Failed to convert mouse position from screen space to window space: {}", GetPlatformError());
else
io.MousePos = { static_cast<float>(mousePosition.x), static_cast<float>(mousePosition.y) };
}
Expand Down Expand Up @@ -176,7 +176,7 @@ namespace Input

if (!::SetCursor(::LoadCursor(nullptr, platformCursor)))
{
VGLogWarning(Core) << "Failed to set cursor: " << GetPlatformError();
VGLogWarning(logCore, "Failed to set cursor: {}", GetPlatformError());
}
}
}
Expand All @@ -195,7 +195,7 @@ namespace Input
// Ensure we have an ImGui context.
if (!ImGui::GetCurrentContext())
{
VGLogFatal(Core) << "Missing ImGui context!";
VGLogCritical(logCore, "Missing ImGui context!");
}

auto& io = ImGui::GetIO();
Expand Down
102 changes: 0 additions & 102 deletions VanguardEngine/Source/Core/LogOutputs.h

This file was deleted.

0 comments on commit 359ed8e

Please sign in to comment.