View Single Post
  #1  
Old 05-25-2004, 02:15 PM
maaltan
Fire Beetle
 
Join Date: May 2004
Posts: 9
Default Jr Dev Code - Error logging

i mentioned a need for error logging centralization in the bugs forum. well i got my laptop back and slapped together the following. it sets up the infrastructure for the above as well as an extendable "utility function" set. By using this, this allows you to quickly and easily change often performed tasks(logging, etc)


lets see if i can get this code in here before my session times out.

First I am going to paste two files needed to use this, util.h and util.c

util.h
Code:
// 
// UTIL.h

// Include this in any file you wish to use the utilities in.

#pragma once

#ifndef UTIL_INCLUDED //make sure that this header is only compiled once to prevent link errors
#define UTIL_INCLUDED

//return value termination constants
const int RET_ERROR = 666;
const int RET_NORMAL = 0;

//severity indexies 
const int MAX_SEVERITY = 4;

const int SEV_NULL = 0;  //probably not needed, more of a place holder
const int SEV_INFO = 1;  //Information only; nothing to be alarmed about
const int SEV_WARN = 2;  //Warning; Not critical, but should be looked at when time allows
const int SEV_ERRO = 3;  //ERROR; Big problem, normal program operation can continue 
						 //but bad things might happen
const int SEV_FATL = 4;  //FATAL ERROR; MAJOR ERROR, program operation cannot continue, 
						 //You should use this before terminating the server.

//If you expand the error messages make sure you add it to the array in util.cpp
//Tried putting array in header but vc++.net's linker didn't like it

//Function prototypes
int info_report(int severity, char *buf);

#endif
util.c
Code:
//util.c 
// Add this to your project along with the header to use these functions

#include "stdafx.h"
#include "util.h"
#include <time.h>

//severity label array 
const char* severity_lookup[] = {	{"Null"},
								{"info"},
								{"Warn"},
								{"ERRO"},
								{"FATL"} };


int info_report(int severity, char* error)
// return value as defined in util.h
{
	time_t timer;
	struct tm * curtime;
	
	if (severity > MAX_SEVERITY)
	{
		info_report(SEV_ERRO,"INVALID SEVERITY IN INFO_REPORT:  Please use valid severity value");
		return(RET_ERROR);
	} else
	{   
			timer=time(NULL);
			curtime = localtime(&timer);	

			printf( "%s[%.2d:%.2d:%.2d] %s\n" ,
					severity_lookup[severity],
					(*curtime).tm_hour,
					(*curtime).tm_min,
					(*curtime).tm_sec,
					error);
			
			return(RET_NORMAL);
	}
	return(RET_NORMAL);
}

Ok there it is. Its semi readable. I'm a bit rusty in c/c++ but this compiles in vc++.net . please let me know if you have problems with it and i will try to polish it.

Tto use the only (at the moment ) function in this file simply call it as follows:

info_report( severity , "Error string");

severity is defined as a number between 0 and 4. defined as constants as follows:

SEV_NULL=0 - unused, makes the numbers make more sense
SEV_INFO=1 - Information, nothing wrong, maybe a login
SEV_WARN=2 - Warning, minor problem
SEV_ERRO=3 - error, something broke but not critical
SEV_FATL=4 - severe error, server must terminate/restart

so for example, to log an error with the database and a character could not be loaded, but the server can continue, it would be used as follows

info_report(SEV_ERRO, "Error Loading Character, <enter reason here>");

THis can be edited and/or extended if need be. i think my comments explain how, let me know if you need clairification.

Well thats it i think.
Thanks for your time
Maaltan
Reply With Quote