Skip to content

Commit

Permalink
Replace use of std::format with fmt:format in logger
Browse files Browse the repository at this point in the history
I'm leaving this commit here instead of squashing, so that in the
future, we could technically just reverse this commit.
  • Loading branch information
psyomn committed Dec 25, 2023
1 parent 7ae101c commit 6a79db8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
2 changes: 2 additions & 0 deletions source/util/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ std::optional<std::ofstream> logFile;

std::filesystem::path logPath;

std::mutex lock;

std::string all()
{
std::stringstream ss;
Expand Down
16 changes: 11 additions & 5 deletions source/util/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
#include <iomanip> /* put_time */
#include <fstream>
#include <filesystem>
#include <format>

#include <optional>
#include <vector>
#include <mutex>

/* TODO: fmtlib: replace with std, when all compilers support `format'. */
#include <fmt/core.h>

namespace Log {

Expand All @@ -47,6 +49,8 @@ extern std::filesystem::path logPath;

extern std::optional<std::ofstream> logFile;

extern std::mutex lock;

void init(enum Level lvl, std::filesystem::path logPath);

/**
Expand All @@ -57,6 +61,8 @@ std::string all();
template <typename... Args>
void backend(enum Level level, std::string_view fmt, Args&&... args)
{
std::lock_guard<std::mutex> _guard(Log::lock);

/* formats a timestamp in a rfc3339 fashion; I believe the system clock
* defaults to epoch, which should be UTC, but I might be wrong. */
const auto timestamp_now_str_fn = []() -> std::string {
Expand Down Expand Up @@ -84,7 +90,7 @@ void backend(enum Level level, std::string_view fmt, Args&&... args)
std::cout
<< timestamp_now_str_fn() << ": "
<< level_str_fn(level) << ": "
<< std::vformat(fmt, std::make_format_args(args...))
<< fmt::vformat(fmt, fmt::make_format_args(args...))
<< std::endl;

if (!logFile) {
Expand All @@ -96,10 +102,10 @@ void backend(enum Level level, std::string_view fmt, Args&&... args)
logFile.value()
<< timestamp_now_str_fn() << ": "
<< level_str_fn(level) << ": "
<< std::vformat(fmt, std::make_format_args(args...))
<< fmt::vformat(fmt, fmt::make_format_args(args...))
<< std::endl;
} else {
std::cout << std::format(
std::cout << fmt::format(
"{} {} could not open file at ({})",
timestamp_now_str_fn(),
level_str_fn(Level::Warning),
Expand Down

0 comments on commit 6a79db8

Please sign in to comment.