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 #ifndef BACKENDS_LOG_LOG_H
24 #define BACKENDS_LOG_LOG_H
25 
26 #include "common/scummsys.h"
27 
28 class OSystem;
29 
30 namespace Common {
31 class WriteStream;
32 } // End of namespace Common
33 
34 namespace Backends {
35 namespace Log {
36 
37 /**
38  * Log file writer.
39  *
40  * This can be used by the backends to implement file logging functionality.
41  */
42 class Log {
43 public:
44 	/**
45 	 * Constructor for the logger object.
46 	 *
47 	 * @param system The OSystem instance to use. Must be non-null.
48 	 */
49 	Log(OSystem *system);
~Log()50 	~Log() { close(); }
51 
52 	/**
53 	 * Opens a new log file.
54 	 *
55 	 * The previous log, which was handled by this logger, will be closed
56 	 * before the new stream is associated.
57 	 *
58 	 * The current implemention will always call flush after data is written
59 	 * to the log file. It might thus be wise to pass an unbuffered write
60 	 * stream here to avoid unnecessary overhead.
61 	 * @see Common::WriteStream::flush
62 	 *
63 	 * Calling open with stream being 0 is valid and will result in the same
64 	 * behavior as calling close, but it may have additional overhead.
65 	 * @see close
66 	 *
67 	 * This function will output information about the ScummVM version and
68 	 * the features built into ScummVM automatically. It will also add a short
69 	 * notice to indicate that the log was opened successfully.
70 	 *
71 	 * @param stream Stream where to output the log contents.
72 	 *               Note that the stream will be deleted by the logger.
73 	 */
74 	void open(Common::WriteStream *stream);
75 
76 	/**
77 	 * Closes the current log file.
78 	 *
79 	 * This function will output a line saying that the log was closed
80 	 * successfully. This can be used to check whether a log is incomplete
81 	 * because of whatever reasons.
82 	 */
83 	void close();
84 
85 	/**
86 	 * Prints a message to the log stream.
87 	 *
88 	 * This has optional support to output a timestamp on every new line.
89 	 * The timestamp will look like: "[YYYY-MM-DD HH:MM:SS] ".
90 	 * Printing of a timestamp is done by default.
91 	 *
92 	 * It might be noteworthy that this function does not append a new line
93 	 * to the given message.
94 	 *
95 	 * In case no stream is associated with this logger, this function will
96 	 * quit immediatly.
97 	 *
98 	 * @param message            The message to write.
99 	 * @param printTimeOnNewline Whether to print a timestamp on the start of
100 	 *                           a new line.
101 	 */
102 	void print(const char *message, const bool printTimeOnNewline = true);
103 private:
104 	/**
105 	 * Prints a time stamp in the form: "[YYYY-MM-DD HH:MM:SS] ".
106 	 */
107 	void printTimeStamp();
108 
109 	/**
110 	 * The OSystem instance used to query data like the time.
111 	 */
112 	OSystem *_system;
113 
114 	/**
115 	 * Where to write the output too.
116 	 */
117 	Common::WriteStream *_stream;
118 
119 	/**
120 	 * Whether we are at the start of a line.
121 	 */
122 	bool _startOfLine;
123 };
124 
125 } // End of namespace Log
126 } // End of namespace Backends
127 
128 #endif
129