Refactor logging to use util::logf and add log utility header

This commit is contained in:
scawful
2025-01-22 13:36:37 -05:00
parent a5b94ab173
commit b245b10963
9 changed files with 75 additions and 50 deletions

View File

@@ -2,7 +2,7 @@
#include <string>
#include "absl/strings/str_cat"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"

View File

@@ -2,6 +2,7 @@
#define YAZE_UTIL_HEX_H
#include <cstdint>
#include <string>
namespace yaze {
namespace util {
@@ -17,4 +18,6 @@ std::string HexLong(uint32_t dword, HexStringParams params = {});
std::string HexLongLong(uint64_t qword, HexStringParams params = {});
} // namespace util
} // namespace yaze
} // namespace yaze
#endif

36
src/util/log.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef YAZE_UTIL_LOG_H
#define YAZE_UTIL_LOG_H
#include <chrono>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
namespace yaze {
namespace util {
static const std::string kLogFileOut = "yaze_log.txt";
template <typename... Args>
static void logf(const absl::FormatSpec<Args...> &format, const Args &...args) {
std::string message = absl::StrFormat(format, args...);
auto timestamp = std::chrono::system_clock::now();
std::time_t now_tt = std::chrono::system_clock::to_time_t(timestamp);
std::tm tm = *std::localtime(&now_tt);
message = absl::StrCat("[", tm.tm_hour, ":", tm.tm_min, ":", tm.tm_sec, "] ",
message, "\n");
if (ExperimentFlags::get().kLogToConsole) {
std::cout << message;
}
static std::ofstream fout(kLogFileOut, std::ios::out | std::ios::app);
fout << message;
}
} // namespace util
} // namespace yaze