ETCH-282 Runtime accepts now log configuration
A new constructor for the EtchRuntime has been added.
It enables the client/server to register a different log appender for
different log levels.
This is especially useful if you e.g. would like to log errors to your console,
but log info and debug messages to a file.
Change-Id: I0952b4019ef8dc9c5a227fa18a60cc3f5e428058
git-svn-id: https://svn.apache.org/repos/asf/etch/trunk@1578866 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/binding-cpp/runtime/CMakeLists.txt b/binding-cpp/runtime/CMakeLists.txt
index 0a94792..02dc989 100644
--- a/binding-cpp/runtime/CMakeLists.txt
+++ b/binding-cpp/runtime/CMakeLists.txt
@@ -41,12 +41,12 @@
#VLD
SET(VLD ${ETCH_EXTERNAL_DEPENDS}/vld/1.9h)
-# GTest
-SET(GTEST ${ETCH_EXTERNAL_DEPENDS}/gtest/1.6.0)
-
# GMock
SET(GMOCK ${ETCH_EXTERNAL_DEPENDS}/gmock/1.6.0)
+# GTest
+SET(GTEST ${GMOCK}/gtest)
+
#Build external CAPU project (OS Abstraction)
set (CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_CURRENT_SOURCE_DIR})
find_package(Capu)
diff --git a/binding-cpp/runtime/include/support/EtchRuntime.h b/binding-cpp/runtime/include/support/EtchRuntime.h
index f8ca116..96a1537 100644
--- a/binding-cpp/runtime/include/support/EtchRuntime.h
+++ b/binding-cpp/runtime/include/support/EtchRuntime.h
@@ -43,10 +43,18 @@
/**
* Create a new instance of the EtchRuntime class
+ * It uses the default Console Log Appender for logging.
*/
EtchRuntime();
/**
+ * Create a new instance of the EtchRuntime class
+ * @param logAppender the log appender
+ * @param logLevel the log level
+ */
+ EtchRuntime(IEtchLogAppender& logAppender, EtchLogLevel logLevel);
+
+ /**
* Destructor
*/
virtual ~EtchRuntime();
@@ -94,12 +102,18 @@
private:
capu::bool_t mIsClosed;
EtchLogger mLogger;
- EtchConsoleLogAppender mLogAppender;
+ EtchConsoleLogAppender mDefaultLogAppender;
static capu::uint64_t sId;
capu::uint64_t mId;
capu::Mutex mMutex;
capu::List<EtchRuntimeListener*> mListeners;
+
+ /**
+ * Assigns an unique id to the runtime
+ */
+ void generateRuntimeId();
+
/**
* Return the next id
*/
diff --git a/binding-cpp/runtime/include/util/EtchLogger.h b/binding-cpp/runtime/include/util/EtchLogger.h
index df528e4..fe78251 100644
--- a/binding-cpp/runtime/include/util/EtchLogger.h
+++ b/binding-cpp/runtime/include/util/EtchLogger.h
@@ -25,6 +25,7 @@
#include "capu/util/ILogAppender.h"
typedef capu::ILogAppender IEtchLogAppender;
+typedef capu::ELogLevel EtchLogLevel;
typedef capu::ConsoleLogAppender EtchConsoleLogAppender;
typedef capu::LogContext EtchLogContext;
@@ -82,14 +83,14 @@
return mRuntimeContext;
}
private:
- EtchLogContext mSerializerContext;
- EtchLogContext mDeliveryServiceContext;
- EtchLogContext mTransportContext;
- EtchLogContext mPacketizerContext;
- EtchLogContext mMessagizerContext;
- EtchLogContext mValidatorContext;
- EtchLogContext mMailboxContext;
- EtchLogContext mRuntimeContext;
+ EtchLogContext& mSerializerContext;
+ EtchLogContext& mDeliveryServiceContext;
+ EtchLogContext& mTransportContext;
+ EtchLogContext& mPacketizerContext;
+ EtchLogContext& mMessagizerContext;
+ EtchLogContext& mValidatorContext;
+ EtchLogContext& mMailboxContext;
+ EtchLogContext& mRuntimeContext;
};
diff --git a/binding-cpp/runtime/src/main/support/EtchRuntime.cpp b/binding-cpp/runtime/src/main/support/EtchRuntime.cpp
index 95f619c..d75a0fe 100644
--- a/binding-cpp/runtime/src/main/support/EtchRuntime.cpp
+++ b/binding-cpp/runtime/src/main/support/EtchRuntime.cpp
@@ -23,13 +23,24 @@
EtchRuntime::EtchRuntime()
: mIsClosed(false)
- , mLogger(mLogAppender)
+ , mLogger(mDefaultLogAppender)
{
- mMutex.lock();
- mId = getNextId();
- mMutex.unlock();
+ //Default log level is WARN
+ mLogger.setLogLevel(capu::LL_WARN);
- mLogger.setLogLevel(capu::LL_ALL);
+ //assign a unique id to this runtime
+ generateRuntimeId();
+}
+
+EtchRuntime::EtchRuntime(IEtchLogAppender& logAppender, EtchLogLevel logLevel)
+ : mIsClosed(false)
+ , mLogger(logAppender)
+{
+ //Default log level is WARN
+ mLogger.setLogLevel(logLevel);
+
+ //assign a unique id to this runtime
+ generateRuntimeId();
}
EtchRuntime::~EtchRuntime() {
@@ -94,6 +105,12 @@
return ETCH_OK;
}
+void EtchRuntime::generateRuntimeId() {
+ mMutex.lock();
+ mId = getNextId();
+ mMutex.unlock();
+}
+
capu::uint64_t EtchRuntime::getNextId() {
static capu::uint64_t sId = 0;
return sId++;
diff --git a/binding-cpp/runtime/src/test/support/EtchRuntimeTest.cpp b/binding-cpp/runtime/src/test/support/EtchRuntimeTest.cpp
index d4fb730..f37abf9 100644
--- a/binding-cpp/runtime/src/test/support/EtchRuntimeTest.cpp
+++ b/binding-cpp/runtime/src/test/support/EtchRuntimeTest.cpp
@@ -18,6 +18,12 @@
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "support/EtchRuntime.h"
+#include "capu/util/LogMessage.h"
+
+class MockLogAppender : public IEtchLogAppender {
+public:
+ MOCK_METHOD1(log, void(const capu::LogMessage& message));
+};
capu::uint64_t currentIdCount;
@@ -27,13 +33,30 @@
delete runtime;
}
-TEST(EtchRuntime, getId) {
- EtchRuntime* runtime = new EtchRuntime(); //ID = 1
- EXPECT_EQ(currentIdCount + 1, runtime->getId());
+TEST(EtchRuntime, Logging) {
+ MockLogAppender* logAppender = new MockLogAppender();
+
+ EtchRuntime* runtime = new EtchRuntime(*logAppender, capu::LL_ALL); //ID = 2
+
+ EXPECT_CALL(*logAppender, log(::testing::_)).Times(2);
+
+ EtchLogContext testContext = runtime->getLogger().createContext("testcontext");
+ testContext.setLogLevel(capu::LL_ALL);
+
+ ETCH_LOG_DEBUG(runtime->getLogger(), testContext, "test log message for runtime " << runtime->getId());
+ ETCH_LOG_DEBUG(runtime->getLogger(), runtime->getLogger().getMessagizerContext(), "test log message for runtime " << runtime->getId());
+
delete runtime;
- runtime = new EtchRuntime(); //ID = 2
+ delete logAppender;
+}
+
+TEST(EtchRuntime, getId) {
+ EtchRuntime* runtime = new EtchRuntime(); //ID = 3
EXPECT_EQ(currentIdCount + 2, runtime->getId());
delete runtime;
+ runtime = new EtchRuntime(); //ID = 4
+ EXPECT_EQ(currentIdCount + 3, runtime->getId());
+ delete runtime;
}
TEST(EtchRuntime, isClosed) {