Skip to content

Commit

Permalink
logging refactor (#3)
Browse files Browse the repository at this point in the history
* Add basic logging functions

* Move to new logging functions

* Fix compilation

* Gracefully terminate when our directory isnt writable

* Add `-wconsole` argument

* Use new functions in more places

* Add helper funcs

* Thanks spoon

* Print to game console

* Stop using NS::log and spdlog:: in squirrel

* add log context string to each print

* Print to win terminal

* Don't automatically add eol

* Properly shutdown spdlog

* Fixup some messages

* Formatting

* Use spaces in CMakeLists.txt

* Formatting

* Use new funcs fol plugin log

* Always allocate console when we'rea  dedi

* Update dedilogtoclient

* Remove old logging classes

* Fix PLUGIN_LOG

* Color logging

* Don't log searchpath

* Fix masterserver error log

* Properly break in switch in spew func

* Remove comment

* Log to disk

* Format

* Terminate on fatal error call

* Improve doc comments in `dbg.cpp`

* Use displayName for plugin logging

* Improve more doc comments

* add `-nologfiles` argument

* Rename `g_bSpdLog_UseAnsiColor` to `g_bConsole_UseAnsiColor`

* Add plugin color

* Adjust colors

* Add missing newlines

* Fix specifier

* Separate SQ_GetLogContext into native and script versions

* Replace spdlog::info i missed

* Format

* Only close console once gamewindow has been created

* Improve log categories

* Fix compile error context

* Fix missed format in `scriptdatatables.cpp`

* Rename `pszAnsiString` to `svAnsiString`

* Add lock guard

* Don't try to show messagebox when we're a dedicated server on error

* Rename `fLogger` to `pLogger`

* Use new funcs for system info

* Rename `StartupLog` to `Sys_PrintOSVer`

* Initilaze `g_svLogDirectory` directly in `InitilazeNorthstar`

* Cleanup `InitilaseNorthstar`

* Add back ntdll check
  • Loading branch information
F1F7Y authored and ASpoonPlaysGames committed Dec 18, 2023
1 parent 407bcc7 commit 76b5049
Show file tree
Hide file tree
Showing 61 changed files with 1,320 additions and 891 deletions.
2 changes: 2 additions & 0 deletions NorthstarDLL/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ add_library(NorthstarDLL SHARED
"engine/runframe.cpp"
"logging/crashhandler.cpp"
"logging/crashhandler.h"
"logging/dbg.cpp"
"logging/dbg.h"
"logging/logging.cpp"
"logging/logging.h"
"logging/loghooks.cpp"
Expand Down
107 changes: 66 additions & 41 deletions NorthstarDLL/client/audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CustomAudioManager g_CustomAudioManager;

EventOverrideData::EventOverrideData()
{
spdlog::warn("Initialised struct EventOverrideData without any data!");
Warning(eLog::AUDIO, "Initialised struct EventOverrideData without any data!\n");
LoadedSuccessfully = false;
}

Expand All @@ -36,7 +36,7 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
{
if (data.length() <= 0)
{
spdlog::error("Failed reading audio override file {}: file is empty", path.string());
Error(eLog::AUDIO, NO_ERROR, "Failed reading audio override file %s: file is empty\n", path.string().c_str());
return;
}

Expand All @@ -45,10 +45,12 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa

if (!fs::exists(samplesFolder))
{
spdlog::error(
"Failed reading audio override file {}: samples folder doesn't exist; should be named the same as the definition file without "
"JSON extension.",
path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: samples folder doesn't exist; should be named the same as the definition file without "
"JSON extension.\n",
path.string().c_str());
return;
}

Expand All @@ -58,9 +60,11 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
// fail if parse error
if (dataJson.HasParseError())
{
spdlog::error(
"Failed reading audio override file {}: encountered parse error \"{}\" at offset {}",
path.string(),
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: encountered parse error \"%s\" at offset %i\n",
path.string().c_str(),
GetParseError_En(dataJson.GetParseError()),
dataJson.GetErrorOffset());
return;
Expand All @@ -69,14 +73,18 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
// fail if it's not a json obj (could be an array, string, etc)
if (!dataJson.IsObject())
{
spdlog::error("Failed reading audio override file {}: file is not a JSON object", path.string());
Error(eLog::AUDIO, NO_ERROR, "Failed reading audio override file %s: file is not a JSON object\n", path.string().c_str());
return;
}

// fail if no event ids given
if (!dataJson.HasMember("EventId"))
{
spdlog::error("Failed reading audio override file {}: JSON object does not have the EventId property", path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: JSON object does not have the EventId property\n",
path.string().c_str());
return;
}

Expand All @@ -87,8 +95,11 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
{
if (!eventId.IsString())
{
spdlog::error(
"Failed reading audio override file {}: EventId array has a value of invalid type, all must be strings", path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: EventId array has a value of invalid type, all must be strings\n",
path.string().c_str());
return;
}

Expand All @@ -103,9 +114,11 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
// incorrect type
else
{
spdlog::error(
"Failed reading audio override file {}: EventId property is of invalid type (must be a string or an array of strings)",
path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: EventId property is of invalid type (must be a string or an array of strings)\n",
path.string().c_str());
return;
}

Expand All @@ -118,9 +131,11 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
{
if (!eventId.IsString())
{
spdlog::error(
"Failed reading audio override file {}: EventIdRegex array has a value of invalid type, all must be strings",
path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: EventIdRegex array has a value of invalid type, all must be strings\n",
path.string().c_str());
return;
}

Expand All @@ -132,7 +147,8 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
}
catch (...)
{
spdlog::error("Malformed regex \"{}\" in audio override file {}", regex, path.string());
Error(
eLog::AUDIO, NO_ERROR, "Malformed regex \"%s\" in audio override file %s\n", regex.c_str(), path.string().c_str());
return;
}
}
Expand All @@ -147,16 +163,19 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
}
catch (...)
{
spdlog::error("Malformed regex \"{}\" in audio override file {}", regex, path.string());
Error(eLog::AUDIO, NO_ERROR, "Malformed regex \"%s\" in audio override file %s\n", regex.c_str(), path.string().c_str());
return;
}
}
// incorrect type
else
{
spdlog::error(
"Failed reading audio override file {}: EventIdRegex property is of invalid type (must be a string or an array of strings)",
path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: EventIdRegex property is of invalid type (must be a string or an array of "
"strings)\n",
path.string().c_str());
return;
}
}
Expand All @@ -165,7 +184,11 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
{
if (!dataJson["AudioSelectionStrategy"].IsString())
{
spdlog::error("Failed reading audio override file {}: AudioSelectionStrategy property must be a string", path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: AudioSelectionStrategy property must be a string\n",
path.string().c_str());
return;
}

Expand All @@ -181,9 +204,11 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
}
else
{
spdlog::error(
"Failed reading audio override file {}: AudioSelectionStrategy string must be either \"sequential\" or \"random\"",
path.string());
Error(
eLog::AUDIO,
NO_ERROR,
"Failed reading audio override file %s: AudioSelectionStrategy string must be either \"sequential\" or \"random\"\n",
path.string().c_str());
return;
}
}
Expand All @@ -200,7 +225,7 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa

if (wavStream.fail())
{
spdlog::error("Failed reading audio sample {}", file.path().string());
Error(eLog::AUDIO, NO_ERROR, "Failed reading audio sample %s\n", file.path().string().c_str());
continue;
}

Expand All @@ -226,7 +251,7 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
// would be weird if this got hit, since it would've worked previously
if (wavStream.fail())
{
spdlog::error("Failed async read of audio sample {}", pathString);
Error(eLog::AUDIO, NO_ERROR, "Failed async read of audio sample %s\n", pathString.c_str());
return;
}

Expand All @@ -235,7 +260,7 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
wavStream.read(reinterpret_cast<char*>(data), fileSize);
wavStream.close();

spdlog::info("Finished async read of audio sample {}", pathString);
DevMsg(eLog::AUDIO, "Finished async read of audio sample %s\n", pathString.c_str());
});

readThread.detach();
Expand All @@ -256,9 +281,9 @@ EventOverrideData::EventOverrideData(const std::string& data, const fs::path& pa
*/

if (Samples.size() == 0)
spdlog::warn("Audio override {} has no valid samples! Sounds will not play for this event.", path.string());
Warning(eLog::AUDIO, "Audio override %s has no valid samples! Sounds will not play for this event.\n", path.string().c_str());

spdlog::info("Loaded audio override file {}", path.string());
DevMsg(eLog::AUDIO, "Loaded audio override file %s\n", path.string().c_str());

LoadedSuccessfully = true;
}
Expand All @@ -274,7 +299,7 @@ bool CustomAudioManager::TryLoadAudioOverride(const fs::path& defPath)
// fail if no audio json
if (jsonStream.fail())
{
spdlog::warn("Unable to read audio override from file {}", defPath.string());
Warning(eLog::AUDIO, "Unable to read audio override from file %s\n", defPath.string().c_str());
return false;
}

Expand All @@ -290,13 +315,13 @@ bool CustomAudioManager::TryLoadAudioOverride(const fs::path& defPath)

for (const std::string& eventId : data->EventIds)
{
spdlog::info("Registering sound event {}", eventId);
DevMsg(eLog::AUDIO, "Registering sound event %s\n", eventId.c_str());
m_loadedAudioOverrides.insert({eventId, data});
}

