Added a LogDebug

- Added debug logging
- Created vLog, a root function for all log functions
- Placed failed to load script log under this new LogDebug function
- Updated included config functions
This commit is contained in:
Jett 2021-12-11 12:29:34 +00:00
parent bd609dfc90
commit 74742771c4
11 changed files with 72 additions and 76 deletions

View File

@ -45,6 +45,7 @@ int main(int argc, char** argv) {
dConfig config("authconfig.ini");
Game::config = &config;
Game::logger->SetLogToConsole(bool(std::stoi(config.GetValue("log_to_console"))));
Game::logger->SetLogDebugStatements(bool(std::stoi(config.GetValue("log_debug_statements"))));
//Connect to the MySQL Database
std::string mysql_host = config.GetValue("mysql_host");
@ -152,11 +153,13 @@ int main(int argc, char** argv) {
dLogger * SetupLogger() {
std::string logPath = "./logs/AuthServer_" + std::to_string(time(nullptr)) + ".log";
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif
return new dLogger(logPath, logToConsole);
return new dLogger(logPath, logToConsole, logDebugStatements);
}
void HandlePacket(Packet* packet) {

View File

@ -48,6 +48,7 @@ int main(int argc, char** argv) {
dConfig config("chatconfig.ini");
Game::config = &config;
Game::logger->SetLogToConsole(bool(std::stoi(config.GetValue("log_to_console"))));
Game::logger->SetLogDebugStatements(bool(std::stoi(config.GetValue("log_debug_statements"))));
//Connect to the MySQL Database
std::string mysql_host = config.GetValue("mysql_host");
@ -159,11 +160,13 @@ int main(int argc, char** argv) {
dLogger * SetupLogger() {
std::string logPath = "./logs/ChatServer_" + std::to_string(time(nullptr)) + ".log";
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif
return new dLogger(logPath, logToConsole);
return new dLogger(logPath, logToConsole, logDebugStatements);
}
void HandlePacket(Packet* packet) {

View File

@ -1,7 +1,8 @@
#include "dLogger.h"
dLogger::dLogger(const std::string& outpath, bool logToConsole) {
dLogger::dLogger(const std::string& outpath, bool logToConsole, bool logDebugStatements) {
m_logToConsole = logToConsole;
m_logDebugStatements = logDebugStatements;
m_outpath = outpath;
#ifdef _WIN32
@ -24,39 +25,26 @@ dLogger::~dLogger() {
#endif
}
void dLogger::LogBasic(const std::string & message) {
LogBasic(message.c_str());
}
void dLogger::LogBasic(const char * format, ...) {
void dLogger::vLog(const char* format, va_list args) {
#ifdef _WIN32
time_t t = time(NULL);
struct tm time;
localtime_s(&time, &t);
char timeStr[70];
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", &time);
char message[2048];
va_list args;
va_start(args, format);
vsprintf_s(message, format, args);
va_end(args);
if (m_logToConsole) std::cout << "[" << "time machine broke" << "] " << message;
mFile << "[" << "time" << "] " << message;
if (m_logToConsole) std::cout << "[" << timeStr << "] " << message;
mFile << "[" << timeStr << "] " << message;
#else
time_t t = time(NULL);
struct tm * time = localtime(&t);
struct tm time;
localtime_s(&time, &t);
char timeStr[70];
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", time);
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", &time);
char message[2048];
va_list args;
va_start(args, format);
vsprintf(message, format, args);
va_end(args);
if (m_logToConsole) {
fputs("[", stdout);
@ -76,62 +64,42 @@ void dLogger::LogBasic(const char * format, ...) {
#endif
}
void dLogger::LogBasic(const char * format, ...) {
va_list args;
va_start(args, format);
vLog(format, args);
va_end(args);
}
void dLogger::LogBasic(const std::string & message) {
LogBasic(message.c_str());
}
void dLogger::Log(const char * className, const char * format, ...) {
#ifdef _WIN32
time_t t = time(NULL);
struct tm time;
localtime_s(&time, &t);
char timeStr[70];
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", &time);
char message[2048];
va_list args;
std::string log = "[" + std::string(className) + "] " + std::string(format);
va_start(args, format);
vsprintf_s(message, format, args);
vLog(log.c_str(), args);
va_end(args);
if (m_logToConsole) std::cout << "[" << timeStr << "] [" << className << "]: " << message;
mFile << "[" << timeStr << "] [" << className << "]: " << message;
#else
time_t t = time(NULL);
struct tm * time = localtime(&t);
char timeStr[70];
strftime(timeStr, sizeof(timeStr), "%d-%m-%y %H:%M:%S", time);
char message[2048];
va_list args;
va_start(args, format);
vsprintf(message, format, args);
va_end(args);
if (m_logToConsole) {
fputs("[", stdout);
fputs(timeStr, stdout);
fputs("] ", stdout);
fputs("[", stdout);
fputs(className, stdout);
fputs("]: ", stdout);
fputs(message, stdout);
}
if (fp != NULL) {
fputs("[", fp);
fputs(timeStr, fp);
fputs("] ", fp);
fputs("[", fp);
fputs(className, fp);
fputs("]: ", fp);
fputs(message, fp);
}
#endif
}
void dLogger::Log(const std::string & className, const std::string & message) {
Log(className.c_str(), message.c_str());
}
void dLogger::LogDebug(const char * className, const char * format, ...) {
if (!m_logDebugStatements) return;
va_list args;
std::string log = "[" + std::string(className) + "] " + std::string(format);
va_start(args, format);
vLog(log.c_str(), args);
va_end(args);
}
void dLogger::LogDebug(const std::string & className, const std::string & message) {
LogDebug(className.c_str(), message.c_str());
}
void dLogger::Flush() {
#ifdef _WIN32
mFile.flush();

View File

@ -7,21 +7,26 @@
class dLogger {
public:
dLogger(const std::string& outpath, bool logToConsole);
dLogger(const std::string& outpath, bool logToConsole, bool logDebugStatements);
~dLogger();
void SetLogToConsole(bool logToConsole) { m_logToConsole = logToConsole; }
void SetLogDebugStatements(bool logDebugStatements) { m_logDebugStatements = logDebugStatements; }
void vLog(const char* format, va_list args);
void LogBasic(const std::string& message);
void LogBasic(const char* format, ...);
void Log(const char* className, const char* format, ...);
void Log(const std::string& className, const std::string& message);
void LogDebug(const std::string& className, const std::string& message);
void LogDebug(const char* className, const char* format, ...);
void Flush();
const bool GetIsLoggingToConsole() const { return m_logToConsole; }
private:
bool m_logDebugStatements;
bool m_logToConsole;
std::string m_outpath;
std::ofstream mFile;

View File

@ -72,6 +72,7 @@ int main(int argc, char** argv) {
dConfig config("masterconfig.ini");
Game::config = &config;
Game::logger->SetLogToConsole(bool(std::stoi(config.GetValue("log_to_console"))));
Game::logger->SetLogDebugStatements(bool(std::stoi(config.GetValue("log_debug_statements"))));
//Connect to CDClient
try {
@ -320,11 +321,13 @@ dLogger* SetupLogger() {
std::string logPath =
"./logs/MasterServer_" + std::to_string(time(nullptr)) + ".log";
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif
return new dLogger(logPath, logToConsole);
return new dLogger(logPath, logToConsole, logDebugStatements);
}
void HandlePacket(Packet* packet) {

View File

@ -781,11 +781,10 @@ CppScripts::Script* CppScripts::GetScript(Entity* parent, const std::string& scr
script = invalidToReturn;
else if (scriptName == "scripts\\02_server\\Enemy\\General\\L_BASE_ENEMY_SPIDERLING.lua")
script = invalidToReturn;
else if (scriptName == "scripts\\ai\\AG\\L_AG_SHIP_SHAKE.lua")
script = invalidToReturn; //Set ship shake to not log it is missing, it is implemented in AgSpaceStuff
else if (script == invalidToReturn) {
if (scriptName.length() > 0)
Game::logger->Log("CppScripts", "Attempted to load CppScript for '" + scriptName + "', but returned InvalidScript.\n");
Game::logger->LogDebug("CppScripts", "Attempted to load CppScript for '" + scriptName + "', but returned InvalidScript.\n");
// information not really needed for sys admins but is for developers
script = invalidToReturn;
}

View File

@ -136,6 +136,7 @@ int main(int argc, char** argv) {
dConfig config("worldconfig.ini");
Game::config = &config;
Game::logger->SetLogToConsole(bool(std::stoi(config.GetValue("log_to_console"))));
Game::logger->SetLogDebugStatements(bool(std::stoi(config.GetValue("log_debug_statements"))));
if (config.GetValue("disable_chat") == "1") chatDisabled = true;
// Connect to CDClient
@ -503,11 +504,13 @@ int main(int argc, char** argv) {
dLogger * SetupLogger(int zoneID, int instanceID) {
std::string logPath = "./logs/WorldServer_" + std::to_string(zoneID) + "_" + std::to_string(instanceID) + "_" + std::to_string(time(nullptr)) + ".log";
bool logToConsole = false;
bool logDebugStatements = false;
#ifdef _DEBUG
logToConsole = true;
logDebugStatements = true;
#endif
return new dLogger(logPath, logToConsole);
return new dLogger(logPath, logToConsole, logDebugStatements);
}
void HandlePacketChat(Packet* packet) {

View File

@ -19,6 +19,9 @@ max_clients=999
# 0 or 1, should log to console
log_to_console=1
# 0 or 1, should log debug (developer only) statements to console for debugging, not needed for normal operation
log_debug_statements=0
# 0 or 1, should ignore playkeys
# If 1 everyone with an account will be able to login, regardless of if they have a key or not
dont_use_keys=0

View File

@ -19,5 +19,8 @@ max_clients=999
# 0 or 1, should log to console
log_to_console=1
# 0 or 1, should log debug (developer only) statements to console for debugging, not needed for normal operation
log_debug_statements=0
# 0 or 1, should not compile chat hash map to file
dont_generate_dcf=0

View File

@ -32,5 +32,8 @@ max_clients=999
# 0 or 1, should log to console
log_to_console=1
# 0 or 1, should log debug (developer only) statements to console for debugging, not needed for normal operation
log_debug_statements=0
# 0 or 1, should autostart auth, chat, and char servers
prestart_servers=1

View File

@ -20,6 +20,9 @@ max_clients=999
# 0 or 1, should log to console
log_to_console=1
# 0 or 1, should log debug (developer only) statements to console for debugging, not needed for normal operation
log_debug_statements=0
# 0 or 1, should not compile chat hash map to file
dont_generate_dcf=0