Skip to content

Commit

Permalink
feat: add new logger system
Browse files Browse the repository at this point in the history
  • Loading branch information
OEOTYAN committed Aug 28, 2024
1 parent 863b5c4 commit eee4463
Show file tree
Hide file tree
Showing 29 changed files with 668 additions and 100 deletions.
6 changes: 3 additions & 3 deletions src-test/server/ConfigTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

#include "mc/world/actor/DataItem.h"

#include "ll/api/memory/IndirectValue.h"
#include "ll/api/data/IndirectValue.h"

// [0, 8, 16, 96, 97, 98, 104, 136, 144, 160, 176, 184, 196, 208, 232, 248, 304, 328, 360, 392, 408]

Expand Down Expand Up @@ -186,8 +186,8 @@ LL_AUTO_TYPE_INSTANCE_HOOK(ConfigTest, HookPriority::Normal, ServerInstance, &Se
ll::getLogger().debug("{}", 3 / v1);


auto indirect = ll::memory::makePolymorphic<Test>();
indirect = ll::memory::makePolymorphic<TestD>(13, 23);
auto indirect = ll::makePolymorphic<Test>();
indirect = ll::makePolymorphic<TestD>(13, 23);

auto indirect2 = indirect;
auto indirect3 = indirect;
Expand Down
2 changes: 1 addition & 1 deletion src-test/server/ECSTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
#include <string>
#include <thread>

#include "ll/api/io/Logger.h"
#include "ll/api/base/StdInt.h"
#include "ll/api/command/CommandHandle.h"
#include "ll/api/command/CommandRegistrar.h"
#include "ll/api/io/Logger.h"
#include "ll/api/memory/Hook.h"
#include "ll/api/thread/TickSyncSleep.h"
#include "mc/entity/systems/DefaultEntitySystemsCollection.h"
Expand Down
2 changes: 1 addition & 1 deletion src-test/server/FormTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct TestFormParam {

void registerFormTestCommand() {
static ll::io::Logger logger{"FormTest"};
auto& cmd =
auto& cmd =
ll::command::CommandRegistrar::getInstance()
.getOrCreateCommand("formtest", "formtest", CommandPermissionLevel::GameDirectors, CommandFlagValue::None);
cmd.overload<TestFormParam>().required("type").execute(
Expand Down
7 changes: 5 additions & 2 deletions src-test/server/TestNbt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,11 @@ LL_AUTO_TYPE_INSTANCE_HOOK(NbtTest, HookPriority::Normal, ServerInstance, &Serve

using namespace ll::string_utils;

ll::getLogger().debug("\n{}", replaceAnsiToMcCode(nbt.toSnbt(SnbtFormat::Colored | SnbtFormat::Console)));
ll::getLogger().debug("\n{}", (nbt2.value().toSnbt(SnbtFormat::Colored)));
ll::getLogger().debug(
"\n{}",
replaceMcToAnsiCode(replaceAnsiToMcCode(nbt.toSnbt(SnbtFormat::Colored | SnbtFormat::Console)))
);
ll::getLogger().debug("\n{}", replaceMcToAnsiCode(nbt2.value().toSnbt(SnbtFormat::Colored)));


ll::getLogger().debug(
Expand Down
32 changes: 32 additions & 0 deletions src/ll/api/base/CompilerPredefine.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,22 @@
#define LL_RETURN_ADDRESS _ReturnAddress()
#endif

#ifndef LL_CURRENT_LINE
#define LL_CURRENT_LINE __builtin_LINE()
#endif

#ifndef LL_CURRENT_COLUMN
#define LL_CURRENT_COLUMN __builtin_COLUMN()
#endif

#ifndef LL_CURRENT_FILE
#define LL_CURRENT_FILE __builtin_FILE()
#endif

#ifndef LL_CURRENT_FUNCTION
#define LL_CURRENT_FUNCTION __builtin_FUNCTION()
#endif

// MSVC has customized some functions and classes inside the compiler, but they are not included in IntelliSense. This
// header file is only used for IntelliSense.
#if defined(__INTELLISENSE__) || defined(__clang__) || defined(__clangd__)
Expand Down Expand Up @@ -248,6 +264,22 @@ using FileHandleT = void*;
#define LL_RETURN_ADDRESS __builtin_return_address(0)
#endif

#ifndef LL_CURRENT_LINE
#define LL_CURRENT_LINE __builtin_LINE()
#endif

#ifndef LL_CURRENT_COLUMN
#define LL_CURRENT_COLUMN 0
#endif

#ifndef LL_CURRENT_FILE
#define LL_CURRENT_FILE __builtin_FILE()
#endif

#ifndef LL_CURRENT_FUNCTION
#define LL_CURRENT_FUNCTION __builtin_FUNCTION()
#endif

namespace ll::internal {

[[nodiscard]] LL_FORCEINLINE void* getCurrentModuleHandle() noexcept; // Implemented in SystemUtils_linux.cpp
Expand Down
20 changes: 12 additions & 8 deletions src/ll/api/base/SourceLocation.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

#include <string>

#include "ll/api/base/CompilerPredefine.h"

#include "fmt/format.h"

namespace ll {
class SourceLocation {
public:
static consteval SourceLocation current(
int line = __builtin_LINE(),
int column = __builtin_COLUMN(),
char const* file = __builtin_FILE(),
char const* function = __builtin_FUNCTION()
char const* file = LL_CURRENT_FILE,
char const* function = LL_CURRENT_FUNCTION,
int line = LL_CURRENT_LINE,
int column = LL_CURRENT_COLUMN
) noexcept {
return {line, column, file, function};
return {file, function, line, column};
}

consteval SourceLocation(int line, int column, char const* file, char const* function) noexcept
static consteval SourceLocation unknown() noexcept { return {"", "", 0, 0}; }

consteval SourceLocation(char const* file, char const* function, int line, int column) noexcept
: mLine(line),
mColumn(column),
mFile(file),
Expand All @@ -32,9 +36,9 @@ class SourceLocation {
}

private:
int mLine;
int mColumn;
char const* mFile;
char const* mFunction;
int mLine;
int mColumn;
};
} // namespace ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "ll/api/base/Concepts.h"
#include "ll/api/data/TightPair.h"

namespace ll::memory {
namespace ll::data {

template <class T>
struct defaultCopy {
Expand Down Expand Up @@ -150,22 +150,48 @@ class IndirectValue {
return *this;
}

constexpr T* operator->() noexcept { return get(); }
constexpr T const* operator->() const noexcept { return get(); }
constexpr T& operator*() & noexcept { return *get(); }
constexpr T const& operator*() const& noexcept { return *get(); }
constexpr T&& operator*() && noexcept { return std::forward<T>(*get()); }
constexpr T const&& operator*() const&& noexcept { return std::forward<T const>(*get()); }
[[nodiscard]] constexpr T* operator->() noexcept { return get(); }
[[nodiscard]] constexpr T const* operator->() const noexcept { return get(); }
[[nodiscard]] constexpr T& operator*() & noexcept { return *get(); }
[[nodiscard]] constexpr T const& operator*() const& noexcept { return *get(); }
[[nodiscard]] constexpr T&& operator*() && noexcept { return std::forward<T>(*get()); }
[[nodiscard]] constexpr T const&& operator*() const&& noexcept { return std::forward<T const>(*get()); }
};
template <class T, class C, class D>
[[nodiscard]] constexpr bool operator==(IndirectValue<T, C, D> const& lhs, IndirectValue<T, C, D> const& rhs) {
return lhs.get() == rhs.get();
}
template <class T, class C, class D>
[[nodiscard]] constexpr auto operator<=>(IndirectValue<T, C, D> const& lhs, IndirectValue<T, C, D> const& rhs) {
return lhs.get() <=> rhs.get();
}
template <class T, class C, class D>
[[nodiscard]] constexpr bool operator==(IndirectValue<T, C, D> const& lhs, std::nullptr_t) {
return lhs.get() == nullptr;
}
template <class T, class C, class D>
[[nodiscard]] constexpr auto operator<=>(IndirectValue<T, C, D> const& lhs, std::nullptr_t) {
return lhs.get() <=> nullptr;
}
} // namespace ll::data

namespace std {
template <class T, class C, class D>
struct hash<::ll::data::IndirectValue<T, C, D>> {
std::size_t operator()(::ll::data::IndirectValue<T, C, D> const& value) const { return hash<T*>{}(value.get()); }
};
} // namespace std

namespace ll {
template <class T, class D = std::default_delete<T>>
using Indirect = IndirectValue<T, defaultCopy<T>, D>;
using Indirect = data::IndirectValue<T, data::defaultCopy<T>, D>;

template <
class T,
class C = std::conditional_t<concepts::is_virtual_cloneable_v<T>, virtualCloneCopy<T>, polymorphicCopy<T>>,
class C =
std::conditional_t<concepts::is_virtual_cloneable_v<T>, data::virtualCloneCopy<T>, data::polymorphicCopy<T>>,
class D = std::default_delete<T>>
using Polymorphic = IndirectValue<T, C, D>;
using Polymorphic = data::IndirectValue<T, C, D>;

template <class T, class... Args>
Indirect<T> makeIndirect(Args&&... args) {
Expand All @@ -176,5 +202,4 @@ template <class T, class... Args>
Polymorphic<T> makePolymorphic(Args&&... args) {
return Polymorphic<T>(new T(std::forward<Args>(args)...));
}

} // namespace ll::memory
} // namespace ll
42 changes: 42 additions & 0 deletions src/ll/api/data/TmWithMs.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#pragma once

#include <ctime>

#include "ll/api/base/StdInt.h"

#include "fmt/chrono.h"

namespace ll::data {
struct TmWithMs : public std::tm {
ushort ms;
};
} // namespace ll::data

template <typename Char>
struct fmt::formatter<ll::data::TmWithMs, Char> : formatter<std::tm, Char> {
private:
detail::arg_ref<Char> precisionRef;
int precision{0};

public:
FMT_CONSTEXPR formatter() { this->format_str_ = detail::string_literal<Char, '%', 'T'>{}; }

template <typename FormatContext>
auto format(ll::data::TmWithMs const& val, FormatContext& ctx) const -> decltype(ctx.out()) {
formatter<std::tm, Char>::format(val, ctx);
auto out = ctx.out();
if (precision > 0) {
fmt::format_to(out, "{0:0>{1}}", val.ms, precision);
}
return out;
}
constexpr auto parse(basic_format_parse_context<Char>& ctx) -> decltype(ctx.begin()) {
auto it = ctx.begin(), end = ctx.end();
if (it == end || *it == '}') return it;
if (*it == '.') {
it = detail::parse_precision(it, end, precision, precisionRef, ctx);
}
ctx.advance_to(it);
return formatter<std::tm, Char>::parse(ctx);
}
};
2 changes: 1 addition & 1 deletion src/ll/api/i18n/I18n.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include "ll/api/Expected.h"
#include "ll/api/base/Concepts.h"
#include "ll/api/base/FixedString.h"
#include "ll/api/Expected.h"

#include <string>

Expand Down
40 changes: 40 additions & 0 deletions src/ll/api/io/DefaultSink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "ll/api/io/DefaultSink.h"

#include "ll/api/io/FileUtils.h"
#include "ll/api/io/PatternFormatter.h"
#include "ll/core/io/Output.h"

#include "pl/Config.h"

namespace ll::io {
struct DefaultSink::Impl {
std::mutex mutex;
std::ofstream logFile{file_utils::u8path(pl::pl_log_path) / u8"latest.log"};
PatternFormatter logFileFormatter{"[{tm:.3%F %T.} {lvl}][{tit}] {msg}", false};
};

DefaultSink::Impl& DefaultSink::getImpl() {
static Impl impl;
return impl;
}

DefaultSink::DefaultSink()
: Sink(makePolymorphic<PatternFormatter>("{tm:.3%T.} {lvl} {tit} {msg}", true, 0b0010)),
impl(getImpl()) {}

DefaultSink::~DefaultSink() = default;

void DefaultSink::setFormatter(Polymorphic<Formatter> fmter) {
std::lock_guard lock(impl.mutex);
formatter = std::move(fmter);
}
void DefaultSink::append(LogMessageView const& view) {
std::lock_guard lock(impl.mutex);
std::string buffer;
formatter->format(view, buffer);
defaultOutput(buffer);
buffer.clear();
impl.logFileFormatter.format(view, buffer);
impl.logFile << buffer;
}
} // namespace ll::io
23 changes: 23 additions & 0 deletions src/ll/api/io/DefaultSink.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <mutex>

#include "ll/api/io/Sink.h"

namespace ll::io {
class DefaultSink : public Sink {
struct Impl;
Impl& impl;

static Impl& getImpl();

public:
LLAPI DefaultSink();

LLAPI ~DefaultSink() override;

LLAPI void setFormatter(Polymorphic<Formatter> fmter) override;

LLAPI void append(LogMessageView const& view) override;
};
} // namespace ll::io
34 changes: 34 additions & 0 deletions src/ll/api/io/FileSink.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "ll/api/io/FileSink.h"

namespace ll::io {
FileSink::FileSink(std::filesystem::path const& path, Polymorphic<Formatter> formatter, std::ios::openmode mode)
: Sink(std::move(formatter)),
file(path, mode),
flushLevel(LogLevel::Off) {}

FileSink::~FileSink() = default;

void FileSink::setFormatter(Polymorphic<Formatter> fmter) {
std::lock_guard lock(mutex);
formatter = std::move(fmter);
}
void FileSink::append(LogMessageView const& view) {
std::lock_guard lock(mutex);
std::string buffer;
formatter->format(view, buffer);
file << buffer;
if (view.lvl <= flushLevel) {
file.flush();
}
}

void FileSink::flush() {
std::lock_guard lock(mutex);
file.flush();
}

void FileSink::setFlushLevel(LogLevel level) {
std::lock_guard lock(mutex);
flushLevel = level;
}
} // namespace ll::io
Loading

0 comments on commit eee4463

Please sign in to comment.