[Logging] Simplify Log Settings Initialization (#1394)

* Simplify logging loading

* Fix log injections and reduce verbosity
This commit is contained in:
Chris Miles
2021-06-13 18:06:43 -05:00
committed by GitHub
parent 0e4361955d
commit bcb0e43d13
21 changed files with 154 additions and 481 deletions
-89
View File
@@ -2191,95 +2191,6 @@ uint32 Database::GetRaidIDByCharID(uint32 character_id) {
return 0;
}
/**
* @param log_settings
*/
void Database::LoadLogSettings(EQEmuLogSys::LogSettings *log_settings)
{
std::string query =
"SELECT "
"log_category_id, "
"log_category_description, "
"log_to_console, "
"log_to_file, "
"log_to_gmsay "
"FROM "
"logsys_categories "
"ORDER BY log_category_id";
auto results = QueryDatabase(query);
int log_category_id = 0;
int *categories_in_database = new int[1000];
for (auto row = results.begin(); row != results.end(); ++row) {
log_category_id = atoi(row[0]);
if (log_category_id <= Logs::None || log_category_id >= Logs::MaxCategoryID) {
continue;
}
log_settings[log_category_id].log_to_console = static_cast<uint8>(atoi(row[2]));
log_settings[log_category_id].log_to_file = static_cast<uint8>(atoi(row[3]));
log_settings[log_category_id].log_to_gmsay = static_cast<uint8>(atoi(row[4]));
/**
* Determine if any output method is enabled for the category
* and set it to 1 so it can used to check if category is enabled
*/
const bool log_to_console = log_settings[log_category_id].log_to_console > 0;
const bool log_to_file = log_settings[log_category_id].log_to_file > 0;
const bool log_to_gmsay = log_settings[log_category_id].log_to_gmsay > 0;
const bool is_category_enabled = log_to_console || log_to_file || log_to_gmsay;
if (is_category_enabled) {
log_settings[log_category_id].is_category_enabled = 1;
}
/**
* This determines whether or not the process needs to actually file log anything.
* If we go through this whole loop and nothing is set to any debug level, there is no point to create a file or keep anything open
*/
if (log_settings[log_category_id].log_to_file > 0) {
LogSys.file_logs_enabled = true;
}
categories_in_database[log_category_id] = 1;
}
/**
* Auto inject categories that don't exist in the database...
*/
for (int log_index = Logs::AA; log_index != Logs::MaxCategoryID; log_index++) {
if (categories_in_database[log_index] != 1) {
LogInfo(
"New Log Category [{0}] doesn't exist... Automatically adding to [logsys_categories] table...",
Logs::LogCategoryName[log_index]
);
auto inject_query = fmt::format(
"INSERT INTO logsys_categories "
"(log_category_id, "
"log_category_description, "
"log_to_console, "
"log_to_file, "
"log_to_gmsay) "
"VALUES "
"({0}, '{1}', {2}, {3}, {4})",
log_index,
EscapeString(Logs::LogCategoryName[log_index]),
std::to_string(log_settings[log_index].log_to_console),
std::to_string(log_settings[log_index].log_to_file),
std::to_string(log_settings[log_index].log_to_gmsay)
);
QueryDatabase(inject_query);
}
}
delete[] categories_in_database;
}
int Database::CountInvSnapshots() {
std::string query = StringFormat("SELECT COUNT(*) FROM (SELECT * FROM `inventory_snapshots` a GROUP BY `charid`, `time_index`) b");
auto results = QueryDatabase(query);
+1 -2
View File
@@ -78,6 +78,7 @@ class PTimerList;
#define SQL(...) #__VA_ARGS__
class LogSettings;
class Database : public DBcore {
public:
Database();
@@ -268,8 +269,6 @@ public:
int CountInvSnapshots();
void ClearInvSnapshots(bool from_now = false);
/* EQEmuLogSys */
void LoadLogSettings(EQEmuLogSys::LogSettings* log_settings);
private:
+78 -2
View File
@@ -22,8 +22,8 @@
#include "rulesys.h"
#include "platform.h"
#include "string_util.h"
#include "database.h"
#include "misc.h"
#include "repositories/logsys_categories_repository.h"
#include <iostream>
#include <fstream>
@@ -31,6 +31,7 @@
#include <iomanip>
#include <time.h>
#include <sys/stat.h>
#include <algorithm>
std::ofstream process_log;
@@ -96,7 +97,7 @@ EQEmuLogSys::EQEmuLogSys()
*/
EQEmuLogSys::~EQEmuLogSys() = default;
void EQEmuLogSys::LoadLogSettingsDefaults()
EQEmuLogSys *EQEmuLogSys::LoadLogSettingsDefaults()
{
/**
* Get Executable platform currently running this code (Zone/World/etc)
@@ -177,6 +178,8 @@ void EQEmuLogSys::LoadLogSettingsDefaults()
else if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformHC) {
platform_file_name = "hc";
}
return this;
}
/**
@@ -601,3 +604,76 @@ void EQEmuLogSys::EnableConsoleLogging()
log_settings[log_index].is_category_enabled = 1;
}
}
EQEmuLogSys *EQEmuLogSys::LoadLogDatabaseSettings()
{
auto categories = LogsysCategoriesRepository::GetWhere(
*m_database,
"TRUE ORDER BY log_category_id"
);
// keep track of categories
std::vector<int> db_categories{};
db_categories.reserve(categories.size());
// loop through database categories
for (auto &c: categories) {
if (c.log_category_id <= Logs::None || c.log_category_id >= Logs::MaxCategoryID) {
continue;
}
log_settings[c.log_category_id].log_to_console = static_cast<uint8>(c.log_to_console);
log_settings[c.log_category_id].log_to_file = static_cast<uint8>(c.log_to_file);
log_settings[c.log_category_id].log_to_gmsay = static_cast<uint8>(c.log_to_gmsay);
// Determine if any output method is enabled for the category
// and set it to 1 so it can used to check if category is enabled
const bool log_to_console = log_settings[c.log_category_id].log_to_console > 0;
const bool log_to_file = log_settings[c.log_category_id].log_to_file > 0;
const bool log_to_gmsay = log_settings[c.log_category_id].log_to_gmsay > 0;
const bool is_category_enabled = log_to_console || log_to_file || log_to_gmsay;
if (is_category_enabled) {
log_settings[c.log_category_id].is_category_enabled = 1;
}
// This determines whether or not the process needs to actually file log anything.
// If we go through this whole loop and nothing is set to any debug level, there
// is no point to create a file or keep anything open
if (log_settings[c.log_category_id].log_to_file > 0) {
LogSys.file_logs_enabled = true;
}
db_categories.emplace_back(c.log_category_id);
}
// Auto inject categories that don't exist in the database...
for (int i = Logs::AA; i != Logs::MaxCategoryID; i++) {
if (std::find(db_categories.begin(), db_categories.end(), i) == db_categories.end()) {
LogInfo(
"Automatically adding new log category [{0}]",
Logs::LogCategoryName[i]
);
auto new_category = LogsysCategoriesRepository::NewEntity();
new_category.log_category_id = i;
new_category.log_category_description = EscapeString(Logs::LogCategoryName[i]);
new_category.log_to_console = log_settings[i].log_to_console;
new_category.log_to_gmsay = log_settings[i].log_to_gmsay;
new_category.log_to_file = log_settings[i].log_to_file;
LogsysCategoriesRepository::InsertOne(*m_database, new_category);
}
}
LogInfo("Loaded [{}] log categories", categories.size());
return this;
}
EQEmuLogSys *EQEmuLogSys::SetDatabase(Database *db)
{
m_database = db;
return this;
}
+35 -79
View File
@@ -23,8 +23,9 @@
#include <iostream>
#include <fstream>
#include <stdio.h>
#include <cstdio>
#include <functional>
#include <algorithm>
#ifdef _WIN32
#ifdef utf16_to_utf8
@@ -37,9 +38,9 @@
namespace Logs {
enum DebugLevel {
General = 1, /* 1 - Low-Level general debugging, useful info on single line */
Moderate, /* 2 - Informational based, used in functions, when particular things load */
Detail /* 3 - Use this for extreme detail in logging, usually in extreme debugging in the stack or interprocess communication */
General = 1, // 1 - Low-Level general debugging, useful info on single line
Moderate, // 2 - Informational based, used in functions, when particular things load
Detail // 3 - Use this for extreme detail in logging, usually in extreme debugging in the stack or interprocess communication
};
/**
@@ -127,7 +128,7 @@ namespace Logs {
/**
* If you add to this, make sure you update LogCategory
*/
static const char* LogCategoryName[LogCategory::MaxCategoryID] = {
static const char *LogCategoryName[LogCategory::MaxCategoryID] = {
"",
"AA",
"AI",
@@ -206,6 +207,8 @@ namespace Logs {
#include "eqemu_logsys_log_aliases.h"
class Database;
class EQEmuLogSys {
public:
EQEmuLogSys();
@@ -216,7 +219,8 @@ public:
* This should be handled on deconstructor but to be safe we use it anyways.
*/
void CloseFileLogs();
void LoadLogSettingsDefaults();
EQEmuLogSys *LoadLogSettingsDefaults();
EQEmuLogSys *LoadLogDatabaseSettings();
/**
* @param directory_name
@@ -246,7 +250,7 @@ public:
* Used in file logs to prepend a timestamp entry for logs
* @param time_stamp
*/
void SetCurrentTimeStamp(char* time_stamp);
void SetCurrentTimeStamp(char *time_stamp);
/**
* @param log_name
@@ -273,101 +277,53 @@ public:
/**
* Internally used memory reference for all log settings per category
* These are loaded via DB and have defaults loaded in LoadLogSettingsDefaults
* Database loaded via Database::LoadLogSettings(log_settings)
* Database loaded via LogSys.SetDatabase(&database)->LoadLogDatabaseSettings();
*/
LogSettings log_settings[Logs::LogCategory::MaxCategoryID]{};
bool file_logs_enabled = false;
/**
* Sets Executable platform (Zone/World/UCS) etc.
*/
int log_platform = 0;
int log_platform = 0;
std::string platform_file_name;
/**
* File name used in writing logs
*/
std::string platform_file_name;
/**
* GMSay Client Message colors mapped by category
*
* @param log_category
* @return
*/
// gmsay
uint16 GetGMSayColorFromCategory(uint16 log_category);
/**
* @param f
*/
void SetGMSayHandler(std::function<void(uint16 log_type, const std::string&)> f) { on_log_gmsay_hook = f; }
EQEmuLogSys * SetGMSayHandler(std::function<void(uint16 log_type, const std::string &)> f) {
on_log_gmsay_hook = f;
return this;
}
/**
* @param f
*/
void SetConsoleHandler(std::function<void(uint16 debug_level, uint16 log_type, const std::string&)> f) { on_log_console_hook = f; }
/**
* Silence console logging
*/
// console
void SetConsoleHandler(
std::function<void(
uint16 debug_level,
uint16 log_type,
const std::string &
)> f
) { on_log_console_hook = f; }
void SilenceConsoleLogging();
/**
* Turn on all console logging
*/
void EnableConsoleLogging();
// database
EQEmuLogSys *SetDatabase(Database *db);
private:
/**
* Callback pointer to zone process for hooking logs to zone using GMSay
*/
std::function<void(uint16 log_category, const std::string&)> on_log_gmsay_hook;
std::function<void(uint16 debug_level, uint16 log_category, const std::string&)> on_log_console_hook;
// reference to database
Database *m_database;
std::function<void(uint16 log_category, const std::string &)> on_log_gmsay_hook;
std::function<void(uint16 debug_level, uint16 log_category, const std::string &)> on_log_console_hook;
/**
* Formats log messages like '[Category] This is a log message'
*/
std::string FormatOutMessageString(uint16 log_category, const std::string &in_message);
/**
* Linux console color messages mapped by category
*
* @param log_category
* @return
*/
std::string GetLinuxConsoleColorFromCategory(uint16 log_category);
/**
* Windows console color messages mapped by category
*/
uint16 GetWindowsConsoleColorFromCategory(uint16 log_category);
/**
* @param debug_level
* @param log_category
* @param message
*/
void ProcessConsoleMessage(uint16 debug_level, uint16 log_category, const std::string &message);
/**
* @param debug_level
* @param log_category
* @param message
*/
void ProcessGMSay(uint16 debug_level, uint16 log_category, const std::string &message);
/**
* @param debug_level
* @param log_category
* @param message
*/
void ProcessLogWrite(uint16 debug_level, uint16 log_category, const std::string &message);
/**
* @param log_category
* @return
*/
bool IsRfc5424LogCategory(uint16 log_category);
};
@@ -4,7 +4,7 @@
* This repository was automatically generated and is NOT to be modified directly.
* Any repository modifications are meant to be made to the repository extending the base.
* Any modifications to base repositories are to be made by the generator only
*
*
* @generator ./utils/scripts/generators/repository-generator.pl
* @docs https://eqemu.gitbook.io/server/in-development/developer-area/repositories
*/