diff options
-rw-r--r-- | backends/log/log.cpp | 107 | ||||
-rw-r--r-- | backends/log/log.h | 132 | ||||
-rw-r--r-- | backends/module.mk | 1 |
3 files changed, 240 insertions, 0 deletions
diff --git a/backends/log/log.cpp b/backends/log/log.cpp new file mode 100644 index 0000000000..e7fb924189 --- /dev/null +++ b/backends/log/log.cpp @@ -0,0 +1,107 @@ +/* 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$ + * + */ + +#include "backends/log/log.h" + +#include "common/stream.h" +#include "common/str.h" +#include "common/system.h" + +#include "base/version.h" + +namespace Backends { +namespace Log { + +Log::Log(OSystem *system) + : _system(system), _stream(0), _startOfLine(true) { + assert(system); +} + +void Log::open(Common::WriteStream *stream) { + // Close the previous log + close(); + + _stream = stream; + + // Output information about the ScummVM version at the start of the log + // file + print(gScummVMFullVersion); + print("\n"); + print(gScummVMFeatures); + print("\n"); + print("--- Log opened.\n"); +} + +void Log::close() { + if (_stream) { + // Output a message to indicate that the log was closed successfully + print("--- Log closed successfully.\n"); + + delete _stream; + _stream = 0; + } +} + +void Log::print(const char *message, const bool printTime) { + if (!_stream) + return; + + while (*message) { + if (_startOfLine) { + _startOfLine = false; + if (printTime) + printTimeStamp(); + } + + const char *msgStart = message; + // scan for end of line/string + while (*message && *message != '\n') + ++message; + + if (*message == '\n') { + ++message; + _startOfLine = true; + } + + // TODO: It might be wise to check for write errors and/or incomplete + // writes here, since losing certain bits of the log is not nice. + _stream->write(msgStart, message - msgStart); + } + + _stream->flush(); +} + +void Log::printTimeStamp() { + TimeDate date; + _system->getTimeAndDate(date); + + _stream->writeString(Common::String::format("[%d-%02d-%02d %02d:%02d:%02d] ", + date.tm_year + 1900, date.tm_mon, date.tm_mday, + date.tm_hour, date.tm_min, date.tm_sec)); +} + +} // End of namespace Log +} // End of namespace Backends + diff --git a/backends/log/log.h b/backends/log/log.h new file mode 100644 index 0000000000..b869191541 --- /dev/null +++ b/backends/log/log.h @@ -0,0 +1,132 @@ +/* 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$ + * + */ + +#ifndef BACKENDS_LOG_LOG_H +#define BACKENDS_LOG_LOG_H + +#include "common/scummsys.h" + +class OSystem; + +namespace Common { +class WriteStream; +} // End of namespace Common + +namespace Backends { +namespace Log { + +/** + * Log file writer. + * + * This can be used by the backends to implement file logging functionality. + */ +class Log { +public: + /** + * Constructor for the logger object. + * + * @param system The OSystem instance to use. Must be non-null. + */ + Log(OSystem *system); + ~Log() { close(); } + + /** + * Opens a new log file. + * + * The previous log, which was handled by this logger, will be closed + * before the new stream is associated. + * + * The current implemention will always call flush after data is written + * to the log file. It might thus be wise to pass an unbuffered write + * stream here to avoid unnecessary overhead. + * @see Common::WriteStream::flush + * + * Calling open with stream being 0 is valid and will result in the same + * behavior as calling close, but it may have additional overhead. + * @see close + * + * This function will output information about the ScummVM version and + * the features built into ScummVM automatically. It will also add a short + * notice to indicate that the log was opened successfully. + * + * @param stream Stream where to output the log contents. + * Note that the stream will be deleted by the logger. + */ + void open(Common::WriteStream *stream); + + /** + * Closes the current log file. + * + * This function will output a line saying that the log was closed + * successfully. This can be used to check whether a log is incomplete + * because of whatever reasons. + */ + void close(); + + /** + * Prints a message to the log stream. + * + * This has optional support to output a timestamp on every new line. + * The timestamp will look like: "[YYYY-MM-DD HH:MM:SS] ". + * Printing of a timestamp is done by default. + * + * It might be noteworthy that this function does not append a new line + * to the given message. + * + * In case no stream is associated with this logger, this function will + * quit immediatly. + * + * @param message The message to write. + * @param printTimeOnNewline Whether to print a timestamp on the start of + * a new line. + */ + void print(const char *message, const bool printTimeOnNewline = true); +private: + /** + * Prints a time stamp in the form: "[YYYY-MM-DD HH:MM:SS] ". + */ + void printTimeStamp(); + + /** + * The OSystem instance used to query data like the time. + */ + OSystem *_system; + + /** + * Where to write the output too. + */ + Common::WriteStream *_stream; + + /** + * Whether we are at the start of a line. + */ + bool _startOfLine; +}; + +} // End of namespace Log +} // End of namespace Backends + +#endif + diff --git a/backends/module.mk b/backends/module.mk index db03e803d8..fab07511f9 100644 --- a/backends/module.mk +++ b/backends/module.mk @@ -15,6 +15,7 @@ MODULE_OBJS := \ keymapper/keymap.o \ keymapper/keymapper.o \ keymapper/remap-dialog.o \ + log/log.o \ midi/alsa.o \ midi/camd.o \ midi/coreaudio.o \ |