compile fix + timed flush

This commit is contained in:
janis 2022-06-20 20:47:42 +01:00
parent c79004413d
commit b78ffe90ce
3 changed files with 27 additions and 33 deletions

View file

@ -70,10 +70,10 @@ auto init_std_out_err(const LogVerbosity &min = LogVerbosity::Info) -> void;
auto init() -> void;
auto add_file(std::string_view file_path,
std::ios_base::openmode = std::ios_base::ate,
const LogVerbosity &min = LogVerbosity::Info,
const LogVerbosity &min = LogVerbosity::Trace,
const LogVerbosity &max = LogVerbosity::Error) -> void;
auto add_sink(std::shared_ptr<std::ostream> stream,
const LogVerbosity &min = LogVerbosity::Info,
const LogVerbosity &min = LogVerbosity::Trace,
const LogVerbosity &max = LogVerbosity::Error) -> void;
#ifdef JOURNAL_ALL

View file

@ -5,6 +5,7 @@
#include <concepts>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <memory>
#include <mutex>
#include <optional>
@ -176,10 +177,16 @@ struct Verbosity {
};
namespace sinks {
using namespace std::chrono_literals;
struct BaseSink {
Verbosity min_verbosity;
Verbosity max_verbosity;
std::optional<std::chrono::system_clock::time_point> last_flushed;
static constexpr auto TIME_BETWEEN_FLUSH = 2s;
BaseSink()
: min_verbosity(LogVerbosity::Trace), max_verbosity(LogVerbosity::Error) {
}
@ -199,8 +206,18 @@ struct BaseSink {
return std::exchange(min_verbosity, new_min);
}
virtual auto flush() -> void;
virtual auto operator<<(std::string_view str) -> void;
auto should_flush() -> bool {
const auto now = std::chrono::system_clock::now();
if (!last_flushed || (now - last_flushed.value()) >= TIME_BETWEEN_FLUSH) {
last_flushed = now;
return true;
} else {
return false;
}
}
virtual auto flush() -> void {}
virtual auto operator<<(std::string_view str) -> void {}
};
struct StdOutSink : public BaseSink {
@ -235,48 +252,24 @@ struct FileSink : public OStreamSink {
std::ios::ios_base::openmode open_mode = std::ios::ios_base::app,
const Verbosity &min = LogVerbosity::Trace,
const Verbosity &max = LogVerbosity::Error)
: OStreamSink(std::make_shared<std::ostream>(
std::ofstream(file_path, std::ios::ios_base::out | open_mode))) {}
: OStreamSink(std::make_shared<std::ofstream>(file_path, open_mode), min,
max) {}
};
} // namespace sinks
struct Sink {
std::shared_ptr<std::ostream> stream;
Verbosity min_verbosity;
Verbosity max_verbosity;
Sink(std::shared_ptr<std::ostream> &&stream)
: stream(std::move(stream)),
min_verbosity(journal::detail::LogVerbosity::Trace),
max_verbosity(journal::detail::LogVerbosity::Error) {}
Sink(std::shared_ptr<std::ostream>&& stream, const Verbosity &min,
const Verbosity &max)
: stream(std::move(stream)),min_verbosity(min), max_verbosity(max) {}
Sink(std::shared_ptr<std::ostream> &&stream, const Verbosity &min)
: stream(std::move(stream)), min_verbosity(min),
max_verbosity(journal::detail::Error) {}
~Sink() { flush(); }
auto flush() -> void { stream->flush(); }
auto operator<<(std::string_view msg) { (*stream) << msg; }
};
class Journal {
std::vector<std::unique_ptr<sinks::BaseSink>> sinks;
std::chrono::system_clock::time_point last_flushed;
public:
Journal() {}
auto log(const Verbosity &verbosity, source_location source,
std::string_view msg) {
const auto now = std::chrono::system_clock::now();
const auto do_flush = ((now - last_flushed).count() <= 5);
for (auto &&sink : sinks) {
if (sink->should_sink_verbosity(verbosity)) {
(*sink) << format(msg, verbosity, source);
}
if (do_flush) {
if (sink->should_flush()) {
sink->flush();
}
}

View file

@ -1,2 +1,3 @@
[2022-06-20 00:49:18 UTC] [INFO ] [main.cc::8] some info!
[2022-06-20 00:49:18 UTC] [ERROR] [main.cc::9] something went wrong!
[2022-06-20 19:45:53 UTC] [INFO ] [main.cc::8] some info!
[2022-06-20 19:45:53 UTC] [ERROR] [main.cc::9] something went wrong!
[2022-06-20 19:45:59 UTC] [TRACE] [main.cc::11] just some very verbose stuff here