From: Sebastian Goth Date: Mon, 28 Jul 2008 10:56:32 +0000 (+0200) Subject: Optimized memory usage, cleaned up api. X-Git-Tag: 0.3.0~195 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=c9b7f5cfe4377cc242c24212fff48aad70192b48;hp=99b15ffe7d25b4574f6a85ba394d9514d8b6248a Optimized memory usage, cleaned up api. Loggerobjects don't get copied after initialization anymore. Removed Stream-struct; no more heap allocations made. Thanks for teaching me so good EgS! --- diff --git a/src/common/logger.cpp b/src/common/logger.cpp index 60cc98c2..59efa6b6 100644 --- a/src/common/logger.cpp +++ b/src/common/logger.cpp @@ -27,13 +27,12 @@ Logger::~Logger() { QDateTime date = QDateTime::currentDateTime(); - if(_stream->logLevel == DebugLevel) _stream->buffer.prepend("Debug: "); - else if (_stream->logLevel == InfoLevel) _stream->buffer.prepend("Info: "); - else if (_stream->logLevel == WarningLevel) _stream->buffer.prepend("Warning: "); - else if (_stream->logLevel == ErrorLevel) _stream->buffer.prepend("Error: "); - _stream->buffer.prepend(date.toString("yyyy-MM-dd hh:mm:ss ")); + if(_logLevel == DebugLevel) _buffer.prepend("Debug: "); + else if (_logLevel == InfoLevel) _buffer.prepend("Info: "); + else if (_logLevel == WarningLevel) _buffer.prepend("Warning: "); + else if (_logLevel == ErrorLevel) _buffer.prepend("Error: "); + _buffer.prepend(date.toString("yyyy-MM-dd hh:mm:ss ")); log(); - delete _stream; } void Logger::log() { @@ -44,7 +43,7 @@ void Logger::log() { else if (Global::parser.value("loglevel") == "Error") lvl = ErrorLevel; else lvl = InfoLevel; - if(_stream->logLevel < lvl) return; + if(_logLevel < lvl) return; // if we can't open logfile we log to stdout QTextStream out(stdout); @@ -53,9 +52,9 @@ void Logger::log() { file.setFileName(Global::parser.value("logfile")); if (file.open(QIODevice::Append | QIODevice::Text)) { out.setDevice(&file); - _stream->buffer.remove(QChar('\n')); + _buffer.remove(QChar('\n')); } } - out << _stream->buffer << "\n"; + out << _buffer << endl; if(file.isOpen()) file.close(); } diff --git a/src/common/logger.h b/src/common/logger.h index 708801ad..c372a11c 100644 --- a/src/common/logger.h +++ b/src/common/logger.h @@ -36,50 +36,39 @@ class Logger { ErrorLevel }; - inline Logger(LogLevel level) : _stream(new Stream(level)) {} + inline Logger(LogLevel level) : _stream(&_buffer, QIODevice::WriteOnly), _logLevel(level) {} ~Logger(); - inline Logger &operator<<(const char* t) { _stream->internalStream << QString::fromAscii(t); return *this; } - inline Logger &operator<<(QChar t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(bool t) { _stream->internalStream << (t ? "true" : "false"); return *this; } - inline Logger &operator<<(char t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(signed short t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(unsigned short t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(signed int t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(unsigned int t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(signed long t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(unsigned long t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(qint64 t) { _stream->internalStream << QString::number(t); return *this; } - inline Logger &operator<<(quint64 t) { _stream->internalStream << QString::number(t); return *this; } - inline Logger &operator<<(float t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(double t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(const QString & t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(const QLatin1String &t) { _stream->internalStream << t.latin1(); return *this; } - inline Logger &operator<<(const QByteArray & t) { _stream->internalStream << t ; return *this; } - inline Logger &operator<<(const void * t) { _stream->internalStream << t; return *this; } - inline Logger &operator<<(const QStringList & t) { _stream->internalStream << t.join(" "); return *this; } - inline Logger &operator<<(const BufferId & t) { _stream->internalStream << QVariant::fromValue(t).toInt(); return *this; } - inline Logger &operator<<(const NetworkId & t) { _stream->internalStream << QVariant::fromValue(t).toInt(); return *this; } - inline Logger &operator<<(const UserId & t) { _stream->internalStream << QVariant::fromValue(t).toInt(); return *this; } - inline Logger &operator<<(const MsgId & t) { _stream->internalStream << QVariant::fromValue(t).toInt(); return *this; } - inline Logger &operator<<(const IdentityId & t) { _stream->internalStream << QVariant::fromValue(t).toInt(); return *this; } - inline Logger &operator<<(const AccountId & t) { _stream->internalStream << QVariant::fromValue(t).toInt(); return *this; } + template + inline Logger &operator<<(const T &value) { _stream << value; return *this; } + inline Logger &operator<<(const QStringList & t) { _stream << t.join(" "); return *this; } + inline Logger &operator<<(const char* t) { _stream << QString::fromLocal8Bit(t); return *this; } + inline Logger &operator<<(bool t) { _stream << (t ? "true" : "false"); return *this; } - void log(); private: - struct Stream { - Stream(LogLevel level) - : internalStream(&buffer, QIODevice::WriteOnly), - logLevel(level) {} - QTextStream internalStream; - QString buffer; - LogLevel logLevel; - } *_stream; + void log(); + QTextStream _stream; + QString _buffer; + LogLevel _logLevel; +}; + +class quDebug : public Logger { + public: + inline quDebug() : Logger(Logger::DebugLevel) {} +}; + +class quInfo : public Logger { + public: + inline quInfo() : Logger(Logger::InfoLevel) {} }; -inline Logger quDebug() { return Logger(Logger::DebugLevel); } -inline Logger quInfo() { return Logger(Logger::InfoLevel); } -inline Logger quWarning() { return Logger(Logger::WarningLevel); } -inline Logger quError() { return Logger(Logger::ErrorLevel); } +class quWarning : public Logger { + public: + inline quWarning() : Logger(Logger::WarningLevel) {} +}; +class quError : public Logger { + public: + inline quError() : Logger(Logger::ErrorLevel) {} +}; #endif diff --git a/src/common/types.h b/src/common/types.h index 0154247e..c6a4bb1d 100644 --- a/src/common/types.h +++ b/src/common/types.h @@ -24,6 +24,7 @@ #include #include #include +#include class SignedId { protected: @@ -54,6 +55,7 @@ class SignedId { inline QDataStream &operator<<(QDataStream &out, const SignedId &signedId) { out << signedId.toInt(); return out; } inline QDataStream &operator>>(QDataStream &in, SignedId &signedId) { in >> signedId.id; return in; } +inline QTextStream &operator<<(QTextStream &out, const SignedId &signedId) { out << QString::number(signedId.toInt()); return out; } inline QDebug operator<<(QDebug dbg, const SignedId &signedId) { dbg.space() << signedId.toInt(); return dbg; } inline uint qHash(const SignedId &id) { return qHash(id.toInt()); }