Skip to content

Commit

Permalink
make Logger and Log loader only, refactor Logger too
Browse files Browse the repository at this point in the history
  • Loading branch information
matcool committed Dec 28, 2023
1 parent 237109c commit 1df146a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 98 deletions.
64 changes: 6 additions & 58 deletions loader/include/Geode/loader/Log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,16 @@
#include <ccTypes.h>
#include <chrono>
#include <ghc/fs_fwd.hpp>
#include <sstream>
#include <vector>
#include <span>

#include <fmt/core.h>

namespace cocos2d {
GEODE_DLL std::string format_as(cocos2d::CCArray*);
// ive had enough, fmtlib wont let me do what i want
// GEODE_DLL std::string format_as(cocos2d::CCObject*);
// GEODE_DLL std::string format_as(cocos2d::CCArray*);
// GEODE_DLL std::string format_as(cocos2d::CCNode*);
GEODE_DLL std::string format_as(cocos2d::ccColor3B const&);
GEODE_DLL std::string format_as(cocos2d::ccColor4B const&);
GEODE_DLL std::string format_as(cocos2d::ccColor4F const&);
GEODE_DLL std::string format_as(cocos2d::CCNode*);
GEODE_DLL std::string format_as(cocos2d::CCObject*);
GEODE_DLL std::string format_as(cocos2d::CCPoint const&);
GEODE_DLL std::string format_as(cocos2d::CCRect const&);
GEODE_DLL std::string format_as(cocos2d::CCSize const&);
Expand Down Expand Up @@ -50,51 +47,6 @@ namespace geode {
using log_clock = std::chrono::system_clock;
GEODE_DLL std::string generateLogName();

// Log

class GEODE_DLL Log final {
Mod* m_sender;
log_clock::time_point m_time;
Severity m_severity;
std::string m_content;

friend class Logger;
public:
~Log();
Log(Severity sev, Mod* mod, std::string content);

std::string toString(bool logTime = true) const;
std::string toString(bool logTime, uint32_t nestLevel) const;

std::string const& getContent() const;
log_clock::time_point getTime() const;
Mod* getSender() const;
Severity getSeverity() const;
};

class GEODE_DLL Logger {
private:
static std::vector<Log>& logs();
static std::ofstream& logStream();
static uint32_t& nestLevel();

Logger() = delete;
~Logger() = delete;

// logs
public:
static void setup();

static void push(Log&& log);
static void pop(Log* log);

static void pushNest();
static void popNest();

static std::vector<Log> const& list();
static void clear();
};

GEODE_DLL void vlogImpl(Severity, Mod*, fmt::string_view format, fmt::format_args args);

template <typename... Args>
Expand Down Expand Up @@ -122,11 +74,7 @@ namespace geode {
logImpl(Severity::Error, getMod(), str, std::forward<Args>(args)...);
}

static void pushNest() {
Logger::pushNest();
}
static void popNest() {
Logger::popNest();
}
GEODE_DLL void pushNest();
GEODE_DLL void popNest();
}
}
5 changes: 3 additions & 2 deletions loader/src/load.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "loader/LoaderImpl.hpp"
#include <loader/LoaderImpl.hpp>

#include <Geode/loader/IPC.hpp>
#include <Geode/loader/Loader.hpp>
Expand All @@ -7,6 +7,7 @@
#include <Geode/loader/SettingEvent.hpp>
#include <Geode/loader/ModJsonTest.hpp>
#include <Geode/utils/JsonValidation.hpp>
#include <loader/LogImpl.hpp>

#include <array>

Expand Down Expand Up @@ -58,7 +59,7 @@ using namespace geode::prelude;
}

int geodeEntry(void* platformData) {
log::Logger::setup();
log::Logger::get()->setup();

log::info("Running {} {}", Mod::get()->getName(), Mod::get()->getVersion());

Expand Down
3 changes: 2 additions & 1 deletion loader/src/loader/LoaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "ModImpl.hpp"
#include "ModMetadataImpl.hpp"
#include "LogImpl.hpp"

#include <Geode/loader/Dirs.hpp>
#include <Geode/loader/IPC.hpp>
Expand Down Expand Up @@ -669,7 +670,7 @@ void Loader::Impl::forceReset() {
delete mod;
}
m_mods.clear();
log::Logger::clear();
log::Logger::get()->clear();
ghc::filesystem::remove_all(dirs::getModRuntimeDir());
ghc::filesystem::remove_all(dirs::getTempDir());
}
Expand Down
69 changes: 34 additions & 35 deletions loader/src/loader/Log.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "LoaderImpl.hpp"
#include "LogImpl.hpp"

#include <Geode/loader/Dirs.hpp>
#include <Geode/loader/Log.hpp>
Expand All @@ -24,6 +25,7 @@ std::string geode::format_as(Mod* mod) {
}
}

