File: log.c

package info (click to toggle)
glimpse 4.1-2
  • links: PTS
  • area: non-free
  • in suites: slink
  • size: 2,344 kB
  • ctags: 2,254
  • sloc: ansic: 32,194; makefile: 561; sh: 170; perl: 142
file content (277 lines) | stat: -rw-r--r-- 6,041 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
static char rcsid[] = "$Id: log.c,v 1.15 1995/02/04 01:37:54 hardy Exp $";
/*
 *  log.c - Logging facilities for Essence system.
 *
 *  Darren Hardy, hardy@cs.colorado.edu, February 1994
 *
 *  ----------------------------------------------------------------------
 *  Copyright (c) 1994, 1995.  All rights reserved.
 *  
 *          Mic Bowman of Transarc Corporation.
 *          Peter Danzig of the University of Southern California.
 *          Darren R. Hardy of the University of Colorado at Boulder.
 *          Udi Manber of the University of Arizona.
 *          Michael F. Schwartz of the University of Colorado at Boulder. 
 *  
 *  This copyright notice applies to all code in Harvest other than
 *  subsystems developed elsewhere, which contain other copyright notices
 *  in their source text.
 *  
 *  The Harvest software was developed by the Internet Research Task
 *  Force Research Group on Resource Discovery (IRTF-RD).  The Harvest
 *  software may be used for academic, research, government, and internal
 *  business purposes without charge.  If you wish to sell or distribute
 *  the Harvest software to commercial clients or partners, you must
 *  license the software.  See
 *  http://harvest.cs.colorado.edu/harvest/copyright,licensing.html#licensing.
 *  
 *  The Harvest software is provided ``as is'', without express or
 *  implied warranty, and with no support nor obligation to assist in its
 *  use, correction, modification or enhancement.  We assume no liability
 *  with respect to the infringement of copyrights, trade secrets, or any
 *  patents, and are not responsible for consequential damages.  Proper
 *  use of the Harvest software is entirely the responsibility of the user.
 *  
 *  For those who are using Harvest for non-commercial purposes, you may
 *  make derivative works, subject to the following constraints:
 *  
 *  - You must include the above copyright notice and these accompanying 
 *    paragraphs in all forms of derivative works, and any documentation 
 *    and other materials related to such distribution and use acknowledge 
 *    that the software was developed at the above institutions.
 *  
 *  - You must notify IRTF-RD regarding your distribution of the 
 *    derivative work.
 *  
 *  - You must clearly notify users that your are distributing a modified 
 *    version and not the original Harvest software.
 *  
 *  - Any derivative product is also subject to the restrictions of the 
 *    copyright, including distribution and use limitations.
 */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/file.h>
#if defined(__STRICT_ANSI__)
#include <stdarg.h>
#else
#include <varargs.h>
#endif
#include "util.h"

/* Local functions */
static void standard_msg();
static void log_flush();

/* Local variables */
static FILE *fp_log = NULL;
static FILE *fp_errs = NULL;
static int pid;
static char *pname = NULL;

#if defined(USE_LOG_SYNC) && defined(HAVE_FLOCK)
static void lock_file(fp)
FILE *fp;
{
	if (flock(fileno(fp), LOCK_EX) < 0)
		log_errno("lockf");
	if (fseek(fp, 0, SEEK_END) < 0)
		log_errno("fseek");
}

static void unlock_file(fp)
FILE *fp;
{
	if (flock(fileno(fp), LOCK_UN) < 0)
		log_errno("lockf");
}
#else
#define lock_file(fp)		/* nops */
#define unlock_file(fp)
#endif

/*
 *  init_log() - Initializes the logging routines.  log() prints to 
 *  FILE *a, and errorlog() prints to FILE *b;
 */
void init_log(a, b)
FILE *a, *b;
{
	fp_log = a;
	fp_errs = b;
	pid = getpid();
	pname = NULL;
}

void init_log3(pn, a, b)
char *pn;
FILE *a, *b;
{
	fp_log = a;
	fp_errs = b;
	pid = getpid();
	pname = strdup(pn);
}

/*
 *  log() - used like printf(3).  Prints message to stdout.
 */
#if defined(__STRICT_ANSI__)
void log(char *fmt,...)
{
	va_list ap;

	if (fp_log == NULL)
		return;

	va_start(ap, fmt);
#else
void log(va_alist)
va_dcl
{
	va_list ap;
	char *fmt;

	if (fp_log == NULL)
		return;

	va_start(ap);
	fmt = va_arg(ap, char *);
#endif				/* __STRICT_ANSI__ */
	if (fp_log == NULL)
		return;

	lock_file(fp_log);
	standard_msg(fp_log);
	vfprintf(fp_log, fmt, ap);
	va_end(ap);
	log_flush(fp_log);
	unlock_file(fp_log);
}

/*
 *  errorlog() - used like printf(3).  Prints error message to stderr.
 */
#if defined(__STRICT_ANSI__)
void errorlog(char *fmt,...)
{
	va_list ap;

	if (fp_errs == NULL)
		return;

	va_start(ap, fmt);
#else
void errorlog(va_alist)
va_dcl
{
	va_list ap;
	char *fmt;

	if (fp_errs == NULL)
		return;

	va_start(ap);
	fmt = va_arg(ap, char *);
#endif				/* __STRICT_ANSI__ */

	if (fp_errs == NULL)
		return;

	lock_file(fp_errs);
	standard_msg(fp_errs);
	fprintf(fp_errs, "ERROR: ");
	vfprintf(fp_errs, fmt, ap);
	va_end(ap);
	log_flush(fp_errs);
	unlock_file(fp_errs);
}

/*
 *  fatal() - used like printf(3).  Prints error message to stderr and exits
 */
#if defined(__STRICT_ANSI__)
void fatal(char *fmt,...)
{
	va_list ap;

	if (fp_errs == NULL)
		exit(1);

	va_start(ap, fmt);
#else
void fatal(va_alist)
va_dcl
{
	va_list ap;
	char *fmt;

	if (fp_errs == NULL)
		exit(1);

	va_start(ap);
	fmt = va_arg(ap, char *);
#endif				/* __STRICT_ANSI__ */

	if (fp_errs == NULL)
		exit(1);

	lock_file(fp_errs);
	standard_msg(fp_errs);
	fprintf(fp_errs, "FATAL: ");
	vfprintf(fp_errs, fmt, ap);
	va_end(ap);
	log_flush(fp_errs);
	unlock_file(fp_errs);
	exit(1);
}

/*
 *  log_errno() - Same as perror(); doesn't print when errno == 0
 */
void log_errno(s)
char *s;
{
	if (errno != 0)
		errorlog("%s: %s\n", s, strerror(errno));
}


/*
 *  fatal_errno() - Same as perror()
 */
void fatal_errno(s)
char *s;
{
	fatal("%s: %s\n", s, strerror(errno));
}

/*
 *  standard_msg() - Prints the standard pid and timestamp
 */
static void standard_msg(fp)
FILE *fp;
{
	if (pname != NULL)
		fprintf(fp, "%7s: ", pname);
	else
		fprintf(fp, "%7d: ", pid);
#ifdef LOG_TIMES
	{
		time_t t = time(NULL);
		char buf[BUFSIZ];

		strftime(buf, BUFSIZ - 1, "%y%m%d %H:%M:%S:", localtime(&t));
		fprintf(fp, "%s ", buf);
	}
#endif
}

static void log_flush(fp)
FILE *fp;
{
	fflush(fp);
}