1 /* ScummVM - Graphic Adventure Engine
2 *
3 * ScummVM is the legal property of its developers, whose names
4 * are too numerous to list here. Please refer to the COPYRIGHT
5 * file distributed with this source distribution.
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 *
21 */
22
23 #include "backends/log/log.h"
24
25 #include "common/stream.h"
26 #include "common/str.h"
27 #include "common/system.h"
28
29 #include "base/version.h"
30
31 namespace Backends {
32 namespace Log {
33
Log(OSystem * system)34 Log::Log(OSystem *system)
35 : _system(system), _stream(0), _startOfLine(true) {
36 assert(system);
37 }
38
open(Common::WriteStream * stream)39 void Log::open(Common::WriteStream *stream) {
40 // Close the previous log
41 close();
42
43 _stream = stream;
44
45 // Output information about the ScummVM version at the start of the log
46 // file
47 print(gScummVMFullVersion);
48 print("\n");
49 print(gScummVMFeatures);
50 print("\n");
51 print("--- Log opened.\n");
52 _startOfLine = true;
53 }
54
close()55 void Log::close() {
56 if (_stream) {
57 // Output a message to indicate that the log was closed successfully
58 print("--- Log closed successfully.\n");
59
60 delete _stream;
61 _stream = 0;
62 }
63 }
64
print(const char * message,const bool printTime)65 void Log::print(const char *message, const bool printTime) {
66 if (!_stream)
67 return;
68
69 while (*message) {
70 if (_startOfLine) {
71 _startOfLine = false;
72 if (printTime)
73 printTimeStamp();
74 }
75
76 const char *msgStart = message;
77 // scan for end of line/string
78 while (*message && *message != '\n')
79 ++message;
80
81 if (*message == '\n') {
82 ++message;
83 _startOfLine = true;
84 }
85
86 // TODO: It might be wise to check for write errors and/or incomplete
87 // writes here, since losing certain bits of the log is not nice.
88 _stream->write(msgStart, message - msgStart);
89 }
90
91 _stream->flush();
92 }
93
printTimeStamp()94 void Log::printTimeStamp() {
95 TimeDate date;
96 int curMonth;
97 _system->getTimeAndDate(date);
98 curMonth = date.tm_mon + 1; // month is base 0, we need base 1 (1 = january and so on)
99
100 _stream->writeString(Common::String::format("[%d-%02d-%02d %02d:%02d:%02d] ",
101 date.tm_year + 1900, curMonth, date.tm_mday,
102 date.tm_hour, date.tm_min, date.tm_sec));
103 }
104
105 } // End of namespace Log
106 } // End of namespace Backends
107