  | 
   Multi-level Trace Function 
   Submitted by  |   
  
  
My submission for a Code of the Day is a trace function as follows:
These two files provide a useful method of logging to a file supporting different levels of output at run time.  A source file would include the header and the following code:
  
static char THIS_FILE[] = __FILE__;
static char THIS_VERSION[] = "1.0";   |  
 
 
 
  Then whenever you wish to log something call like this:
  
TRACEF (TRACE_VERBOSE, ("PageManager ()\n"));
TRACEF (TRACE_ERROR, ("fopen (%s) failed\n", filename));
TRACEF (TRACE_FATAL, ("Out of memory\n"));  |  
 
  
and you would get out like this:
2001-01-09 08:32:25 Trace:d:\proggy\igdi\page.cpp 1.0:44:PageManager ()
2001-01-09 08:32:25 Error:d:\proggy\igdi\map.cpp 1.0:165:fopen (map.dat) failed
2001-01-09 08:32:25 Fatal:D:\proggy\igdi\input.cpp 1.0:114:Out of memory
  [date] [time] [role] [file] [version] [line] ...   |  
 
  --  
Diane Youdale 
louise@imbrugliamail.com
  
 | 
 
 
 
Currently browsing [trace.zip] (1,162 bytes) - [console.h] - (606 bytes)
 
 /* tab setting : 4
 *
 * Console definition
 *
 * 6 december 2k	fnjordy
 * 30 december 2k            added trace function
 */
  #ifndef __CONSOLE_H				
#define __CONSOLE_H
  
/*
 * Global variables
 */
  typedef enum { TRACE_VERBOSE, TRACE_INFO, TRACE_WARNING, TRACE_ERROR, TRACE_FATAL } trace_t;
  
/*
 * Global functions
 */
  #define TRACEF(r,x)	tracef (THIS_FILE, THIS_VERSION, __LINE__, (r)); \
					printf x
  extern int tracef (char*, char*, int, trace_t);
  extern void printf (char*, ...);
extern void init_console (void);
  
#endif /* __CONSOLE_H */
  /* end of file */
   |  
  
 | 
 
  
Currently browsing [trace.zip] (1,162 bytes) - [console.cpp] - (1,233 bytes)
 
 /* tab setting : 4
 *
 * Console definition
 *
 * 6 december 2k	fnjordy
 * 30 december 2k             added trace
 */
  /* precompiled jism */
#include "StdAfx.h"
  #include <stdio.h>
#include <stdlib.h>
#include <time.h>
  #include "console.h"
  
/* locals */
static FILE *fptr;
  #define DEBUG_FILE "debug.log"
  static char* time_format = "%Y-%m-%d %H:%M:%S";
static char* roles[] = { "Trace",
						 "Info",
						 "Warning",
						 "Error",
						 "Fatal" };
 
 
  /* code */
  int
tracef (char* this_file, 
		char* this_version,
		int line_number, 
		trace_t role)
{
	char buffer[1024];
	time_t uct_time;
	struct tm* display_time;
  	time (&uct_time);
	display_time = localtime (&uct_time);
	strftime (buffer, sizeof (buffer), time_format, display_time);
	return fprintf (fptr, "%s %s:%s %s:%i:", buffer, roles[role], this_file, this_version, line_number);
}
  
void 
printf (char *fmt, ...)
{
	char buffer[1024];
    va_list	va;
      va_start (va, fmt);
    vsprintf (buffer, fmt, va);
    va_end (va);
  	fputs (buffer, fptr);
	fflush (fptr);
}
  void 
init_console (void)
{
	if ((fptr = fopen (DEBUG_FILE, "w")) == NULL) {
		/* arse */
		exit (-1);
	}
}
  /* eof */   |  
  
 | 
 
 
 
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
 
 
 |