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

📄 debug.c

📁 bonddb 是一个源于PostgreSQL封装包的对象。它是一个由C/C++编写的快速数据提取层应用软件
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * debug.c module. * treshna Enterprises Ltd *  * This file provides message features for logging, error messages and other * useful functions associated with debugging. Different debugging levels * can be set up, you can control how what areas you report messages from, * and where to ignore from.  Colouring can be used. *  *  *//** I AM TIGER */#include <stdio.h>#include <stdarg.h>#include <stdlib.h>#include <unistd.h>#include <glib.h>/* This is normally defined in autoconf *//* #define GTK_DEBUGNOTE 0 *//* If GTK debugging is enabled, then use it */#if GTK_DEBUGNOTE#include <gtk/gtk.h>#define MSG_BUF_SIZE 1000#endif/* Protype headers */#include "debug.h"/* Francis: Remained for the old debug API... */gint debug_linenumshow;gint debug_info;FILE *debug_stream;/* Define globaldebuginfo */static DebugInformation globaldebuginfo;static gint debug_comparestring(gpointer a, gpointer b);/** * Set default values. */voiddebug_init(void)	{	debug_stream = stdout;	globaldebuginfo.debug_stream = stdout;	 /* debug_stream */	globaldebuginfo.error_stream = stderr;	 /* error_stream */	globaldebuginfo.dump_stream = NULL;		 /* where it all goes in the rubbish */	globaldebuginfo.showlinenumbers = TRUE; /* showlinenumbers */	globaldebuginfo.showmessages = TRUE;	 /* showmessages */	globaldebuginfo.breakonerror = FALSE;	 /* breakonerror */	globaldebuginfo.breakonwarning = FALSE; /* breakonwarning */	globaldebuginfo.excludeall = FALSE;		 /* excludeall */	globaldebuginfo.colour = TRUE;			 /* colour */	globaldebuginfo.testinprogress = TRUE;	 /* testinprogress */	globaldebuginfo.errortoallstreams = FALSE;	globaldebuginfo.debuglevel = 100;		 /* debuglevel */	globaldebuginfo.exceptionfiles = NULL;	 /* exceptionfiles */	globaldebuginfo.currentcolour = mem_strdup("");	/* 0 length string. */	}/** * debug_getdebuginfo: * Obtaining the structure you can manipulate the detail of the debug. * However, but please do not try anything too much this structure dependant * work. I could have provided functions (or methods) for each member of * structure... */DebugInformation *debug_getdebuginfo(void)	{	return &globaldebuginfo;	}/** * debug_appendexceptionfile: * @filename: Filename to add. *  * Add a file into the exception list so that debugging information is now displayed * on it.  * * Returns: Absolutly nothing. */voiddebug_appendexceptionfile(gchar * filename)	{	globaldebuginfo.exceptionfiles = (GList *) g_list_prepend(globaldebuginfo.exceptionfiles, (gchar *) filename);	}static gchar *truefalse(gboolean val)	{	return val ? "TRUE" : "FALSE";	}static gintdebug_comparestring(gpointer a, gpointer b)	{	return strcmp((const char *)a, (const char *)b);	}static gintdebug_printwhereabout(gchar * file, gint line, gchar * colour, gchar * function)	{	gint retval = 0;	if (globaldebuginfo.showlinenumbers)		retval = fprintf(globaldebuginfo.debug_stream, " <%s%s:%d %s();> ", colour, file, line, function);	else		retval = fprintf(globaldebuginfo.debug_stream, " <%s%s: %s();> ", colour, file, function);	return retval;	}/** * debug_msg: * @fmt: The format follows standard C library's printf() function series * * * Returns: the number of characters printed excluding the location info */gintdebug_msg(gchar * file, gint line, gchar * function, gint level, DebugMsgType msgtype, const char *fmt, ...)	{	gint retval = 0;	va_list args;	g_assert(level >= 0 && level <= 100);	/* i say . Um if within debug level and u have no exclude all, or if you have exclude all and your in the right file	   then show debugging info. right yes thats what it does */	if (level <= globaldebuginfo.debuglevel &&	        (globaldebuginfo.excludeall == FALSE || (globaldebuginfo.excludeall == TRUE &&	                g_list_find_custom(globaldebuginfo.exceptionfiles, file,	                                   debug_comparestring))))		{		/* To use colour or not to use colour ... */		if (msgtype == Debug_Msg && globaldebuginfo.colour == TRUE)			retval += debug_printwhereabout(file, line, DEBUG_MSGCOLOUR, function);		else if (globaldebuginfo.colour == TRUE && globaldebuginfo.currentcolour != NULL)			retval += debug_printwhereabout(file, line, globaldebuginfo.currentcolour, function);		else			retval += debug_printwhereabout(file, line, "", function);		/* Print the actual message */		va_start(args, fmt);		retval += vfprintf(globaldebuginfo.debug_stream, fmt, args);		va_end(args);		fprintf(globaldebuginfo.debug_stream, "%s", DEBUG_RESETCOLOUR);		if (msgtype != Debug_Printf)			fprintf(globaldebuginfo.debug_stream, "\n");		}	/* This is the dump stream where the unwanted go */	else if (globaldebuginfo.dump_stream != NULL)		{		retval += fprintf(globaldebuginfo.dump_stream, " <%s:%d %s();> ", file, line, function);		va_start(args, fmt);		retval += vfprintf(globaldebuginfo.dump_stream, fmt, args);		va_end(args);		if (msgtype != Debug_Printf)			fprintf(globaldebuginfo.dump_stream, "\n");		}	return retval;	}/* high level error message to stderr */gintdebug_error(gchar * file, gint line, gchar * function, const char *fmt, ...)	{	gint retval = 0;				  /* Let's keep this init althoug it's useless */	va_list args;	va_start(args, fmt);	if (globaldebuginfo.colour)		{		fprintf(globaldebuginfo.error_stream, "%sError %s: %d: %s(): ", DEBUG_ERRORCOLOUR, file, line, function);		retval = vfprintf(globaldebuginfo.error_stream, fmt, args);		fprintf(globaldebuginfo.error_stream, "%s\n", DEBUG_RESETCOLOUR);		if (globaldebuginfo.errortoallstreams == TRUE)			{			fprintf(globaldebuginfo.debug_stream, "%sError %s: %d: %s(): ", DEBUG_ERRORCOLOUR, file, line, function);			retval = vfprintf(globaldebuginfo.debug_stream, fmt, args);			fprintf(globaldebuginfo.debug_stream, "%s\n", DEBUG_RESETCOLOUR);			}		}	else		{		retval = fprintf(globaldebuginfo.error_stream, "Error %s: %d: %s(): ", file, line, function);		vfprintf(globaldebuginfo.error_stream, fmt, args);		if (globaldebuginfo.errortoallstreams == TRUE)			{			retval = fprintf(globaldebuginfo.debug_stream, "Error %s: %d: %s(): ", file, line, function);			vfprintf(globaldebuginfo.debug_stream, fmt, args);			}		}	va_end(args);	if (globaldebuginfo.breakonerror)		{		g_assert(FALSE);		}	return retval;	}/** * debug_warning: *  *  *  */gintdebug_warning(gchar * file, gint line, gchar * function, const gchar * fmt, ...)	{	gint retval = 0;				  /* Let's keep this init althoug it's useless */	va_list args;	va_start(args, fmt);	if (globaldebuginfo.colour)		{		fprintf(globaldebuginfo.error_stream, "%sWarning %s: %d: %s(): ", DEBUG_ERRORCOLOUR, file, line, function);		retval = vfprintf(globaldebuginfo.error_stream, fmt, args);		fprintf(globaldebuginfo.error_stream, "%s\n", DEBUG_RESETCOLOUR);		if (globaldebuginfo.errortoallstreams == TRUE)			{			fprintf(globaldebuginfo.debug_stream, "%sWarning %s: %d: %s(): ", DEBUG_ERRORCOLOUR, file, line,			        function);			retval = vfprintf(globaldebuginfo.debug_stream, fmt, args);			fprintf(globaldebuginfo.debug_stream, "%s\n", DEBUG_RESETCOLOUR);			}		}	else		{		retval = fprintf(globaldebuginfo.error_stream, "Warning %s: %d: %s(): ", file, line, function);		vfprintf(globaldebuginfo.error_stream, fmt, args);		if (globaldebuginfo.errortoallstreams == TRUE)			{			retval = fprintf(globaldebuginfo.debug_stream, "Warning %s: %d: %s(): ", file, line, function);			vfprintf(globaldebuginfo.debug_stream, fmt, args);			}		}	va_end(args);	if (globaldebuginfo.breakonwarning)		{		g_assert(NULL);		}	return retval;	}gintdebug_line(gchar * file, gchar * function, DebugLineType linetype)	{	gint retval = 0;	/* Now thats an if statement */	if ((linetype == Debug_MinorHeader || linetype == Debug_MinorFooter) && globaldebuginfo.debuglevel >= 40)		;	else if ((linetype == Debug_Header || linetype == Debug_Footer) && globaldebuginfo.debuglevel >= 10)		;	else		return 0;	if (globaldebuginfo.excludeall == FALSE)		;	else		if (globaldebuginfo.excludeall == TRUE &&		        g_list_find_custom(globaldebuginfo.exceptionfiles, file, debug_comparestring))			;		else			return 0;	switch (linetype)		{	case Debug_Header:		retval += fprintf(globaldebuginfo.debug_stream, "\n\n__________ --> %s: %s() __________\n", file, function);		break;	case Debug_Footer:		retval += fprintf(globaldebuginfo.debug_stream, "__________ %s() <-- __________\n\n", function);		break;	case Debug_MinorHeader:		retval += fprintf(globaldebuginfo.debug_stream, "\n..........  --> %s: %s() ..........\n", file, function);		break;	case Debug_MinorFooter:		retval += fprintf(globaldebuginfo.debug_stream, ".......... %s() <-- ..........\n\n", function);		break;	default:		retval += fprintf(globaldebuginfo.debug_stream, "\nUnkown line type!\n");		break;		}	return retval;	}voiddebug_printstatus(void)	{	GList *lI = NULL;	FILE *output = stdout;	fprintf(output, "Show line numbers: %s\n", truefalse(globaldebuginfo.showlinenumbers));	fprintf(output, "Show message: %s\n", truefalse(globaldebuginfo.showmessages));	fprintf(output, "Break on error: %s\n", truefalse(globaldebuginfo.breakonerror));	fprintf(output, "Break on warning: %s\n", truefalse(globaldebuginfo.breakonwarning));	fprintf(output, "Exclude all debug message: %s\n", truefalse(globaldebuginfo.excludeall));	fprintf(output, "Use ANSI terminal colour: %s\n", truefalse(globaldebuginfo.colour));	fprintf(output, "Test in progress: %s\n", truefalse(globaldebuginfo.testinprogress));	fprintf(output, "Debug level (0-100): %d\n", globaldebuginfo.debuglevel);	fprintf(output, "Debug exceptions files (%s):\n",	        (globaldebuginfo.excludeall) ? "to be included" : "to be excluded");	fprintf(output, "Current colour: %s\n", globaldebuginfo.currentcolour);	/* Farncis: lI = list Iterator. Intentionally avoided 'l' or 'li' because in case I need to change those name or	   search in editor they have to be unique enough. Just image if typed 'l' or 'li' the search will highlight	   virtually all source code lines !!! */	for (lI = g_list_first(globaldebuginfo.exceptionfiles); lI; lI = g_list_next(lI))		{		fprintf(output, "\t%s", (gchar *) lI->data);		}	}/** * debug_begintest: *  * This call is used in unit tests where you want to compare the results with * pre-stored results.   */voiddebug_begintest(void)	{	globaldebuginfo.testinprogress = TRUE;	globaldebuginfo.debuglevel = 0;	}/** * debug_begintestingzone: * * Inform the debugging message that this is a testing zone so dont display any * debug messages unless your inside a debug_begintest() and debug_endtest(). *  */voiddebug_begintestingzone(void)	{	globaldebuginfo.testinprogress = FALSE;	globaldebuginfo.olddebuglevel = globaldebuginfo.debuglevel;	globaldebuginfo.debuglevel = 100;	}/** * debug_endtestingzone: *  * End the testing zone and go back to normal operation. */voiddebug_endtestingzone(void)	{	globaldebuginfo.testinprogress = TRUE;	globaldebuginfo.debuglevel = globaldebuginfo.olddebuglevel;	}/** * debug_endtest: * * Call this after you have finished a unit test, this will keep the system in the * testing zone though. */voiddebug_endtest(void)	{	globaldebuginfo.testinprogress = FALSE;	globaldebuginfo.debuglevel = 100;	}voiddebug_selftest(void)	{	DebugInformation *di;	debug_init();	debug_printstatus();	di = debug_getdebuginfo();	di->breakonerror = FALSE;	di->colour = TRUE;	debug_error(__FILE__, __LINE__, __PRETTY_FUNCTION__, "This is errormsg %s %d", "number", 1);

⌨️ 快捷键说明

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