Skip to content

Commit

Permalink
feat(log): implement log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Fleeym committed Jan 6, 2025
1 parent 17107a2 commit 143ef8d
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 5 deletions.
15 changes: 15 additions & 0 deletions loader/resources/mod.json.in
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,21 @@
"platforms": ["win", "mac"],
"requires-restart": true
},
"console-log-level": {
"type": "string",
"default": "info",
"name": "Console Log Level",
"description": "Sets the log level for the <cb>platform console</c>.",
"platforms": ["win", "mac"],
"one-of": ["debug", "info", "warn", "error"]
},
"file-log-level": {
"type": "string",
"default": "warn",
"name": "File Log Level",
"description": "Sets the log level for the <cb>log files</c>.",
"one-of": ["debug", "info", "warn", "error"]
},
"server-cache-size-limit": {
"type": "int",
"default": 20,
Expand Down
5 changes: 4 additions & 1 deletion loader/src/load.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ bool safeModeCheck() {
int geodeEntry(void* platformData) {
thread::setName("Main");

log::Logger::get()->setup();
console::setup();
if (LoaderImpl::get()->isForwardCompatMode()) {
console::openIfClosed();
Expand Down Expand Up @@ -152,6 +151,10 @@ int geodeEntry(void* platformData) {
}
}

// Setup logger here so that internal mod is setup and we can read log level
// Logging before this point does store the log, and everything gets logged in this setup call
log::Logger::get()->setup();

tryShowForwardCompat();

// open console
Expand Down
64 changes: 62 additions & 2 deletions loader/src/loader/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
#include "LogImpl.hpp"

#include <Geode/loader/Dirs.hpp>
#include <Geode/loader/Loader.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/loader/Mod.hpp>
#include <Geode/loader/Types.hpp>
#include <Geode/utils/casts.hpp>
#include <Geode/utils/general.hpp>
#include <fmt/chrono.h>
Expand Down Expand Up @@ -212,24 +214,82 @@ Logger* Logger::get() {
}

void Logger::setup() {
if (m_initialized) {
return;
}

m_logStream = std::ofstream(dirs::getGeodeLogDir() / log::generateLogName());

// Logs can and will probably be added before setup() is called, so we'll write them now
for (Log const& log : m_logs) {
const std::string logStr = log.toString();
if (log.getSeverity() >= this->getConsoleLogLevel()) {
console::log(logStr, log.getSeverity());
}
if (log.getSeverity() >= this->getFileLogLevel()) {
m_logStream << logStr << std::endl;
}
}

m_initialized = true;
}

std::mutex& getLogMutex() {
static std::mutex mutex;
return mutex;
}

Severity Logger::getConsoleLogLevel() {
const std::string level = Mod::get()->getSettingValue<std::string>("console-log-level");
if (level == "debug") {
return Severity::Debug;
} else if (level == "info") {
return Severity::Info;
} else if (level == "warn") {
return Severity::Warning;
} else if (level == "error") {
return Severity::Error;
} else {
return Severity::Info;
}
}

Severity Logger::getFileLogLevel() {
const std::string level = Mod::get()->getSettingValue<std::string>("file-log-level");
if (level == "debug") {
return Severity::Debug;
} else if (level == "info") {
return Severity::Info;
} else if (level == "warn") {
return Severity::Warning;
} else if (level == "error") {
return Severity::Error;
} else {
return Severity::Warning;
}
}


void Logger::push(Severity sev, std::string&& thread, std::string&& source, int32_t nestCount,
std::string&& content) {
std::lock_guard g(getLogMutex());

Log& log = m_logs.emplace_back(sev, std::move(thread), std::move(source), nestCount,
std::move(content));

// If logger is not initialized, store the log anyway. When the logger is initialized the pending logs will be logged.
if (!m_initialized) {
return;
}

auto const logStr = log.toString();
console::log(logStr, log.getSeverity());
m_logStream << logStr << std::endl;

if (sev >= this->getConsoleLogLevel()) {
console::log(logStr, log.getSeverity());
}
if (sev >= this->getFileLogLevel()) {
m_logStream << logStr << std::endl;
}
}

Nest::Nest(std::shared_ptr<Nest::Impl> impl) : m_impl(std::move(impl)) { }
Expand Down
4 changes: 4 additions & 0 deletions loader/src/loader/LogImpl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <Geode/DefaultInclude.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/loader/Mod.hpp>
#include <Geode/loader/Types.hpp>
#include <vector>
#include <fstream>
#include <string>
Expand All @@ -28,6 +29,7 @@ namespace geode::log {

class Logger {
private:
bool m_initialized = false;
std::vector<Log> m_logs;
std::ofstream m_logStream;

Expand All @@ -41,6 +43,8 @@ namespace geode::log {
std::string&& content);

std::vector<Log> const& list();
Severity getConsoleLogLevel();
Severity getFileLogLevel();
void clear();
};

Expand Down
6 changes: 5 additions & 1 deletion loader/src/platform/mac/LoaderImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,12 @@

s_isOpen = true;

Severity consoleLevel = log::Logger::get()->getConsoleLogLevel();

for (auto const& log : log::Logger::get()->list()) {
console::log(log.toString(), log.getSeverity());
if (log.getSeverity() >= consoleLevel) {
console::log(log.toString(), log.getSeverity());
}
}
}

Expand Down
6 changes: 5 additions & 1 deletion loader/src/platform/windows/console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ void setupConsole(bool forceUseEscapeCodes = false) {
}
}

Severity consoleLevel = log::Logger::get()->getConsoleLogLevel();

for (auto const& log : log::Logger::get()->list()) {
console::log(log.toString(), log.getSeverity());
if (log.getSeverity() >= consoleLevel) {
console::log(log.toString(), log.getSeverity());
}
}
}

Expand Down

0 comments on commit 143ef8d

Please sign in to comment.