EQEmulator Forums

EQEmulator Forums (https://www.eqemulator.org/forums/index.php)
-   Archive::Development (https://www.eqemulator.org/forums/forumdisplay.php?f=621)
-   -   Jr Dev Code - Error logging (https://www.eqemulator.org/forums/showthread.php?t=13974)

maaltan 05-25-2004 02:15 PM

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

Trumpcard 05-25-2004 02:19 PM

This looks alot like an enterprise level logging model.. You don't happen to work in an IT industry do you?

maaltan 05-25-2004 02:24 PM

used to, mostly just a hobby coder though. but the coms bombed and now I just code for fun.

thats not quite enterprise level, but it does lay the infrastructure for it though.

enterprise would probably involve fault tolerant databases and redundancy. which would be possibly. using this as a starting point you could, say, recode info_report to insert logs into a mysql table. or a log file.

This is nasty code. I would have probably been fired for this code, well not quite. :)

Trumpcard 05-25-2004 02:31 PM

Now you just need to create a stand alone logging daemon process and you have a robust logging solution !

maaltan 05-25-2004 02:40 PM

Hmmm, thats do-able. Would you prefer to use named pipe, UDP, or Process hook? but I digress, its unnecessary for at this stage of development though.

It would be of benifit in the future if people start running high volume persistant clustered servers.

do you know how to create patch files in visual c++? I will go through and replace all the errors with info_report calls and create a patch file that will allow you to quickly make the changes. i used to know how to do it in unix ..hmm . to GOOGLE.

kathgar 05-25-2004 07:04 PM

Now THIS is the kind of code we are looking for, or examples of things you have done with the emulator code. It needs to show skill, knowledge and competence.

maaltan 05-27-2004 12:37 PM

total recall
 
ok i got bored so i learned how to use variable parameters. here is the updated util.cpp file

This update will let you use this exactly like printf. for example:

Code:

opcode = 55;
info_report(SEV_WARN,"Unknown opcode: %d", opcode);

will print:

Code:

Warn [20:33:18] Unknown opcode: 55
all of printf's codes should work. Please let me know if wierdness happens. this is new ground for me.

Code:

#include "stdafx.h"
#include "util.h"
#include <time.h>
#include <stdarg.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
        { 
                        // setup for variable argument list
                        va_list arglist;
                        va_start(arglist, error);
                        //end setup

                        timer=time(NULL);
                        curtime = localtime(&timer);

                        printf( "%s[%.2d:%.2d:%.2d] " ,
                                        severity_lookup[severity],
                                        (*curtime).tm_hour,
                                        (*curtime).tm_min,
                                        (*curtime).tm_sec);

                        vprintf( error,arglist); //its maaaaagic

                        printf("\n");
                                                va_end(arglist); //close the list up
                        return(RET_NORMAL);
        }
        return(RET_NORMAL);
}

you will need to change the function prototype in util.h from:

int info_report(int severity, char* error);

to

int info_report(int severity, char* error, ...);


All times are GMT -4. The time now is 11:54 AM.

Powered by vBulletin®, Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.