File: log.c

package info (click to toggle)
onak 0.3.2-1.1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 892 kB
  • ctags: 514
  • sloc: ansic: 7,879; perl: 223; makefile: 87; sh: 25; sql: 21
file content (196 lines) | stat: -rw-r--r-- 4,713 bytes parent folder | download | duplicates (4)
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
/*
 * log.c - Simple logging framework.
 *
 * Jonathan McDowell <noodles@earth.li>
 *
 * Copyright 2003 Project Purple
 */

#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>

#include "log.h"

/*
 *	logthres - holds the minimum log level we'll output
 *
 *	This variable keeps track of the threshold we've set for outputting
 *	logs - if we're asked to log something below this level we won't output
 *	it.
 */
static loglevels logthres = LOGTHING_NOTICE;

/*
 *	logappname - the name of the application using us.
 *
 *	This holds information about the name of the application we're being
 *	called by. It's set when we're initialized.
 */
static char *logappname = NULL;

/*
 *	logfilename - the file to log to.
 *
 *	The full name and path of the file we should log to.
 */
static char *logfilename = NULL;

/*
 *	initlogthing - initialize the logging module
 *	@appname: The application name to use in the log.
 *	@filename: The filename to log to. NULL means stderr.
 *
 *      This function sets up the logging module ready to log. The appname is
 *      written as part of every log entry and the filename is the file we
 *      should log to. If the appname is NULL then none is written. If the
 *      filename is NULL all output is sent to stderr.
 */
int initlogthing(const char *appname, const char *filename)
{
	if (appname != NULL) {
		logappname = strdup(appname);
	}

	if (filename != NULL) {
		logfilename = strdup(filename);
	}

	return 0;
}

/*
 *	cleanuplogthing - clean up the logging module
 *
 *      This function cleans up the logging module after use.
 */
void cleanuplogthing(void)
{
	if (logappname != NULL) {
		free(logappname);
		logappname = NULL;
	}

	if (logfilename != NULL) {
		free(logfilename);
		logfilename = NULL;
	}

	return;
}

/*
 *	setlogthreshold - set the threshold for log output
 *	@loglevel: The minimum log level we should output
 *
 *	Sets the threshold for log output; anything logged with a log level
 *	lower than this will be silently dropped. Returns the old log threshold
 *	value.
 */
loglevels setlogthreshold(loglevels loglevel)
{
	loglevels oldlevel;

	oldlevel = logthres;
	logthres = loglevel;

	return oldlevel;
}

/*
 *	vflog - write a log entry to an already opened log file.
 *      @logfile: The FILE * handle of the open log file.
 *      @format: A format string.
 *      @ap: The va_list of the parmeters for the format string.
 *
 *	This function outputs a log entry to an opened file. A leading
 *	time/date stamp and a trailing newline are automatically added. The
 *	format parameter is of the same nature as that used in vprintf.
 */
static void vflog(FILE *logfile, const char *format, va_list ap)
{
	struct tm *timestamp = NULL;
	time_t     timer = 0;

	timer = time(NULL);
	timestamp = localtime(&timer);

	fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
			timestamp->tm_mday,
			timestamp->tm_mon + 1,
			timestamp->tm_year + 1900,
			timestamp->tm_hour,
			timestamp->tm_min,
			timestamp->tm_sec,
			(logappname == NULL) ? "" : logappname,
			getpid());
	vfprintf(logfile, format, ap);
	fprintf(logfile, "\n");

	return;
}

/*
 *	flog - write a log entry to an already opened log file.
 *      @logfile: The FILE * handle of the open log file.
 *      @format: A format string.
 *
 *	This function outputs a log entry to an opened file. A leading
 *	time/date stamp and a trailing newline are automatically added. The
 *	format parameter is of the same nature as that used in printf.
 */
static void flog(FILE *logfile, const char *format, ...)
{
	va_list ap;

	va_start(ap, format);
	vflog(logfile, format, ap);
	va_end(ap);
}

/*
 *	logthing - output a log entry
 *      @loglevel: The level of the log.
 *      @format: A format string, followed by any parameters required.
 *
 *	This function outputs a log entry. A leading time/date stamp and a
 *	trailing newline are automatically added. The loglevel is compared to
 *	the current log threshold and if equal or above the log entry is
 *	output. The format parameter is of the same nature as that used in
 *	printf.
 */
int logthing(loglevels loglevel, const char *format, ...)
{
	FILE      *logfile = NULL;
	va_list    ap;

	if (loglevel >= logthres) {
		if (logfilename != NULL) {
			logfile = fopen(logfilename, "a");
			if (logfile != NULL) {
				flockfile(logfile);
			} else {
				logfile = stderr;
				flog(logfile, "Couldn't open logfile: %s",
						logfilename);
			}
		} else {
			logfile = stderr;
		}
	
		va_start(ap, format);
		vflog(logfile, format, ap);
		va_end(ap);

		if (logfile != stderr) {
			funlockfile(logfile);
			fclose(logfile);
			logfile = NULL;
		}
	}

	return 0;
}