diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-04 16:56:09 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-02-04 16:56:09 +0100 |
commit | afa7c80114a327d85ee6c99b9d3d811107d9e204 (patch) | |
tree | d7b518fb1313f52f76f843a28628ee9329770c29 /common/resourceaccess.cpp | |
parent | 639cdb811c721eedf20b9f81b8cdc626b85c5f24 (diff) | |
download | sink-afa7c80114a327d85ee6c99b9d3d811107d9e204.tar.gz sink-afa7c80114a327d85ee6c99b9d3d811107d9e204.zip |
Introduced debug areas and prettified output.
Diffstat (limited to 'common/resourceaccess.cpp')
-rw-r--r-- | common/resourceaccess.cpp | 46 |
1 files changed, 21 insertions, 25 deletions
diff --git a/common/resourceaccess.cpp b/common/resourceaccess.cpp index 2e87bc7..345622c 100644 --- a/common/resourceaccess.cpp +++ b/common/resourceaccess.cpp | |||
@@ -43,9 +43,10 @@ | |||
43 | #include <QBuffer> | 43 | #include <QBuffer> |
44 | 44 | ||
45 | #undef Trace | 45 | #undef Trace |
46 | #define Trace() Sink::Log::debugStream(Sink::Log::DebugLevel::Trace, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess") | 46 | #define TracePrivate() Trace_area("client.communication." + resourceInstanceIdentifier) |
47 | #define Trace() Trace_area("client.communication." + d->resourceInstanceIdentifier) | ||
47 | #undef Log | 48 | #undef Log |
48 | #define Log(IDENTIFIER) Sink::Log::debugStream(Sink::Log::DebugLevel::Log, __LINE__, __FILE__, Q_FUNC_INFO, "ResourceAccess("+IDENTIFIER+")") | 49 | #define Log() Log_area("client.communication." + d->resourceInstanceIdentifier) |
49 | 50 | ||
50 | static void queuedInvoke(const std::function<void()> &f, QObject *context = 0) | 51 | static void queuedInvoke(const std::function<void()> &f, QObject *context = 0) |
51 | { | 52 | { |
@@ -168,7 +169,7 @@ KAsync::Job<void> ResourceAccess::Private::tryToConnect() | |||
168 | return !socket; | 169 | return !socket; |
169 | }, | 170 | }, |
170 | [this, counter](KAsync::Future<void> &future) { | 171 | [this, counter](KAsync::Future<void> &future) { |
171 | Trace() << "Loop"; | 172 | TracePrivate() << "Loop"; |
172 | KAsync::wait(50) | 173 | KAsync::wait(50) |
173 | .then(connectToServer(resourceInstanceIdentifier)) | 174 | .then(connectToServer(resourceInstanceIdentifier)) |
174 | .then<void, QSharedPointer<QLocalSocket> >([this, &future](const QSharedPointer<QLocalSocket> &s) { | 175 | .then<void, QSharedPointer<QLocalSocket> >([this, &future](const QSharedPointer<QLocalSocket> &s) { |
@@ -176,10 +177,10 @@ KAsync::Job<void> ResourceAccess::Private::tryToConnect() | |||
176 | socket = s; | 177 | socket = s; |
177 | future.setFinished(); | 178 | future.setFinished(); |
178 | }, | 179 | }, |
179 | [&future, counter](int errorCode, const QString &errorString) { | 180 | [&future, counter, this](int errorCode, const QString &errorString) { |
180 | const int maxRetries = 10; | 181 | const int maxRetries = 10; |
181 | if (*counter > maxRetries) { | 182 | if (*counter > maxRetries) { |
182 | Trace() << "Giving up"; | 183 | TracePrivate() << "Giving up"; |
183 | future.setError(-1, "Failed to connect to socket"); | 184 | future.setError(-1, "Failed to connect to socket"); |
184 | } else { | 185 | } else { |
185 | future.setFinished(); | 186 | future.setFinished(); |
@@ -192,21 +193,21 @@ KAsync::Job<void> ResourceAccess::Private::tryToConnect() | |||
192 | KAsync::Job<void> ResourceAccess::Private::initializeSocket() | 193 | KAsync::Job<void> ResourceAccess::Private::initializeSocket() |
193 | { | 194 | { |
194 | return KAsync::start<void>([this](KAsync::Future<void> &future) { | 195 | return KAsync::start<void>([this](KAsync::Future<void> &future) { |
195 | Trace() << "Trying to connect"; | 196 | TracePrivate() << "Trying to connect"; |
196 | connectToServer(resourceInstanceIdentifier).then<void, QSharedPointer<QLocalSocket> >([this, &future](const QSharedPointer<QLocalSocket> &s) { | 197 | connectToServer(resourceInstanceIdentifier).then<void, QSharedPointer<QLocalSocket> >([this, &future](const QSharedPointer<QLocalSocket> &s) { |
197 | Trace() << "Connected to resource, without having to start it."; | 198 | TracePrivate() << "Connected to resource, without having to start it."; |
198 | Q_ASSERT(s); | 199 | Q_ASSERT(s); |
199 | socket = s; | 200 | socket = s; |
200 | future.setFinished(); | 201 | future.setFinished(); |
201 | }, | 202 | }, |
202 | [this, &future](int errorCode, const QString &errorString) { | 203 | [this, &future](int errorCode, const QString &errorString) { |
203 | Trace() << "Failed to connect, starting resource"; | 204 | TracePrivate() << "Failed to connect, starting resource"; |
204 | //We failed to connect, so let's start the resource | 205 | //We failed to connect, so let's start the resource |
205 | QStringList args; | 206 | QStringList args; |
206 | args << resourceInstanceIdentifier; | 207 | args << resourceInstanceIdentifier; |
207 | qint64 pid = 0; | 208 | qint64 pid = 0; |
208 | if (QProcess::startDetached("sink_synchronizer", args, QDir::homePath(), &pid)) { | 209 | if (QProcess::startDetached("sink_synchronizer", args, QDir::homePath(), &pid)) { |
209 | Trace() << "Started resource " << pid; | 210 | TracePrivate() << "Started resource " << pid; |
210 | tryToConnect() | 211 | tryToConnect() |
211 | .then<void>([&future]() { | 212 | .then<void>([&future]() { |
212 | future.setFinished(); | 213 | future.setFinished(); |
@@ -232,12 +233,12 @@ ResourceAccess::ResourceAccess(const QByteArray &resourceInstanceIdentifier) | |||
232 | : ResourceAccessInterface(), | 233 | : ResourceAccessInterface(), |
233 | d(new Private(getResourceName(resourceInstanceIdentifier), resourceInstanceIdentifier, this)) | 234 | d(new Private(getResourceName(resourceInstanceIdentifier), resourceInstanceIdentifier, this)) |
234 | { | 235 | { |
235 | log("Starting access"); | 236 | Log() << "Starting access"; |
236 | } | 237 | } |
237 | 238 | ||
238 | ResourceAccess::~ResourceAccess() | 239 | ResourceAccess::~ResourceAccess() |
239 | { | 240 | { |
240 | log("Closing access"); | 241 | Log() << "Closing access"; |
241 | if (!d->resultHandler.isEmpty()) { | 242 | if (!d->resultHandler.isEmpty()) { |
242 | Warning() << "Left jobs running while shutting down ResourceAccess: " << d->resultHandler.keys(); | 243 | Warning() << "Left jobs running while shutting down ResourceAccess: " << d->resultHandler.keys(); |
243 | } | 244 | } |
@@ -400,7 +401,7 @@ void ResourceAccess::open() | |||
400 | 401 | ||
401 | void ResourceAccess::close() | 402 | void ResourceAccess::close() |
402 | { | 403 | { |
403 | log(QString("Closing %1").arg(d->socket->fullServerName())); | 404 | Log() << QString("Closing %1").arg(d->socket->fullServerName()); |
404 | Trace() << "Pending commands: " << d->pendingCommands.size(); | 405 | Trace() << "Pending commands: " << d->pendingCommands.size(); |
405 | Trace() << "Queued commands: " << d->commandQueue.size(); | 406 | Trace() << "Queued commands: " << d->commandQueue.size(); |
406 | d->socket->close(); | 407 | d->socket->close(); |
@@ -412,7 +413,7 @@ void ResourceAccess::sendCommand(const QSharedPointer<QueuedCommand> &command) | |||
412 | //TODO: we should have a timeout for commands | 413 | //TODO: we should have a timeout for commands |
413 | d->messageId++; | 414 | d->messageId++; |
414 | const auto messageId = d->messageId; | 415 | const auto messageId = d->messageId; |
415 | log(QString("Sending command \"%1\" with messageId %2").arg(QString(Sink::Commands::name(command->commandId))).arg(d->messageId)); | 416 | Log() << QString("Sending command \"%1\" with messageId %2").arg(QString(Sink::Commands::name(command->commandId))).arg(d->messageId); |
416 | Q_ASSERT(command->callback); | 417 | Q_ASSERT(command->callback); |
417 | registerCallback(d->messageId, [this, messageId, command](int errorCode, QString errorMessage) { | 418 | registerCallback(d->messageId, [this, messageId, command](int errorCode, QString errorMessage) { |
418 | Trace() << "Command complete " << messageId; | 419 | Trace() << "Command complete " << messageId; |
@@ -452,7 +453,7 @@ void ResourceAccess::connected() | |||
452 | return; | 453 | return; |
453 | } | 454 | } |
454 | 455 | ||
455 | log(QString("Connected: %1").arg(d->socket->fullServerName())); | 456 | Log() << QString("Connected: %1").arg(d->socket->fullServerName()); |
456 | 457 | ||
457 | { | 458 | { |
458 | flatbuffers::FlatBufferBuilder fbb; | 459 | flatbuffers::FlatBufferBuilder fbb; |
@@ -472,7 +473,7 @@ void ResourceAccess::connected() | |||
472 | 473 | ||
473 | void ResourceAccess::disconnected() | 474 | void ResourceAccess::disconnected() |
474 | { | 475 | { |
475 | log(QString("Disconnected from %1").arg(d->socket->fullServerName())); | 476 | Log() << QString("Disconnected from %1").arg(d->socket->fullServerName()); |
476 | d->socket->close(); | 477 | d->socket->close(); |
477 | emit ready(false); | 478 | emit ready(false); |
478 | } | 479 | } |
@@ -480,7 +481,7 @@ void ResourceAccess::disconnected() | |||
480 | void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) | 481 | void ResourceAccess::connectionError(QLocalSocket::LocalSocketError error) |
481 | { | 482 | { |
482 | if (error == QLocalSocket::PeerClosedError) { | 483 | if (error == QLocalSocket::PeerClosedError) { |
483 | Log(d->resourceInstanceIdentifier) << "The resource closed the connection."; | 484 | Log() << "The resource closed the connection."; |
484 | d->abortPendingOperations(); | 485 | d->abortPendingOperations(); |
485 | } else { | 486 | } else { |
486 | Warning() << QString("Connection error: %1 : %2").arg(error).arg(d->socket->errorString()); | 487 | Warning() << QString("Connection error: %1 : %2").arg(error).arg(d->socket->errorString()); |
@@ -524,14 +525,14 @@ bool ResourceAccess::processMessageBuffer() | |||
524 | switch (commandId) { | 525 | switch (commandId) { |
525 | case Commands::RevisionUpdateCommand: { | 526 | case Commands::RevisionUpdateCommand: { |
526 | auto buffer = Commands::GetRevisionUpdate(d->partialMessageBuffer.constData() + headerSize); | 527 | auto buffer = Commands::GetRevisionUpdate(d->partialMessageBuffer.constData() + headerSize); |
527 | log(QString("Revision updated to: %1").arg(buffer->revision())); | 528 | Log() << QString("Revision updated to: %1").arg(buffer->revision()); |
528 | emit revisionChanged(buffer->revision()); | 529 | emit revisionChanged(buffer->revision()); |
529 | 530 | ||
530 | break; | 531 | break; |
531 | } | 532 | } |
532 | case Commands::CommandCompletionCommand: { | 533 | case Commands::CommandCompletionCommand: { |
533 | auto buffer = Commands::GetCommandCompletion(d->partialMessageBuffer.constData() + headerSize); | 534 | auto buffer = Commands::GetCommandCompletion(d->partialMessageBuffer.constData() + headerSize); |
534 | log(QString("Command with messageId %1 completed %2").arg(buffer->id()).arg(buffer->success() ? "sucessfully" : "unsuccessfully")); | 535 | Log() << QString("Command with messageId %1 completed %2").arg(buffer->id()).arg(buffer->success() ? "sucessfully" : "unsuccessfully"); |
535 | 536 | ||
536 | d->completeCommands << buffer->id(); | 537 | d->completeCommands << buffer->id(); |
537 | //The callbacks can result in this object getting destroyed directly, so we need to ensure we finish our work first | 538 | //The callbacks can result in this object getting destroyed directly, so we need to ensure we finish our work first |
@@ -544,11 +545,11 @@ bool ResourceAccess::processMessageBuffer() | |||
544 | auto buffer = Commands::GetNotification(d->partialMessageBuffer.constData() + headerSize); | 545 | auto buffer = Commands::GetNotification(d->partialMessageBuffer.constData() + headerSize); |
545 | switch (buffer->type()) { | 546 | switch (buffer->type()) { |
546 | case Sink::Commands::NotificationType::NotificationType_Shutdown: | 547 | case Sink::Commands::NotificationType::NotificationType_Shutdown: |
547 | Log(d->resourceInstanceIdentifier) << "Received shutdown notification."; | 548 | Log() << "Received shutdown notification."; |
548 | close(); | 549 | close(); |
549 | break; | 550 | break; |
550 | case Sink::Commands::NotificationType::NotificationType_Inspection: { | 551 | case Sink::Commands::NotificationType::NotificationType_Inspection: { |
551 | Log(d->resourceInstanceIdentifier) << "Received inspection notification."; | 552 | Log() << "Received inspection notification."; |
552 | Notification n; | 553 | Notification n; |
553 | if (buffer->identifier()) { | 554 | if (buffer->identifier()) { |
554 | //Don't use fromRawData, the buffer is gone once we invoke emit notification | 555 | //Don't use fromRawData, the buffer is gone once we invoke emit notification |
@@ -583,11 +584,6 @@ bool ResourceAccess::processMessageBuffer() | |||
583 | return d->partialMessageBuffer.size() >= headerSize; | 584 | return d->partialMessageBuffer.size() >= headerSize; |
584 | } | 585 | } |
585 | 586 | ||
586 | void ResourceAccess::log(const QString &message) | ||
587 | { | ||
588 | Log(d->resourceInstanceIdentifier) << this << message; | ||
589 | } | ||
590 | |||
591 | } | 587 | } |
592 | 588 | ||
593 | #pragma clang diagnostic push | 589 | #pragma clang diagnostic push |