Skip to content

Commit

Permalink
refactor: refactoring logger
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Aug 26, 2024
1 parent 0397ec6 commit 7122a50
Show file tree
Hide file tree
Showing 53 changed files with 519 additions and 361 deletions.
2 changes: 1 addition & 1 deletion src-client/ll/core/main_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ void leviLaminaMain() {

if (auto res = ::ll::i18n::getInstance().load(getSelfModIns()->getLangDir()); !res) {
getLogger().error("i18n load failed");
res.error().log(getLogger().error);
res.error().log(getLogger());
}

auto& config = getLeviConfig();
Expand Down
4 changes: 3 additions & 1 deletion src-server/ll/core/io/Output_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

namespace ll::io {
void defaultOutput(std::string_view sv) {
WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), sv.data(), (DWORD)sv.size(), nullptr, nullptr);
auto wstr = string_utils::str2wstr(sv);
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), wstr.data(), (DWORD)wstr.size(), nullptr, nullptr);
// WriteConsoleA(GetStdHandle(STD_OUTPUT_HANDLE), sv.data(), (DWORD)sv.size(), nullptr, nullptr);
}
} // namespace ll::io
4 changes: 2 additions & 2 deletions src-server/ll/core/main_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@

#include "fmt/core.h"

#include "ll/api/Logger.h"
#include "ll/api/Versions.h"
#include "ll/api/command/CommandRegistrar.h"
#include "ll/api/i18n/I18n.h"
#include "ll/api/io/Logger.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/mod/Mod.h"
#include "ll/api/service/Bedrock.h"
Expand Down Expand Up @@ -158,7 +158,7 @@ void leviLaminaMain() {

if (auto res = ::ll::i18n::getInstance().load(getSelfModIns()->getLangDir()); !res) {
getLogger().error("i18n load failed");
res.error().log(getLogger().error);
res.error().log(getLogger());
}

auto& config = getLeviConfig();
Expand Down
36 changes: 36 additions & 0 deletions src-server/ll/core/plugin-abi/ErrorUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

#include "ll/api/utils/ErrorUtils.h"
#include "ll/core/plugin-abi/Plugin.h"

namespace ll::inline utils::error_utils {
LLAPI void printCurrentException(Logger& logger, std::exception_ptr const& e) noexcept {
auto l = io::Logger(logger.title);
printCurrentException(l, io::LogLevel::Error, e);
}
LLAPI void printCurrentException(OutputStream& stream, std::exception_ptr const& e) noexcept {
auto l = io::Logger(stream.logger->title);

io::LogLevel lvl;

switch (stream.level) {
case 5:
lvl = io::LogLevel::Debug;
break;
case 4:
lvl = io::LogLevel::Info;
break;
case 3:
lvl = io::LogLevel::Warn;
break;
case 2:
lvl = io::LogLevel::Error;
break;
default:
case 1:
lvl = io::LogLevel::Fatal;
break;
}

printCurrentException(l, lvl, e);
}
} // namespace ll::inline utils::error_utils
155 changes: 154 additions & 1 deletion src-server/ll/core/plugin-abi/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,156 @@
#include "ll/api/Logger.h"
#include "ll/core/plugin-abi/Logger.h"

#include <array>
#include <exception>
#include <filesystem>
#include <fstream>
#include <ios>
#include <mutex>
#include <optional>
#include <ostream>
#include <string>
#include <string_view>
#include <system_error>
#include <utility>

#include "fmt/chrono.h" // IWYU pragma: keep
#include "fmt/color.h"
#include "fmt/core.h"

#include "ll/api/Config.h"
#include "ll/api/io/Logger.h"
#include "ll/api/utils/ErrorUtils.h"
#include "ll/api/utils/StringUtils.h"
#include "ll/api/utils/SystemUtils.h"
#include "ll/core/Config.h"

#include "pl/Config.h"

using namespace ll::string_utils;

namespace ll {
void OutputStream::print(std::string_view s) const noexcept {

io::LogLevel l;

switch (level) {
case 5:
l = io::LogLevel::Debug;
break;
case 4:
l = io::LogLevel::Info;
break;
case 3:
l = io::LogLevel::Warn;
break;
case 2:
l = io::LogLevel::Error;
break;
default:
case 1:
l = io::LogLevel::Fatal;
break;
}

io::Logger(logger->title).log(l, s);
}
OutputStream::OutputStream(
Logger& logger,
std::string levelPrefix,
int level,
std::array<fmt::text_style, 4> const& style,
std::array<std::string, 5> const& consoleFormat,
std::array<std::string, 5> const& fileFormat
)
: logger(&logger),
levelPrefix(std::move(levelPrefix)),
level(level),
style(style),
consoleFormat(consoleFormat),
fileFormat(fileFormat) {}

Logger::Logger(std::string_view title)
: title(title),
debug(OutputStream{
*this,
"DEBUG",
5,
{
fmt::fg(fmt::color::light_blue),
fmt::fg(fmt::color::light_golden_rod_yellow),
fmt::fg(fmt::color::light_golden_rod_yellow),
fmt::fg(fmt::color::light_golden_rod_yellow) | fmt::emphasis::italic,
}
}),
info(OutputStream{
*this,
"INFO",
4,
{
fmt::fg(fmt::color::light_blue),
fmt::fg(fmt::color::light_sea_green),
{},
{},
}
}),
warn(OutputStream{
*this,
"WARN",
3,
{
fmt::fg(fmt::color::light_blue),
fmt::fg(fmt::terminal_color::bright_yellow),
fmt::fg(fmt::terminal_color::bright_yellow),
fmt::fg(fmt::terminal_color::bright_yellow) | fmt::emphasis::bold,
}
}),
error(OutputStream{
*this,
"ERROR",
2,
{
fmt::fg(fmt::color::light_blue),
fmt::fg(fmt::terminal_color::bright_red),
fmt::fg(fmt::terminal_color::bright_red),
fmt::fg(fmt::terminal_color::bright_red) | fmt::emphasis::bold,
}
}),
fatal(OutputStream{
*this,
"FATAL",
1,
{
fmt::fg(fmt::color::light_blue),
fmt::fg(fmt::color::red),
fmt::fg(fmt::color::red),
fmt::fg(fmt::color::red) | fmt::emphasis::bold,
}
}) {}

LLNDAPI ll::Logger::Logger(std::string_view title, bool) : Logger(title) {}

void Logger::resetFile() {
if (ofs) {
auto& value = *ofs;
if (value.is_open()) value.close();
ofs = std::nullopt;
}
}

bool Logger::setFile(std::filesystem::path const& logFile, bool appendMode) {
resetFile();
if (logFile.empty()) {
return true;
}

std::error_code ec;
std::filesystem::create_directories(logFile.parent_path(), ec);
ofs = std::ofstream(logFile, appendMode ? std::ios::app : std::ios::out);
return ofs->is_open();
}

std::lock_guard<std::recursive_mutex> Logger::lock() {
static std::recursive_mutex mutex;
return std::lock_guard(mutex);
}
} // namespace ll
12 changes: 4 additions & 8 deletions src/ll/api/Logger.h → src-server/ll/core/plugin-abi/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class OutputStream {
std::array<fmt::text_style, 4> style;
std::array<std::string, 5> consoleFormat;
std::array<std::string, 5> fileFormat;
std::array<std::string, 5> playerFormat; // TODO: remove in release
std::function<void()> playerOutputCallback; // TODO: remove in release
std::array<std::string, 5> playerFormat = {"<{2}|{1}> [{0}] {3}", "{:%T}", "{}", "{}", "{}"};
std::function<void()> playerOutputCallback;

LLAPI explicit OutputStream(
Logger& logger,
Expand Down Expand Up @@ -79,7 +79,7 @@ class Logger {
std::optional<std::ofstream> ofs = std::nullopt;
int consoleLevel = -1;
int fileLevel = -1;
int filler; // TODO: remove in release
int filler;

OutputStream debug;
OutputStream info;
Expand All @@ -89,17 +89,13 @@ class Logger {

LLNDAPI explicit Logger(std::string_view title = __builtin_FUNCTION());

LLNDAPI Logger(std::string_view, bool); // TODO: remove in release
LLNDAPI Logger(std::string_view, bool);

~Logger() { resetFile(); }

LLAPI void resetFile();
LLAPI bool setFile(std::filesystem::path const& logFile, bool appendMode = true);

LLAPI static bool setDefaultFile(std::filesystem::path const& logFile, bool appendMode);

LLAPI std::ofstream& getFile();

LLAPI static std::lock_guard<std::recursive_mutex> lock();
};
} // namespace ll
2 changes: 1 addition & 1 deletion src-server/ll/core/plugin-abi/Memory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "pl/SymbolProvider.h"

#include "ll/api/Logger.h"
#include "ll/api/io/Logger.h"
#include "ll/api/service/GamingStatus.h"
#include "ll/api/thread/GlobalThreadPauser.h"
#include "ll/api/utils/StringUtils.h"
Expand Down
14 changes: 13 additions & 1 deletion src-server/ll/core/plugin-abi/Plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,22 @@ void Plugin::setState(State state) const { return Mod::setState((Mod::State)stat

Plugin::State Plugin::getState() const { return (State)Mod::getState(); }

Logger& Plugin::getLogger() const { return Mod::getLogger(); }
static StringNodeMap<Logger> oldLoggers;

Logger& Plugin::getLogger() const {
return oldLoggers.lazy_emplace(
Mod::getName(),
[&](auto const& ctor) { ctor(Mod::getName(), Mod::getName()); }
)->second;
}

} // namespace ll::plugin

#pragma comment( \
linker, \
"/export:?getLogger@Mod@mod@ll@@QEBAAEAVLogger@3@XZ=?getLogger@Plugin@plugin@ll@@QEBAAEAVLogger@3@XZ" \
)

#pragma comment( \
linker, \
"/export:?getOrCreateCommand@CommandRegistrar@command@ll@@QEAAAEAVCommandHandle@23@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0W4CommandPermissionLevel@@UCommandFlag@@V?$weak_ptr@VPlugin@plugin@ll@@@6@@Z=?getOrCreateCommand@CommandRegistrar@command@ll@@QEAAAEAVCommandHandle@23@AEBV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@0W4CommandPermissionLevel@@UCommandFlag@@V?$weak_ptr@VMod@mod@ll@@@6@@Z" \
Expand Down
3 changes: 2 additions & 1 deletion src-server/ll/core/plugin-abi/Plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
#include <memory>

#include "ll/api/Expected.h"
#include "ll/api/Logger.h"
#include "ll/api/base/Macro.h"
#include "ll/api/io/Logger.h"
#include "ll/api/mod/Mod.h"
#include "ll/core/plugin-abi/Logger.h"
#include "ll/core/plugin-abi/Manifest.h"

namespace ll::plugin {
Expand Down
8 changes: 4 additions & 4 deletions src-server/ll/core/tweak/SimpleServerLogger.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#include "ll/core/tweak/SimpleServerLogger.h"

#include "ll/api/Logger.h"
#include "ll/api/event/EventBus.h"
#include "ll/api/event/command/ExecuteCommandEvent.h"
#include "ll/api/event/player/PlayerChangePermEvent.h"
#include "ll/api/event/player/PlayerChatEvent.h"
#include "ll/api/event/player/ServerPlayerEvent.h"
#include "ll/api/io/Logger.h"

#include "magic_enum.hpp"

Expand All @@ -29,7 +29,7 @@ void SimpleServerLogger::call(SimpleServerLoggerConfig const& config) {
auto& bus = EventBus::getInstance();
if (config.playerChat && !impl->playerChat) {
impl->playerChat = bus.emplaceListener<PlayerChatEvent>([](PlayerChatEvent& ev) {
static Logger logger("PlayerChat");
static io::Logger logger("PlayerChat");
logger.info("<{}> {}", ev.self().getRealName(), ev.message());
});
}
Expand All @@ -39,7 +39,7 @@ void SimpleServerLogger::call(SimpleServerLoggerConfig const& config) {
if (context.getCommandOrigin().getOriginType() != CommandOriginType::Player) {
return;
}
static Logger logger("PlayerCmd");
static io::Logger logger("PlayerCmd");
logger.info(
"<{}> {}",
((Player*)(context.getCommandOrigin().getEntity()))->getRealName(),
Expand All @@ -49,7 +49,7 @@ void SimpleServerLogger::call(SimpleServerLoggerConfig const& config) {
}
if (config.playerPermission && !impl->playerPermission) {
impl->playerPermission = bus.emplaceListener<PlayerChangePermEvent>([](PlayerChangePermEvent& ev) {
static Logger logger("PlayerPerm");
static io::Logger logger("PlayerPerm");
logger.info(
"<{}> {}({}) -> {}({})",
ev.self().getRealName(),
Expand Down
Loading

0 comments on commit 7122a50

Please sign in to comment.