Minor tweaking to new logsys

This commit is contained in:
Michael Cook (mackal) 2015-01-23 15:48:20 -05:00
parent 451983ffc9
commit 06ff4823d3
3 changed files with 75 additions and 79 deletions

View File

@ -24,10 +24,11 @@
#include "misc.h"
#include <iostream>
#include <fstream>
#include <fstream>
#include <string>
#include <iomanip>
#include <time.h>
#include <sys/stat.h>
std::ofstream process_log;
@ -75,13 +76,15 @@ namespace Console {
};
}
EQEmuLogSys::EQEmuLogSys(){
on_log_gmsay_hook = [](uint16 log_type, std::string&) {};
bool file_logs_enabled = false;
EQEmuLogSys::EQEmuLogSys()
{
on_log_gmsay_hook = [](uint16 log_type, const std::string&) {};
bool file_logs_enabled = false;
int log_platform = 0;
}
EQEmuLogSys::~EQEmuLogSys(){
EQEmuLogSys::~EQEmuLogSys()
{
}
void EQEmuLogSys::LoadLogSettingsDefaults()
@ -90,11 +93,7 @@ void EQEmuLogSys::LoadLogSettingsDefaults()
log_platform = GetExecutablePlatformInt();
/* Zero out Array */
for (int i = 0; i < Logs::LogCategory::MaxCategoryID; i++){
log_settings[i].log_to_console = 0;
log_settings[i].log_to_file = 0;
log_settings[i].log_to_gmsay = 0;
}
memset(log_settings, 0, sizeof(LogSettings) * Logs::LogCategory::MaxCategoryID);
/* Set Defaults */
log_settings[Logs::World_Server].log_to_console = Logs::General;
@ -104,38 +103,32 @@ void EQEmuLogSys::LoadLogSettingsDefaults()
log_settings[Logs::Crash].log_to_console = Logs::General;
log_settings[Logs::MySQLError].log_to_console = Logs::General;
/* Declare process file names for log writing
/* Declare process file names for log writing
If there is no process_file_name declared, no log file will be written, simply
*/
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformWorld){
platform_file_name = "world";
}
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformQueryServ){
platform_file_name = "query_server";
}
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformZone){
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformWorld)
platform_file_name = "world";
else if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformQueryServ)
platform_file_name = "query_server";
else if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformZone)
platform_file_name = "zone";
}
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformUCS){
else if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformUCS)
platform_file_name = "ucs";
}
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformLogin){
else if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformLogin)
platform_file_name = "login";
}
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformLogin){
else if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformLogin)
platform_file_name = "launcher";
}
}
std::string EQEmuLogSys::FormatOutMessageString(uint16 log_category, std::string in_message){
std::string category_string = "";
if (log_category > 0 && Logs::LogCategoryName[log_category]){
std::string EQEmuLogSys::FormatOutMessageString(uint16 log_category, const std::string &in_message)
{
std::string category_string;
if (log_category > 0 && Logs::LogCategoryName[log_category])
category_string = StringFormat("[%s] ", Logs::LogCategoryName[log_category]);
}
return StringFormat("%s%s", category_string.c_str(), in_message.c_str());
return StringFormat("%s%s", category_string.c_str(), in_message.c_str());
}
void EQEmuLogSys::ProcessGMSay(uint16 debug_level, uint16 log_category, std::string message)
void EQEmuLogSys::ProcessGMSay(uint16 debug_level, uint16 log_category, const std::string &message)
{
/* Check if category enabled for process */
if (log_settings[log_category].log_to_gmsay == 0)
@ -150,17 +143,16 @@ void EQEmuLogSys::ProcessGMSay(uint16 debug_level, uint16 log_category, std::str
return;
/* Check to see if the process that actually ran this is zone */
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformZone){
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformZone)
on_log_gmsay_hook(log_category, message);
}
}
void EQEmuLogSys::ProcessLogWrite(uint16 debug_level, uint16 log_category, std::string message)
void EQEmuLogSys::ProcessLogWrite(uint16 debug_level, uint16 log_category, const std::string &message)
{
if (log_category == Logs::Crash){
if (log_category == Logs::Crash) {
char time_stamp[80];
EQEmuLogSys::SetCurrentTimeStamp(time_stamp);
std::ofstream crash_log;
std::ofstream crash_log;
EQEmuLogSys::MakeDirectory("logs/crashes");
crash_log.open(StringFormat("logs/crashes/crash_%s_%i.log", platform_file_name.c_str(), getpid()), std::ios_base::app | std::ios_base::out);
crash_log << time_stamp << " " << message << "\n";
@ -178,9 +170,8 @@ void EQEmuLogSys::ProcessLogWrite(uint16 debug_level, uint16 log_category, std::
char time_stamp[80];
EQEmuLogSys::SetCurrentTimeStamp(time_stamp);
if (process_log){
if (process_log)
process_log << time_stamp << " " << message << std::endl;
}
}
uint16 EQEmuLogSys::GetWindowsConsoleColorFromCategory(uint16 log_category){
@ -198,7 +189,7 @@ uint16 EQEmuLogSys::GetWindowsConsoleColorFromCategory(uint16 log_category){
return Console::Color::LightCyan;
case Logs::Commands:
return Console::Color::LightMagenta;
case Logs::Crash:
case Logs::Crash:
return Console::Color::LightRed;
default:
return Console::Color::Yellow;
@ -239,7 +230,7 @@ uint16 EQEmuLogSys::GetGMSayColorFromCategory(uint16 log_category){
case Logs::Debug:
return 14; /* Light Green */
case Logs::Quests:
return 258; /* Light Cyan */
return 258; /* Light Cyan */
case Logs::Commands:
return 5; /* Light Purple */
case Logs::Crash:
@ -249,7 +240,7 @@ uint16 EQEmuLogSys::GetGMSayColorFromCategory(uint16 log_category){
}
}
void EQEmuLogSys::ProcessConsoleMessage(uint16 debug_level, uint16 log_category, std::string message)
void EQEmuLogSys::ProcessConsoleMessage(uint16 debug_level, uint16 log_category, const std::string &message)
{
/* Check if category enabled for process */
if (log_settings[log_category].log_to_console == 0)
@ -290,7 +281,8 @@ void EQEmuLogSys::Out(Logs::DebugLevel debug_level, uint16 log_category, std::st
EQEmuLogSys::ProcessLogWrite(debug_level, log_category, output_message);
}
void EQEmuLogSys::SetCurrentTimeStamp(char* time_stamp){
void EQEmuLogSys::SetCurrentTimeStamp(char* time_stamp)
{
time_t raw_time;
struct tm * time_info;
time(&raw_time);
@ -298,10 +290,17 @@ void EQEmuLogSys::SetCurrentTimeStamp(char* time_stamp){
strftime(time_stamp, 80, "[%m-%d-%Y :: %H:%M:%S]", time_info);
}
void EQEmuLogSys::MakeDirectory(std::string directory_name){
void EQEmuLogSys::MakeDirectory(const std::string &directory_name)
{
#ifdef _WINDOWS
struct _stat st;
if (_stat(directory_name.c_str(), &st) == 0) // exists
return;
_mkdir(directory_name.c_str());
#else
struct stat st;
if (stat(directory_name.c_str(), &st) == 0) // exists
return;
mkdir(directory_name.c_str(), 0755);
#endif
}
@ -313,7 +312,7 @@ void EQEmuLogSys::CloseFileLogs()
}
}
void EQEmuLogSys::StartFileLogs(std::string log_name)
void EQEmuLogSys::StartFileLogs(const std::string &log_name)
{
EQEmuLogSys::CloseFileLogs();
@ -321,23 +320,21 @@ void EQEmuLogSys::StartFileLogs(std::string log_name)
if (file_logs_enabled == false)
return;
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformZone){
if (log_name != "")
if (EQEmuLogSys::log_platform == EQEmuExePlatform::ExePlatformZone) {
if (!log_name.empty())
platform_file_name = log_name;
if (platform_file_name == ""){
if (platform_file_name.empty())
return;
}
EQEmuLogSys::Out(Logs::General, Logs::Status, "Starting File Log 'logs/%s_%i.log'", platform_file_name.c_str(), getpid());
EQEmuLogSys::MakeDirectory("logs/zone");
process_log.open(StringFormat("logs/zone/%s_%i.log", platform_file_name.c_str(), getpid()), std::ios_base::app | std::ios_base::out);
}
else{
if (platform_file_name == ""){
} else {
if (platform_file_name.empty())
return;
}
EQEmuLogSys::Out(Logs::General, Logs::Status, "Starting File Log 'logs/%s_%i.txt'", platform_file_name.c_str(), getpid());
process_log.open(StringFormat("logs/%s_%i.txt", platform_file_name.c_str(), getpid()), std::ios_base::app | std::ios_base::out);
EQEmuLogSys::Out(Logs::General, Logs::Status, "Starting File Log 'logs/%s_%i.log'", platform_file_name.c_str(), getpid());
process_log.open(StringFormat("logs/%s_%i.log", platform_file_name.c_str(), getpid()), std::ios_base::app | std::ios_base::out);
}
}
}

View File

@ -20,7 +20,7 @@
#define EQEMU_LOGSYS_H
#include <iostream>
#include <fstream>
#include <fstream>
#include <stdio.h>
#include <functional>
@ -118,7 +118,7 @@ namespace Logs{
"Zone Server",
"MySQL Error",
"MySQL Query",
};
};
}
class EQEmuLogSys {
@ -128,23 +128,23 @@ public:
void CloseFileLogs(); /* Close File Logs wherever necessary, either at zone shutdown or entire process shutdown for everything else. This should be handled on deconstructor but to be safe we use it anyways. */
void LoadLogSettingsDefaults(); /* Initializes log_settings and sets some defaults if DB is not present */
void MakeDirectory(std::string directory_name); /* Platform independent way of performing a MakeDirectory based on name */
/*
void MakeDirectory(const std::string &directory_name); /* Platform independent way of performing a MakeDirectory based on name */
/*
The one and only Logging function that uses a debug level as a parameter, as well as a log_category
log_category - defined in Logs::LogCategory::[]
log_category name resolution works by passing the enum int ID to Logs::LogCategoryName[category_id]
Example: EQEmuLogSys::Out(Logs::General, Logs::Guilds, "This guild has no leader present");
- This would pipe the same category and debug level to all output formats, but the internal memory reference of log_settings would
- This would pipe the same category and debug level to all output formats, but the internal memory reference of log_settings would
be checked against to see if that piped output is set to actually process it for the category and debug level
*/
void Out(Logs::DebugLevel debug_level, uint16 log_category, std::string message, ...);
void Out(Logs::DebugLevel debug_level, uint16 log_category, std::string message, ...);
void SetCurrentTimeStamp(char* time_stamp); /* Used in file logs to prepend a timestamp entry for logs */
void StartFileLogs(std::string log_name = ""); /* Used to declare the processes file log and to keep it open for later use */
void StartFileLogs(const std::string &log_name = ""); /* Used to declare the processes file log and to keep it open for later use */
/*
/*
LogSettings Struct
This struct is the master reference for all settings for each category, and for each output
log_to_file[category_id] = [1-3] - Sets debug level for category to output to file
@ -162,31 +162,30 @@ public:
These are loaded via DB and have defaults loaded in LoadLogSettingsDefaults.
Database loaded via Database::LoadLogSettings(log_settings)
*/
LogSettings log_settings[Logs::LogCategory::MaxCategoryID];
LogSettings log_settings[Logs::LogCategory::MaxCategoryID];
bool file_logs_enabled; /* Set when log settings are loaded to determine if keeping a file open is necessary */
int log_platform; /* Sets Executable platform (Zone/World/UCS) etc. */
std::string platform_file_name; /* File name used in writing logs */
std::string platform_file_name; /* File name used in writing logs */
uint16 GetGMSayColorFromCategory(uint16 log_category); /* GMSay Client Message colors mapped by category */
void OnLogHookCallBackZone(std::function<void(uint16 log_type, std::string&)> f) { on_log_gmsay_hook = f; }
void OnLogHookCallBackZone(std::function<void(uint16 log_type, const std::string&)> f) { on_log_gmsay_hook = f; }
private:
std::function<void(uint16 log_category, std::string&)> on_log_gmsay_hook; /* Callback pointer to zone process for hooking logs to zone using GMSay */
std::string FormatOutMessageString(uint16 log_category, std::string in_message); /* Formats log messages like '[Category] This is a log message' */
std::function<void(uint16 log_category, const std::string&)> on_log_gmsay_hook; /* Callback pointer to zone process for hooking logs to zone using GMSay */
std::string FormatOutMessageString(uint16 log_category, const std::string &in_message); /* Formats log messages like '[Category] This is a log message' */
std::string GetLinuxConsoleColorFromCategory(uint16 log_category); /* Linux console color messages mapped by category */
uint16 GetWindowsConsoleColorFromCategory(uint16 log_category); /* Windows console color messages mapped by category */
void ProcessConsoleMessage(uint16 debug_level, uint16 log_category, std::string message); /* ProcessConsoleMessage called via Log.Out */
void ProcessGMSay(uint16 debug_level, uint16 log_category, std::string message); /* ProcessGMSay called via Log.Out */
void ProcessLogWrite(uint16 debug_level, uint16 log_category, std::string message); /* ProcessLogWrite called via Log.Out */
void ProcessConsoleMessage(uint16 debug_level, uint16 log_category, const std::string &message); /* ProcessConsoleMessage called via Log.Out */
void ProcessGMSay(uint16 debug_level, uint16 log_category, const std::string &message); /* ProcessGMSay called via Log.Out */
void ProcessLogWrite(uint16 debug_level, uint16 log_category, const std::string &message); /* ProcessLogWrite called via Log.Out */
};
extern EQEmuLogSys Log;
#endif
#endif

View File

@ -258,15 +258,15 @@ public:
LinkedList<NPC_Emote_Struct*> NPCEmoteList;
void LoadTickItems();
uint32 GetSpawnKillCount(uint32 in_spawnid);
void UpdateHotzone();
void LoadTickItems();
uint32 GetSpawnKillCount(uint32 in_spawnid);
void UpdateHotzone();
std::unordered_map<int, item_tick_struct> tick_items;
// random object that provides random values for the zone
EQEmu::Random random;
static void GMSayHookCallBackProcess(uint16 log_category, std::string& message){ entity_list.MessageStatus(0, 80, Log.GetGMSayColorFromCategory(log_category), "%s", message.c_str()); }
static void GMSayHookCallBackProcess(uint16 log_category, const std::string& message){ entity_list.MessageStatus(0, 80, Log.GetGMSayColorFromCategory(log_category), "%s", message.c_str()); }
//MODDING HOOKS
void mod_init();