-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
668 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.