File: log.c

package info (click to toggle)
fdclone 3.00k-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 5,120 kB
  • sloc: ansic: 99,013; makefile: 4,257; sh: 1,432; sed: 196
file content (226 lines) | stat: -rw-r--r-- 4,258 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
/*
 *	log.c
 *
 *	system logging
 */

#include "fd.h"
#include "func.h"

#if	MSDOS
#include <process.h>
#endif
#ifndef	NOSYSLOG
#include <syslog.h>
#endif

#ifndef	LOG_PID
#define	LOG_PID			0
#endif
#ifndef	LOG_ERR
#define	LOG_ERR			3
#endif
#ifndef	LOG_INFO
#define	LOG_INFO		6
#endif

#ifdef	DEP_LOGGING

extern char *progname;

static lockbuf_t *NEAR openlogfile __P_((VOID_A));
static VOID NEAR writelog __P_((int, int, CONST char *));

char *logfile = NULL;
int logsize = 0;
#ifndef	NOSYSLOG
int usesyslog = 0;
#endif
int loglevel = 0;
#ifndef	NOUID
int rootloglevel = 0;
#endif

static char *logfname = NULL;
#ifndef	NOSYSLOG
static int syslogged = 0;
#endif


static lockbuf_t *NEAR openlogfile(VOID_A)
{
	lockbuf_t *lck;
	struct stat st;
	CONST char *home;
	char *cp, *top, path[MAXPATHLEN];
	ALLOC_T size;

	cp = logfname;
	if (logfname) {
		if (!*logfname) return(NULL);
	}
	else {
		if (!logfile || !*logfile) return(NULL);

		logfname = vnullstr;
		top = logfile;
#ifdef	DEP_DOSPATH
		if (_dospath(top)) top += 2;
#endif
		if (*top == _SC_) cp = logfile;
		else {
			if (!(home = gethomedir())) return(NULL);
			strcatdelim2(path, home, top);
			if (!*path) return(NULL);
			cp = Xstrdup(path);
		}
	}

	size = (ALLOC_T)logsize * (ALLOC_T)1024;
	if (size > 0 && Xstat(cp, &st) >= 0 && st.st_size > size) {
		VOID_C Xsnprintf(path, sizeof(path), "%s.old", cp);
		if (Xrename(cp, path) < 0) VOID_C Xunlink(cp);
	}

	lck = lockopen(cp, O_TEXT | O_WRONLY | O_CREAT | O_APPEND, 0666);
	if (!lck) {
		Xfree(cp);
		return(NULL);
	}

	logfname = cp;
	return(lck);
}

VOID logclose(VOID_A)
{
	if (logfname != vnullstr) Xfree(logfname);
	logfname = vnullstr;
#ifndef	NOSYSLOG
	if (syslogged > 0) closelog();
	syslogged = -1;
#endif
}

/*ARGSUSED*/
static VOID NEAR writelog(lvl, p, buf)
int lvl, p;
CONST char *buf;
{
	static int logging = 0;
	lockbuf_t *lck;
	struct tm *tm;
	char hbuf[MAXLOGLEN + 1];
	time_t t;
	u_char uc;
	int n;

	if (logging) return;
#ifndef	NOUID
	if (!getuid()) {
		n = rootloglevel;
		lvl--;
	}
	else
#endif
	n = loglevel;
	if (!n || n < lvl) return;

	logging = 1;
	if ((lck = openlogfile())) {
		t = time(NULL);
		tm = localtime(&t);
#ifdef	NOUID
		VOID_C Xsnprintf(hbuf, sizeof(hbuf),
			"%04u/%02u/%02u %02u:%02u:%02u %s[%d]:\n ",
			tm -> tm_year + 1900, tm -> tm_mon + 1, tm -> tm_mday,
			tm -> tm_hour, tm -> tm_min, tm -> tm_sec,
			progname, getpid());
#else
		VOID_C Xsnprintf(hbuf, sizeof(hbuf),
			"%04u/%02u/%02u %02u:%02u:%02u uid=%d %s[%d]:\n ",
			tm -> tm_year + 1900, tm -> tm_mon + 1, tm -> tm_mday,
			tm -> tm_hour, tm -> tm_min, tm -> tm_sec,
			getuid(), progname, getpid());
#endif
		VOID_C Xwrite(lck -> fd, hbuf, strlen(hbuf));
		VOID_C Xwrite(lck -> fd, buf, strlen(buf));
		uc = '\n';
		VOID_C Xwrite(lck -> fd, (char *)&uc, sizeof(uc));
		lockclose(lck);
	}
	logging = 0;
#ifndef	NOSYSLOG
	if (usesyslog && syslogged >= 0) {
		if (!syslogged) {
			syslogged++;
# ifdef	LOG_USER
			openlog(progname, LOG_PID, LOG_USER);
# else
			openlog(progname, LOG_PID);
# endif
		}
		syslog(p, "%s", buf);
	}
#endif	/* !NOSYSLOG */
}

#ifdef	USESTDARGH
/*VARARGS3*/
VOID logsyscall(int lvl, int val, CONST char *fmt, ...)
#else
/*VARARGS3*/
VOID logsyscall(lvl, val, fmt, va_alist)
int lvl, val;
CONST char *fmt;
va_dcl
#endif
{
	va_list args;
	char buf[MAXLOGLEN + 1];
	int n, len, duperrno;

	duperrno = errno;
	if (val >= 0) lvl++;
	VA_START(args, fmt);
	len = Xvsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

	if (len >= 0) {
#ifdef	CODEEUC
		len = strlen(buf);
#endif
		if (val >= 0) n = Xsnprintf(&(buf[len]),
			(int)sizeof(buf) - len, " succeeded");
		else n = Xsnprintf(&(buf[len]), (int)sizeof(buf) - len,
			" -- FAILED -- (%k)", Xstrerror(duperrno));
		if (n < 0) buf[len] = '\0';
		writelog(lvl, (val < 0) ? LOG_ERR : LOG_INFO, buf);
	}
	errno = duperrno;
}

#ifdef	USESTDARGH
/*VARARGS2*/
VOID logmessage(int lvl, CONST char *fmt, ...)
#else
/*VARARGS2*/
VOID logmessage(lvl, fmt, va_alist)
int lvl;
CONST char *fmt;
va_dcl
#endif
{
	va_list args;
	char buf[MAXLOGLEN + 1];
	int len, duperrno;

	duperrno = errno;
	VA_START(args, fmt);
	len = Xvsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

	if (len >= 0) writelog(lvl, LOG_INFO, buf);
	errno = duperrno;
}
#endif	/* DEP_LOGGING */