Skip to content

Commit

Permalink
Refactor|libshell|libdeng2: Added MemoryLogSink, used it in shell::Lo…
Browse files Browse the repository at this point in the history
…gWidget
  • Loading branch information
skyjake committed May 15, 2013
1 parent 2984a62 commit 0771314
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 49 deletions.
1 change: 1 addition & 0 deletions doomsday/libdeng2/include/de/MemoryLogSink
@@ -0,0 +1 @@
#include "core/memorylogsink.h"
55 changes: 55 additions & 0 deletions 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 <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/

#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<LogEntry *> _entries;
};

} // namespace de

#endif // LIBDENG2_MEMORYLOGSINK_H
3 changes: 3 additions & 0 deletions doomsday/libdeng2/libdeng2.pro
Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down Expand Up @@ -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 \
Expand Down
77 changes: 77 additions & 0 deletions 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 <jaakko.keranen@iki.fi>
*
* @par License
* GPL: http://www.gnu.org/licenses/gpl.html
*
* <small>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</small>
*/

#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
53 changes: 4 additions & 49 deletions doomsday/libshell/src/logwidget.cpp
Expand Up @@ -20,7 +20,7 @@
#include "de/shell/KeyEvent"
#include "de/shell/TextRootWidget"
#include <de/MonospaceLogSinkFormatter>
#include <de/Lockable>
#include <de/MemoryLogSink>
#include <de/LogBuffer>
#include <QList>

Expand All @@ -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<LogEntry *> _entries;
};

DENG2_PIMPL(LogWidget)
Expand Down

0 comments on commit 0771314

Please sign in to comment.