diff --git a/source/common/debug.cpp b/source/common/debug.cpp deleted file mode 100644 index 20d2dbe..0000000 --- a/source/common/debug.cpp +++ /dev/null @@ -1,319 +0,0 @@ -// Copyright (C) 2007-2025 EQ2EMulator -// Licensed under GPL v3 - -/* - JA: File rendered obsolete (2011-08-12) - -#include "debug.h" - -#include -using namespace std; -#include -#include -#ifdef WIN32 - #include - - #define snprintf _snprintf - #define vsnprintf _vsnprintf - #define strncasecmp _strnicmp - #define strcasecmp _stricmp -#else - #include - #include - #include -#endif -#include "../common/MiscFunctions.h" - -EQEMuLog* LogFile = new EQEMuLog; -AutoDelete adlf(&LogFile); - -static const char* FileNames[EQEMuLog::MaxLogID] = { "logs/eq2emu", "logs/eq2emu", "logs/eq2emu_error", "logs/eq2emu_debug", "logs/eq2emu_quest", "logs/eq2emu_commands" }; -static const char* LogNames[EQEMuLog::MaxLogID] = { "Status", "Normal", "Error", "Debug", "Quest", "Command" }; - -EQEMuLog::EQEMuLog() { - for (int i=0; i= 2 - pLogStatus[i] = 1 | 2; -#else - pLogStatus[i] = 0; -#endif - logCallbackFmt[i] = NULL; - logCallbackBuf[i] = NULL; - } -#if EQDEBUG < 2 - pLogStatus[Status] = 3; - pLogStatus[Error] = 3; - pLogStatus[Debug] = 3; - pLogStatus[Quest] = 2; - pLogStatus[Commands] = 2; -#endif -} - -EQEMuLog::~EQEMuLog() { - for (int i=0; i= MaxLogID) { - return false; - } - LockMutex lock(&MOpen); - if (pLogStatus[id] & 4) { - return false; - } - if (fp[id]) { - return true; - } - - char exename[200] = ""; -#if defined(WORLD) - snprintf(exename, sizeof(exename), "_world"); -#elif defined(ZONE) - snprintf(exename, sizeof(exename), "_zone"); -#endif - char filename[200]; -#ifndef NO_PIDLOG - snprintf(filename, sizeof(filename), "%s%s_%04i.log", FileNames[id], exename, getpid()); -#else - snprintf(filename, sizeof(filename), "%s%s.log", FileNames[id], exename); -#endif - fp[id] = fopen(filename, "a"); - if (!fp[id]) { - cerr << "Failed to open log file: " << filename << endl; - pLogStatus[id] |= 4; // set file state to error - return false; - } - fputs("---------------------------------------------\n",fp[id]); - return true; -} - -bool EQEMuLog::write(LogIDs id, const char *fmt, ...) { - char buffer[4096]; - - if (!this) { - return false; - } - if (id >= MaxLogID) { - return false; - } - bool dofile = false; - if (pLogStatus[id] & 1) { - dofile = open(id); - } - if (!(dofile || pLogStatus[id] & 2)) - return false; - LockMutex lock(&MLog[id]); - - time_t aclock; - struct tm *newtime; - - time( &aclock ); //Get time in seconds - newtime = localtime( &aclock ); //Convert time to struct - - if (dofile){ -#ifndef NO_PIDLOG - fprintf(fp[id], "[%04d%02d%02d %02d:%02d:%02d] ", newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec); -#else - fprintf(fp[id], "%04i [%04d%02d%02d %02d:%02d:%02d] ", getpid(), newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec); -#endif - } - - va_list argptr; - va_start(argptr, fmt); - vsnprintf(buffer, sizeof(buffer), fmt, argptr); - va_end(argptr); - if (dofile) - fprintf(fp[id], "%s\n", buffer); - if(logCallbackFmt[id]) { - msgCallbackFmt p = logCallbackFmt[id]; - p(id, fmt, argptr ); - } - - if (pLogStatus[id] & 2) { - if (pLogStatus[id] & 8) { - fprintf(stderr, "[%04d%02d%02d %02d:%02d:%02d] [%s] ", newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec, LogNames[id]); - fprintf(stderr, "%s\n", buffer); - } - else { - fprintf(stdout, "[%04d%02d%02d %02d:%02d:%02d] [%s] ", newtime->tm_year+1900, newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec, LogNames[id]); - fprintf(stdout, "%s\n", buffer); - } - } - if (dofile) - fprintf(fp[id], "\n"); - if (pLogStatus[id] & 2) { - if (pLogStatus[id] & 8) - fprintf(stderr, "\n"); - else - fprintf(stdout, "\n"); - } - if(dofile) - fflush(fp[id]); - return true; -} - -bool EQEMuLog::writebuf(LogIDs id, const char *buf, int8 size, int32 count) { - if (!this) { - return false; - } - if (id >= MaxLogID) { - return false; - } - bool dofile = false; - if (pLogStatus[id] & 1) { - dofile = open(id); - } - if (!(dofile || pLogStatus[id] & 2)) - return false; - LockMutex lock(&MLog[id]); - - time_t aclock; - struct tm *newtime; - - time( &aclock ); // Get time in seconds - newtime = localtime( &aclock ); // Convert time to struct - - if (dofile){ -#ifndef NO_PIDLOG - fprintf(fp[id], "[%02d.%02d. - %02d:%02d:%02d] ", newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec); -#else - fprintf(fp[id], "%04i [%02d.%02d. - %02d:%02d:%02d] ", getpid(), newtime->tm_mon+1, newtime->tm_mday, newtime->tm_hour, newtime->tm_min, newtime->tm_sec); -#endif - } - - if (dofile) { - fwrite(buf, size, count, fp[id]); - fprintf(fp[id], "\n"); - } - if(logCallbackBuf[id]) { - msgCallbackBuf p = logCallbackBuf[id]; - p(id, buf, size, count); - } - if (pLogStatus[id] & 2) { - if (pLogStatus[id] & 8) { - fprintf(stderr, "[%s] ", LogNames[id]); - fwrite(buf, size, count, stderr); - fprintf(stderr, "\n"); - } else { - fprintf(stdout, "[%s] ", LogNames[id]); - fwrite(buf, size, count, stdout); - fprintf(stdout, "\n"); - } - } - if(dofile) - fflush(fp[id]); - return true; -} - -bool EQEMuLog::writeNTS(LogIDs id, bool dofile, const char *fmt, ...) { - char buffer[4096]; - va_list argptr; - va_start(argptr, fmt); - vsnprintf(buffer, sizeof(buffer), fmt, argptr); - va_end(argptr); - if (dofile) - fprintf(fp[id], "%s\n", buffer); - if (pLogStatus[id] & 2) { - if (pLogStatus[id] & 8) - fprintf(stderr, "%s\n", buffer); - else - fprintf(stdout, "%s\n", buffer); - } - return true; -}; - -bool EQEMuLog::Dump(LogIDs id, int8* data, int32 size, int32 cols, int32 skip) { - if (!this) { -#if EQDEBUG >= 10 - cerr << "Error: Dump() from null pointer"<= MaxLogID) - return false; - bool dofile = false; - if (pLogStatus[id] & 1) { - dofile = open(id); - } - if (!(dofile || pLogStatus[id] & 2)) - return false; - LockMutex lock(&MLog[id]); - write(id, "Dumping Packet: %i", size); - // Output as HEX - int j = 0; char* ascii = new char[cols+1]; memset(ascii, 0, cols+1); - int32 i; - for(i=skip; i= 32 && data[i] < 127) - ascii[j++] = data[i]; - else - ascii[j++] = '.'; - } - int32 k = ((i-skip)-1)%cols; - if (k < 8) - writeNTS(id, dofile, " "); - for (int32 h = k+1; h < cols; h++) { - writeNTS(id, dofile, " "); - } - writeNTS(id, dofile, " | %s\n", ascii); - if (dofile) - fflush(fp[id]); - safe_delete_array(ascii); - return true; -} - -void EQEMuLog::SetCallback(LogIDs id, msgCallbackFmt proc) { - if (!this) - return; - if (id >= MaxLogID) { - return; - } - logCallbackFmt[id] = proc; -} - -void EQEMuLog::SetCallback(LogIDs id, msgCallbackBuf proc) { - if (!this) - return; - if (id >= MaxLogID) { - return; - } - logCallbackBuf[id] = proc; -} - -void EQEMuLog::SetAllCallbacks(msgCallbackFmt proc) { - if (!this) - return; - int r; - for(r = Status; r < MaxLogID; r++) { - SetCallback((LogIDs)r, proc); - } -} - -void EQEMuLog::SetAllCallbacks(msgCallbackBuf proc) { - if (!this) - return; - int r; - for(r = Status; r < MaxLogID; r++) { - SetCallback((LogIDs)r, proc); - } -} -*/ \ No newline at end of file diff --git a/source/common/debug.h b/source/common/debug.h index 9cff7a7..4e1bf96 100644 --- a/source/common/debug.h +++ b/source/common/debug.h @@ -1,7 +1,13 @@ // Copyright (C) 2007-2025 EQ2EMulator // Licensed under GPL v3 -#ifndef EQDEBUG_H -#define EQDEBUG_H + +#pragma once + +#include +#include +#include +#include +#include // Debug Levels /* @@ -16,55 +22,33 @@ #define EQDEBUG 1 #endif - -#if defined(DEBUG) && defined(WIN32) - //#ifndef _CRTDBG_MAP_ALLOC - #include - #include - #if (_MSC_VER < 1300) - #include - #include - #define _CRTDBG_MAP_ALLOC - #define new new(_NORMAL_BLOCK, __FILE__, __LINE__) - #define malloc(s) _malloc_dbg(s, _NORMAL_BLOCK, __FILE__, __LINE__) - #endif - //#endif -#endif - #ifndef ThrowError void CatchSignal(int); #if defined(CATCH_CRASH) || defined(_EQDEBUG) - #define ThrowError(errstr) { cout << "Fatal error: " << errstr << " (" << __FILE__ << ", line " << __LINE__ << ")" << endl; LogWrite(WORLD__ERROR, 0, "Debug", "Thrown Error: %s (%s:%i)", errstr, __FILE__, __LINE__); throw errstr; } + #define ThrowError(errstr) { \ + std::cout << "Fatal error: " << errstr << " (" << __FILE__ << ", line " << __LINE__ << ")" << std::endl; \ + LogWrite(WORLD__ERROR, 0, "Debug", "Thrown Error: %s (%s:%i)", errstr, __FILE__, __LINE__); \ + throw errstr; \ + } #else - #define ThrowError(errstr) { cout << "Fatal error: " << errstr << " (" << __FILE__ << ", line " << __LINE__ << ")" << endl; LogWrite(WORLD__ERROR, 0, "Debug", "Thrown Error: %s (%s:%i)", errstr, __FILE__, __LINE__); CatchSignal(0); } + #define ThrowError(errstr) { \ + std::cout << "Fatal error: " << errstr << " (" << __FILE__ << ", line " << __LINE__ << ")" << std::endl; \ + LogWrite(WORLD__ERROR, 0, "Debug", "Thrown Error: %s (%s:%i)", errstr, __FILE__, __LINE__); \ + CatchSignal(0); \ + } #endif #endif -#ifdef WIN32 - // VS6 doesn't like the length of STL generated names: disabling - #pragma warning(disable:4786) -#endif +#define DebugBreak() if(0) {} -#ifndef WIN32 - #define DebugBreak() if(0) {} -#endif - -#ifdef WIN32 - #include - #include -#endif - -#include "../common/Mutex.h" -#include -#include - - -class EQEMuLog { +class EQEMuLog +{ public: EQEMuLog(); ~EQEMuLog(); - enum LogIDs { + enum LogIDs + { Status = 0, //this must stay the first entry in this list Normal, Error, @@ -73,54 +57,58 @@ public: Commands, MaxLogID }; - - //these are callbacks called for each - typedef void (* msgCallbackBuf)(LogIDs id, const char *buf, int8 size, int32 count); - typedef void (* msgCallbackFmt)(LogIDs id, const char *fmt, va_list ap); - + + // Callbacks called for each log entry + using msgCallbackBuf = void (*)(LogIDs id, const char* buf, std::int8_t size, std::int32_t count); + using msgCallbackFmt = void (*)(LogIDs id, const char* fmt, va_list ap); + void SetAllCallbacks(msgCallbackFmt proc); void SetAllCallbacks(msgCallbackBuf proc); void SetCallback(LogIDs id, msgCallbackFmt proc); void SetCallback(LogIDs id, msgCallbackBuf proc); - - bool writebuf(LogIDs id, const char *buf, int8 size, int32 count); - bool write(LogIDs id, const char *fmt, ...); - bool Dump(LogIDs id, int8* data, int32 size, int32 cols=16, int32 skip=0); + + bool writebuf(LogIDs id, const char* buf, std::int8_t size, std::int32_t count); + bool write(LogIDs id, const char* fmt, ...); + bool Dump(LogIDs id, std::uint8_t* data, std::int32_t size, std::int32_t cols = 16, std::int32_t skip = 0); + private: bool open(LogIDs id); - bool writeNTS(LogIDs id, bool dofile, const char *fmt, ...); // no error checking, assumes is open, no locking, no timestamp, no newline + bool writeNTS(LogIDs id, bool dofile, const char* fmt, ...); // no error checking, assumes is open, no locking, no timestamp, no newline + + std::mutex MOpen; + std::mutex MLog[MaxLogID]; + FILE* fp[MaxLogID]; + + /* LogStatus: bitwise variable + 1 = output to file + 2 = output to stdout + 4 = fopen error, dont retry + 8 = use stderr instead (2 must be set) + */ + std::int8_t pLogStatus[MaxLogID]; - Mutex MOpen; - Mutex MLog[MaxLogID]; - FILE* fp[MaxLogID]; -/* LogStatus: bitwise variable - 1 = output to file - 2 = output to stdout - 4 = fopen error, dont retry - 8 = use stderr instead (2 must be set) -*/ - int8 pLogStatus[MaxLogID]; - msgCallbackFmt logCallbackFmt[MaxLogID]; msgCallbackBuf logCallbackBuf[MaxLogID]; }; -//extern EQEMuLog* LogFile; - #ifdef _EQDEBUG -class PerformanceMonitor { +class PerformanceMonitor +{ public: - PerformanceMonitor(sint64* ip) { + PerformanceMonitor(std::int64_t* ip) + { p = ip; QueryPerformanceCounter(&tmp); } - ~PerformanceMonitor() { + + ~PerformanceMonitor() + { LARGE_INTEGER tmp2; QueryPerformanceCounter(&tmp2); *p += tmp2.QuadPart - tmp.QuadPart; } + LARGE_INTEGER tmp; - sint64* p; + std::int64_t* p; }; #endif -#endif