mirror of
https://github.com/HikikoMarmy/Champions-Reborn-Server.git
synced 2026-04-05 08:59:54 -03:00
"Format" style arguments for logging.
This commit is contained in:
95
logging.cpp
95
logging.cpp
@@ -1,12 +1,10 @@
|
|||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include "logging.h"
|
#include "logging.h"
|
||||||
|
|
||||||
|
static const char *LOG_PATH[] = {
|
||||||
static const char* LOG_PATH[] = {
|
|
||||||
"./generic",
|
"./generic",
|
||||||
"./debug",
|
"./debug",
|
||||||
"./error"
|
"./error",
|
||||||
|
"./warning",
|
||||||
};
|
};
|
||||||
|
|
||||||
Log::Log()
|
Log::Log()
|
||||||
@@ -68,73 +66,26 @@ void Log::CheckFileStatus( LOG_TYPE type )
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::WriteToLog( LOG_TYPE type, std::string format )
|
void Log::WriteToLog( LOG_TYPE type, const std::string &message )
|
||||||
{
|
{
|
||||||
log_lock.lock();
|
std::lock_guard lock( log_lock );
|
||||||
|
|
||||||
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
|
HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
|
||||||
SetConsoleTextAttribute( hConsole, type + 10 );
|
SetConsoleTextAttribute( hConsole, type + 10 );
|
||||||
|
|
||||||
switch( type )
|
switch( type )
|
||||||
{
|
{
|
||||||
case LOG_TYPE::log_generic: printf( "[INFO]: " ); break;
|
case log_generic: printf( "[INFO]: " ); break;
|
||||||
case LOG_TYPE::log_debug: printf( "[DEBUG]: " ); break;
|
case log_debug: printf( "[DEBUG]: " ); break;
|
||||||
case LOG_TYPE::log_warn: printf( "[WARN]: " ); break;
|
case log_warn: printf( "[WARN]: " ); break;
|
||||||
case LOG_TYPE::log_error: printf( "[ERROR]: " ); break;
|
case log_error: printf( "[ERROR]: " ); break;
|
||||||
}
|
}
|
||||||
|
|
||||||
SetConsoleTextAttribute( hConsole, 15 );
|
|
||||||
|
|
||||||
printf( "%s\n", format.c_str() );
|
SetConsoleTextAttribute( hConsole, 15 );
|
||||||
|
printf( "%s\n", message.c_str() );
|
||||||
|
|
||||||
CheckFileStatus( type );
|
CheckFileStatus( type );
|
||||||
file_stream[ type ] << GetTimeStamp() << format << '\n';
|
file_stream[ type ] << GetTimeStamp() << message << '\n';
|
||||||
file_stream[ type ].close();
|
|
||||||
log_lock.unlock();
|
|
||||||
}
|
|
||||||
|
|
||||||
void Log::Info( std::string format, ... )
|
|
||||||
{
|
|
||||||
std::vector< char > buf( 512 );
|
|
||||||
va_list args;
|
|
||||||
va_start( args, format );
|
|
||||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
|
||||||
va_end( args );
|
|
||||||
|
|
||||||
WriteToLog( log_generic, &buf[ 0 ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
void Log::Warn( std::string format, ... )
|
|
||||||
{
|
|
||||||
std::vector< char > buf( 512 );
|
|
||||||
va_list args;
|
|
||||||
va_start( args, format );
|
|
||||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
|
||||||
va_end( args );
|
|
||||||
|
|
||||||
WriteToLog( log_warn, &buf[ 0 ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
void Log::Debug( std::string format, ... )
|
|
||||||
{
|
|
||||||
std::vector< char > buf( 512 );
|
|
||||||
va_list args;
|
|
||||||
va_start( args, format );
|
|
||||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
|
||||||
va_end( args );
|
|
||||||
|
|
||||||
WriteToLog( log_debug, &buf[ 0 ] );
|
|
||||||
}
|
|
||||||
|
|
||||||
void Log::Error( std::string format, ... )
|
|
||||||
{
|
|
||||||
std::vector< char > buf( 512 );
|
|
||||||
va_list args;
|
|
||||||
va_start( args, format );
|
|
||||||
vsnprintf_s( &buf[ 0 ], buf.size(), buf.size() + strlen( format.c_str() ), format.c_str(), args );
|
|
||||||
va_end( args );
|
|
||||||
|
|
||||||
WriteToLog( log_error, &buf[ 0 ] );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::Packet( std::vector<uint8_t> p, size_t size, bool send )
|
void Log::Packet( std::vector<uint8_t> p, size_t size, bool send )
|
||||||
@@ -175,7 +126,7 @@ void Log::Packet( std::vector<uint8_t> p, size_t size, bool send )
|
|||||||
line[ r++ ] = p[ i ];
|
line[ r++ ] = p[ i ];
|
||||||
|
|
||||||
// Highlight packet type or flags
|
// Highlight packet type or flags
|
||||||
if( i == 4 || i == 5 )
|
if( i == 0 || i == 1 )
|
||||||
{
|
{
|
||||||
SetConsoleTextAttribute( hConsole, send ? 11 : 10 );
|
SetConsoleTextAttribute( hConsole, send ? 11 : 10 );
|
||||||
printf( "%02X ", p[ i ] );
|
printf( "%02X ", p[ i ] );
|
||||||
@@ -206,31 +157,31 @@ void Log::Packet( std::vector<uint8_t> p, size_t size, bool send )
|
|||||||
printf( "\n\n" );
|
printf( "\n\n" );
|
||||||
}
|
}
|
||||||
|
|
||||||
void Log::PacketToFile( std::string prefix, std::vector<uint8_t> p, size_t size)
|
void Log::ToFile( std::string prefix, std::vector<uint8_t> p, size_t size )
|
||||||
{
|
{
|
||||||
static char timestamp[64] = "";
|
static char timestamp[ 64 ] = "";
|
||||||
|
|
||||||
time_t t; time(&t);
|
time_t t; time( &t );
|
||||||
struct tm date_tm;
|
struct tm date_tm;
|
||||||
localtime_s(&date_tm, &t);
|
localtime_s( &date_tm, &t );
|
||||||
|
|
||||||
_snprintf_s(timestamp, _TRUNCATE, 63,
|
_snprintf_s( timestamp, _TRUNCATE, 63,
|
||||||
"%02d-%02d-%02d.packet.bin",
|
"%02d-%02d-%02d.packet.bin",
|
||||||
date_tm.tm_hour,
|
date_tm.tm_hour,
|
||||||
date_tm.tm_min,
|
date_tm.tm_min,
|
||||||
date_tm.tm_sec);
|
date_tm.tm_sec );
|
||||||
|
|
||||||
std::string filename = prefix + '_' + timestamp;
|
std::string filename = prefix + '_' + timestamp;
|
||||||
|
|
||||||
std::fstream file;
|
std::fstream file;
|
||||||
file.open(filename, std::ios::out | std::ios::binary);
|
file.open( filename, std::ios::out | std::ios::binary );
|
||||||
|
|
||||||
if (!file.is_open())
|
if( !file.is_open() )
|
||||||
{
|
{
|
||||||
Error("Failed to open packet log file: %s", filename.c_str());
|
Error( "Failed to open packet log file: %s", filename.c_str() );
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
file.write(reinterpret_cast<const char*>(p.data()), size);
|
file.write( reinterpret_cast< const char * >( p.data() ), size );
|
||||||
file.close();
|
file.close();
|
||||||
}
|
}
|
||||||
|
|||||||
100
logging.h
100
logging.h
@@ -1,11 +1,11 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <fstream>
|
|
||||||
#include <mutex>
|
|
||||||
#include <cstdarg>
|
|
||||||
#include <iostream>
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <fstream>
|
||||||
|
#include <mutex>
|
||||||
|
#include <sstream>
|
||||||
|
#include <type_traits>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
|
|
||||||
class Log {
|
class Log {
|
||||||
@@ -22,11 +22,63 @@ private:
|
|||||||
static inline std::fstream file_stream[ num_log_type ];
|
static inline std::fstream file_stream[ num_log_type ];
|
||||||
static inline std::mutex log_lock;
|
static inline std::mutex log_lock;
|
||||||
|
|
||||||
static const char* GetTimeStamp();
|
static const char *GetTimeStamp();
|
||||||
static void CheckFileStatus( LOG_TYPE type );
|
static void CheckFileStatus( LOG_TYPE type );
|
||||||
static void WriteToLog( LOG_TYPE type, std::string format );
|
static void WriteToLog( LOG_TYPE type, const std::string &message );
|
||||||
public:
|
|
||||||
|
|
||||||
|
static std::string WideToMulti( const std::wstring &input )
|
||||||
|
{
|
||||||
|
if( input.empty() ) return "";
|
||||||
|
int sizeNeeded = WideCharToMultiByte( CP_UTF8, 0, input.data(), ( int )input.size(), nullptr, 0, nullptr, nullptr );
|
||||||
|
std::string result( sizeNeeded, 0 );
|
||||||
|
WideCharToMultiByte( CP_UTF8, 0, input.data(), ( int )input.size(), std::data( result ), sizeNeeded, nullptr, nullptr );
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
static std::string ToString( const T &value )
|
||||||
|
{
|
||||||
|
if constexpr( std::is_same_v<T, std::wstring> )
|
||||||
|
{
|
||||||
|
return WideToMulti( value );
|
||||||
|
}
|
||||||
|
else if constexpr( std::is_same_v<T, std::string> )
|
||||||
|
{
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
oss << value;
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
static std::string Format( const std::string &formatStr, Args&&... args )
|
||||||
|
{
|
||||||
|
std::ostringstream oss;
|
||||||
|
size_t last = 0, next = 0;
|
||||||
|
std::vector<std::string> values = { ToString( std::forward<Args>( args ) )... };
|
||||||
|
size_t index = 0;
|
||||||
|
while( ( next = formatStr.find( "{}", last ) ) != std::string::npos )
|
||||||
|
{
|
||||||
|
oss << formatStr.substr( last, next - last );
|
||||||
|
if( index < values.size() )
|
||||||
|
{
|
||||||
|
oss << values[ index++ ];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
oss << "{}"; // unmatched placeholder
|
||||||
|
}
|
||||||
|
last = next + 2;
|
||||||
|
}
|
||||||
|
oss << formatStr.substr( last );
|
||||||
|
return oss.str();
|
||||||
|
}
|
||||||
|
|
||||||
|
public:
|
||||||
static Log &Get()
|
static Log &Get()
|
||||||
{
|
{
|
||||||
static Log instance;
|
static Log instance;
|
||||||
@@ -36,10 +88,30 @@ public:
|
|||||||
Log();
|
Log();
|
||||||
~Log();
|
~Log();
|
||||||
|
|
||||||
static void Info( std::string format, ... );
|
template<typename... Args>
|
||||||
static void Warn( std::string format, ... );
|
static void Info( const std::string &format, Args&&... args )
|
||||||
static void Debug( std::string format, ... );
|
{
|
||||||
static void Error( std::string format, ... );
|
WriteToLog( log_generic, Format( format, std::forward<Args>( args )... ) );
|
||||||
static void Packet( std::vector< uint8_t > p, size_t size, bool send );
|
}
|
||||||
static void PacketToFile( std::string prefix, std::vector< uint8_t > p, size_t size );
|
|
||||||
};
|
template<typename... Args>
|
||||||
|
static void Warn( const std::string &format, Args&&... args )
|
||||||
|
{
|
||||||
|
WriteToLog( log_warn, Format( format, std::forward<Args>( args )... ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
static void Debug( const std::string &format, Args&&... args )
|
||||||
|
{
|
||||||
|
WriteToLog( log_debug, Format( format, std::forward<Args>( args )... ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename... Args>
|
||||||
|
static void Error( const std::string &format, Args&&... args )
|
||||||
|
{
|
||||||
|
WriteToLog( log_error, Format( format, std::forward<Args>( args )... ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
static void Packet( std::vector<uint8_t> p, size_t size, bool send );
|
||||||
|
static void ToFile( std::string prefix, std::vector<uint8_t> p, size_t size );
|
||||||
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user