logII.c 2.74 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
Per Cederqvist's avatar
Per Cederqvist committed
2
 * $Id: logII.c,v 0.3 1993/10/10 17:04:28 ceder Exp $
Linus Tolke Y's avatar
Linus Tolke Y committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Copyright (C) 1991  Lysator Academic Computer Association.
 *
 * This file is part of the LysKOM server.
 * 
 * LysKOM is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by 
 * the Free Software Foundation; either version 1, or (at your option) 
 * any later version.
 * 
 * LysKOM 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.  See the GNU General Public License
 * for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with LysKOM; see the file COPYING.  If not, write to
 * Lysator, c/o ISY, Linkoping University, S-581 83 Linkoping, SWEDEN,
 * or the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, 
 * MA 02139, USA.
 *
 * Please mail bug reports to bug-lyskom@lysator.liu.se. 
 */
Per Cederqvist's avatar
Per Cederqvist committed
25 26 27 28 29 30 31
/*
 * log.c
 *
 * File created by ceder 1990-05-25.
 */

#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
32
#include <stdarg.h>
Per Cederqvist's avatar
Per Cederqvist committed
33 34
#include <stdlib.h>
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
35
#include <unistd.h>
Per Cederqvist's avatar
Per Cederqvist committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114

enum 
{
    MESSAGE,			/* No action when message_counter is 0. */
    WARNING,			/* Sync&Restart when message_counter is 0. */
    RESTART,			/* Sync&Restart immediately. */
    CRASH			/* Don't sync, but restart immediately. */
    } log_class;

typedef enum log_class Log_class;

    
extern void
log (Log_class	 class,
     const char *file,
     const char *function,
     int	*message_counter,
     const char *format, ...)
{
    va_list AP;
    time_t clock;
    Bool log_it = TRUE;
    Bool restart = FALSE;

    va_start(AP, format);
    time(&clock);

    switch(log_class)
    {
    case MESSAGE:
	if ( message_counter != NULL && message_counter > 0 )
	    message_counter--;
	else
	    log_it = FALSE;
	break;
    case WARNING:
	if ( message_counter != NULL )
	    if ( *message_counter > 0 )
		message_counter--;
	    else
		restart = TRUE;
	break;
    case RESTART:
    case CRASH:
	break;
#ifndef COMPILE_CHECKS
    default:
	fprintf(stderr, "Deep Internal Error: log() called"
		"with log_class %d.\n", log_class);
#endif
    }

    if ( log_it == TRUE )
    {
	fprintf(stderr, "--> %-19s %-29s %s", ctime(&clock));
	vfprintf(stderr, format, AP);
    }

    if ( log_class == RESTART || (log_class == WARNING
				  && message_counter != NULL
				  && *message_counter <= 0))
    {
	fprintf(stderr, "++> Syncing.\n");
	cache_sync();
    }

    if ( restart == TRUE || log_class == RESTART || log_class == CRASH )
    {
	fprintf(stderr, "+++> Restarting LysKOM.\n");
/* #include "CloseFileDescriptors" */
	execl("/usr/lyskom/bin/ramkomd", "ramkomd", NULL);
	fprintf(stderr, "+++> execl() failed.\n");
	exit(1);
    }

    va_end(AP);
}