/*									tab:8
 *
 * print_error.c - source file for simple error logging utilities
 *
 * "Copyright (c) 1999 by Steven S. Lumetta."
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 * 
 * IN NO EVENT SHALL THE AUTHOR OR THE UNIVERSITY OF ILLINOIS BE LIABLE TO 
 * ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL 
 * DAMAGES ARISING OUT  OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, 
 * EVEN IF THE AUTHOR AND/OR THE UNIVERSITY OF ILLINOIS HAS BEEN ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 * THE AUTHOR AND THE UNIVERSITY OF ILLINOIS SPECIFICALLY DISCLAIM ANY 
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE 
 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND NEITHER THE AUTHOR NOR
 * THE UNIVERSITY OF ILLINOIS HAS NO OBLIGATION TO PROVIDE MAINTENANCE, 
 * SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS."
 *
 * Author:	    Steve Lumetta
 * Version:	    1
 * Creation Date:   Wed Feb 17 22:19:15 1999
 * Filename:	    print_error.c
 * History:
 *	SL	1	Wed Feb 17 22:19:15 1999
 *		Culled from other code.
 */


#ident "$Id$"


/* NOTE: pthread.h must be included before errno.h for correct errno location
   definition.  Alternatively, define _REENTRANT as a compiler flag. */
#include <pthread.h>

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

#include "local_assert.h"
#include "print_error.h"


ASSERT_STRING;  /* Static copy of file name for assertions. */


static int log_header ();  /* log current time to stderr */


/* Print a timestamp, a formatted string, and an error description.  Returns
   the number of characters printed.
*/

int
print_error (int num, const char* fmt, ...)
{
    char* err;
    va_list args;
    int result;

    /* Check arguments. */
    ASSERT (num > 0 && fmt != NULL);

    /* Find the human-readable error code. */
    err = strerror (num);

    /* Print a timestamp to stderr. */
    result = log_header ();

    /* Print the formatted string to stderr. */
    va_start (args, fmt);
    result += vfprintf (stderr, fmt, args);
    va_end (args);

    /* If the error code was valid, append the explanation. */
    if (err != NULL)
        result += fprintf (stderr, " : %s\n", err);
    else
	fputc ('\n', stderr);
    
    return result;
}


/* Print a timestamp and a formatted string.  Return the number of characters
   printed.
*/

int
print_log (const char* fmt, ...)
{
    va_list args;
    int result;

    /* Check arguments. */
    ASSERT (fmt != NULL);

    result = log_header ();

    /* Print a timestamp to stderr. */
    va_start (args, fmt);

    /* Print the formatted string to stderr. */
    result += vfprintf (stderr, fmt, args);
    va_end (args);

    /* End the output line. */
    fputc ('\n', stderr);

    return result;
}


/* Print a timestamp to stderr. */

static int
log_header ()
{
    time_t tm = time (NULL);
    char header[20];

    strftime (header, 19, "%m/%d %H:%M : ", localtime (&tm));
    fputs (header, stderr);
    
    return strlen (header);
}