for (const auto& eventIdRegexData : data->EventIdsRegex)
{
spdlog::info("Registering sound event regex {}", eventIdRegexData.first);
DevMsg(eLog::AUDIO, "Registering sound event regex %s\n", eventIdRegexData.first.c_str());
m_loadedAudioOverridesRegex.insert({eventIdRegexData.first, data});
}

Expand Down Expand Up @@ -394,7 +419,7 @@ bool __declspec(noinline) __fastcall LoadSampleMetadata_Internal(
char* eventName = (char*)parentEvent + 0x110;

if (Cvar_ns_print_played_sounds->GetInt() > 0)
spdlog::info("[AUDIO] Playing event {}", eventName);
DevMsg(eLog::AUDIO, "Playing event %s\n", eventName);

auto iter = g_CustomAudioManager.m_loadedAudioOverrides.find(eventName);
std::shared_ptr<EventOverrideData> overrideData;
Expand Down Expand Up @@ -461,7 +486,7 @@ bool __declspec(noinline) __fastcall LoadSampleMetadata_Internal(
}

if (!dat)
spdlog::warn("Could not get sample data from override struct for event {}! Shouldn't happen", eventName);
Warning(eLog::AUDIO, "Could not get sample data from override struct for event %s! Shouldn't happen\n", eventName);
else
{
data = dat->second.get();
Expand All @@ -471,7 +496,7 @@ bool __declspec(noinline) __fastcall LoadSampleMetadata_Internal(

if (!data)
{
spdlog::warn("Could not fetch override sample data for event {}! Using original data instead.", eventName);
Warning(eLog::AUDIO, "Could not fetch override sample data for event {}! Using original data instead.\n", eventName);
return LoadSampleMetadata(sample, audioBuffer, audioBufferLength, audioType);
}

Expand All @@ -485,7 +510,7 @@ bool __declspec(noinline) __fastcall LoadSampleMetadata_Internal(
// 64 - Auto-detect sample type
bool res = LoadSampleMetadata(sample, audioBuffer, audioBufferLength, 64);
if (!res)
spdlog::error("LoadSampleMetadata failed! The game will crash :(");
Error(eLog::AUDIO, NO_ERROR, "LoadSampleMetadata failed! The game will crash :(\n");

return res;
}
Expand All @@ -498,7 +523,7 @@ void, __fastcall, (int level, const char* string))
if (!Cvar_mileslog_enable->GetBool())
return;

spdlog::info("[MSS] {} - {}", level, string);
DevMsg(eLog::AUDIO, "%i - %s\n", level, string);
}

ON_DLL_LOAD_RELIESON("engine.dll", MilesLogFuncHooks, ConVar, (CModule module))
Expand Down
2 changes: 1 addition & 1 deletion NorthstarDLL/client/clientvideooverrides.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ void*, __fastcall, (const char* path, uint32_t flags))
// clang-format on
{
std::string filename(fs::path(path).filename().string());
spdlog::info("BinkOpen {}", filename);
DevMsg(eLog::VIDEO, "BinkOpen %s\n", filename);

// figure out which mod is handling the bink
Mod* fileOwner = nullptr;
Expand Down
27 changes: 16 additions & 11 deletions NorthstarDLL/client/languagehooks.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,15 @@ char*, __fastcall, ())
{
if (!CheckLangAudioExists((char*)forcedLanguage))
{
spdlog::info(
"User tried to force the language (-language) to \"{}\", but audio for this language doesn't exist and the game is bound "
"to error, falling back to next option...",
Warning(
eLog::AUDIO,
"User tried to force the language (-language) to \"%s\", but audio for this language doesn't exist and the game is bound "
"to error, falling back to next option...\n",
forcedLanguage);
}
else
{
spdlog::info("User forcing the language (-language) to: {}", forcedLanguage);
DevMsg(eLog::AUDIO, "User forcing the language (-language) to: %s\n", forcedLanguage);
strncpy(ingameLang1, forcedLanguage, 256);
return ingameLang1;
}
Expand All @@ -81,12 +82,14 @@ char*, __fastcall, ())
{
if (strcmp(lang, "russian") !=
0) // don't log for "russian" since it's the default and that means Origin detection just didn't change it most likely
spdlog::info(
"Origin detected language \"{}\", but we do not have audio for it installed, falling back to the next option", lang);
DevMsg(
eLog::AUDIO,
"Origin detected language \"%s\", but we do not have audio for it installed, falling back to the next option\n",
lang);
}
else
{
spdlog::info("Origin detected language: {}", lang);
DevMsg(eLog::AUDIO, "Origin detected language: %s\n", lang);
return lang;
}
}
Expand All @@ -95,13 +98,15 @@ char*, __fastcall, ())
// defaulting to Russian
canOriginDictateLang = false; // Origin has no say anymore, we will fallback to user's system setup language
auto lang = GetGameLanguage();
spdlog::info("Detected system language: {}", lang);
DevMsg(eLog::AUDIO, "Detected system language: %s\n", lang);
if (!CheckLangAudioExists(lang))
{
spdlog::warn("Caution, audio for this language does NOT exist. You might want to override your game language with -language "
"command line option.");
Warning(
eLog::AUDIO,
"Caution, audio for this language does NOT exist. You might want to override your game language with -language command line "
"option.\n");
auto lang = GetAnyInstalledAudioLanguage();
spdlog::warn("Falling back to the first installed audio language: {}", lang.c_str());
Warning(eLog::AUDIO, "Falling back to the first installed audio language: %s\n", lang.c_str());
strncpy(ingameLang1, lang.c_str(), 256);
return ingameLang1;
}
Expand Down
4 changes: 2 additions & 2 deletions NorthstarDLL/client/latencyflex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ ON_DLL_LOAD_CLIENT_RELIESON("client.dll", LatencyFlex, ConVar, (CModule module))
reinterpret_cast<void (*)()>(reinterpret_cast<void*>(GetProcAddress(pLfxModule, "winelfx_WaitAndBeginFrame")));
else
{
spdlog::info("Unable to load LatencyFleX library, LatencyFleX disabled.");
Warning(eLog::NS, "Unable to load LatencyFleX library, LatencyFleX disabled.\n");
return;
}

AUTOHOOK_DISPATCH()

spdlog::info("LatencyFleX initialized.");
DevMsg(eLog::NS, "LatencyFleX initialized.\n");
Cvar_r_latencyflex = new ConVar("r_latencyflex", "1", FCVAR_ARCHIVE, "Whether or not to use LatencyFleX input latency reduction.");
}
2 changes: 1 addition & 1 deletion NorthstarDLL/client/localchatwriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ void LocalChatWriter::InsertChar(wchar_t ch)

void LocalChatWriter::InsertText(const char* str)
{
spdlog::info(str);
DevMsg(eLog::CHAT, "%s\n", str);

WCHAR messageUnicode[288];
ConvertANSIToUnicode(str, -1, messageUnicode, 274);
Expand Down
Loading

0 comments on commit 76b5049

Please sign in to comment.