⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 debug.cpp

📁 PPP服务端实现例子
💻 CPP
字号:
//Debug.cpp - Generic debug information output engine
//Robert Merrison - 2001

#include <time.h>

#include "debug.h"
#include "timer.h"

/*Constructor: Initialises a few things*/
debug_engine::debug_engine( )
{
	is_inited	= false; //Not initialized yet
	write_file	= false; //Don't output to a file by default
	level		= DBLEVEL_MED; //Set default debug level to medium
	debug_file	= NULL;	
	bytes_written	= 0;
	file_size_limit	= 0; //Turn file size limit off by default
	timer		= NULL;
	log_time	= false;

	memset( filename, 0, sizeof( filename ) );
}

/*Destructor: Do nothing*/
debug_engine::~debug_engine( )
{
}

/*Initialization: Init stuff*/
bool debug_engine::init( short debug_level, char *debug_filename, time_engine* timer_engine )
{	
		//Make sure the debug level is valid
	if( debug_level != DBLEVEL_LOW && debug_level != DBLEVEL_MED && debug_level != DBLEVEL_HIGH){
		return false;
	}	
		//Check if a filename is specfied and if so, activate file writing
	if( debug_filename != NULL ){
		write_file	= true;
	}
	else{
		write_file = false;
	}
	timer	= timer_engine;
		//Enable time outputting if a timer engine is specified
	if( timer_engine != NULL ){
		log_time	= true;
	}
	else{
		log_time	= false;
	}
		//If file writing is activated, open the file for writing
	if( write_file ){
		if( !open_debug_file( debug_filename ) ){
			write_file = false;	//Disable file writing if there was an error
		}
	}
		//Set the debug level
	level		= debug_level;
		//Initialization is complete
	is_inited	= true;
	log_message( MSG_NOTICE, "Debug engine started");

	return is_inited;
}

/*Open debug file: Opens the debug file (if used) for writing*/
bool debug_engine::open_debug_file( char *debug_filename )
{	
		//Open the file and check for errors
	debug_file = fopen( debug_filename, "w" );
	if( !debug_file ){
		return false;
	}
    bytes_written	= 0;
	strcpy( filename, debug_filename );
	return true;
}

/*Kill: Clears everything up when we are done*/
bool debug_engine::kill( )
{	
	if( is_inited == false ){
		return true;
	}
	log_message( MSG_NOTICE, "Debug engine killed" );

		//Close the debug logging file if it exists
	if( debug_file ){
		fclose( debug_file );
	}
		//No longer initialized
	is_inited	= false;


	return true;
}

/*Log Message: Logs an error/status message to the screen/log file*/
bool debug_engine::log_message( short msg_type, char* message, ... )
{
		//Check the logging engine has been initialized
	if( !is_inited ) {
		return false;
	}

	if( timer != NULL ){
		log_time	= true;
	}
	else{
		log_time	= false;
	}	
		//Now read in any paramters that may have been passed
	va_list	parameters;
	char	debug_string[256];
	memset( debug_string, 0, sizeof( debug_string ) );
	va_start( parameters, message );
	vsprintf( debug_string, message, parameters );
	va_end( parameters );
		//Add the time/date information if needed
	char	string_to_output[1024];
	memset	(string_to_output, 0, sizeof( string_to_output ));
	if( log_time == true ){
		tm	time	= timer->get_current_time();
		sprintf( string_to_output, "[%i/%i/%i %i:%i] %s", time.tm_mday, time.tm_mon+1, time.tm_year+1900, 
			time.tm_hour, time.tm_min+1, debug_string );
	}
	else
	{
		sprintf( string_to_output, "%s", debug_string );
	}
	bool output_string	= must_log( msg_type );

	if( output_string ){
		printf( "%s\n", string_to_output );
		if( write_file ){	
			bytes_written += (int)(strlen( string_to_output) + 1);	//Just make sure we haven't gone over the file size
			check_file_size();	
			fprintf( debug_file, "%s\n", string_to_output );
			fflush( debug_file );
		}
	}
	
	return true;
}

/*Check Logging: Checks the logging level and type of message and detemines if it should be logged*/
bool debug_engine::must_log( short msg_type )
{
		//Always log critical errors and notices
	if( msg_type == MSG_NOTICE || msg_type == MSG_CRITICAL ){	 
		return true;
	}

		//Check the message type against the logging level and return accordingly
	switch( msg_type ){
		case MSG_STATUS:{
			if( level == DBLEVEL_HIGH ){
				return true;
			}
			else{
				return false;
			}
		}
		case MSG_ERROR:{
			if( level == DBLEVEL_MED || level == DBLEVEL_HIGH ){
				return true;
			}
			else{
				return false;
			}
		}
	}

	return false;
}

/*Check file size: Checks that logging hasn't gone over the allowable file size, and if it has, 
	restarts the log file*/
void debug_engine::check_file_size()
{
		//Check if the filesize limit is enabled or not
	if( file_size_limit == 0 ){
		return;
	}
		//Check if we have hit the limit, and if so close the file and then re-open it to clear it
	if( bytes_written > file_size_limit ){
		if( debug_file ){
			printf( "Remaking log file\n" );
			fclose( debug_file );
			open_debug_file( filename );
		}
	}

	return;
}














⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -