summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/MessagingLogger.cpp
diff options
context:
space:
mode:
authorAndrew Stitcher <astitcher@apache.org>2014-01-03 18:54:46 +0000
committerAndrew Stitcher <astitcher@apache.org>2014-01-03 18:54:46 +0000
commite497a8527d42a93dce9320b5919cad93aa00a32b (patch)
tree4aafb677ff8f5af6f01d3b07b7d8497cdeed22d8 /qpid/cpp/src/tests/MessagingLogger.cpp
parent3130ec0a9ea6bf541d0b365c0eb2081f67224474 (diff)
downloadqpid-python-e497a8527d42a93dce9320b5919cad93aa00a32b.tar.gz
QPID-5415: Implement control of internal log output in qpid::messaging API
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1555202 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/cpp/src/tests/MessagingLogger.cpp')
-rw-r--r--qpid/cpp/src/tests/MessagingLogger.cpp149
1 files changed, 149 insertions, 0 deletions
diff --git a/qpid/cpp/src/tests/MessagingLogger.cpp b/qpid/cpp/src/tests/MessagingLogger.cpp
new file mode 100644
index 0000000000..195a33db12
--- /dev/null
+++ b/qpid/cpp/src/tests/MessagingLogger.cpp
@@ -0,0 +1,149 @@
+/*
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+
+#include "qpid/log/Statement.h"
+#include "qpid/messaging/Connection.h"
+#include "qpid/messaging/exceptions.h"
+#include "qpid/messaging/Logger.h"
+
+#include <iostream>
+#include <memory>
+#include <stdexcept>
+
+#include <vector>
+
+#include "unit_test.h"
+
+namespace qpid {
+namespace tests {
+
+QPID_AUTO_TEST_SUITE(MessagingLoggerSuite)
+
+class StringLogger : public qpid::messaging::LoggerOutput {
+ std::string& outString;
+
+ void log(qpid::messaging::Level /*level*/, bool user, const char* /*file*/, int /*line*/, const char* /*function*/, const std::string& message){
+ if (user) outString += "User ";
+ outString += message;
+ }
+
+public:
+ StringLogger(std::string& os) :
+ outString(os)
+ {}
+};
+
+#define SETUP_LOGGING(logger, ...) \
+do {\
+ const char* args[]={"", __VA_ARGS__, 0};\
+ qpid::messaging::Logger::configure((sizeof (args)/sizeof (char*))-1, args);\
+ logOutput.clear();\
+ qpid::messaging::Logger::setOutput(logger);\
+} while (0)
+#define LOG_LEVEL(level)\
+ QPID_LOG(level, #level " level output")
+#define LOG_ALL_LOGGING_LEVELS \
+do { \
+ LOG_LEVEL(trace); \
+ LOG_LEVEL(debug); \
+ LOG_LEVEL(info); \
+ LOG_LEVEL(notice); \
+ LOG_LEVEL(warning); \
+ LOG_LEVEL(critical); \
+} while (0)
+#define LOG_USER_LEVEL(level)\
+ qpid::messaging::Logger::log(qpid::messaging::level, __FILE__, __LINE__, __FUNCTION__, #level " message")
+#define LOG_ALL_USER_LOGGING_LEVELS \
+do { \
+ LOG_USER_LEVEL(trace); \
+ LOG_USER_LEVEL(debug); \
+ LOG_USER_LEVEL(info); \
+ LOG_USER_LEVEL(notice); \
+ LOG_USER_LEVEL(warning); \
+ LOG_USER_LEVEL(critical); \
+} while (0)
+
+std::string logOutput;
+
+QPID_AUTO_TEST_CASE(testLoggerLevels)
+{
+ StringLogger logger(logOutput);
+
+ SETUP_LOGGING(logger, "--log-enable", "debug");
+ LOG_ALL_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "debug level output\ncritical level output\n");
+
+ SETUP_LOGGING(logger, "--log-enable", "trace+", "--log-disable", "notice");
+ LOG_ALL_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "trace level output\ndebug level output\ninfo level output\nwarning level output\ncritical level output\n");
+
+ SETUP_LOGGING(logger, "--log-enable", "info-");
+ LOG_ALL_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "trace level output\ndebug level output\ninfo level output\ncritical level output\n");
+
+ SETUP_LOGGING(logger, "--log-enable", "trace+", "--log-disable", "notice+");
+ LOG_ALL_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "trace level output\ndebug level output\ninfo level output\ncritical level output\n");
+}
+
+QPID_AUTO_TEST_CASE(testUserLoggerLevels)
+{
+ StringLogger logger(logOutput);
+
+ SETUP_LOGGING(logger, "--log-enable", "debug");
+ LOG_ALL_USER_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "User debug message\nUser critical message\n");
+
+ SETUP_LOGGING(logger, "--log-enable", "trace+", "--log-disable", "notice");
+ LOG_ALL_USER_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "User trace message\nUser debug message\nUser info message\nUser warning message\nUser critical message\n");
+
+ SETUP_LOGGING(logger, "--log-enable", "info-");
+ LOG_ALL_USER_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "User trace message\nUser debug message\nUser info message\nUser critical message\n");
+
+ SETUP_LOGGING(logger, "--log-enable", "trace+", "--log-disable", "notice+");
+ LOG_ALL_USER_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "User trace message\nUser debug message\nUser info message\nUser critical message\n");
+
+ SETUP_LOGGING(logger, "--log-disable", "trace+");
+ LOG_ALL_LOGGING_LEVELS;
+ LOG_ALL_USER_LOGGING_LEVELS;
+ BOOST_CHECK_EQUAL(logOutput, "critical level output\nUser critical message\n");
+}
+
+QPID_AUTO_TEST_CASE(testLoggerUsage)
+{
+ qpid::messaging::Logger::configure(0, 0, "blah");
+ std::string u = qpid::messaging::Logger::usage();
+
+ BOOST_CHECK(!u.empty());
+ BOOST_CHECK( u.find("--blah-log-enable")!=u.npos );
+}
+
+QPID_AUTO_TEST_CASE(testLoggerException)
+{
+ const char* args[]={"", "--blah-log-enable", "illegal", 0};
+ BOOST_CHECK_THROW(qpid::messaging::Logger::configure(3, args, "blah"), qpid::messaging::MessagingException);
+}
+
+QPID_AUTO_TEST_SUITE_END()
+}}