Skip to content

Commit 8056df9

Browse files
committed
Added logging functionality
1 parent 74c4bbd commit 8056df9

16 files changed

+358
-28
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ lib
366366
bin
367367
build
368368
out
369+
temp
369370

370371
.vscode
371372
*.lock

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ endif()
150150
find_package(eventpp REQUIRED)
151151
find_package(SDL2 REQUIRED CONFIG)
152152
find_package(rmlui REQUIRED)
153+
find_package(fmt REQUIRED)
153154

154155
set(DOUBLE_PRECISION OFF)
155156
set(GENERATE_DEBUG_SYMBOLS OFF)

conanfile.txt

22 Bytes
Binary file not shown.

multisink.txt

Whitespace-only changes.

src/application/ApplicationUtils.h

+29-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,18 @@
1212
#include <memory>
1313
#include <mutex>
1414

15+
#include <Common.h>
16+
#include <modding/ScriptingAPI.h>
17+
#include <renderer/Renderer.h>
18+
#include <renderer/WindowDecorations.h>
19+
#include <conf/Config.h>
20+
21+
#include <project.h>
22+
#include <raylib.h>
23+
#include <thread>
1524

25+
#include <utils/MiscUtils.h>
26+
#include <dbg/Logging.h>
1627
namespace Techstorm::Application {
1728

1829
/* class GameThread : public Singleton<GameThread> {
@@ -138,6 +149,7 @@ namespace Techstorm::Application {
138149

139150
class FrameManager : public Singleton<FrameManager> {
140151
public:
152+
141153
float sFrameTime = 0.0f; // This is provided by the main thread. This is read only and should only be set by the main thread
142154
volatile bool sIsWaitingForOthers = false; // This is read only and should only be set by the main thread
143155
volatile bool sExit = false;
@@ -157,7 +169,9 @@ namespace Techstorm::Application {
157169
while (sIsWaitingForOthers && !sExit) {
158170
std::this_thread::sleep_for(std::chrono::milliseconds(1));
159171
}
160-
std::cout << "Update thread started\n";
172+
173+
Log("Update thread started");
174+
161175
while (!sExit) {
162176

163177
userProject->preObjectUpdate();
@@ -183,7 +197,8 @@ namespace Techstorm::Application {
183197
while (sIsWaitingForOthers && !sExit) {
184198
std::this_thread::sleep_for(std::chrono::milliseconds(1));
185199
}
186-
std::cout << "Worker thread started\n";
200+
Log("Worker thread started");
201+
187202
while (!sExit) {
188203
// This is here because it reduces CPU consumption
189204
std::this_thread::yield();
@@ -211,19 +226,26 @@ namespace Techstorm::Application {
211226
while (sIsWaitingForOthers && !sExit) {
212227

213228
if (isWorkerWaiting && isUpdateWaiting) {
214-
std::cout << "All threads are ready\n";
229+
230+
231+
Log("All threads are ready");
232+
215233
sIsWaitingForOthers = false;
216234
break;
217235
}
218236
else {
219-
std::cout << "Waiting for threads...\n";
237+
Log("Waiting for threads...", ELogLevel::TRACE);
220238
std::this_thread::sleep_for(std::chrono::milliseconds(1));
221239

222240
}
223241

224242

225243
}
226244
}
245+
246+
void killThreads() {
247+
sExit = true;
248+
}
227249
private:
228250

229251

@@ -234,6 +256,9 @@ namespace Techstorm::Application {
234256
//ThreadStatus renderThreadStatus;
235257
};
236258

259+
260+
261+
237262
using GameThreadInfo = std::unordered_map<std::string, bool>;
238263

239264
GameThreadInfo CalculateGameThreadInfo(GameSettings& gameSettings);

src/application/main.cpp

+31-10
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
#include <Common.h>
2-
#include <modding/ScriptingAPI.h>
3-
#include <renderer/Renderer.h>
4-
#include <renderer/WindowDecorations.h>
5-
#include <conf/Config.h>
6-
7-
#include <project.h>
8-
#include <raylib.h>
9-
#include <thread>
101
#include "ApplicationUtils.h"
2+
113
using namespace Techstorm;
124

135
void InitWindow(WindowDecorations& decorations) {
6+
7+
Log("Decorating window...");
148
decorations.title = LookupConfig("Project.cfg", "projectWindowTitle");
159
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_INTERLACED_HINT);
10+
11+
Log("Finalizing window initialization...");
1612
InitWindow(decorations.width, decorations.height, decorations.title);
1713

14+
Log("Loading and setting window icon...");
1815
const char* iconPath = TextFormat("%s%s", TS_ASSET_DIR.c_str(), decorations.icon);
1916

2017
Image icon = LoadImage(iconPath);
@@ -52,37 +49,61 @@ class UI : public GameObject {
5249
};*/
5350

5451
int main(int argc, char* argv[]) {
52+
Log("Pre-Initializing project...");
5553
PROJECT_TYPENAME project = PROJECT_TYPENAME();
5654
project.preInit();
55+
Log("Project pre-initialized.");
56+
57+
Log("Finishing Initialization...");
5758

5859
InitializeConfigRegistry();
5960
ConfigFileRegistry& configRegistry = GetConfigFileRegistry();
6061

6162
Renderer& renderer = project.getRenderer();
6263
WindowDecorations& decorations = project.getWindowDecorations();
6364

65+
Log("Setting up scripting API...");
6466
ScriptingAPI scriptingAPI;
6567

6668
InitScripting(scriptingAPI, project);
6769

70+
Log("Done setting up scripting API.");
71+
72+
Log("Initializing window...");
6873
InitWindow(decorations);
6974

75+
Log("Done initializing window.");
76+
77+
Log("Finishing project's initialization...");
7078
project.init(argc, argv);
7179
project.postInit();
7280

81+
Log("Done finishing project's initialization.");
82+
83+
Log("Launching threads.");
84+
7385
Techstorm::Application::FrameManager& manager = Techstorm::Application::FrameManager::Instance();
7486

7587
SetTargetFPS(decorations.targetFPS);
7688

7789
manager.launchThreads(project);
7890

91+
Log("Initialization is now finished, starting main loop.");
7992
while (!WindowShouldClose()) {
80-
//context->Update();
8193
HandleFrame(project);
8294
}
8395

96+
Log("Shutting down...");
97+
98+
Log("Cleaning up...");
8499
project.cleanup(0);
85100

101+
manager.killThreads();
102+
103+
Log("Done cleaning up.");
104+
86105
CloseWindow();
106+
107+
Log("Done shutting down. Goodbye!");
87108
return 0;
88109
}

src/engine/CMakeLists.txt

+3
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ set(TS_UTIL_SOURCES
5555
"utils/DoublyLinkedList.h"
5656
"utils/Gate.h"
5757
"utils/Globals.h"
58+
"utils/MiscUtils.h"
5859
)
5960

6061
set(TS_MATH_SOURCES
@@ -144,6 +145,8 @@ set(TS_ENGINE_DEPENDENCIES
144145
rmlui_DEPS_TARGET
145146

146147
spdlog::spdlog
148+
149+
fmt::fmt
147150
)
148151

149152
include("${CMAKE_SOURCE_DIR}/CMakeOptions.txt")

src/engine/Common.h

+18
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
const std::string TS_ASSET_DIR = std::string(TS_TOP_LAYER) + "/game/assets/";
2626
const std::string TS_DATA_DIR = std::string(TS_TOP_LAYER) + "/game/data/";
2727
const std::string TS_GAME_DIR = std::string(TS_TOP_LAYER) + "/game/";
28+
const std::string TS_TEMP_DIR = std::string(TS_TOP_LAYER) + "/temp/";
2829

2930
namespace Techstorm {
3031

@@ -494,7 +495,24 @@ namespace Techstorm {
494495
};*/
495496

496497

498+
template<typename T>
499+
class ReferenceHolder {
500+
public:
497501

502+
explicit ReferenceHolder(T& value) : mValue(value) {}
503+
504+
void set(T& value) {
505+
mValue = value;
506+
}
507+
508+
T& get() {
509+
return mValue;
510+
}
511+
512+
513+
private:
514+
T& mValue;
515+
};
498516

499517

500518
/// This is a module for haptic experiences (ie. controller rumble) and helps make more immersive experiences.

src/engine/conf/Config.cpp

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <any>
55
#include <memory>
66
#include <string>
7+
#include "../dbg/Logging.h"
78

89
std::any LoadConfigFile(std::shared_ptr<Techstorm::FileMeta> fileMeta) {
910
using namespace Techstorm;
@@ -22,7 +23,12 @@ Techstorm::ConfigFileRegistry::~ConfigFileRegistry()
2223
}
2324

2425
void Techstorm::ConfigFileRegistry::init() {
26+
Log("Initializing config registry.");
27+
28+
// only show this in the log file
29+
Log("Setting config file load functions.", ELogLevel::TRACE);
2530
AddFileRegistryLoadFunction("cfg", [](std::shared_ptr<FileMeta> fileMeta) {
31+
Log("Loading config file: " + fileMeta->path, ELogLevel::TRACE);
2632
libconfig::Config* conf = new libconfig::Config();
2733
conf->readFile(fileMeta->path);
2834
return std::make_any<libconfig::Config*>(conf);
@@ -63,6 +69,8 @@ libconfig::Setting& Techstorm::ConfigFileRegistry::lookup(const std::string& fil
6369
libconfig::Config const* conf = GetFile(fileName).get()->get<libconfig::Config*>();
6470
libconfig::Setting& setting = conf->lookup(lookupTarget);
6571

72+
Log("Looking up value in :" + fileName + " for key: " + lookupTarget + " and found value: " + setting.c_str(), ELogLevel::TRACE);
73+
6674
return setting;
6775

6876
}

src/engine/dbg/ELogLevel.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44
namespace Techstorm {
55
/// <summary>
66
/// What level of logging to use. This will affect the output color, severity, etc.
7-
/// \note This is not the same as spdlog's log level. Also, this inherits from uint8_t.
7+
/// \note This is not the same as spdlog's log level. Also, this inherits from int to make it easier to compare.
88
/// </summary>
9-
enum class ELogLevel : uint8_t {
9+
enum class ELogLevel : int {
1010
TRACE = 0,
1111
DEBUG,
1212
INFO,
1313
WARNING,
1414
ERROR,
15-
FATAL
15+
FATAL,
16+
NONE
1617
};
1718
}

src/engine/dbg/Logging.cpp

+80-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,87 @@
11
#include "Logging.h"
2-
#include <spdlog/spdlog.h>
32

4-
Techstorm::Logger::Logger()
3+
4+
5+
void Techstorm::Logger::log(const std::string& message, ELogLevel level, const std::source_location& location)
56
{
7+
std::string locFileName = location.file_name();
8+
9+
// shorten the filename to only the last part of it
10+
locFileName = locFileName.substr(locFileName.find_last_of("/\\") + 1);
11+
12+
13+
int locLine = location.line();
14+
15+
std::string finalMessage = "[" + locFileName + ":" + std::to_string(locLine) + "] " + message;
16+
17+
switch (level)
18+
{
19+
case Techstorm::ELogLevel::TRACE:
20+
mLogger->trace(finalMessage);
21+
break;
22+
case Techstorm::ELogLevel::DEBUG:
23+
mLogger->debug(finalMessage);
24+
break;
25+
case Techstorm::ELogLevel::INFO:
26+
mLogger->info(finalMessage);
27+
break;
28+
case Techstorm::ELogLevel::WARNING:
29+
mLogger->warn(finalMessage);
30+
break;
31+
case Techstorm::ELogLevel::ERROR:
32+
mLogger->error(finalMessage);
33+
break;
34+
case Techstorm::ELogLevel::FATAL:
35+
mLogger->critical(finalMessage);
36+
break;
37+
case Techstorm::ELogLevel::NONE:
38+
break;
39+
default:
40+
#ifdef RELEASE
41+
// if on release, log this as an error
42+
std::string failMsg = "Failed to log the message: " + finalMessage;
43+
failMsg << "Because the log level " << level << " is not supported!";
44+
mLogger->error(failMsg);
45+
46+
#else
47+
mLogger->trace(finalMessage);
48+
#endif
49+
break;
50+
}
651
}
752

8-
void Techstorm::Logger::Log(const std::string& message, const std::source_location& location)
53+
void Techstorm::TerminalSink::init()
954
{
55+
mColorSink = std::make_shared<spdlog::sinks::stdout_color_sink_mt>();
56+
mColorSink->set_level(spdlog::level::warn);
57+
mColorSink->set_pattern(mFormat);
58+
}
59+
60+
std::string Techstorm::TerminalSink::getFormat()
61+
{
62+
return this->mFormat;
63+
}
64+
65+
std::string Techstorm::CreateLogFileName() {
66+
// Filename should follow this format: YYYY-MM-DD_HH-MM-SS.log
67+
time_t now = time(0);
68+
struct tm tstruct;
69+
localtime_s(&tstruct, &now);
70+
char buf[80];
71+
strftime(buf, sizeof(buf), "%Y-%m-%d_%H-%M-%S", &tstruct);
72+
73+
std::string name = std::string(TS_TEMP_DIR);
74+
name += "logs/";
75+
name += std::string(buf);
76+
name += ".log";
77+
78+
return name;
79+
}
80+
81+
spdlog::level::level_enum Techstorm::GetSpdlogLevel(ELogLevel level) {
82+
return static_cast<spdlog::level::level_enum>(level);
83+
}
84+
85+
void Techstorm::Log(const std::string& message, ELogLevel level, const std::source_location& location) {
86+
Logger::Instance().log(message, level, location);
1087
}

0 commit comments

Comments
 (0)