Author: aconway
Date: Thu Sep 25 07:16:51 2008
New Revision: 698981
URL: http://svn.apache.org/viewvc?rev=698981&view=rev
Log:
Added ScopedSuppressLogging, used to suppress expected error messages in tests.
For examples see src/tests/exception_test.cpp
Modified:
incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.cpp
incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.h
incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.cpp
incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.h
incubator/qpid/trunk/qpid/cpp/src/tests/QueuePolicyTest.cpp
incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp
incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp
incubator/qpid/trunk/qpid/cpp/src/tests/test_tools.h
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.cpp
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.cpp?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.cpp Thu Sep 25 07:16:51
2008
@@ -17,7 +17,6 @@
*/
#include "Logger.h"
-#include "Options.h"
#include "qpid/memory.h"
#include "qpid/sys/Thread.h"
#include <boost/pool/detail/singleton.hpp>
@@ -212,6 +211,7 @@
}
void Logger::configure(const Options& opts) {
+ options = opts;
clear();
Options o(opts);
if (o.trace)
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.h
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.h?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/log/Logger.h Thu Sep 25 07:16:51 2008
@@ -1,5 +1,5 @@
-#ifndef LOGGER_H
-#define LOGGER_H
+#ifndef QPID_LOG_LOGGER_H
+#define QPID_LOG_LOGGER_H
/*
* http://www.apache.org/licenses/LICENSE-2.0
@@ -13,6 +13,7 @@
*/
#include "Selector.h"
+#include "Options.h"
#include "qpid/sys/Mutex.h"
#include <boost/ptr_container/ptr_vector.hpp>
#include <boost/noncopyable.hpp>
@@ -21,8 +22,6 @@
namespace qpid {
namespace log {
-class Options;
-
/**
* Central logging agent.
*
@@ -93,10 +92,13 @@
/** Set a prefix for all messages */
void setPrefix(const std::string& prefix);
- /** Reset the logger to it's original state. */
+ /** Reset the logger. */
void clear();
-
+ /** Get the options used to configure the logger. */
+ const Options& getOptions() const { return options; }
+
+
private:
typedef boost::ptr_vector<Output> Outputs;
typedef std::set<Statement*> Statements;
@@ -109,9 +111,10 @@
Selector selector;
int flags;
std::string prefix;
+ Options options;
};
}} // namespace qpid::log
-#endif /*!LOGGER_H*/
+#endif /*!QPID_LOG_LOGGER_H*/
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.cpp
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.cpp?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.cpp Thu Sep 25 07:16:51
2008
@@ -146,6 +146,23 @@
("syslog-name", optValue(syslogName, "NAME"), "Name to use in syslog
messages")
("syslog-facility", optValue(syslogFacility,"LOG_XXX"), "Facility to
use in syslog messages")
;
-}
+}
+
+Options& Options::operator=(const Options& x) {
+ if (this != &x) {
+ selectors = x.selectors;
+ outputs = x.outputs;
+ time = x.time;
+ level= x.level;
+ thread = x.thread;
+ source = x.source;
+ function = x.function;
+ trace = x.trace;
+ syslogName = x.syslogName;
+ syslogFacility = x.syslogFacility;
+ prefix = x.prefix;
+ }
+ return *this;
+}
}} // namespace qpid::log
Modified: incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.h
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.h?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/qpid/log/Options.h Thu Sep 25 07:16:51
2008
@@ -36,9 +36,11 @@
/** Logging options for config parser. */
struct Options : public qpid::Options {
/** Pass argv[0] for use in syslog output */
- Options(const std::string& argv0,
+ Options(const std::string& argv0=std::string(),
const std::string& name="Logging options");
+ Options& operator=(const Options&);
+
std::vector<std::string> selectors;
std::vector<std::string> outputs;
bool time, level, thread, source, function;
Modified: incubator/qpid/trunk/qpid/cpp/src/tests/QueuePolicyTest.cpp
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/QueuePolicyTest.cpp?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/QueuePolicyTest.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/QueuePolicyTest.cpp Thu Sep 25
07:16:51 2008
@@ -18,9 +18,11 @@
* under the License.
*
*/
+#include "unit_test.h"
+#include "test_tools.h"
+
#include "qpid/broker/QueuePolicy.h"
#include "qpid/sys/Time.h"
-#include "unit_test.h"
#include "MessageUtils.h"
#include "BrokerFixture.h"
@@ -174,8 +176,9 @@
BOOST_CHECK_EQUAL(incoming.pop().getData(), (boost::format("%1%_%2%")
% "Message" % (i+1)).str());
}
try {
+ ScopedSuppressLogging sl; // Suppress messages for expected errors.
f.session.messageTransfer(arg::content=client::Message("Message_6",
q));
- BOOST_FAIL("Transfer should have failed as ");
+ BOOST_FAIL("expecting ResourceLimitExceededException.");
} catch (const ResourceLimitExceededException&) {}
}
Modified: incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/exception_test.cpp Thu Sep 25
07:16:51 2008
@@ -20,6 +20,7 @@
*/
#include "unit_test.h"
+#include "test_tools.h"
#include "BrokerFixture.h"
#include "qpid/client/SubscriptionManager.h"
#include "qpid/sys/Runnable.h"
@@ -51,7 +52,10 @@
~Catcher() { join(); }
void run() {
- try { f(); }
+ try {
+ ScopedSuppressLogging sl; // Suppress messages for expected errors.
+ f();
+ }
catch(const Ex& e) {
caught=true;
BOOST_MESSAGE(string("Caught expected exception:
")+e.what()+"["+typeid(e).name()+"]");
@@ -76,6 +80,7 @@
QPID_AUTO_TEST_CASE(TestSessionBusy) {
SessionFixture f;
try {
+ ScopedSuppressLogging sl; // Suppress messages for expected errors.
f.connection.newSession(f.session.getId().getName());
BOOST_FAIL("Expected SessionBusyException for " <<
f.session.getId().getName());
} catch (const Exception&) {} // FIXME aconway 2008-09-22: client is not
throwing correct exception.
@@ -99,6 +104,8 @@
ProxyConnection c(fix.broker->getPort());
fix.session.queueDeclare(arg::queue="q");
fix.subs.subscribe(l, "q");
+
+ ScopedSuppressLogging sl; // Suppress messages for expected errors.
Thread t(fix.subs);
fix.connection.proxy.close();
t.join();
@@ -107,6 +114,7 @@
QPID_AUTO_TEST_CASE(NoSuchQueueTest) {
ProxySessionFixture fix;
+ ScopedSuppressLogging sl; // Suppress messages for expected errors.
BOOST_CHECK_THROW(fix.subs.subscribe(fix.lq, "no such queue"),
NotFoundException);
}
Modified: incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/logging.cpp Thu Sep 25 07:16:51 2008
@@ -77,6 +77,7 @@
// Verify that the singleton enables and disables static
// log statements.
Logger& l = Logger::instance();
+ ScopedSuppressLogging ls(l);
l.select(Selector(debug));
static Statement s=QPID_LOG_STATEMENT_INIT(debug);
BOOST_CHECK(!s.enabled);
@@ -110,7 +111,7 @@
using boost::assign::list_of;
QPID_AUTO_TEST_CASE(testLoggerOutput) {
- Logger l;
+ Logger l;
l.clear();
l.select(Selector(debug));
Statement s=QPID_LOG_STATEMENT_INIT(debug);
@@ -133,7 +134,7 @@
QPID_AUTO_TEST_CASE(testMacro) {
Logger& l=Logger::instance();
- l.clear();
+ ScopedSuppressLogging ls(l);
l.select(Selector(info));
TestOutput* out=new TestOutput(l);
QPID_LOG(info, "foo");
@@ -152,6 +153,7 @@
QPID_AUTO_TEST_CASE(testLoggerFormat) {
Logger& l = Logger::instance();
+ ScopedSuppressLogging ls(l);
l.select(Selector(critical));
TestOutput* out=new TestOutput(l);
@@ -165,20 +167,16 @@
l.format(Logger::FUNCTION);
QPID_LOG(critical, "foo");
+ BOOST_CHECK_REGEX("void .*testLoggerFormat.*\\(\\): foo\n", out->last());
l.format(Logger::LEVEL);
QPID_LOG(critical, "foo");
BOOST_CHECK_EQUAL("critical foo\n", out->last());
-
- l.format(~0); // Everything
- QPID_LOG(critical, "foo");
- string re=".* critical -?\\[[0-9a-f]*] "+string(__FILE__)+":\\d+:void
.*testLoggerFormat.*\\(\\): foo\n";
- BOOST_CHECK_REGEX(re, out->last());
}
QPID_AUTO_TEST_CASE(testOstreamOutput) {
Logger& l=Logger::instance();
- l.clear();
+ ScopedSuppressLogging ls(l);
l.select(Selector(error));
ostringstream os;
l.output(os);
@@ -191,6 +189,7 @@
#if 0 // This test requires manual intervention. Normally disabled.
QPID_AUTO_TEST_CASE(testSyslogOutput) {
Logger& l=Logger::instance();
+ Logger::StateSaver ls(l);
l.clear();
l.select(Selector(info));
l.syslog("qpid_test");
@@ -306,41 +305,9 @@
BOOST_CHECK(s.isEnabled(critical, "foo"));
}
-QPID_AUTO_TEST_CASE(testOptionsFormat) {
- Logger l;
- {
- Options opts("");
- BOOST_CHECK_EQUAL(Logger::TIME|Logger::LEVEL, l.format(opts));
- const char* argv[]={
- 0,
- "--log-time", "no",
- "--log-level", "no",
- "--log-source", "1",
- "--log-thread", "1"
- };
- opts.parse(ARGC(argv), const_cast<char**>(argv));
- BOOST_CHECK_EQUAL(
- Logger::FILE|Logger::LINE|Logger::THREAD, l.format(opts));
- }
- {
- Options opts(""); // Clear.
- const char* argv[]={
- 0,
- "--log-level", "no",
- "--log-thread", "true",
- "--log-function", "YES",
- "--log-time", "YES"
- };
- opts.parse(ARGC(argv), const_cast<char**>(argv));
- BOOST_CHECK_EQUAL(
- Logger::THREAD|Logger::FUNCTION|Logger::TIME,
- l.format(opts));
- }
-}
-
-QPID_AUTO_TEST_CASE(testLoggerConfigure) {
+QPID_AUTO_TEST_CASE(testLoggerStateure) {
Logger& l=Logger::instance();
- l.clear();
+ ScopedSuppressLogging ls(l);
Options opts("test");
const char* argv[]={
0,
@@ -363,7 +330,7 @@
QPID_AUTO_TEST_CASE(testQuoteNonPrintable) {
Logger& l=Logger::instance();
- l.clear();
+ ScopedSuppressLogging ls(l);
Options opts("test");
opts.outputs.clear();
opts.outputs.push_back("logging.tmp");
Modified: incubator/qpid/trunk/qpid/cpp/src/tests/test_tools.h
URL:
http://svn.apache.org/viewvc/incubator/qpid/trunk/qpid/cpp/src/tests/test_tools.h?rev=698981&r1=698980&r2=698981&view=diff
==============================================================================
--- incubator/qpid/trunk/qpid/cpp/src/tests/test_tools.h (original)
+++ incubator/qpid/trunk/qpid/cpp/src/tests/test_tools.h Thu Sep 25 07:16:51
2008
@@ -18,9 +18,9 @@
* limitations under the License.
*
*/
+#include "qpid/log/Logger.h"
#include <limits.h> // Include before boost/test headers.
-
#include <boost/test/test_tools.hpp>
#include <boost/assign/list_of.hpp>
#include <boost/regex.hpp>
@@ -78,5 +78,17 @@
/** Check if types of two objects (as given by typeinfo::name()) match. */
#define BOOST_CHECK_TYPEID_EQUAL(a,b)
BOOST_CHECK_EQUAL(typeid(a).name(),typeid(b).name())
+/**
+ * Supress all logging in a scope, restore to previous configuration in
destructor.
+ */
+struct ScopedSuppressLogging {
+ typedef qpid::log::Logger Logger;
+ ScopedSuppressLogging(Logger& l=Logger::instance()) : logger(l),
opts(l.getOptions()) { l.clear(); }
+ ~ScopedSuppressLogging() { logger.configure(opts); }
+ Logger& logger;
+ qpid::log::Options opts;
+};
+
+
#endif /*!TEST_TOOLS_H*/