diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am index 7a79fad501..7fbde40efc 100644 --- a/src/engine/Makefile.am +++ b/src/engine/Makefile.am @@ -37,6 +37,7 @@ libgncmod_engine_la_SOURCES = \ gnc-pricedb.c \ gnc-session.c \ gnc-session-scm.c \ + gnc-trace.c \ gncmod-engine.c \ guid.c \ gncObject.c \ @@ -79,6 +80,7 @@ gncinclude_HEADERS = \ gnc-pricedb.h \ gnc-session.h \ gnc-session-scm.h \ + gnc-trace.h \ guid.h \ gncObject.h \ kvp_frame.h \ diff --git a/src/engine/gnc-engine-util.c b/src/engine/gnc-engine-util.c index f2e170052d..862c075c8b 100644 --- a/src/engine/gnc-engine-util.c +++ b/src/engine/gnc-engine-util.c @@ -27,256 +27,11 @@ #include "config.h" #include -#include -#include -#include -#include -#include -#include #include -#include #include "gnc-engine-util.h" #include "gnc-engine.h" -/** GLOBALS *********************************************************/ - -/* This static indicates the debugging module that this .o belongs to. */ -/* static short module = MOD_ENGINE; */ - -static gncLogLevel loglevel[MOD_LAST + 1] = -{ - GNC_LOG_FATAL, /* DUMMY */ - GNC_LOG_WARNING, /* ENGINE */ - GNC_LOG_WARNING, /* IO */ - GNC_LOG_WARNING, /* REGISTER */ - GNC_LOG_WARNING, /* LEDGER */ - GNC_LOG_WARNING, /* HTML */ - GNC_LOG_WARNING, /* GUI */ - GNC_LOG_WARNING, /* SCRUB */ - GNC_LOG_WARNING, /* GTK_REG */ - GNC_LOG_WARNING, /* GUILE */ - GNC_LOG_WARNING, /* BACKEND */ - GNC_LOG_WARNING, /* QUERY */ - GNC_LOG_WARNING, /* PRICE */ - GNC_LOG_WARNING, /* SQL EVENT */ - GNC_LOG_WARNING, /* SQL TXN */ - GNC_LOG_WARNING, /* KVP */ - GNC_LOG_WARNING, /* SX */ - GNC_LOG_WARNING, /* BOOK */ - GNC_LOG_TRACE, /* TEST */ - GNC_LOG_WARNING, /* LOT */ - GNC_LOG_WARNING, /* ACCOUNT */ - GNC_LOG_WARNING, /* IMPORT */ - GNC_LOG_WARNING, /* BUSINESS */ -}; - -static FILE *fout = NULL; - -/* Set the logging level of the given module. */ -void -gnc_set_log_level(gncModuleType module, gncLogLevel level) -{ - if ((module < 0) || (module > MOD_LAST)) - return; - - loglevel[module] = level; -} - -/* Set the logging level for all modules. */ -void -gnc_set_log_level_global(gncLogLevel level) -{ - gncModuleType module; - - for (module = 0; module <= MOD_LAST; module++) - loglevel[module] = level; -} - -void -gnc_set_logfile (FILE *outfile) -{ - fout = outfile; -} - -/* prettify() cleans up subroutine names. AIX/xlC has the habit of - * printing signatures not names; clean this up. On other operating - * systems, truncate name to 30 chars. Note this routine is not thread - * safe. Note we wouldn't need this routine if AIX did something more - * reasonable. Hope thread safety doesn't poke us in eye. */ -static const char * -prettify (const char *name) -{ - static char bf[128]; - char *p; - - if (!name) - return ""; - - strncpy (bf, name, 29); bf[28] = 0; - p = strchr (bf, '('); - - if (p) - { - *(p+1) = ')'; - *(p+2) = 0x0; - } - else - strcpy (&bf[26], "...()"); - - return bf; -} - -gboolean -gnc_should_log (gncModuleType module, gncLogLevel log_level) -{ - if (module < 0 || module > MOD_LAST) - { - PERR ("Bad module: %d", module); - return FALSE; - } - - if (log_level > loglevel[module]) - return FALSE; - - return TRUE; -} - -void -gnc_log (gncModuleType module, gncLogLevel log_level, const char *prefix, - const char *function_name, const char *format, ...) -{ - va_list ap; - - if (!gnc_should_log (module, log_level)) - return; - - if (!fout) fout = stderr; - - fprintf (fout, "%s: %s: ", - prefix ? prefix : "(null)", - prettify (function_name)); - - va_start (ap, format); - - vfprintf (fout, format, ap); - - va_end (ap); - - fprintf (fout, "\n"); -} - - -/********************************************************************\ -\********************************************************************/ - -#define NUM_CLOCKS 10 - -static -struct timeval gnc_clock[NUM_CLOCKS] = { - {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, - {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, -}; - -static -struct timeval gnc_clock_total[NUM_CLOCKS] = { - {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, - {0, 0}, {0, 0}, {0, 0}, {0, 0}, {0, 0}, -}; - -void -gnc_start_clock (int clockno, gncModuleType module, gncLogLevel log_level, - const char *function_name, const char *format, ...) -{ - struct timezone tz; - va_list ap; - - if ((0>clockno) || (NUM_CLOCKS <= clockno)) return; - gettimeofday (&gnc_clock[clockno], &tz); - - if (!fout) fout = stderr; - - fprintf (fout, "Clock %d Start: %s: ", - clockno, prettify (function_name)); - - va_start (ap, format); - - vfprintf (fout, format, ap); - - va_end (ap); - - fprintf (fout, "\n"); -} - -void -gnc_report_clock (int clockno, gncModuleType module, gncLogLevel log_level, - const char *function_name, const char *format, ...) -{ - struct timezone tz; - struct timeval now; - va_list ap; - - if ((0>clockno) || (NUM_CLOCKS <= clockno)) return; - gettimeofday (&now, &tz); - - /* need to borrow to make difference */ - if (now.tv_usec < gnc_clock[clockno].tv_usec) - { - now.tv_sec --; - now.tv_usec += 1000000; - } - now.tv_sec -= gnc_clock[clockno].tv_sec; - now.tv_usec -= gnc_clock[clockno].tv_usec; - - gnc_clock_total[clockno].tv_sec += now.tv_sec; - gnc_clock_total[clockno].tv_usec += now.tv_usec; - - if (!fout) fout = stderr; - - fprintf (fout, "Clock %d Elapsed: %ld.%06lds %s: ", - clockno, now.tv_sec, now.tv_usec, prettify (function_name)); - - va_start (ap, format); - - vfprintf (fout, format, ap); - - va_end (ap); - - fprintf (fout, "\n"); -} - -void -gnc_report_clock_total (int clockno, - gncModuleType module, gncLogLevel log_level, - const char *function_name, const char *format, ...) -{ - va_list ap; - - if ((0>clockno) || (NUM_CLOCKS <= clockno)) return; - - /* need to normalize usec */ - while (gnc_clock_total[clockno].tv_usec >= 1000000) - { - gnc_clock_total[clockno].tv_sec ++; - gnc_clock_total[clockno].tv_usec -= 1000000; - } - - if (!fout) fout = stderr; - - fprintf (fout, "Clock %d Total Elapsed: %ld.%06lds %s: ", - clockno, - gnc_clock_total[clockno].tv_sec, - gnc_clock_total[clockno].tv_usec, - prettify (function_name)); - - va_start (ap, format); - - vfprintf (fout, format, ap); - - va_end (ap); - - fprintf (fout, "\n"); -} /********************************************************************\ \********************************************************************/ @@ -424,51 +179,5 @@ gnc_stpcpy (char *dest, const char *src) return (dest + strlen (src)); } -/********************************************************************\ - Callbacks so that the engine can display gui messages. -\********************************************************************/ - -static GNCGuiMessage gnc_gui_warning_func = NULL; -static GNCGuiMessage gnc_gui_error_func = NULL; - -void gnc_set_warning_message (GNCGuiMessage func) -{ - gnc_gui_warning_func = func; -} - -void gnc_set_error_message (GNCGuiMessage func) -{ - gnc_gui_error_func = func; -} - -gboolean -gnc_send_gui_warning(const gchar *format, ...) -{ - va_list args; - - if (!gnc_gui_warning_func) - return(FALSE); - - va_start(args, format); - gnc_gui_warning_func(format, args); - va_end(args); - return(TRUE); -} - -gboolean -gnc_send_gui_error(const gchar *format, ...) -{ - va_list args; - - if (!gnc_gui_error_func) - return(FALSE); - - va_start(args, format); - gnc_gui_error_func(format, args); - va_end(args); - return(TRUE); -} - - /************************* END OF FILE ******************************\ \********************************************************************/ diff --git a/src/engine/gnc-engine-util.h b/src/engine/gnc-engine-util.h index e3c00f57fa..4324d60fb2 100644 --- a/src/engine/gnc-engine-util.h +++ b/src/engine/gnc-engine-util.h @@ -29,186 +29,11 @@ #ifndef GNC_ENGINE_UTIL_H #define GNC_ENGINE_UTIL_H -#include -#include -#include -#include -#include - #include "config.h" -/** DEBUGGING MACROS ************************************************/ -/* The debuging macros enable the setting of trace messages */ +#include -/** If you modify this, modify the loglevel table in the .c file. */ -typedef enum -{ - MOD_DUMMY = 0, - MOD_ENGINE = 1, - MOD_IO = 2, - MOD_REGISTER= 3, - MOD_LEDGER = 4, - MOD_HTML = 5, - MOD_GUI = 6, - MOD_SCRUB = 7, - MOD_GTK_REG = 8, - MOD_GUILE = 9, - MOD_BACKEND = 10, - MOD_QUERY = 11, - MOD_PRICE = 12, - MOD_EVENT = 13, - MOD_TXN = 14, - MOD_KVP = 15, - MOD_SX = 16, - MOD_BOOK = 17, - MOD_TEST = 18, - MOD_LOT = 19, - MOD_ACCOUNT = 20, - MOD_IMPORT = 21, - MOD_BUSINESS= 22, - MOD_LAST = 22 -} gncModuleType; - -typedef enum -{ - GNC_LOG_FATAL = 0, - GNC_LOG_ERROR = 1, - GNC_LOG_WARNING = 2, - GNC_LOG_INFO = 3, - GNC_LOG_DEBUG = 4, - GNC_LOG_DETAIL = 5, - GNC_LOG_TRACE = 6, -} gncLogLevel; - - -typedef void (*GNCGuiMessage) (const gchar *format, va_list args); -void gnc_set_warning_message (GNCGuiMessage func); -void gnc_set_error_message (GNCGuiMessage func); - -gboolean gnc_send_gui_warning (const gchar *format, ...); -gboolean gnc_send_gui_error (const gchar *format, ...); - -/* FIXME: these logging functions should proably get replaced by - * the glib.h g_error(), etc functions. That way, we would have - * unified logging mechanism, instead of having some messages - * work one way, and other a different way ... - * - * FIXME: the if test should not be a subroutine call, it should - * not use that many CPU cycles. These logging functions are supposed - * to be lightweight. Who changed this ??? Why ??? - * - */ -gboolean gnc_should_log (gncModuleType module, gncLogLevel log_level); -void gnc_log (gncModuleType module, gncLogLevel log_level, - const char *prefix, const char *function_name, - const char *format, ...) G_GNUC_PRINTF(5,6); - -#define FATAL(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_FATAL)) \ - gnc_log (module, GNC_LOG_FATAL, "Fatal Error", \ - __FUNCTION__, format, ## args); \ -} - -#define PERR(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_ERROR)) \ - gnc_log (module, GNC_LOG_ERROR, "Error", \ - __FUNCTION__, format, ##args); \ -} - -#define PWARN(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_WARNING)) \ - gnc_log (module, GNC_LOG_WARNING, "Warning", \ - __FUNCTION__, format, ## args); \ -} - -#define PWARN_GUI(format, args...) { \ - if (!gnc_send_gui_error(format, ## args)) \ - PWARN(format, ## args); \ -} - -#define PINFO(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_INFO)) \ - gnc_log (module, GNC_LOG_INFO, "Info", \ - __FUNCTION__, format, ## args); \ -} - -#define DEBUG(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_DEBUG)) \ - gnc_log (module, GNC_LOG_DEBUG, "Debug", \ - __FUNCTION__, format, ## args); \ -} - -#define ENTER(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_DEBUG)) \ - gnc_log (module, GNC_LOG_DEBUG, "Enter", \ - __FUNCTION__, format, ## args); \ -} - -#define LEAVE(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_DEBUG)) \ - gnc_log (module, GNC_LOG_DEBUG, "Leave", \ - __FUNCTION__, format, ## args); \ -} - -#define DETAIL(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_DETAIL)) \ - gnc_log (module, GNC_LOG_DETAIL, "Detail", \ - __FUNCTION__, format, ## args); \ -} - - -#define DEBUGCMD(x) { if (gnc_should_log (module, GNC_LOG_DEBUG)) { x; }} - -#define ERROR() fprintf(stderr,"%s: Line %d, error = %s\n", \ - __FILE__, __LINE__, strerror(errno)); - -#define TRACE(format, args...) { \ - if (gnc_should_log (module, GNC_LOG_TRACE)) \ - gnc_log (module, GNC_LOG_TRACE, "Trace", \ - __FUNCTION__, format, ## args); \ -} - -void gnc_start_clock (int clockno, gncModuleType module, gncLogLevel log_level, - const char *function_name, const char *format, ...); - -void gnc_report_clock (int clockno, - gncModuleType module, - gncLogLevel log_level, - const char *function_name, - const char *format, ...); - -void gnc_report_clock_total (int clockno, - gncModuleType module, - gncLogLevel log_level, - const char *function_name, - const char *format, ...); - -#define START_CLOCK(clockno,format, args...) { \ - if (gnc_should_log (module, GNC_LOG_INFO)) \ - gnc_start_clock (clockno, module, GNC_LOG_INFO,\ - __FUNCTION__, format, ## args); \ -} - -#define REPORT_CLOCK(clockno,format, args...) { \ - if (gnc_should_log (module, GNC_LOG_INFO)) \ - gnc_report_clock (clockno, module, GNC_LOG_INFO,\ - __FUNCTION__, format, ## args); \ -} - -#define REPORT_CLOCK_TOTAL(clockno,format, args...) { \ - if (gnc_should_log (module, GNC_LOG_INFO)) \ - gnc_report_clock_total (clockno, module, GNC_LOG_INFO, \ - __FUNCTION__, format, ## args); \ -} - -/* Set the logging level of the given module. */ -void gnc_set_log_level(gncModuleType module, gncLogLevel level); - -/* Set the logging level for all modules. */ -void gnc_set_log_level_global(gncLogLevel level); - -/* Pipe log output to pipe or file */ -void gnc_set_logfile (FILE *outfile); +#include "gnc-trace.h" /** Macros *****************************************************/