diff --git a/doomsday/libdeng2/include/de/MemoryLogSink b/doomsday/libdeng2/include/de/MemoryLogSink new file mode 100644 index 0000000000..fc5ba73357 --- /dev/null +++ b/doomsday/libdeng2/include/de/MemoryLogSink @@ -0,0 +1 @@ +#include "core/memorylogsink.h" diff --git a/doomsday/libdeng2/include/de/core/memorylogsink.h b/doomsday/libdeng2/include/de/core/memorylogsink.h new file mode 100644 index 0000000000..8fc2ec3418 --- /dev/null +++ b/doomsday/libdeng2/include/de/core/memorylogsink.h @@ -0,0 +1,55 @@ +/** @file memorylogsink.h Log sink that stores log entries in memory. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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, see: + * http://www.gnu.org/licenses + */ + +#ifndef LIBDENG2_MEMORYLOGSINK_H +#define LIBDENG2_MEMORYLOGSINK_H + +#include "../LogSink" +#include "../Lockable" + +namespace de { + +/** + * Log sink that stores log entries in memory. + */ +class MemoryLogSink : public LogSink, public Lockable +{ +public: + MemoryLogSink(); + ~MemoryLogSink(); + + LogSink &operator << (LogEntry const &entry); + LogSink &operator << (String const &plainText); + + void flush() {} + + int entryCount() const; + LogEntry const &entry(int index) const; + void remove(int pos, int n = 1); + void clear(); + +protected: + virtual void addedNewEntry(LogEntry &entry); + +private: + QList _entries; +}; + +} // namespace de + +#endif // LIBDENG2_MEMORYLOGSINK_H diff --git a/doomsday/libdeng2/libdeng2.pro b/doomsday/libdeng2/libdeng2.pro index 59aa4af7cc..a5304b7003 100644 --- a/doomsday/libdeng2/libdeng2.pro +++ b/doomsday/libdeng2/libdeng2.pro @@ -80,6 +80,7 @@ HEADERS += \ include/de/LogSink \ include/de/Loop \ include/de/Matrix \ + include/de/MemoryLogSink \ include/de/MonospaceLogSinkFormatter \ include/de/Rectangle \ include/de/System \ @@ -112,6 +113,7 @@ HEADERS += \ include/de/core/logbuffer.h \ include/de/core/logsink.h \ include/de/core/loop.h \ + include/de/core/memorylogsink.h \ include/de/core/monospacelogsinkformatter.h \ include/de/core/system.h \ include/de/core/textapp.h \ @@ -142,6 +144,7 @@ SOURCES += \ src/core/logbuffer.cpp \ src/core/logsink.cpp \ src/core/loop.cpp \ + src/core/memorylogsink.cpp \ src/core/monospacelogsinkformatter.cpp \ src/core/system.cpp \ src/core/textapp.cpp \ diff --git a/doomsday/libdeng2/src/core/memorylogsink.cpp b/doomsday/libdeng2/src/core/memorylogsink.cpp new file mode 100644 index 0000000000..2308686567 --- /dev/null +++ b/doomsday/libdeng2/src/core/memorylogsink.cpp @@ -0,0 +1,77 @@ +/** @file memorylogsink.h Log sink that stores log entries in memory. + * + * @authors Copyright (c) 2013 Jaakko Keränen + * + * @par License + * GPL: http://www.gnu.org/licenses/gpl.html + * + * 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, see: + * http://www.gnu.org/licenses + */ + +#include "de/MemoryLogSink" + +namespace de { + +MemoryLogSink::MemoryLogSink() +{} + +MemoryLogSink::~MemoryLogSink() +{ + DENG2_GUARD(this); + + qDeleteAll(_entries); +} + +void MemoryLogSink::clear() +{ + DENG2_GUARD(this); + + qDeleteAll(_entries); + _entries.clear(); +} + +LogSink &MemoryLogSink::operator << (LogEntry const &entry) +{ + DENG2_GUARD(this); + + _entries.append(new LogEntry(entry)); + addedNewEntry(*_entries.back()); + return *this; +} + +LogSink &MemoryLogSink::operator << (String const &) +{ + // Ignore... + return *this; +} + +int MemoryLogSink::entryCount() const +{ + return _entries.size(); +} + +LogEntry const &MemoryLogSink::entry(int index) const +{ + return *_entries[index]; +} + +void MemoryLogSink::remove(int pos, int n) +{ + while(n-- > 0) + { + delete _entries.takeAt(pos); + } +} + +void MemoryLogSink::addedNewEntry(LogEntry &) +{} + +} // namespace de diff --git a/doomsday/libshell/src/logwidget.cpp b/doomsday/libshell/src/logwidget.cpp index 7f48e46d1b..53758552cc 100644 --- a/doomsday/libshell/src/logwidget.cpp +++ b/doomsday/libshell/src/logwidget.cpp @@ -20,7 +20,7 @@ #include "de/shell/KeyEvent" #include "de/shell/TextRootWidget" #include -#include +#include #include #include @@ -31,63 +31,18 @@ namespace shell { * Log sink for incoming entries (local and remote). Rather than formatting the * entries immediately, we keep a copy of them for formatting prior to drawing. */ -class Sink : public LogSink, public Lockable +class Sink : public MemoryLogSink { public: - Sink(LogWidget &widget) : LogSink(), _widget(widget) - {} + Sink(LogWidget &widget) : MemoryLogSink(), _widget(widget) {} - ~Sink() + void addedNewEntry(LogEntry &) { - qDeleteAll(_entries); - } - - void clear() - { - DENG2_GUARD(this); - - qDeleteAll(_entries); - _entries.clear(); - } - - LogSink &operator << (LogEntry const &entry) - { - DENG2_GUARD(this); - - _entries.append(new LogEntry(entry)); _widget.root().requestDraw(); - return *this; - } - - LogSink &operator << (String const &plainText) - { - DENG2_UNUSED(plainText); - return *this; - } - - void flush() {} - - int entryCount() const - { - return _entries.size(); - } - - LogEntry const &entry(int index) const - { - return *_entries[index]; - } - - void remove(int pos, int n = 1) - { - while(n-- > 0) - { - delete _entries.takeAt(pos); - } } private: LogWidget &_widget; - QList _entries; }; DENG2_PIMPL(LogWidget)