66 lines
1.6 KiB
C++
66 lines
1.6 KiB
C++
#include <IACore/Logger.hpp>
|
|
#include <chrono>
|
|
#include <fstream>
|
|
#include <iostream>
|
|
|
|
namespace IACore {
|
|
Logger::LogLevel Logger::m_log_level = Logger::LogLevel::Info;
|
|
std::ofstream Logger::m_log_file;
|
|
|
|
static auto get_seconds_count() -> f64 {
|
|
static const auto start_time = std::chrono::steady_clock::now();
|
|
const auto now = std::chrono::steady_clock::now();
|
|
const std::chrono::duration<f64> duration = now - start_time;
|
|
return duration.count();
|
|
}
|
|
|
|
auto Logger::initialize() -> void {}
|
|
|
|
auto Logger::terminate() -> void {
|
|
if (m_log_file.is_open()) {
|
|
m_log_file.flush();
|
|
m_log_file.close();
|
|
}
|
|
}
|
|
|
|
auto Logger::enable_logging_to_disk(const char *file_path) -> Result<void> {
|
|
if (m_log_file.is_open()) {
|
|
m_log_file.flush();
|
|
m_log_file.close();
|
|
}
|
|
|
|
m_log_file.open(file_path);
|
|
|
|
if (!m_log_file.is_open()) {
|
|
return fail("Failed to open log file: {}", file_path);
|
|
}
|
|
|
|
return {};
|
|
}
|
|
|
|
auto Logger::set_log_level(LogLevel log_level) -> void {
|
|
m_log_level = log_level;
|
|
}
|
|
|
|
auto Logger::flush_logs() -> void {
|
|
std::cout.flush();
|
|
if (m_log_file.is_open()) {
|
|
m_log_file.flush();
|
|
}
|
|
}
|
|
|
|
auto Logger::log_internal(const char *prefix, const char *tag, String &&msg)
|
|
-> void {
|
|
const auto seconds = get_seconds_count();
|
|
const auto out_line = std::format("[{:>8.3f}]: [{}]: {}", seconds, tag, msg);
|
|
|
|
std::cout << prefix << out_line << console::RESET << '\n';
|
|
|
|
if (m_log_file.is_open()) {
|
|
m_log_file.write(out_line.data(),
|
|
static_cast<std::streamsize>(out_line.size()));
|
|
m_log_file.put('\n');
|
|
m_log_file.flush();
|
|
}
|
|
}
|
|
} // namespace IACore
|