#if 0
std::string cocos2d::format_as(CCObject* obj) {
if (obj) {
// TODO: try catch incase typeid fails
Expand Down Expand Up @@ -65,6 +67,7 @@ std::string cocos2d::format_as(CCArray* arr) {

return out + "]";
}
#endif

std::string cocos2d::format_as(CCPoint const& pt) {
return fmt::format("{}, {}", pt.x, pt.y);
Expand All @@ -89,19 +92,19 @@ std::string cocos2d::format_as(cocos2d::ccColor4B const& col) {
// Log

void log::vlogImpl(Severity sev, Mod* mod, fmt::string_view format, fmt::format_args args) {
Logger::push(Log(
Logger::get()->push(
sev,
mod,
fmt::vformat(format, args)
));
);
}


Log::Log(Severity sev, Mod* mod, std::string content) :
Log::Log(Severity sev, Mod* mod, std::string&& content) :
m_sender(mod),
m_time(log_clock::now()),
m_severity(sev),
m_content(std::move(content)) {}
m_content(content) {}

Log::~Log() {}

Expand Down Expand Up @@ -169,63 +172,59 @@ Severity Log::getSeverity() const {
return m_severity;
}

std::string_view Log::getContent() const {
return m_content;
}

// Logger

std::vector<Log>& Logger::logs() {
static std::vector<Log> logs;
return logs;
}
std::ofstream& Logger::logStream() {
static std::ofstream logStream;
return logStream;
}
uint32_t& Logger::nestLevel() {
static std::uint32_t nestLevel = 0;
return nestLevel;
Logger* Logger::get() {
static Logger inst;
return &inst;
}

void Logger::setup() {
logStream() = std::ofstream(dirs::getGeodeLogDir() / log::generateLogName());
m_logStream = std::ofstream(dirs::getGeodeLogDir() / log::generateLogName());
}

void Logger::push(Log&& log) {
if (log.getSender()->isLoggingEnabled()) {
std::string logStr = log.toString(true, nestLevel());
void Logger::push(Severity sev, Mod* mod, std::string&& content) {
if (mod->isLoggingEnabled()) {
auto& log = m_logs.emplace_back(sev, mod, std::move(content));
auto const logStr = log.toString(true, m_nestLevel);

LoaderImpl::get()->logConsoleMessageWithSeverity(logStr, log.getSeverity());
logStream() << logStr << std::endl;

logs().emplace_back(std::forward<Log>(log));
m_logStream << logStr << std::endl;
}
}

void Logger::pop(Log* log) {
geode::utils::ranges::remove(Logger::logs(), [&](auto& elem) {
return &elem == log;
});
}

void Logger::pushNest() {
if (nestLevel() == std::numeric_limits<uint32_t>::max())
return;
nestLevel()++;
m_nestLevel++;
}

void Logger::popNest() {
if (nestLevel() == 0)
if (m_nestLevel == 0)
return;
nestLevel()--;
m_nestLevel--;
}

std::vector<Log> const& Logger::list() {
return logs();
return m_logs;
}

void Logger::clear() {
logs().clear();
m_logs.clear();
}

// Misc

std::string geode::log::generateLogName() {
return fmt::format("Geode {:%d %b %H.%M.%S}.log", log_clock::now());
}

void log::pushNest() {
Logger::get()->pushNest();
}

void log::popNest() {
Logger::get()->popNest();
}
52 changes: 52 additions & 0 deletions loader/src/loader/LogImpl.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <Geode/DefaultInclude.hpp>
#include <Geode/loader/Log.hpp>
#include <Geode/loader/Mod.hpp>
#include <vector>
#include <fstream>
#include <string>

namespace geode::log {
class GEODE_DLL Log final {
Mod* m_sender;
log_clock::time_point m_time;
Severity m_severity;
std::string m_content;

public:
~Log();
Log(Severity sev, Mod* mod, std::string&& content);

std::string toString(bool logTime = true) const;
std::string toString(bool logTime, uint32_t nestLevel) const;

std::string_view getContent() const;
log_clock::time_point getTime() const;
Mod* getSender() const;
Severity getSeverity() const;
};

class Logger {
private:
std::vector<Log> m_logs;
std::ofstream m_logStream;
int m_nestLevel;

Logger() {}
public:
static Logger* get();

void setup();

void push(Severity sev, Mod* mod, std::string&& content);
// why would you need this lol
// void pop(Log* log);

void pushNest();
void popNest();

std::vector<Log> const& list();
void clear();
};
}
3 changes: 2 additions & 1 deletion loader/src/platform/mac/LoaderImpl.mm
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <loader/ModImpl.hpp>
#import <Foundation/Foundation.h>
#include <sys/stat.h>
#include <loader/LogImpl.hpp>

using namespace geode::prelude;

Expand Down Expand Up @@ -79,7 +80,7 @@

m_platformConsoleOpen = true;

for (auto const& log : log::Logger::list()) {
for (auto const& log : log::Logger::get()->list()) {
this->logConsoleMessageWithSeverity(log.toString(true), log.getSeverity());
}
}
Expand Down
3 changes: 2 additions & 1 deletion loader/src/platform/windows/LoaderImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <iostream>
#include <loader/LoaderImpl.hpp>
#include <Geode/utils/string.hpp>
#include <loader/LogImpl.hpp>

using namespace geode::prelude;

Expand Down Expand Up @@ -74,7 +75,7 @@ void Loader::Impl::openPlatformConsole() {

m_platformConsoleOpen = true;

for (auto const& log : log::Logger::list()) {
for (auto const& log : log::Logger::get()->list()) {
this->logConsoleMessageWithSeverity(log.toString(true), log.getSeverity());
}
}
Expand Down

0 comments on commit 1df146a

Please sign in to comment.