log.c 3.72 KB
Newer Older
Linus Tolke Y's avatar
Linus Tolke Y committed
1
/*
2
 * $Id: log.c,v 0.22 1999/07/11 09:27:10 ceder Exp $
3
 * Copyright (C) 1991-1995, 1998-1999  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

37
static const char *
38
rcsid = "$Id: log.c,v 0.22 1999/07/11 09:27:10 ceder Exp $";
39
40
#include "rcs.h"
USE(rcsid);
Per Cederqvist's avatar
Per Cederqvist committed
41

Per Cederqvist's avatar
Per Cederqvist committed
42
43
44
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
45
#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
46
#include <sys/types.h>
Per Cederqvist's avatar
Per Cederqvist committed
47
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
48
#include <unistd.h>
49
#include <errno.h>
Per Cederqvist's avatar
Per Cederqvist committed
50

Per Cederqvist's avatar
Per Cederqvist committed
51
#include "log.h"
52
#include "lyskomd.h"
Per Cederqvist's avatar
Per Cederqvist committed
53
54
55
56
57

/*
 * Add a string to the log file.
 */

Per Cederqvist's avatar
Per Cederqvist committed
58
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
Per Cederqvist's avatar
Per Cederqvist committed
59
60
61

static void kom_logv(const char * format, va_list AP);

Per Cederqvist's avatar
Per Cederqvist committed
62
extern void
David Byers's avatar
David Byers committed
63
kom_log (const char * format, ...)
Per Cederqvist's avatar
Per Cederqvist committed
64
65
66
67
{
    va_list AP;

    va_start(AP, format);
David Byers's avatar
David Byers committed
68
    kom_logv(format, AP);
Per Cederqvist's avatar
Per Cederqvist committed
69
70
71
72
    va_end(AP);
}


Per Cederqvist's avatar
Per Cederqvist committed
73
static void
David Byers's avatar
David Byers committed
74
kom_logv (const char *format, va_list AP)
Per Cederqvist's avatar
Per Cederqvist committed
75
{
76
    time_t clk;
77
    struct tm *t;
Per Cederqvist's avatar
Per Cederqvist committed
78

79
80
    time(&clk);
    t = localtime(&clk);
Per Cederqvist's avatar
Per Cederqvist committed
81

82
    fprintf(stderr, "%02d%02d%02d %02d:%02d:%02d %ld ",
83
	    t->tm_year % 100, t->tm_mon + 1, t->tm_mday,
84
	    t->tm_hour, t->tm_min, t->tm_sec, (long)getpid());
Per Cederqvist's avatar
Per Cederqvist committed
85
86
87
88
    vfprintf(stderr, format, AP);

    fflush(stderr);
}
89
90
#else  /* !HAVE_VFPRINTF */
extern void
David Byers's avatar
David Byers committed
91
kom_log (const char * format, 
92
93
     int a, int b, int c, int d, int e, int f, int g)
{
94
    time_t clk;
95
96
    struct tm *t;

97
98
    time(&clk);
    t = localtime(&clk);
99

100
    fprintf(stderr, "%02d%02d%02d %02d:%02d:%02d %ld ",
101
	    t->tm_year % 100, t->tm_mon + 1, t->tm_mday,
102
	    t->tm_hour, t->tm_min, t->tm_sec, (long)getpid());
103
104
105
106
107
108
    fprintf(stderr, format, a, b, c, d, e, f, g);

    fflush(stderr);
    
}
#endif /* !HAVE_VFPRINTF */
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136


void
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
restart_kom(const char * format, ...)
#else
restart_kom(format,
	    a, b, c, d, e, f, g)
     const char *format;
     int a, b, c, d, e, f, g;
#endif
{
#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
    va_list AP;
#endif
#if defined(HAVE_GETCWD)
    char pathname[1026];
#else /* !defined(HAVE_GETCWD) */
    char *getwd(char *); /* +++ should be in sys/param.h */
# if defined(MAXPATHLEN)
    char pathname[MAXPATHLEN];
# else
    char pathname[1024];
# endif /* !defined(MAXPATHLEN) */
#endif /* !defined(HAVE_GETCWD) */

#if defined(HAVE_VFPRINTF) && defined(HAVE_STDARG_H)
    va_start(AP, format);
David Byers's avatar
David Byers committed
137
    kom_logv(format, AP);
138
139
    va_end(AP);
#else
David Byers's avatar
David Byers committed
140
    kom_log(format, a, b, c, d, e, f, g);
141
142
#endif

David Byers's avatar
David Byers committed
143
    kom_log("Previous message is fatal. Will dump core now.\n");
144
145
146
147

#ifdef HAVE_GETCWD
    /* getcwd is POSIX, so try that first. */
    if (getcwd(pathname, 1026) == NULL)
David Byers's avatar
David Byers committed
148
	kom_log("getcwd failed: errno %d\n", errno);
149
    else
David Byers's avatar
David Byers committed
150
	kom_log("Search for the core in %s\n", pathname);
151
152
#else
    if ( getwd(pathname) == NULL )
David Byers's avatar
David Byers committed
153
	kom_log("getwd failed: %s\n", pathname);
154
    else
David Byers's avatar
David Byers committed
155
	kom_log("Search for the core in %s\n", pathname);
156
157
#endif
    
David Byers's avatar
David Byers committed
158
159
160
#ifdef AVOID_ABORTS
    exit(255);
#else
161
    abort();
David Byers's avatar
David Byers committed
162
#endif
163
}