From 15bc66a1da8dfe0707837b905b6ceeb737eae425 Mon Sep 17 00:00:00 2001 From: otavepto <153766569+otavepto@users.noreply.github.com> Date: Sat, 29 Jun 2024 06:44:20 +0300 Subject: [PATCH] object based logger --- helpers/dbg_log.cpp | 241 ++++++++++++++++++++++-------------- helpers/dbg_log/dbg_log.hpp | 52 +++++--- 2 files changed, 179 insertions(+), 114 deletions(-) diff --git a/helpers/dbg_log.cpp b/helpers/dbg_log.cpp index 2e6bf12b..7ae1c140 100644 --- a/helpers/dbg_log.cpp +++ b/helpers/dbg_log.cpp @@ -1,94 +1,147 @@ -#include "dbg_log/dbg_log.hpp" -#include "common_helpers/common_helpers.hpp" - -#include -#include -#include -#include -#include -#include -#include - -static FILE* out_file = nullptr; -auto const static start_time = std::chrono::system_clock::now(); -static std::recursive_mutex f_mtx{}; - -bool dbg_log::init(const wchar_t *path) -{ - auto p = std::filesystem::path(path).u8string(); - return init(p.c_str()); -} - -bool dbg_log::init(const char *path) -{ -#ifndef EMU_RELEASE_BUILD - std::lock_guard lk(f_mtx); - if (!out_file) { - out_file = std::fopen(path, "a"); - if (!out_file) { - return false; - } - } - -#endif - - return true; -} - -void dbg_log::write(const std::wstring &str) -{ - -#ifndef EMU_RELEASE_BUILD - write(common_helpers::wstr_to_a(str)); -#endif - -} - -void dbg_log::write(const std::string &str) -{ - -#ifndef EMU_RELEASE_BUILD - write(str.c_str()); -#endif - -} - -void dbg_log::write(const char *fmt, ...) -{ - -#ifndef EMU_RELEASE_BUILD - std::lock_guard lk(f_mtx); - if (out_file) { - auto elapsed = std::chrono::system_clock::now() - start_time; - - std::stringstream ss{}; - ss << "[" << std::chrono::duration_cast(elapsed).count() << " ms] [tid: " << std::this_thread::get_id() << "] "; - auto ss_str = ss.str(); - - std::fprintf(out_file, ss_str.c_str()); - - std::va_list args; - va_start(args, fmt); - std::vfprintf(out_file, fmt, args); - va_end(args); - - std::fprintf(out_file, "\n"); - std::fflush(out_file); - } -#endif - -} - -void dbg_log::close() -{ - -#ifndef EMU_RELEASE_BUILD - std::lock_guard lk(f_mtx); - if (out_file) { - std::fprintf(out_file, "\nLog file closed\n\n"); - std::fclose(out_file); - out_file = nullptr; - } -#endif - -} +#include "dbg_log/dbg_log.hpp" +#include "common_helpers/common_helpers.hpp" +#include "utfcpp/utf8.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "common_helpers/os_detector.h" + + +void dbg_log::open() +{ +#ifndef EMU_RELEASE_BUILD + if (!out_file && filepath.size()) { + + // https://en.cppreference.com/w/cpp/filesystem/path/u8path + const auto fsp = std::filesystem::u8path(filepath); +#if defined(__WINDOWS__) + out_file = _wfopen(fsp.c_str(), L"a"); +#else + out_file = std::fopen(fsp.c_str(), "a"); +#endif + + } + +#endif + +} + +void dbg_log::write_stamp() +{ + auto elapsed = std::chrono::high_resolution_clock::now() - start_time; + auto duration_ms = std::chrono::duration_cast(elapsed).count(); + auto duration_us = std::chrono::duration_cast(elapsed).count(); + + std::stringstream ss{}; + ss << "[" << duration_ms << " ms, " << duration_us << " us] "; + auto ss_str = ss.str(); + + std::fprintf(out_file, "%s", ss_str.c_str()); +} + +dbg_log::dbg_log(std::string_view path) +{ + +#ifndef EMU_RELEASE_BUILD + filepath = path; +#endif + +} + +dbg_log::dbg_log(std::wstring_view path) +{ + +#ifndef EMU_RELEASE_BUILD + filepath = common_helpers::to_str(path); +#endif + +} + +dbg_log::~dbg_log() +{ + +#ifndef EMU_RELEASE_BUILD + close(); +#endif + +} + +void dbg_log::write(std::string_view str) +{ + +#ifndef EMU_RELEASE_BUILD + write(str.data()); +#endif + +} + +void dbg_log::write(std::wstring_view str) +{ + +#ifndef EMU_RELEASE_BUILD + write(str.data()); +#endif + +} + +void dbg_log::write(const char *fmt, ...) +{ + +#ifndef EMU_RELEASE_BUILD + std::lock_guard lk(f_mtx); + open(); + if (out_file) { + write_stamp(); + + std::va_list args; + va_start(args, fmt); + std::vfprintf(out_file, fmt, args); + va_end(args); + + std::fprintf(out_file, "\n"); + std::fflush(out_file); + } +#endif + +} + +void dbg_log::write(const wchar_t *fmt, ...) +{ + +#ifndef EMU_RELEASE_BUILD + std::lock_guard lk(f_mtx); + open(); + if (out_file) { + write_stamp(); + + std::va_list args; + va_start(args, fmt); + std::vfwprintf(out_file, fmt, args); + va_end(args); + + std::fprintf(out_file, "\n"); + std::fflush(out_file); + } +#endif + +} + +void dbg_log::close() +{ + +#ifndef EMU_RELEASE_BUILD + std::lock_guard lk(f_mtx); + if (out_file) { + std::fprintf(out_file, "\nLog file closed\n\n"); + std::fclose(out_file); + out_file = nullptr; + } +#endif + +} diff --git a/helpers/dbg_log/dbg_log.hpp b/helpers/dbg_log/dbg_log.hpp index aeb73d55..c0a4ea46 100644 --- a/helpers/dbg_log/dbg_log.hpp +++ b/helpers/dbg_log/dbg_log.hpp @@ -1,20 +1,32 @@ -#pragma once - -#include - -namespace dbg_log -{ - -bool init(const wchar_t *path); - -bool init(const char *path); - -void write(const std::wstring &str); - -void write(const std::string &str); - -void write(const char* fmt, ...); - -void close(); - -} +#pragma once + +#include +#include +#include +#include +#include + +class dbg_log +{ +private: + std::recursive_mutex f_mtx{}; + std::string filepath{}; + std::FILE *out_file{}; + const std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now(); + + void open(); + void write_stamp(); + +public: + dbg_log(std::string_view path); + dbg_log(std::wstring_view path); + ~dbg_log(); + + void write(std::string_view str); + void write(std::wstring_view str); + + void write(const char* fmt, ...); + void write(const wchar_t* fmt, ...); + + void close(); +};