common/errors.c

///////////////////////////////////////////////////////////////////////////////
// Filename: errors.c
///////////////////////////////////////////////////////////////////////////////
// Purpose: implemenation of class "errors"
///////////////////////////////////////////////////////////////////////////////
// History:
// ========
//
// Date     Time     Name      Description   
// -------- -------- --------  ------------------------------------------------
// 96/02/06 00:00:22 muellerg: created
//
///////////////////////////////////////////////////////////////////////////////


// Feature test switches ///////////////////////////// Feature test switches //
    /* NONE */



// System headers /////////////////////////////////////////// System headers //

#include <iostream.h>
#include <string.h>
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>


// Local headers ///////////////////////////////////////////// Local headers //

#include "errors.h"



// Macros /////////////////////////////////////////////////////////// Macros //
    /* NONE */



// File scope objects /////////////////////////////////// File scope objects //
    /* NONE */



// External variables, functions, and classes ///////////// External objects //
    /* NONE */



// Signal catching functions ///////////////////// Signal catching functions //
    /* NONE */



// Structures, unions, and class definitions /////////////////// Definitions //
    /* NONE */



// Functions and class implementation /// Functions and class implementation //


/*
 * get error from errno 
 */

void errors::system(char *message, errorlevel err_level)
{
    system(errno, message, err_level);
}



/* 
 * error number specified in error_number
 */ 

void errors::system(int errno_number, char *message, errorlevel err_level)
{
    cerr << program_name << ": ";

    if(message)
        cerr << message << ": ";

    cerr << "system error: "
         << strerror(errno_number) << endl;

    if(err_level==fatal)
        exit(EXIT_FAILURE);
}



/*
 * panic: terminate program with message
 */

void errors::panic(char *message)
{
    cerr << program_name << ": Panic";

    if(message)
        cerr << ": " << message;

    cerr << endl;

    exit(EXIT_FAILURE);
}



/*
 * program bug. Always terminates a program
 */

void errors::bug(char *function, char *sourcefile, int lineno, char *message)
{
    cerr << "Congratulations! You have discovered a bug in "
         << "the program \"" << program_name << "\"!" << endl;
    cerr << "The bug was detected here:" << endl;
        
    if(sourcefile)
        cerr << "file: " << sourcefile << endl;

    if(function)
        cerr << "function: " << function << endl;

    if(lineno)
        cerr << "line: " << lineno << endl;

    if(message)
        cerr << "Message: " << message << endl;

    exit(EXIT_FAILURE);
}




/*
 * print warning and return
 */

void errors::warning(char *fmt,...)
{
    va_list args;

    if(fmt)
    {
        char *emesgstr=new char[2000];

        if(emesgstr)
        {       
            va_start(args, fmt);
            vsprintf(emesgstr, fmt, args);
            va_end(args);   

            if(fmt)
                cerr << program_name << ": Warning: " << emesgstr << endl;

            delete[] emesgstr;
        }
        else
            cerr << program_name << ": Warning: not enough memory for "
                                                        "warning" << endl;
    }   

    return;
}





/* 
 * debug output
 */

void errors::debug(char *message, debuglevel dlevel)
{
    if(error_debuglevel != none)
    {
        if(dlevel >= error_debuglevel)
        {
            cerr << program_name << ":" << " Debug: " 
                 << message << endl;
        }
    }
}


    
/*
 * get/set debug level
 */

errors::debuglevel errors::get_debug_level(void)
{
    return error_debuglevel;
}

errors::debuglevel errors::set_debug_level(debuglevel debugl)
{
    debuglevel old = error_debuglevel;
    error_debuglevel = debugl;
    return old;
}



/*
 * set program name
 */

char* errors::set_program_name(char *name)
{
    char *old = program_name;
    program_name = name;
    return old;
}