Skip to content

Commit 03e3e02

Browse files
committed
feat(core): Message Logger
1 parent 5e7910a commit 03e3e02

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

plugin_files/configs/core.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
22
"logging": {
33
"enabled": true,
4-
"mode": "daily"
4+
"mode": "daily",
5+
"save_core_messages": false
56
},
67
"commandPrefixes": [
78
"!"

src/configuration/Configuration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ bool Configuration::LoadConfiguration()
436436

437437
RegisterConfiguration(wasEdited, coreConfigFile, "core", "core", "logging.enabled", true);
438438
RegisterConfiguration(wasEdited, coreConfigFile, "core", "core", "logging.mode", std::string("daily"));
439+
RegisterConfiguration(wasEdited, coreConfigFile, "core", "core", "logging.save_core_messages", false);
439440

440441
std::string loggingMode = std::string(coreConfigFile["logging"]["mode"].GetString());
441442
if (loggingMode != "daily" && loggingMode != "map" && loggingMode != "permanent")

src/entrypoint.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,9 @@ bool Swiftly::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
145145
ConVar_Register(FCVAR_RELEASE | FCVAR_SERVER_CAN_EXECUTE | FCVAR_CLIENT_CAN_EXECUTE | FCVAR_GAMEDLL);
146146

147147
if (!BeginCrashListener())
148-
PRINTRET("Crash Reporter failed to initialize.\n", false)
148+
PRINTRET("Crash Reporter failed to initialize.\n", false);
149149

150-
g_pluginManager = new PluginManager();
150+
g_pluginManager = new PluginManager();
151151
g_Config = new Configuration();
152152
g_conFilter = new ConsoleFilter();
153153
g_Signatures = new Signatures();
@@ -166,9 +166,11 @@ bool Swiftly::Load(PluginId id, ISmmAPI* ismm, char* error, size_t maxlen, bool
166166
if (g_Config->LoadConfiguration())
167167
PRINT("The configurations has been succesfully loaded.\n");
168168
else
169-
PRINTRET("Failed to load configurations. The plugin will not work.\n", false)
169+
PRINTRET("Failed to load configurations. The plugin will not work.\n", false);
170170

171-
g_Config->LoadPluginConfigurations();
171+
g_Logger->AddLogger("core", false);
172+
173+
g_Config->LoadPluginConfigurations();
172174
g_Signatures->LoadSignatures();
173175
g_Offsets->LoadOffsets();
174176
g_Patches->LoadPatches();

src/utils/utils.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#include <sstream>
66

77
#include "../sdk/schema.h"
8+
#include "../configuration/Configuration.h"
9+
#include "../logs/Logger.h"
810

911
std::map<std::string, std::string> terminalColors = {
1012
{"{DEFAULT}", "\e[39m"},
@@ -93,7 +95,7 @@ std::string GetTerminalStringColor(std::string plugin_name)
9395
return terminalColors.at(terminalPrefixColors[steps]);
9496
}
9597

96-
size_t UTIL_FormatArgs(char *buffer, size_t maxlength, const char *fmt, va_list params)
98+
size_t UTIL_FormatArgs(char* buffer, size_t maxlength, const char* fmt, va_list params)
9799
{
98100
size_t len = vsnprintf(buffer, maxlength, fmt, params);
99101

@@ -165,6 +167,12 @@ bool starts_with(std::string value, std::string starting)
165167
void PLUGIN_PRINT(std::string category, std::string str)
166168
{
167169
g_SMAPI->ConPrint((PREFIX " " + GetTerminalStringColor(category) + "[" + category + "]\e[39m " + str).c_str());
170+
if (g_Config) {
171+
if (g_Config->FetchValue<bool>("core.logging.save_core_messages")) {
172+
str.pop_back();
173+
g_Logger->FetchLogger("core")->WriteLog(LogType_t::Common, "[" + category + "] " + str);
174+
}
175+
}
168176
}
169177

170178
void PLUGIN_PRINTF(std::string category, std::string str, ...)
@@ -177,6 +185,13 @@ void PLUGIN_PRINTF(std::string category, std::string str, ...)
177185
va_end(ap);
178186

179187
g_SMAPI->ConPrint((PREFIX " " + GetTerminalStringColor(category) + "[" + category + "]\e[39m " + std::string(buffer)).c_str());
188+
if (g_Config) {
189+
if (g_Config->FetchValue<bool>("core.logging.save_core_messages")) {
190+
std::string buf = buffer;
191+
buf.pop_back();
192+
g_Logger->FetchLogger("core")->WriteLog(LogType_t::Common, "[" + category + "] " + buf);
193+
}
194+
}
180195
}
181196

182197
void PrintTextTable(std::string category, TextTable table)
@@ -196,14 +211,14 @@ uint64_t GetTime()
196211
std::string str_tolower(std::string s)
197212
{
198213
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
199-
{ return std::tolower(c); });
214+
{ return std::tolower(c); });
200215
return s;
201216
}
202217

203218
std::string str_toupper(std::string s)
204219
{
205220
std::transform(s.begin(), s.end(), s.begin(), [](unsigned char c)
206-
{ return std::toupper(c); });
221+
{ return std::toupper(c); });
207222
return s;
208223
}
209224

@@ -214,8 +229,8 @@ std::string get_uuid()
214229

215230
std::uniform_int_distribution<int> dist(0, 15);
216231

217-
const char *v = "0123456789abcdef";
218-
const bool dash[] = {0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0};
232+
const char* v = "0123456789abcdef";
233+
const bool dash[] = { 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 0 };
219234

220235
std::string res;
221236
for (int i = 0; i < 16; i++)

0 commit comments

Comments
 (0)