Minor refactoring

This commit is contained in:
2025-07-29 17:17:28 -04:00
parent c8b5a5ebd4
commit 7a7bfc4930
10 changed files with 69 additions and 57 deletions

View File

@@ -7,6 +7,7 @@ namespace ugly {
class ILogFacility {
public:
~ILogFacility() = default;
virtual void vlog(std::string_view fmt, std::format_args args) = 0;
};

View File

@@ -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;
}

View File

@@ -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;
};

View File

@@ -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);
}
}

View File

@@ -1,3 +1,7 @@
#include "LogUtils.hpp"
ugly::LogAlias ugly::log{nullptr};
namespace ugly {
LogAlias log{nullptr};
}