Skip to content

Commit

Permalink
Require loggers to use configuration stored in C4Config
Browse files Browse the repository at this point in the history
  • Loading branch information
Fulgen301 committed Nov 18, 2024
1 parent 01c8987 commit 207528e
Show file tree
Hide file tree
Showing 24 changed files with 360 additions and 91 deletions.
13 changes: 11 additions & 2 deletions src/C4Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,19 @@ void C4Application::DoInit()
assert(AppState == C4AS_None);
// Config overwrite by parameter
StdStrBuf sConfigFilename;
bool verbose{false};
char szParameter[_MAX_PATH + 1];
for (int32_t iPar = 0; SGetParameter(GetCommandLine(), iPar, szParameter, _MAX_PATH); iPar++)
{
if (SEqual2NoCase(szParameter, "/config:"))
{
sConfigFilename.Copy(szParameter + 8);
}
else if (!verbose && SEqualNoCase(szParameter, "/verbose"))
{
verbose = true;
}
}
// Config check
Config.Init();
Config.Load(true, sConfigFilename.getData());
Expand Down Expand Up @@ -113,7 +122,7 @@ void C4Application::DoInit()
C4Group_SetSortList(C4CFN_FLS);

// Open log
LogSystem.OpenLog();
LogSystem.OpenLog(verbose);

// init system group
if (!SystemGroup.Open(C4CFN_System))
Expand Down Expand Up @@ -204,7 +213,7 @@ void C4Application::DoInit()
spdlog::info("Version: " C4VERSION " " C4_OS);

// Initialize OpenGL
DDraw = DDrawInit(this, Config.Graphics.Engine);
DDraw = DDrawInit(this, LogSystem, Config.Graphics.Engine);
if (!DDraw) { LogFatal(C4ResStrTableKey::IDS_ERR_DDRAW); Clear(); throw StartupException{LogSystem.GetFatalErrorString()}; }

#if defined(_WIN32) && !defined(USE_CONSOLE)
Expand Down
5 changes: 3 additions & 2 deletions src/C4AudioSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* for the above references.
*/

#include "C4Application.h"
#include "C4AudioSystem.h"

#include "C4AudioSystemNone.h"
#include "C4Config.h"
#include "C4Log.h"

#ifdef USE_SDL_MIXER
Expand All @@ -34,7 +35,7 @@ C4AudioSystem *C4AudioSystem::NewInstance(
}
catch (const std::runtime_error &e)
{
CreateLogger("C4AudioSystem")->error(e.what());
Application.LogSystem.CreateLogger(Config.Logging.AudioSystem)->error(e.what());
}
#endif
return new C4AudioSystemNone{};
Expand Down
3 changes: 3 additions & 0 deletions src/C4AudioSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#pragma once

#include "C4Log.h"
#include "Standard.h"

#include <cstddef>
Expand Down Expand Up @@ -101,3 +102,5 @@ class C4AudioSystem
public:
static constexpr auto MaxChannels = 1024;
};

C4LOGGERCONFIG_NAME_TYPE(C4AudioSystem);
5 changes: 3 additions & 2 deletions src/C4AudioSystemSdl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
* for the above references.
*/

#include "C4Application.h"
#include "C4AudioSystemSdl.h"

#include "C4Config.h"
#include "C4Log.h"

#include "StdHelpers.h"
Expand Down Expand Up @@ -144,7 +145,7 @@ C4AudioSystemSdl::C4AudioSystemSdl(const int maxChannels, const bool preferLinea
assert(!instance);
instance = this;

auto logger = CreateLogger("C4AudioSystemSdl");
auto logger = Application.LogSystem.CreateLoggerWithDifferentName(Config.Logging.AudioSystem, "C4AudioSystem");

// Check SDL_mixer version
SDL_version compile_version;
Expand Down
20 changes: 20 additions & 0 deletions src/C4Aul.h
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,15 @@ class C4AulProfiler
static void StopProfiling();
};

C4LOGGERCONFIG_NAME_TYPE(C4AulProfiler);

template<>
struct C4LoggerConfig::Defaults<C4AulProfiler>
{
static constexpr spdlog::level::level_enum GuiLogLevel{spdlog::level::info};
static constexpr bool ShowLoggerNameInGui{false};
};

// script class
class C4AulScript
{
Expand Down Expand Up @@ -587,3 +596,14 @@ class C4AulScriptEngine : public C4AulScript
friend class C4AulFunc;
friend class C4AulParseState;
};

class C4AulExec;

C4LOGGERCONFIG_NAME_TYPE(C4AulExec);

template<>
struct C4LoggerConfig::Defaults<C4AulExec>
{
static constexpr auto GuiLogLevel = spdlog::level::info;
static constexpr bool ShowLoggerNameInGui{false};
};
4 changes: 2 additions & 2 deletions src/C4AulExec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ void C4AulExec::StartTrace()
if (iTraceStart < 0)
{
iTraceStart = ContextStackSize();
traceLogger = CreateLogger("C4AulExec", {.GuiLogLevel = spdlog::level::info, .ShowLoggerNameInGui = false});
traceLogger = Application.LogSystem.CreateLogger(Config.Logging.AulExec);
}
}

