Add logging::File

This commit is contained in:
TotallyNotElite 2019-10-23 21:15:47 +02:00
parent d4d9d5fa48
commit 2938da31d2
2 changed files with 46 additions and 27 deletions

View File

@ -10,17 +10,12 @@
#include <stdio.h> #include <stdio.h>
#include <fstream> #include <fstream>
#ifdef __cplusplus
namespace logging namespace logging
{ {
#endif
extern std::ofstream handle; extern std::ofstream handle;
void Initialize(); void Initialize();
void Shutdown(); void Shutdown();
void Info(const char *fmt, ...); void Info(const char *fmt, ...);
void File(const char *fmt, ...);
#ifdef __cplusplus
} }
#endif

View File

@ -6,14 +6,14 @@
*/ */
#include <stdarg.h> #include <stdarg.h>
#include <string.h> #include <string>
#include <pwd.h> #include <pwd.h>
#include <settings/Bool.hpp> #include <settings/Bool.hpp>
#include "logging.hpp"
#include "common.hpp" #include "helpers.hpp"
#include "hack.hpp"
#include "MiscTemporary.hpp" #include "MiscTemporary.hpp"
#include "hack.hpp"
static settings::Boolean log_to_console{ "hack.log-console", "false" }; static settings::Boolean log_to_console{ "hack.log-console", "false" };
@ -29,25 +29,11 @@ void logging::Initialize()
} }
#endif #endif
void logging::Info(const char *fmt, ...) void Log(std::unique_ptr<char[]> &result, bool file_only)
{ {
#if ENABLE_LOGGING #if ENABLE_LOGGING
if (shut_down) if (!logging::handle.is_open())
return;
if (!handle.is_open())
logging::Initialize(); logging::Initialize();
// Argument list
va_list list;
va_start(list, fmt);
// Allocate buffer
auto result = std::make_unique<char[]>(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; time_t current_time;
struct tm *time_info = nullptr; struct tm *time_info = nullptr;
char timeString[10]; char timeString[10];
@ -62,13 +48,51 @@ void logging::Info(const char *fmt, ...)
#if ENABLE_VISUALS #if ENABLE_VISUALS
if (!hack::shutdown) if (!hack::shutdown)
{ {
if (*log_to_console) if (!file_only && *log_to_console)
g_ICvar->ConsoleColorPrintf(MENU_COLOR, "CAT: %s\n", result.get()); g_ICvar->ConsoleColorPrintf(MENU_COLOR, "CAT: %s\n", result.get());
} }
#endif #endif
#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<char[]>(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<char[]>(512);
// Fill buffer
if (vsnprintf(result.get(), 512, fmt, list) < 0)
return;
va_end(list);
Log(result, true);
#endif
}
void logging::Shutdown() void logging::Shutdown()
{ {
#if ENABLE_LOGGING #if ENABLE_LOGGING