compile fix + timed flush
This commit is contained in:
parent
c79004413d
commit
b78ffe90ce
|
@ -70,10 +70,10 @@ auto init_std_out_err(const LogVerbosity &min = LogVerbosity::Info) -> void;
|
||||||
auto init() -> void;
|
auto init() -> void;
|
||||||
auto add_file(std::string_view file_path,
|
auto add_file(std::string_view file_path,
|
||||||
std::ios_base::openmode = std::ios_base::ate,
|
std::ios_base::openmode = std::ios_base::ate,
|
||||||
const LogVerbosity &min = LogVerbosity::Info,
|
const LogVerbosity &min = LogVerbosity::Trace,
|
||||||
const LogVerbosity &max = LogVerbosity::Error) -> void;
|
const LogVerbosity &max = LogVerbosity::Error) -> void;
|
||||||
auto add_sink(std::shared_ptr<std::ostream> stream,
|
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;
|
const LogVerbosity &max = LogVerbosity::Error) -> void;
|
||||||
|
|
||||||
#ifdef JOURNAL_ALL
|
#ifdef JOURNAL_ALL
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
#include <concepts>
|
#include <concepts>
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <mutex>
|
#include <mutex>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
@ -176,10 +177,16 @@ struct Verbosity {
|
||||||
};
|
};
|
||||||
|
|
||||||
namespace sinks {
|
namespace sinks {
|
||||||
|
|
||||||
|
using namespace std::chrono_literals;
|
||||||
|
|
||||||
struct BaseSink {
|
struct BaseSink {
|
||||||
Verbosity min_verbosity;
|
Verbosity min_verbosity;
|
||||||
Verbosity max_verbosity;
|
Verbosity max_verbosity;
|
||||||
|
|
||||||
|
std::optional<std::chrono::system_clock::time_point> last_flushed;
|
||||||
|
static constexpr auto TIME_BETWEEN_FLUSH = 2s;
|
||||||
|
|
||||||
BaseSink()
|
BaseSink()
|
||||||
: min_verbosity(LogVerbosity::Trace), max_verbosity(LogVerbosity::Error) {
|
: min_verbosity(LogVerbosity::Trace), max_verbosity(LogVerbosity::Error) {
|
||||||
}
|
}
|
||||||
|
@ -199,8 +206,18 @@ struct BaseSink {
|
||||||
return std::exchange(min_verbosity, new_min);
|
return std::exchange(min_verbosity, new_min);
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual auto flush() -> void;
|
auto should_flush() -> bool {
|
||||||
virtual auto operator<<(std::string_view str) -> void;
|
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 {
|
struct StdOutSink : public BaseSink {
|
||||||
|
@ -235,48 +252,24 @@ struct FileSink : public OStreamSink {
|
||||||
std::ios::ios_base::openmode open_mode = std::ios::ios_base::app,
|
std::ios::ios_base::openmode open_mode = std::ios::ios_base::app,
|
||||||
const Verbosity &min = LogVerbosity::Trace,
|
const Verbosity &min = LogVerbosity::Trace,
|
||||||
const Verbosity &max = LogVerbosity::Error)
|
const Verbosity &max = LogVerbosity::Error)
|
||||||
: OStreamSink(std::make_shared<std::ostream>(
|
: OStreamSink(std::make_shared<std::ofstream>(file_path, open_mode), min,
|
||||||
std::ofstream(file_path, std::ios::ios_base::out | open_mode))) {}
|
max) {}
|
||||||
};
|
};
|
||||||
} // namespace sinks
|
} // 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 {
|
class Journal {
|
||||||
std::vector<std::unique_ptr<sinks::BaseSink>> sinks;
|
std::vector<std::unique_ptr<sinks::BaseSink>> sinks;
|
||||||
std::chrono::system_clock::time_point last_flushed;
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Journal() {}
|
Journal() {}
|
||||||
auto log(const Verbosity &verbosity, source_location source,
|
auto log(const Verbosity &verbosity, source_location source,
|
||||||
std::string_view msg) {
|
std::string_view msg) {
|
||||||
const auto now = std::chrono::system_clock::now();
|
const auto now = std::chrono::system_clock::now();
|
||||||
const auto do_flush = ((now - last_flushed).count() <= 5);
|
|
||||||
for (auto &&sink : sinks) {
|
for (auto &&sink : sinks) {
|
||||||
if (sink->should_sink_verbosity(verbosity)) {
|
if (sink->should_sink_verbosity(verbosity)) {
|
||||||
(*sink) << format(msg, verbosity, source);
|
(*sink) << format(msg, verbosity, source);
|
||||||
}
|
}
|
||||||
if (do_flush) {
|
if (sink->should_flush()) {
|
||||||
sink->flush();
|
sink->flush();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
5
test.log
5
test.log
|
@ -1,2 +1,3 @@
|
||||||
[2022-06-20 00:49:18 UTC] [INFO ] [main.cc::8] some info!
|
[2022-06-20 19:45:53 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] [ERROR] [main.cc::9] something went wrong!
|
||||||
|
[2022-06-20 19:45:59 UTC] [TRACE] [main.cc::11] just some very verbose stuff here
|
||||||
|
|
Loading…
Reference in a new issue