
/******************************************************************************
**
**  This program is free software; you can redistribute it and/or
**  modify it, however, you cannot sell it.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
**
**  You should have received a copy of the license attached to the
**  use of this software.  If not, visit www.shmoo.com/osiris for
**  details.
**
******************************************************************************/

/*****************************************************************************
**
**  File:    error.c
**  Date:    June 2, 2002
**
**  Author:  Brian Wotring
**  Purpose: central error reference for all osiris applications.
**
******************************************************************************/

#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>

#include <sys/types.h>
#include <time.h>

#include "utilities.h"
#include "error.h"

char * osi_get_name_for_error( osi_uint64 error )
{
    int index;
    char *name = "?";

    for( index = 0; error_keywords[index].word != NULL; index++ )
    {
        if( error == error_keywords[index].type )
        {
            name = error_keywords[index].word;
            break;
        }
    }

    return name;
}

void wrap_error( OSI_ERROR *error )
{
    if( error != NULL )
    {
        error->type = HTONLL( error->type );
        error->time = HTONLL( error->time );
    }
}

void unwrap_error( OSI_ERROR *error )
{
    if( error != NULL )
    {
        error->type = NTOHLL( error->type );
        error->time = NTOHLL( error->time );
    }
}


void print_osi_error( OSI_ERROR *error )
{
    time_t error_time;
    char *time;

    if( error == NULL )
    {
        fprintf( stderr, "<empty error>\n" );
        return;
    }

    /* get time as a string. */

    error_time = (time_t)error->time;

    if( error_time != 0 )
    {
        time = ctime( &error_time );
    }

    else
    {
        time = "<no timestamp>";
    }

    if( time != NULL )
    {
        if( time[strlen(time)-1] == '\n' )
        {
            time[strlen(time)-1] = '\0';
        }
    }

    fprintf( stdout, "[%llu] [%s] ", error->type, time );

    if( ( error->message != NULL ) && ( strlen( error->message ) > 0 ) )
    {
        fprintf( stdout, "[ %s ]", error->message );
    }

    else
    {
        fprintf( stdout, "[ %s ]", osi_get_name_for_error( error->type ) );
    }

    fprintf( stdout, "\n" );
}

void pack_error( OSI_ERROR *error, char *buffer, int buffer_size )
{
    char *temp = buffer;

    if( ( error == NULL ) || ( buffer == NULL ) || ( buffer_size <= 0 ) )
    {
        return;
    }

    memcpy( temp, &error->type, sizeof( error->type ) );
    temp += sizeof( error->type );
    memcpy( temp, &error->time, sizeof( error->time ) );
    temp += sizeof( error->time );
    memcpy( temp, error->message, sizeof( error->message ) );
}

void unpack_error( OSI_ERROR *error, char *buffer, int buffer_size )
{
    char *temp = buffer;

    if( ( error == NULL ) || ( buffer == NULL ) || ( buffer_size <= 0 ) )
    {
        return;
    }

    memcpy( &error->type, temp, sizeof( error->type ) );
    temp += sizeof( error->type );
    memcpy( &error->time, temp, sizeof( error->time ) );
    temp += sizeof( error->time );
    memcpy( error->message, temp, sizeof( error->message ) );
}

