aboutsummaryrefslogtreecommitdiff
path: root/engines/sword25/kernel/log.cpp
blob: 259c02449fed9080cc7ddcd730aae07837c11248 (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
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
/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * $URL$
 * $Id$
 *
 */

/*
 * This code is based on Broken Sword 2.5 engine
 *
 * Copyright (c) Malte Thiesen, Daniel Queteschiner and Michael Elsdoerfer
 *
 * Licensed under GNU GPL v2
 *
 */

#include "sword25/kernel/log.h"
#include "base/version.h"
#include "common/config-manager.h"
#include "common/fs.h"

namespace Sword25 {

// Constants
static const char *BF_LOG_FILENAME = "log.txt";
static const size_t LOG_BUFFERSIZE = 1024 * 16;

// Logging will take place only when it's activated
#ifdef BS_ACTIVATE_LOGGING

Common::WriteStream                            *BS_Log::_LogFile = NULL;
bool                                            BS_Log::_LineBegin = true;
const char                                     *BS_Log::_Prefix = NULL;
const char                                     *BS_Log::_File = NULL;
int                                             BS_Log::_Line = 0;
bool                                            BS_Log::_AutoNewline = false;
Common::Array<BS_Log::LOG_LISTENER_CALLBACK>    BS_Log::_LogListener;

bool BS_Log::_CreateLog() {
	// Open the log file
	Common::FSNode dataDir(ConfMan.get("path"));
	Common::FSNode file = dataDir.getChild(BF_LOG_FILENAME);

	// Open the file for saving
	_LogFile = file.createWriteStream();

	if (_LogFile) {
		// Add a title into the log file
		Log("Broken Sword 2.5 Engine - Build: %s - %s - VersionID: %s\n", __DATE__, __TIME__, gScummVMFullVersion);
		Log("-----------------------------------------------------------------------------------------------------\n");

		return true;
	}

	// Log file could not be created
	return false;
}

void BS_Log::_CloseLog() {
	delete _LogFile;
	_LogFile = NULL;
}

void BS_Log::Log(const char *Format, ...) {
	char Message[LOG_BUFFERSIZE];

	// Create the message
	va_list ArgList;
	va_start(ArgList, Format);
	vsnprintf(Message, sizeof(Message), Format, ArgList);

	// Log the message
	_WriteLog(Message);

	_FlushLog();
}

void BS_Log::LogPrefix(const char *Prefix, const char *Format, ...) {
	char Message[LOG_BUFFERSIZE];
	char ExtFormat[LOG_BUFFERSIZE];

	// If the issue has ceased at the beginning of a new line, the new issue to begin with the prefix
	ExtFormat[0] = 0;
	if (_LineBegin) {
		snprintf(ExtFormat, sizeof(ExtFormat), "%s%s: ", ExtFormat, Prefix);
		_LineBegin = false;
	}
	// Format String pass line by line and each line with the initial prefix
	for (;;) {
		const char *NextLine = strstr(Format, "\n");
		if (!NextLine || *(NextLine + strlen("\n")) == 0) {
			snprintf(ExtFormat, sizeof(ExtFormat), "%s%s", ExtFormat, Format);
			if (NextLine) _LineBegin = true;
			break;
		} else {
			strncat(ExtFormat, Format, (NextLine - Format) + strlen("\n"));
			snprintf(ExtFormat, sizeof(ExtFormat), "%s%s: ", ExtFormat, Prefix);
		}

		Format = NextLine + strlen("\n");
	}

	// Create message
	va_list ArgList;
	va_start(ArgList, Format);
	vsnprintf(Message, sizeof(Message), ExtFormat, ArgList);

	// Log the message
	_WriteLog(Message);

	_FlushLog();
}

void BS_Log::LogDecorated(const char *Format, ...) {
	// Nachricht erzeugen
	char Message[LOG_BUFFERSIZE];
	va_list ArgList;
	va_start(ArgList, Format);
	vsnprintf(Message, sizeof(Message), Format, ArgList);

	// Zweiten Prefix erzeugen, falls gew�nscht
	char SecondaryPrefix[1024];
	if (_File && _Line)
		snprintf(SecondaryPrefix, sizeof(SecondaryPrefix), "(file: %s, line: %d) - ", _File, _Line);

	// Nachricht zeilenweise ausgeben und an jeden Zeilenanfang das Pr�fix setzen
	char *MessageWalker = Message;
	for (;;) {
		char *NextLine = strstr(MessageWalker, "\n");
		if (NextLine) {
			*NextLine = 0;
			if (_LineBegin) {
				_WriteLog(_Prefix);
				if (_File && _Line)
					_WriteLog(SecondaryPrefix);
			}
			_WriteLog(MessageWalker);
			_WriteLog("\n");
			MessageWalker = NextLine + sizeof("\n") - 1;
			_LineBegin = true;
		} else {
			if (_LineBegin) {
				_WriteLog(_Prefix);
				if (_File && _Line)
					_WriteLog(SecondaryPrefix);
			}
			_WriteLog(MessageWalker);
			_LineBegin = false;
			break;
		}
	}

	// Falls gew�nscht, wird ans Ende der Nachricht automatisch ein Newline angeh�ngt.
	if (_AutoNewline) {
		_WriteLog("\n");
		_LineBegin = true;
	}

	// Pseudoparameter zur�cksetzen
	_Prefix = NULL;
	_File = 0;
	_Line = 0;
	_AutoNewline = false;

	_FlushLog();
}

int BS_Log::_WriteLog(const char *Message) {
	if (!_LogFile) if (!_CreateLog()) return false;

	Common::Array<LOG_LISTENER_CALLBACK>::iterator Iter = _LogListener.begin();
	for (; Iter != _LogListener.end(); ++Iter)
		(*Iter)(Message);

	_LogFile->writeString(Message);

	return true;
}

void BS_Log::_FlushLog() {
	_LogFile->flush();
}

void (*BS_LogPtr)(const char *, ...) = BS_Log::Log;

void BS_Log_C(const char *Message) {
	BS_LogPtr(Message);
}

#else

void BS_Log_C(const char *Message) {};

#endif

} // End of namespace Sword25