From 25c021cf36e2ce1924d2fb253f147c3ed8726bf2 Mon Sep 17 00:00:00 2001 From: Wolf Bergenheim Date: Thu, 18 Nov 2010 07:43:29 +0200 Subject: Added support for the -p (prefix) flag --- resourceqt-client/client.cpp | 51 +++++++++++++++++---------------- resourceqt-client/client.h | 1 + resourceqt-client/commandlineparser.cpp | 15 +++++++++- resourceqt-client/commandlineparser.h | 3 ++ 4 files changed, 45 insertions(+), 25 deletions(-) diff --git a/resourceqt-client/client.cpp b/resourceqt-client/client.cpp index 25ae5a1..d190498 100644 --- a/resourceqt-client/client.cpp +++ b/resourceqt-client/client.cpp @@ -29,7 +29,8 @@ USA. #include "client.h" -#define outputln output << "\n" +#define OUTPUT output << prefix +#define outputln output << "\n" << prefix using namespace ResourcePolicy; @@ -52,7 +53,7 @@ CommandListArgs::~CommandListArgs() Client::Client() : QObject(), standardInput(stdin, QIODevice::ReadOnly), stdInNotifier(0, QSocketNotifier::Read), pendingAddAudio(false), applicationClass(), - resourceSet(NULL), output(stdout) + resourceSet(NULL), output(stdout), prefix("") { commandList["help"] = CommandListArgs("", "print this help message"); commandList["quit"] = CommandListArgs("", "exit application"); @@ -75,7 +76,7 @@ Client::~Client() void Client::showPrompt() { - output << "resource-Qt> " << flush; + OUTPUT << "resource-Qt> " << flush; } bool Client::initialize(const CommandLineParser &parser) @@ -83,12 +84,14 @@ bool Client::initialize(const CommandLineParser &parser) QSet allResources; QSet optionalResources; + prefix = parser.getPrefix(); + if (parser.shouldAlwaysReply()) { - output << "client: AlwaysReply" << endl; + OUTPUT << "client: AlwaysReply" << endl; } if (parser.shouldAutoRelease()) { - output << "client: AutoRelease" << endl; + OUTPUT << "client: AutoRelease" << endl; } resourceSet = new ResourceSet(parser.resourceApplicationClass(), this, @@ -133,7 +136,7 @@ bool Client::initialize(const CommandLineParser &parser) this, SLOT(doExit()))) { return false; } - output << "accepting input" << endl; + OUTPUT << "accepting input" << endl; showPrompt(); return true; @@ -218,7 +221,7 @@ void Client::resourceAcquiredHandler(const QList&) grantedResources << resource->type(); } } - output << "granted:" << grantedResources << endl; + OUTPUT << "granted:" << grantedResources << endl; } showPrompt(); } @@ -230,7 +233,7 @@ void Client::resourceDeniedHandler() outputln << "Operation took " << ms << "ms" << endl; } QList allResources = resourceSet->resources(); - output << "denied:" << allResources << endl; + OUTPUT << "denied:" << allResources << endl; showPrompt(); } @@ -291,11 +294,11 @@ void Client::readLine(int) return; } else if (command == "help") { - output << "Available commands:\n"; + OUTPUT << "Available commands:\n"; QMap::const_iterator i = commandList.constBegin(); while (i != commandList.constEnd()) { - output << qSetFieldWidth(10) << right << i.key() + OUTPUT << qSetFieldWidth(10) << right << i.key() << qSetFieldWidth(1) << " " << qSetFieldWidth(55) << left << i.value().args << qSetFieldWidth(0) << i.value().help << endl; @@ -309,7 +312,7 @@ void Client::readLine(int) else { QList list = resourceSet->resources(); if (!list.count()) { - output << "Resource set is empty, use add command to add some." + OUTPUT << "Resource set is empty, use add command to add some." << endl; } else { @@ -335,10 +338,10 @@ void Client::readLine(int) QString resourceList, flag; input >> resourceList >> flag; if (!resourceSet) { - qCritical("%s failed!", qPrintable(command)); + qCritical("%s%s failed!", qPrintable(prefix), qPrintable(command)); } else if (resourceList.isEmpty() || resourceList.isNull()) { - output << "List of desired resources is missing. Use help."; + OUTPUT << "List of desired resources is missing. Use help."; } else { bool optional = false; @@ -361,10 +364,10 @@ void Client::readLine(int) QString resourceList, flag; input >> resourceList >> flag; if (!resourceSet) { - qCritical("%s failed!", qPrintable(command)); + qCritical("%s%s failed!", qPrintable(prefix), qPrintable(command)); } else if (resourceList.isEmpty() || resourceList.isNull()) { - output << "List of desired resources is missing. Use help."; + OUTPUT << "List of desired resources is missing. Use help."; } else { QSet resToRemove; @@ -383,7 +386,7 @@ void Client::readLine(int) } else if (command == "update") { if (!resourceSet || !resourceSet->update()) { - qCritical("%s failed!", qPrintable(command)); + qCritical("%s%s failed!", qPrintable(prefix), qPrintable(command)); } } else if (command == "audio") { @@ -392,14 +395,14 @@ void Client::readLine(int) input >> what; if (what.isEmpty() || what.isNull()) { - output << "Not enough parameters! See help" << endl; + OUTPUT << "Not enough parameters! See help" << endl; } else { Resource *resource = resourceSet->resource(AudioPlaybackType); AudioResource *audioResource = static_cast(resource); qDebug("resource = %p audioResource = %p", resource, audioResource); if (audioResource == NULL) { - output << "No AudioResource available in set!" << endl; + OUTPUT << "No AudioResource available in set!" << endl; } else { if (what == "group") { @@ -413,14 +416,14 @@ void Client::readLine(int) audioResource->setProcessID(pid); } else { - output << "Bad pid parameter!" << endl; + OUTPUT << "Bad pid parameter!" << endl; } } else if (what == "tag") { input >> tagName >> tagValue; if (tagName.isEmpty() || tagName.isNull() || tagValue.isEmpty() || tagValue.isNull()) { - output << "tag requires 2 parameters name and value. See help" + OUTPUT << "tag requires 2 parameters name and value. See help" << endl; } else { @@ -428,7 +431,7 @@ void Client::readLine(int) } } else { - output << "Unknown audio command!"; + OUTPUT << "Unknown audio command!"; } } } @@ -439,12 +442,12 @@ void Client::readLine(int) input >> group >> pid >> tagName >> tagValue; if (group.isEmpty() || (pid == 0) || tagName.isEmpty() || tagValue.isEmpty()) { - output << "Invalid parameters! See help!" << endl; + OUTPUT << "Invalid parameters! See help!" << endl; } else { AudioResource *audioResource = new AudioResource(group); if (audioResource == NULL) { - output << "Failed to create an AudioResource object!" << endl; + OUTPUT << "Failed to create an AudioResource object!" << endl; } else { audioResource->setProcessID(pid); @@ -460,7 +463,7 @@ void Client::readLine(int) resourceSet = new ResourceSet(applicationClass); } else { - output << "unknown command '" << command << "'" << endl; + OUTPUT << "unknown command '" << command << "'" << endl; } showPrompt(); diff --git a/resourceqt-client/client.h b/resourceqt-client/client.h index c22475d..140c668 100644 --- a/resourceqt-client/client.h +++ b/resourceqt-client/client.h @@ -70,6 +70,7 @@ private: QString applicationClass; ResourcePolicy::ResourceSet *resourceSet; QTextStream output; + QString prefix; static QMap commandList; diff --git a/resourceqt-client/commandlineparser.cpp b/resourceqt-client/commandlineparser.cpp index f169801..24658ea 100644 --- a/resourceqt-client/commandlineparser.cpp +++ b/resourceqt-client/commandlineparser.cpp @@ -28,7 +28,7 @@ QHash CommandLineParser::resourceValues; CommandLineParser::CommandLineParser(): allResources(), optResources(), autoRelease(false), alwaysReply(false), - verbose(false), allowUnkownResourceClass(false), output(stdout) + verbose(false), allowUnkownResourceClass(false), output(stdout), prefix("") { resourceValues["AudioPlayback"] = ResourcePolicy::AudioPlaybackType; resourceValues["VideoPlayback"] = ResourcePolicy::VideoPlaybackType; @@ -82,6 +82,9 @@ bool CommandLineParser::parseArguments() case 'v': verbose = true; break; + case 'p': + parsePrefix(*(++ci)); + break; case 'h': default: usage(); @@ -139,6 +142,16 @@ bool CommandLineParser::parseClassString(const QString &str) return true; } +void CommandLineParser::parsePrefix(const QString &str) +{ + prefix = str + ": "; +} + +QString CommandLineParser::getPrefix() const +{ + return prefix; +} + bool CommandLineParser::parseResourceList(const QString &resourceListStr, QSet &resources) { diff --git a/resourceqt-client/commandlineparser.h b/resourceqt-client/commandlineparser.h index 000fb80..1d106f4 100644 --- a/resourceqt-client/commandlineparser.h +++ b/resourceqt-client/commandlineparser.h @@ -46,6 +46,7 @@ public: bool shouldAutoRelease() const; bool shouldAlwaysReply() const; bool shouldBeVerbose() const; + QString getPrefix() const; private: QSet allResources; @@ -57,8 +58,10 @@ private: bool verbose; bool allowUnkownResourceClass; QTextStream output; + QString prefix; bool parseClassString(const QString &str); + void parsePrefix(const QString &str); bool parseModeValues(const QString &modeListStr); void usage(); -- cgit v1.2.3 From 7cf4a74e8a1a548d74f58e7f7f16796fa11a6b61 Mon Sep 17 00:00:00 2001 From: Wolf Bergenheim Date: Thu, 18 Nov 2010 07:53:46 +0200 Subject: Updated usage help and added timings flag -i --- resourceqt-client/commandlineparser.cpp | 16 +++++++++++++--- resourceqt-client/commandlineparser.h | 2 ++ 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/resourceqt-client/commandlineparser.cpp b/resourceqt-client/commandlineparser.cpp index 24658ea..cb477fd 100644 --- a/resourceqt-client/commandlineparser.cpp +++ b/resourceqt-client/commandlineparser.cpp @@ -28,7 +28,8 @@ QHash CommandLineParser::resourceValues; CommandLineParser::CommandLineParser(): allResources(), optResources(), autoRelease(false), alwaysReply(false), - verbose(false), allowUnkownResourceClass(false), output(stdout), prefix("") + verbose(false), allowUnkownResourceClass(false), output(stdout), prefix(""), + timings(false) { resourceValues["AudioPlayback"] = ResourcePolicy::AudioPlaybackType; resourceValues["VideoPlayback"] = ResourcePolicy::VideoPlaybackType; @@ -63,6 +64,8 @@ bool CommandLineParser::parseArguments() case 'm': case 's': case 'i': + timings = true; + break; case 't': break; case 'f': @@ -195,11 +198,14 @@ bool CommandLineParser::parseModeValues(const QString &modeListStr) void CommandLineParser::usage() { - output << "usage: resourceqt-client [-h] [-m mode-values]" << - "[-o optional-resources] [-s shared-resources -m shared-mask] " << + output << "usage: resourceqt-client [-h] [-f mode-values]" << + "[-o optional-resources] [-i] [-v] [-p prefix] " << "class all-resources" << endl; output << "\toptions:" << endl; output << "\t h\tprint this help message and exit" << endl; + output << "\t i\tshow timings of requests" << endl; + output << "\t v\tshow debug of libresourceqt" << endl; + output << "\t p\tPrefix all output with the given prefix" << endl; output << "\t f\tmode values. See 'modes' below for the " "\n\t\tsyntax of " << endl; output << "\t o\toptional resources. See 'resources' below for the " @@ -260,3 +266,7 @@ bool CommandLineParser::shouldBeVerbose() const return verbose; } +bool CommandLineParser::showTimings() const +{ + return timings; +} diff --git a/resourceqt-client/commandlineparser.h b/resourceqt-client/commandlineparser.h index 1d106f4..deab478 100644 --- a/resourceqt-client/commandlineparser.h +++ b/resourceqt-client/commandlineparser.h @@ -47,6 +47,7 @@ public: bool shouldAlwaysReply() const; bool shouldBeVerbose() const; QString getPrefix() const; + bool showTimings() const; private: QSet allResources; @@ -59,6 +60,7 @@ private: bool allowUnkownResourceClass; QTextStream output; QString prefix; + bool timings; bool parseClassString(const QString &str); void parsePrefix(const QString &str); -- cgit v1.2.3 From 2db176e028c54684e2fe6adf58ab0c5807d298c9 Mon Sep 17 00:00:00 2001 From: Wolf Bergenheim Date: Thu, 18 Nov 2010 08:11:11 +0200 Subject: Took -i and -v flags into use --- resourceqt-client/client.cpp | 54 +++++++++++++++++---------------- resourceqt-client/client.h | 3 ++ resourceqt-client/resourceqt-client.cpp | 1 + 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/resourceqt-client/client.cpp b/resourceqt-client/client.cpp index d190498..7fdb2f6 100644 --- a/resourceqt-client/client.cpp +++ b/resourceqt-client/client.cpp @@ -53,7 +53,7 @@ CommandListArgs::~CommandListArgs() Client::Client() : QObject(), standardInput(stdin, QIODevice::ReadOnly), stdInNotifier(0, QSocketNotifier::Read), pendingAddAudio(false), applicationClass(), - resourceSet(NULL), output(stdout), prefix("") + resourceSet(NULL), output(stdout), prefix(""), showTimings(false) { commandList["help"] = CommandListArgs("", "print this help message"); commandList["quit"] = CommandListArgs("", "exit application"); @@ -93,6 +93,7 @@ bool Client::initialize(const CommandLineParser &parser) if (parser.shouldAutoRelease()) { OUTPUT << "client: AutoRelease" << endl; } + showTimings = parser.showTimings(); resourceSet = new ResourceSet(parser.resourceApplicationClass(), this, parser.shouldAlwaysReply(), @@ -205,10 +206,7 @@ void Client::showResources(const QList &resList) void Client::resourceAcquiredHandler(const QList&) { - long int ms = stop_timer(); - if (ms > 0) { - outputln << "Operation took " << ms << "ms" << endl; - } + stopTimer(); QList list = resourceSet->resources(); if (!list.count()) { @@ -228,10 +226,7 @@ void Client::resourceAcquiredHandler(const QList&) void Client::resourceDeniedHandler() { - long int ms = stop_timer(); - if (ms > 0) { - outputln << "Operation took " << ms << "ms" << endl; - } + stopTimer(); QList allResources = resourceSet->resources(); OUTPUT << "denied:" << allResources << endl; showPrompt(); @@ -239,10 +234,7 @@ void Client::resourceDeniedHandler() void Client::resourceLostHandler() { - long int ms = stop_timer(); - if (ms > 0) { - outputln << "Operation took " << ms << "ms" << endl; - } + stopTimer(); QList allResources = resourceSet->resources(); outputln << "lost:" << allResources << endl; @@ -251,10 +243,7 @@ void Client::resourceLostHandler() void Client::resourceReleasedHandler() { - long int ms = stop_timer(); - if (ms > 0) { - outputln << "Operation took " << ms << "ms" << endl; - } + stopTimer(); QList allResources = resourceSet->resources(); outputln << "released:"<< allResources << endl; @@ -265,10 +254,7 @@ void Client::resourcesBecameAvailableHandler(const QList 0) { - outputln << "Operation took " << ms << "ms" << endl; - } + stopTimer(); } outputln << "advice:" << availableResources << endl; showPrompt(); @@ -321,16 +307,16 @@ void Client::readLine(int) } } else if (command == "acquire") { - start_timer(); + startTimer(); if (!resourceSet || !resourceSet->acquire()) { - stop_timer(); + stopTimer(); qCritical("%s failed!", qPrintable(command)); } } else if (command == "release") { - start_timer(); + startTimer(); if (!resourceSet || !resourceSet->release()) { - stop_timer(); + stopTimer(); qCritical("%s failed!", qPrintable(command)); } } @@ -453,7 +439,7 @@ void Client::readLine(int) audioResource->setProcessID(pid); audioResource->setStreamTag(tagName, tagValue); pendingAddAudio = true; - start_timer(); + startTimer(); resourceSet->addResourceObject(audioResource); } } @@ -491,3 +477,19 @@ QTextStream & operator<< (QTextStream &output, return output; } +void Client::startTimer() +{ + if (showTimings) { + start_timer(); + } +} + +void Client::stopTimer() +{ + if (showTimings) { + long int ms = stop_timer(); + if (ms > 0) { + outputln << "Operation took " << ms << " ms" << endl; + } + } +} diff --git a/resourceqt-client/client.h b/resourceqt-client/client.h index 140c668..3967fee 100644 --- a/resourceqt-client/client.h +++ b/resourceqt-client/client.h @@ -71,6 +71,7 @@ private: ResourcePolicy::ResourceSet *resourceSet; QTextStream output; QString prefix; + bool showTimings; static QMap commandList; @@ -80,6 +81,8 @@ private: void showPrompt(); void showResources(const QList &resList); void showResources(const QList &resList); + inline void startTimer(); + inline void stopTimer(); }; QTextStream & operator<< (QTextStream &output, diff --git a/resourceqt-client/resourceqt-client.cpp b/resourceqt-client/resourceqt-client.cpp index 240b3ce..9bf61d2 100644 --- a/resourceqt-client/resourceqt-client.cpp +++ b/resourceqt-client/resourceqt-client.cpp @@ -56,6 +56,7 @@ int main(int argc, char *argv[]) if (!parser.parseArguments()) { return 1; } + verbose = parser.shouldBeVerbose(); if (!client.initialize(parser)) { qFatal("Initialization failed"); -- cgit v1.2.3