Expand All @@ -1558,7 +1558,7 @@ void C4AulExec::StopProfiling()
if (!fProfiling) return;
fProfiling = false;
// collect profiler times
C4AulProfiler Profiler{CreateLogger("C4AulProfiler", {.GuiLogLevel = spdlog::level::info, .ShowLoggerNameInGui = false})};
C4AulProfiler Profiler{Application.LogSystem.CreateLogger(Config.Logging.AulProfiler)};
Profiler.CollectEntry(nullptr, tDirectExecTotal);
pProfiledScript->CollectProfilerTimes(Profiler);
Profiler.Show();
Expand Down
20 changes: 20 additions & 0 deletions src/C4Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@
#include "C4Version.h"
#ifdef C4ENGINE
#include <C4Application.h>
#include "C4GameControl.h"
#include <C4Log.h>
#include <C4Network2.h>
#include "C4Network2Reference.h"
#include "C4Network2UPnP.h"
#include "C4Record.h"
#include "C4ResStrTable.h"
#include <C4UpperBoard.h>
#endif
Expand Down Expand Up @@ -397,6 +401,21 @@ void C4ConfigToasts::CompileFunc(StdCompiler *comp)
{
comp->Value(mkNamingAdapt(ReadyCheck, "ReadyCheck", true));
}

void C4ConfigLogging::CompileFunc(StdCompiler *const comp)
{
comp->Value(mkNamingAdapt(LogLevelStdout, "LogLevelStdout", spdlog::level::info));

comp->Value(AudioSystem);
comp->Value(AulExec);
comp->Value(AulProfiler);
comp->Value(DDraw);
comp->Value(GameControl);
comp->Value(Network);
comp->Value(Network2HTTPClient);
comp->Value(Network2UPnP);
comp->Value(Playback);
}
#endif

C4Config::C4Config()
Expand Down Expand Up @@ -849,6 +868,7 @@ void C4Config::CompileFunc(StdCompiler *pComp)
pComp->Value(mkNamingAdapt(Startup, "Startup"));
pComp->Value(mkNamingAdapt(Cooldowns, "Cooldowns"));
pComp->Value(mkNamingAdapt(Toasts, "Toasts"));
pComp->Value(mkNamingAdapt(Logging, "Logging"));
#endif
}

Expand Down
20 changes: 20 additions & 0 deletions src/C4Config.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

#ifdef C4ENGINE
#include "C4AulScriptStrict.h"
#include "C4Log.h"
#include "StdWindow.h"
#endif

Expand Down Expand Up @@ -299,6 +300,24 @@ class C4ConfigToasts
void CompileFunc(StdCompiler *comp);
};

class C4ConfigLogging
{
public:
spdlog::level::level_enum LogLevelStdout;

C4LoggerConfig::Config<class C4AudioSystem> AudioSystem;
C4LoggerConfig::Config<class C4AulExec> AulExec;
C4LoggerConfig::Config<class C4AulProfiler> AulProfiler;
C4LoggerConfig::Config<class CStdDDraw> DDraw;
C4LoggerConfig::Config<class C4GameControl> GameControl;
C4LoggerConfig::Config<class C4Network2> Network;
C4LoggerConfig::Config<class C4Network2HTTPClient> Network2HTTPClient;
C4LoggerConfig::Config<class C4Network2UPnP> Network2UPnP;
C4LoggerConfig::Config<class C4Playback> Playback;

void CompileFunc(StdCompiler *comp);
};

#endif

class C4Config
Expand All @@ -321,6 +340,7 @@ class C4Config
C4ConfigStartup Startup;
C4ConfigCooldowns Cooldowns;
C4ConfigToasts Toasts;
C4ConfigLogging Logging;
#endif
bool fConfigLoaded; // true if config has been successfully loaded
StdStrBuf ConfigFilename; // set for configs loaded from a nondefault config file
Expand Down
1 change: 1 addition & 0 deletions src/C4ForwardDeclarations.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#pragma once

class C4AudioSystem;
class C4AulFunc;
class C4Def;
class C4DefList;
Expand Down
4 changes: 0 additions & 4 deletions src/C4Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3270,10 +3270,6 @@ void C4Game::ParseCommandLine(const char *szCmdLine)
if (SSearchNoCase(szCmdLine, "/console"))
Application.isFullScreen = false;

// verbose
if (SSearchNoCase(szCmdLine, "/verbose"))
Application.LogSystem.SetVerbose(true);

// startup dialog required?
Application.UseStartupDialog = Application.isFullScreen && !*DirectJoinAddress && !*ScenarioFilename && !RecordStream.getSize();
}
Expand Down
4 changes: 2 additions & 2 deletions src/C4GameControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ C4GameControl::~C4GameControl()

void C4GameControl::InitLogger()
{
logger = CreateLogger("C4GameControl");
logger = Application.LogSystem.CreateLogger(Config.Logging.GameControl);
}

bool C4GameControl::InitLocal(C4Client *pLocal)
Expand Down Expand Up @@ -72,7 +72,7 @@ bool C4GameControl::InitNetwork(C4Client *pLocal)
bool C4GameControl::InitReplay(C4Group &rGroup)
{
// open replay
pPlayback = new C4Playback(logger->clone("C4Playback"));
pPlayback = new C4Playback(Application.LogSystem.CreateLogger(Config.Logging.Playback));
if (!pPlayback->Open(rGroup))
{
LogFatal(C4ResStrTableKey::IDS_ERR_REPLAYREAD);
Expand Down
3 changes: 3 additions & 0 deletions src/C4GameControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

#include "C4Control.h"
#include "C4GameControlNetwork.h"
#include "C4Log.h"
#include "C4Network2Client.h"
#include "C4Record.h"

Expand Down Expand Up @@ -148,3 +149,5 @@ class C4GameControl
C4ControlSyncCheck *GetSyncCheck(int32_t iTick);
void RemoveOldSyncChecks();
};

C4LOGGERCONFIG_NAME_TYPE(C4GameControl);
Loading

0 comments on commit 207528e

Please sign in to comment.