Minor refactoring
This commit is contained in:
@@ -7,6 +7,7 @@ namespace ugly {
|
||||
|
||||
class ILogFacility {
|
||||
public:
|
||||
~ILogFacility() = default;
|
||||
virtual void vlog(std::string_view fmt, std::format_args args) = 0;
|
||||
};
|
||||
|
||||
|
||||
@@ -11,9 +11,9 @@ public:
|
||||
ILogFacility *mpLog;
|
||||
|
||||
template<typename ...Args>
|
||||
LogAlias &operator()(std::format_string<Args...> format, Args &&...args) {
|
||||
LogAlias &operator()(std::format_string<Args...> fmt, Args &&...args) {
|
||||
if(mpLog) {
|
||||
mpLog->vlog(format.get(), std::make_format_args(args...));
|
||||
mpLog->vlog(fmt.get(), std::make_format_args(args...));
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -12,12 +12,12 @@ namespace ugly {
|
||||
|
||||
class TimestampLog: virtual public ILogFacility {
|
||||
private:
|
||||
std::chrono::high_resolution_clock::time_point mStartTime;
|
||||
std::ostream &mrOutputStream;
|
||||
std::string mDescription;
|
||||
std::chrono::high_resolution_clock::time_point mStart;
|
||||
|
||||
public:
|
||||
TimestampLog(std::ostream &os, std::string_view description);
|
||||
TimestampLog(std::string_view description, std::ostream &os);
|
||||
void vlog(std::string_view fmt, std::format_args args) override;
|
||||
};
|
||||
|
||||
|
||||
@@ -3,16 +3,20 @@
|
||||
#include <format>
|
||||
#include <chrono>
|
||||
|
||||
namespace ugly {
|
||||
|
||||
ugly::TimestampLog::TimestampLog(std::ostream &os, std::string_view description):
|
||||
TimestampLog::TimestampLog(std::string_view description, std::ostream &os):
|
||||
mStartTime{std::chrono::high_resolution_clock::now()},
|
||||
mrOutputStream{os},
|
||||
mDescription{description},
|
||||
mStart{std::chrono::high_resolution_clock::now()} {}
|
||||
mDescription{description} {}
|
||||
|
||||
|
||||
void ugly::TimestampLog::vlog(std::string_view fmt, std::format_args args) {
|
||||
auto now = std::chrono::high_resolution_clock::now();
|
||||
auto timestamp = std::chrono::duration_cast<std::chrono::duration<float>>(now - mStart).count();
|
||||
void TimestampLog::vlog(std::string_view fmt, std::format_args args) {
|
||||
using namespace std::chrono;
|
||||
auto now = high_resolution_clock::now();
|
||||
auto timestamp = duration_cast<duration<float>>(now - mStartTime).count();
|
||||
auto message = std::vformat(fmt, args);
|
||||
mrOutputStream << std::format("[{:9.4f}] {}: {}\n", timestamp, mDescription, message);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,3 +1,7 @@
|
||||
#include "LogUtils.hpp"
|
||||
|
||||
ugly::LogAlias ugly::log{nullptr};
|
||||
namespace ugly {
|
||||
|
||||
LogAlias log{nullptr};
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user