From 4bbb92fd179fc00258264bd488270179ff246510 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Fri, 1 May 2015 23:50:00 +0200 Subject: Logging --- common/log.cpp | 27 ++++++++++++++++++++++----- common/log.h | 2 +- common/resourceaccess.cpp | 13 ++++++++++--- 3 files changed, 33 insertions(+), 9 deletions(-) diff --git a/common/log.cpp b/common/log.cpp index 5d35f40..14869bb 100644 --- a/common/log.cpp +++ b/common/log.cpp @@ -73,7 +73,7 @@ static QString colorCommand(QList colorCodes) return string; } -QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function) +QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea) { static DebugStream stream; QDebug debug(&stream); @@ -90,25 +90,42 @@ QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char int prefixColorCode = ANSI_Colors::DoNothing; switch (debugLevel) { case DebugLevel::Trace: - prefix = "Trace:"; + prefix = "Trace: "; break; case DebugLevel::Log: - prefix = "Log:"; + prefix = "Log: "; break; case DebugLevel::Warning: prefix = "Warning:"; prefixColorCode = ANSI_Colors::Red; break; case DebugLevel::Error: - prefix = "Error:"; + prefix = "Error: "; prefixColorCode = ANSI_Colors::Red; break; default: break; }; + bool showLocation = false; + bool showProgram = true; + const QString resetColor = colorCommand(ANSI_Colors::Reset); - debug << colorCommand(QList() << ANSI_Colors::Bold << prefixColorCode) + prefix + resetColor + QString(" %1(%2) %3:").arg(QString::fromLatin1(programName)).arg(unsigned(getpid())).arg(function) + resetColor/* << file << ":" << line */; + QString output; + output += colorCommand(QList() << ANSI_Colors::Bold << prefixColorCode) + prefix + resetColor; + if (showProgram) { + output += QString(" %1(%2)").arg(QString::fromLatin1(programName)).arg(unsigned(getpid())); + } + if (debugArea) { + output += colorCommand(QList() << ANSI_Colors::Bold << prefixColorCode) + QString(" %1 ").arg(QString::fromLatin1(debugArea)) + resetColor; + } + if (showLocation) { + output += QString(" %3").arg(function); + /*debug << file << ":" << line */ + } + output += ":"; + + debug << output; return debug; } diff --git a/common/log.h b/common/log.h index e06ac5a..ee92f46 100644 --- a/common/log.h +++ b/common/log.h @@ -9,7 +9,7 @@ enum DebugLevel { Error }; -QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function); +QDebug debugStream(DebugLevel debugLevel, int line, const char* file, const char* function, const char* debugArea = 0); #define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO) #define Log() debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO) diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index f448897..b7d569b 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp @@ -33,6 +33,11 @@ #include #include +#undef Trace +#define Trace() debugStream(DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess") +#undef Log +#define Log(IDENTIFIER) debugStream(DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess("+IDENTIFIER+")") + namespace Akonadi2 { @@ -163,6 +168,7 @@ ResourceAccess::ResourceAccess(const QByteArray &resourceName, QObject *parent) ResourceAccess::~ResourceAccess() { + log("Closing access"); if (!d->resultHandler.isEmpty()) { Warning() << "Left jobs running while shutting down ResourceAccess"; } @@ -310,7 +316,7 @@ void ResourceAccess::disconnected() void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) { if (error == QLocalSocket::PeerClosedError) { - Log() << "The resource closed the connection."; + Log(d->resourceName) << "The resource closed the connection."; } else { Warning() << QString("Connection error: %1 : %2").arg(error).arg(d->socket->errorString()); } @@ -325,6 +331,7 @@ void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) void ResourceAccess::readResourceMessage() { if (!d->socket || !d->socket->isValid()) { + Warning() << "No socket available"; return; } @@ -372,7 +379,7 @@ bool ResourceAccess::processMessageBuffer() auto buffer = GetNotification(d->partialMessageBuffer.constData() + headerSize); switch (buffer->type()) { case Akonadi2::NotificationType::NotificationType_Shutdown: - Log() << "Received shutdown notification."; + Log(d->resourceName) << "Received shutdown notification."; close(); break; default: @@ -399,7 +406,7 @@ void ResourceAccess::callCallbacks(int id) void ResourceAccess::log(const QString &message) { - Log() << d->resourceName + ": " + message; + Log(d->resourceName) << this << message; } } -- cgit v1.2.3