summaryrefslogtreecommitdiff
path: root/src/frontend/swlog.cpp
blob: 74b6bd4683ec52eedd7d8355a304161e4e1c9dea (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
/*
 * Copyright 2009 CrossWire Bible Society (http://www.crosswire.org)
 *	CrossWire Bible Society
 *	P. O. Box 2528
 *	Tempe, AZ  85280-2528
 *
 * 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 version 2.
 *
 * 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.
 *
 */

//---------------------------------------------------------------------------

#include <stdarg.h>
#include <stdio.h>
#include <iostream>
#if defined(_ICU_) && !defined(_ICUSWORD_)
#define _USTDIO_
#include <unicode/ustdio.h>
#include <unicode/ustream.h>
#endif
#include "swlog.h"
//---------------------------------------------------------------------------

SWORD_NAMESPACE_START


SWLog *SWLog::systemLog = 0;

const int SWLog::LOG_ERROR     = 1;
const int SWLog::LOG_WARN      = 2;
const int SWLog::LOG_INFO      = 3;
const int SWLog::LOG_TIMEDINFO = 4;
const int SWLog::LOG_DEBUG     = 5;

SWLog *SWLog::getSystemLog() {
	static class __staticSystemLog {
	SWLog **clear;
	public:
		__staticSystemLog(SWLog **clear) { this->clear = clear; }
		~__staticSystemLog() { delete *clear; *clear = 0; }
	} _staticSystemLog(&SWLog::systemLog);

	if (!systemLog)
		systemLog = new SWLog();

	return systemLog;
}


void SWLog::setSystemLog(SWLog *newLog) {
	delete getSystemLog();
	systemLog = newLog;
}


void SWLog::logWarning(const char *fmt, ...) const {
	char msg[2048];
	va_list argptr;

	if (logLevel >= LOG_WARN) {
		va_start(argptr, fmt);
		vsprintf(msg, fmt, argptr);
		va_end(argptr);
		logMessage(msg, LOG_WARN);
	}
}


void SWLog::logError(const char *fmt, ...) const {
	char msg[2048];
	va_list argptr;

	if (logLevel) {
		va_start(argptr, fmt);
		vsprintf(msg, fmt, argptr);
		va_end(argptr);
		logMessage(msg, LOG_ERROR);
	}
}


void SWLog::logInformation(const char *fmt, ...) const {
	char msg[2048];
	va_list argptr;

	if (logLevel >= LOG_INFO) {
		va_start(argptr, fmt);
		vsprintf(msg, fmt, argptr);
		va_end(argptr);
		logMessage(msg, LOG_INFO);
	}
}


void SWLog::logTimedInformation(const char *fmt, ...) const {
	char msg[2048];
	va_list argptr;

	if (logLevel >= LOG_TIMEDINFO) {
		va_start(argptr, fmt);
		vsprintf(msg, fmt, argptr);
		va_end(argptr);
		logMessage(msg, LOG_TIMEDINFO);
	}
}


void SWLog::logDebug(const char *fmt, ...) const {
	char msg[2048];
	va_list argptr;

	if (logLevel >= LOG_DEBUG) {
		va_start(argptr, fmt);
		vsprintf(msg, fmt, argptr);
		va_end(argptr);
		logMessage(msg, LOG_DEBUG);
	}
}

void SWLog::logMessage(const char *message, int level) const {
	std::cerr << message;
	std::cerr << std::endl;
}
SWORD_NAMESPACE_END