From 2938da31d2f79bf75f6fd489429c91f5897bd6eb Mon Sep 17 00:00:00 2001 From: TotallyNotElite <1yourexperiment@protonmail.com> Date: Wed, 23 Oct 2019 21:15:47 +0200 Subject: [PATCH] Add logging::File --- include/core/logging.hpp | 7 +---- src/core/logging.cpp | 66 +++++++++++++++++++++++++++------------- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/include/core/logging.hpp b/include/core/logging.hpp index 2b202aa5..2f51c130 100755 --- a/include/core/logging.hpp +++ b/include/core/logging.hpp @@ -10,17 +10,12 @@ #include #include -#ifdef __cplusplus namespace logging { -#endif - extern std::ofstream handle; void Initialize(); void Shutdown(); void Info(const char *fmt, ...); - -#ifdef __cplusplus +void File(const char *fmt, ...); } -#endif diff --git a/src/core/logging.cpp b/src/core/logging.cpp index 41b59927..72b776c3 100644 --- a/src/core/logging.cpp +++ b/src/core/logging.cpp @@ -6,14 +6,14 @@ */ #include -#include +#include #include #include - -#include "common.hpp" -#include "hack.hpp" +#include "logging.hpp" +#include "helpers.hpp" #include "MiscTemporary.hpp" +#include "hack.hpp" static settings::Boolean log_to_console{ "hack.log-console", "false" }; @@ -29,25 +29,11 @@ void logging::Initialize() } #endif -void logging::Info(const char *fmt, ...) +void Log(std::unique_ptr &result, bool file_only) { #if ENABLE_LOGGING - if (shut_down) - return; - if (!handle.is_open()) + if (!logging::handle.is_open()) logging::Initialize(); - // Argument list - va_list list; - va_start(list, fmt); - // Allocate buffer - auto result = std::make_unique(512); - // Fill buffer - if (vsnprintf(result.get(), 512, fmt, list) < 0) - return; - va_end(list); - - std::string print_file(result.get()); - time_t current_time; struct tm *time_info = nullptr; char timeString[10]; @@ -62,13 +48,51 @@ void logging::Info(const char *fmt, ...) #if ENABLE_VISUALS if (!hack::shutdown) { - if (*log_to_console) + if (!file_only && *log_to_console) g_ICvar->ConsoleColorPrintf(MENU_COLOR, "CAT: %s\n", result.get()); } #endif #endif } +void logging::Info(const char *fmt, ...) +{ +#if ENABLE_LOGGING + if (shut_down) + return; + // Argument list + va_list list; + va_start(list, fmt); + // Allocate buffer + auto result = std::make_unique(512); + // Fill buffer + if (vsnprintf(result.get(), 512, fmt, list) < 0) + return; + va_end(list); + + Log(result, false); +#endif +} + +void logging::File(const char *fmt, ...) +{ +#if ENABLE_LOGGING + if (shut_down) + return; + // Argument list + va_list list; + va_start(list, fmt); + // Allocate buffer + auto result = std::make_unique(512); + // Fill buffer + if (vsnprintf(result.get(), 512, fmt, list) < 0) + return; + va_end(list); + + Log(result, true); +#endif +} + void logging::Shutdown() { #if ENABLE_LOGGING