logII.c 2.86 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: logII.c,v 0.15 2003/07/12 22:26:33 ceder Exp $
Per Cederqvist's avatar
Per Cederqvist committed
3
 * Copyright (C) 1991, 1993-1995, 1998-1999, 2001-2002  Lysator Academic Computer Association.
Linus Tolke Y's avatar
Linus Tolke Y committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * 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
/*
 * log.c
 *
 * File created by ceder 1990-05-25.
 */

David Byers's avatar
David Byers committed
31
32
33
34
35
36


#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

Per Cederqvist's avatar
Per Cederqvist committed
37
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
38
39
40
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
41
42
43
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
44
#include "timewrap.h"
Per Cederqvist's avatar
Per Cederqvist committed
45
#include <unistd.h>
Per Cederqvist's avatar
Per Cederqvist committed
46
47
48
49
50
51
52

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. */
53
} log_class;
Per Cederqvist's avatar
Per Cederqvist committed
54
55
56

    
extern void
57
log (enum log_class class,
Per Cederqvist's avatar
Per Cederqvist committed
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
     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");
107
	cache_sync_all();
Per Cederqvist's avatar
Per Cederqvist committed
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
    }

    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);
}