File: SimpleLog.c

package info (click to toggle)
spring 98.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 41,928 kB
  • ctags: 60,665
  • sloc: cpp: 356,167; ansic: 39,434; python: 12,228; java: 12,203; awk: 5,856; sh: 1,719; xml: 997; perl: 405; php: 253; objc: 194; makefile: 72; sed: 2
file content (210 lines) | stat: -rw-r--r-- 5,196 bytes parent folder | download
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
/*
	Copyright (c) 2008 Robin Vobruba <hoijui.quaero@gmail.com>

	This program 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 2 of the License, or
	(at your option) any later version.

	This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "SimpleLog.h"

#include "Util.h"

#include "System/maindefines.h"
#include "System/SafeCStrings.h"

#include <stdio.h>	// for file IO
#include <stdlib.h>	// calloc(), exit()
#include <string.h>	// strlen(), strcpy()
#include <time.h>	// for fetching current time
#include <stdarg.h>	// var-arg support

#define SIMPLELOG_OUTPUTBUFFER_SIZE 2048

#define     logFileName_sizeMax 2048
static bool logFileInitialized = false;
static char logFileName[logFileName_sizeMax];
static bool useTimeStamps;
static int logLevel;

static char* simpleLog_createTimeStamp() {

	time_t now;
	now = time(&now);
	struct tm* myTime = localtime(&now);
	unsigned int maxTimeStampSize = 32;
	char* timeStamp = (char*) calloc(maxTimeStampSize + 1, sizeof(char));
	strftime(timeStamp, maxTimeStampSize, "%c", myTime);

	return timeStamp;
}

static void simpleLog_out(int level, const char* msg) {

	if (level > logLevel) {
		return;
	}

	static char outBuffer[SIMPLELOG_OUTPUTBUFFER_SIZE];

	// format the message
	const char* logLevel_str = simpleLog_levelToStr(level);
	if (useTimeStamps) {
		char* timeStamp = simpleLog_createTimeStamp();
		SNPRINTF(outBuffer, SIMPLELOG_OUTPUTBUFFER_SIZE, "%s / %s(%i): %s\n",
				timeStamp, logLevel_str, level, msg);
		free(timeStamp);
		timeStamp = NULL;
	} else {
		SNPRINTF(outBuffer, SIMPLELOG_OUTPUTBUFFER_SIZE, "%s(%i): %s\n",
				logLevel_str, level, msg);
	}

	// try to open the log file
	FILE* file = NULL;
	if (logFileInitialized) {
		file = FOPEN(logFileName, "a");
	}

	// print the message
	if (file != NULL) {
		FPRINTF(file, "%s", outBuffer);
		fclose(file);
		file = NULL;
	} else {
		// fallback method: write to stdout
		if (level > SIMPLELOG_LEVEL_WARNING || level < 0) {
			FPRINTF(stdout, "%s", outBuffer);
		} else {
			FPRINTF(stderr, "%s", outBuffer);
		}
	}
}

static void simpleLog_logv(int level, const char* fmt, va_list argp) {

	if (level > logLevel) {
		return;
	}

	static char outBuffer[SIMPLELOG_OUTPUTBUFFER_SIZE];

	VSNPRINTF(outBuffer, SIMPLELOG_OUTPUTBUFFER_SIZE, fmt, argp);
	simpleLog_out(level, outBuffer);
}

void simpleLog_init(const char* _logFileName, bool _useTimeStamps,
		int _logLevel, bool append) {

	if (_logFileName != NULL) {
		logFileInitialized = false;
		bool initOk = true;
		STRCPY_T(logFileName, logFileName_sizeMax, _logFileName);

		// make sure the dir of the log file exists
		char* logFileDir = util_allocStrCpy(logFileName);
		if (initOk && !util_getParentDir(logFileDir)) {
			simpleLog_logL(SIMPLELOG_LEVEL_ERROR,
					"Failed to evaluate the parent dir of the config file: %s",
					logFileName);
			initOk = false;
		}

		if (initOk && !util_makeDir(logFileDir, true)) {
			simpleLog_logL(SIMPLELOG_LEVEL_ERROR,
					"Failed to create the parent dir of the config file: %s",
					logFileDir);
			initOk = false;
		}

		free(logFileDir);

		// delete the logFile, and try writing to it
		FILE* file = NULL;
		if (initOk) {
			if (append) {
				file = FOPEN(logFileName, "a");
			} else {
				file = FOPEN(logFileName, "w");
			}
		}
		if (file != NULL) {
			// make the file empty
			FPRINTF(file, "%s", "");
			fclose(file);
			file = NULL;
		} else {
			// report the error to stderr
			FPRINTF(stderr, "Failed writing to the log file \"%s\".\n%s",
					logFileName, "We will continue logging to stdout.");
		}

		useTimeStamps = _useTimeStamps;
		logLevel = _logLevel;
		logFileInitialized = initOk;
	} else {
		simpleLog_logL(-1, "No log file name supplied -> logging to stdout and stderr",
			useTimeStamps ? "yes" : "no", logLevel);
		logFileInitialized = false;
	}

	simpleLog_logL(-1, "[logging started (time-stamps: %s / logLevel: %i)]",
			useTimeStamps ? "yes" : "no", logLevel);
}

void simpleLog_logL(int level, const char* fmt, ...) {

	if (level > logLevel) {
		return;
	}

	va_list argp;

	va_start(argp, fmt);
	simpleLog_logv(level, fmt, argp);
	va_end(argp);
}

void simpleLog_log(const char* fmt, ...) {

	static const int level = SIMPLELOG_LEVEL_NORMAL;

	if (level > logLevel) {
		return;
	}

	va_list argp;

	va_start(argp, fmt);
	simpleLog_logv(level, fmt, argp);
	va_end(argp);
}

const char* simpleLog_levelToStr(int logLevel) {

	switch (logLevel) {
		case SIMPLELOG_LEVEL_ERROR:
			return "ERROR";
		case SIMPLELOG_LEVEL_WARNING:
			return "WARNING";
		case SIMPLELOG_LEVEL_NORMAL:
			return "NORMAL";
		case SIMPLELOG_LEVEL_FINE:
			return "FINE";
		case SIMPLELOG_LEVEL_FINER:
			return "FINER";
		case SIMPLELOG_LEVEL_FINEST:
			return "FINEST";
		default:
			return "CUSTOM";
	}
}