object based logger

This commit is contained in:
otavepto 2024-06-29 06:44:20 +03:00
parent dcd3713b3c
commit 15bc66a1da
2 changed files with 179 additions and 114 deletions

View File

@ -1,94 +1,147 @@
#include "dbg_log/dbg_log.hpp" #include "dbg_log/dbg_log.hpp"
#include "common_helpers/common_helpers.hpp" #include "common_helpers/common_helpers.hpp"
#include "utfcpp/utf8.h"
#include <cstdarg>
#include <cstdio> #include <iterator>
#include <filesystem> #include <cwchar>
#include <thread> #include <cstdarg>
#include <sstream> #include <filesystem>
#include <chrono> #include <sstream>
#include <mutex> #include <string>
#include <stdio.h>
static FILE* out_file = nullptr;
auto const static start_time = std::chrono::system_clock::now(); #include "common_helpers/os_detector.h"
static std::recursive_mutex f_mtx{};
bool dbg_log::init(const wchar_t *path) void dbg_log::open()
{ {
auto p = std::filesystem::path(path).u8string(); #ifndef EMU_RELEASE_BUILD
return init(p.c_str()); if (!out_file && filepath.size()) {
}
// https://en.cppreference.com/w/cpp/filesystem/path/u8path
bool dbg_log::init(const char *path) const auto fsp = std::filesystem::u8path(filepath);
{ #if defined(__WINDOWS__)
#ifndef EMU_RELEASE_BUILD out_file = _wfopen(fsp.c_str(), L"a");
std::lock_guard lk(f_mtx); #else
if (!out_file) { out_file = std::fopen(fsp.c_str(), "a");
out_file = std::fopen(path, "a"); #endif
if (!out_file) {
return false; }
}
} #endif
#endif }
return true; void dbg_log::write_stamp()
} {
auto elapsed = std::chrono::high_resolution_clock::now() - start_time;
void dbg_log::write(const std::wstring &str) auto duration_ms = std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count();
{ auto duration_us = std::chrono::duration_cast<std::chrono::microseconds>(elapsed).count();
#ifndef EMU_RELEASE_BUILD std::stringstream ss{};
write(common_helpers::wstr_to_a(str)); ss << "[" << duration_ms << " ms, " << duration_us << " us] ";
#endif auto ss_str = ss.str();
} std::fprintf(out_file, "%s", ss_str.c_str());
}
void dbg_log::write(const std::string &str)
{ dbg_log::dbg_log(std::string_view path)
{
#ifndef EMU_RELEASE_BUILD
write(str.c_str()); #ifndef EMU_RELEASE_BUILD
#endif filepath = path;
#endif
}
}
void dbg_log::write(const char *fmt, ...)
{ dbg_log::dbg_log(std::wstring_view path)
{
#ifndef EMU_RELEASE_BUILD
std::lock_guard lk(f_mtx); #ifndef EMU_RELEASE_BUILD
if (out_file) { filepath = common_helpers::to_str(path);
auto elapsed = std::chrono::system_clock::now() - start_time; #endif
std::stringstream ss{}; }
ss << "[" << std::chrono::duration_cast<std::chrono::milliseconds>(elapsed).count() << " ms] [tid: " << std::this_thread::get_id() << "] ";
auto ss_str = ss.str(); dbg_log::~dbg_log()
{
std::fprintf(out_file, ss_str.c_str());
#ifndef EMU_RELEASE_BUILD
std::va_list args; close();
va_start(args, fmt); #endif
std::vfprintf(out_file, fmt, args);
va_end(args); }
std::fprintf(out_file, "\n"); void dbg_log::write(std::string_view str)
std::fflush(out_file); {
}
#endif #ifndef EMU_RELEASE_BUILD
write(str.data());
} #endif
void dbg_log::close() }
{
void dbg_log::write(std::wstring_view str)
#ifndef EMU_RELEASE_BUILD {
std::lock_guard lk(f_mtx);
if (out_file) { #ifndef EMU_RELEASE_BUILD
std::fprintf(out_file, "\nLog file closed\n\n"); write(str.data());
std::fclose(out_file); #endif
out_file = nullptr;
} }
#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
}

View File

@ -1,20 +1,32 @@
#pragma once #pragma once
#include <string> #include <string>
#include <string_view>
namespace dbg_log #include <mutex>
{ #include <cstdio>
#include <chrono>
bool init(const wchar_t *path);
class dbg_log
bool init(const char *path); {
private:
void write(const std::wstring &str); std::recursive_mutex f_mtx{};
std::string filepath{};
void write(const std::string &str); std::FILE *out_file{};
const std::chrono::high_resolution_clock::time_point start_time = std::chrono::high_resolution_clock::now();
void write(const char* fmt, ...);
void open();
void close(); 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();
};