summaryrefslogtreecommitdiff
path: root/src/tet3/tccd/log.c
blob: 7fd6b47242b1d78aac2dec53b7e1b5aa809f4902 (plain)
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
/*
 *      SCCS:  @(#)log.c	1.12 (98/09/01) 
 *
 *	UniSoft Ltd., London, England
 *
 * (C) Copyright 1992 X/Open Company Limited
 *
 * All rights reserved.  No part of this source code may be reproduced,
 * stored in a retrieval system, or transmitted, in any form or by any
 * means, electronic, mechanical, photocopying, recording or otherwise,
 * except as stated in the end-user licence agreement, without the prior
 * permission of the copyright owners.
 *
 * X/Open and the 'X' symbol are trademarks of X/Open Company Limited in
 * the UK and other countries.
 */

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

/************************************************************************

SCCS:   	@(#)log.c	1.12 98/09/01 TETware release 3.3
NAME:		log.c
PRODUCT:	TETware
AUTHOR:		Andrew Dingwall, UniSoft Ltd.
DATE CREATED:	April 1992

DESCRIPTION:
	tccd error reporting functions

MODIFICATIONS:
	Andrew Dingwall, UniSoft Ltd., July 1997
	added support the MT DLL version of the C runtime support library
	on Win32 systems

	Andrew Dingwall, UniSoft Ltd., July 1998
	Added support for shared API libraries.

	Aaron Plattner, April 2010
	Fixed warnings when compiled with GCC's -Wall option.

************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "dtmac.h"
#include "error.h"
#include "globals.h"
#include "dtmsg.h"
#include "ptab.h"
#include "tccd.h"
#include "dtetlib.h"


/* the log file stream pointer */
static FILE *lfp;


/* static function declarations */
static FILE *getlogfp PROTOLIST((void));
static char *loghdr PROTOLIST((void));


/*
**	loginit() - open the TCCD log file
*/

void loginit()
{
	FILE *fp;
	int n;
	struct STAT_ST stbuf;
	static char devnull[] = "/dev/null";
	static char buf[BUFSIZ];
	extern char *Logfile;

	if (lfp == (FILE *) 0)
		lfp = stderr;

	/* make sure that the logfile open will get a fd > 2, otherwise
		all kinds of trouble breaks out when we dup fds later */
	for (n = 0; n < 3; n++)
		if (FSTAT(n, &stbuf) < 0 && OPEN(devnull, O_RDONLY, 0) < 0)
			fatal(errno, "can't open", devnull);
	errno = 0;

	/* open the log file */
	if ((fp = fopen(Logfile, "a")) == NULL)
		fatal(errno, "can't open log file", Logfile);

	/* allow anyone to read/write to the logfile */
	(void) CHMOD(Logfile, (mode_t)S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);

	/* attach stderr to the logfile */
	errno = 0;
	if ((lfp = freopen(Logfile, "a", stderr)) != stderr) {
		lfp = fp;
		fatal(errno, "can't reopen stderr as", Logfile);
	}

	(void) fclose(fp);

	setbuf(lfp, buf);

	if (tet_fappend(FILENO(lfp)) < 0)
		exit(1);
}

/*
**	logent() - make an entry in the TCCD log file
*/

void logent(s1, s2)
char *s1, *s2;
{
	register FILE *fp;

	fp = getlogfp();

	(void) fprintf(fp, "%s: %s", loghdr(), s1);
	if (s2 && *s2)
		(void) fprintf(fp, " %s", s2);
	(void) putc('\n', fp);
	(void) fflush(fp);
}

/*
**	logerror() - TCCD error printing routine
*/

void logerror(int errnum, const char *file, int line, const char *s1, const char *s2)
{
	tet_prerror(getlogfp(), errnum, loghdr(), file, line, s1, s2);
}

/*
**	loghdr() - construct a TCCD error message preamble
**		and return a pointer thereto
*/

static char *loghdr()
{
	static char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun",
		"Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };
	time_t t;
	register struct tm *tp;
	static char header[50];

	t = time((time_t *) 0);
	tp = localtime(&t);
	(void) sprintf(header, "%.16s (%d) %d %s %d:%02d:%02d",
		tet_progname, tet_mypid, tp->tm_mday, month[tp->tm_mon],
		tp->tm_hour, tp->tm_min, tp->tm_sec);

	return(header);
}

/*
**	getlogfp() - return the logfile stream pointer
*/

static FILE *getlogfp()
{
	struct STAT_ST stbuf;
	static FILE *cfp;
	static int cfd = -1;
	static char console[] = "/dev/console";


	if (!lfp || FSTAT(FILENO(lfp), &stbuf) < 0) {
		if (!cfp) {
			if (cfd < 0)
				cfd = OPEN(console, O_WRONLY | O_NOCTTY, 0);
			if (cfd >= 0)
				cfp = FDOPEN(cfd, "w");
		}
		return(cfp);
	}
	else
		return(lfp);
}