add logger nesting

This commit is contained in:
ConfiG 2023-08-07 01:48:01 +03:00
parent 82e128bb57
commit 7d74f16c01
No known key found for this signature in database
GPG key ID: 44DA1983F524C11B
2 changed files with 35 additions and 2 deletions

View file

@ -154,6 +154,7 @@ namespace geode {
bool operator==(Log const& l);
std::string toString(bool logTime = true) const;
std::string toString(bool logTime, uint32_t nestLevel) const;
std::vector<ComponentTrait*>& getComponents();
log_clock::time_point getTime() const;
@ -170,6 +171,7 @@ namespace geode {
private:
static std::vector<Log>& logs();
static std::ofstream& logStream();
static uint32_t& nestLevel();
Logger() = delete;
~Logger() = delete;
@ -179,9 +181,11 @@ namespace geode {
static void setup();
static void push(Log&& log);
static void pop(Log* log);
static void pushNest();
static void popNest();
static std::vector<Log*> list();
static void clear();
};
@ -223,5 +227,12 @@ namespace geode {
void error(Args... args) {
internalLog(Severity::Error, getMod(), args...);
}
static void pushNest() {
Logger::pushNest();
}
static void popNest() {
Logger::popNest();
}
}
}

View file

@ -104,6 +104,9 @@ bool Log::operator==(Log const& l) {
}
std::string Log::toString(bool logTime) const {
return toString(logTime, 0);
}
std::string Log::toString(bool logTime, uint32_t nestLevel) const {
std::string res;
if (logTime) {
@ -112,6 +115,10 @@ std::string Log::toString(bool logTime) const {
res += fmt::format(" [{}]: ", m_sender ? m_sender->getName() : "Geode?");
for (uint32_t i = 0; i < nestLevel; i++) {
res += " ";
}
for (auto& i : m_components) {
res += i->_toString();
}
@ -205,13 +212,17 @@ std::ofstream& Logger::logStream() {
static std::ofstream logStream;
return logStream;
}
uint32_t& Logger::nestLevel() {
static std::uint32_t nestLevel = 0;
return nestLevel;
}
void Logger::setup() {
logStream() = std::ofstream(dirs::getGeodeLogDir() / log::generateLogName());
}
void Logger::push(Log&& log) {
std::string logStr = log.toString(true);
std::string logStr = log.toString(true, nestLevel());
LoaderImpl::get()->logConsoleMessageWithSeverity(logStr, log.getSeverity());
logStream() << logStr << std::endl;
@ -223,6 +234,17 @@ void Logger::pop(Log* log) {
geode::utils::ranges::remove(Logger::logs(), *log);
}
void Logger::pushNest() {
if (nestLevel() == std::numeric_limits<uint32_t>::max())
return;
nestLevel()++;
}
void Logger::popNest() {
if (nestLevel() == 0)
return;
nestLevel()--;
}
std::vector<Log*> Logger::list() {
std::vector<Log*> logs_;
logs_.reserve(logs().size());