Author: carnold Date: Thu Mar 13 19:42:32 2008 New Revision: 636964 URL: http://svn.apache.org/viewvc?rev=636964&view=rev Log: LOGCXX-251: NDC::cloneStack and NDC::inherit missing in 0.10.0
Modified: logging/log4cxx/trunk/src/changes/changes.xml logging/log4cxx/trunk/src/main/cpp/ndc.cpp logging/log4cxx/trunk/src/main/cpp/threadspecificdata.cpp logging/log4cxx/trunk/src/main/include/log4cxx/helpers/threadspecificdata.h logging/log4cxx/trunk/src/main/include/log4cxx/ndc.h logging/log4cxx/trunk/src/test/cpp/ndctestcase.cpp Modified: logging/log4cxx/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/changes/changes.xml?rev=636964&r1=636963&r2=636964&view=diff ============================================================================== --- logging/log4cxx/trunk/src/changes/changes.xml (original) +++ logging/log4cxx/trunk/src/changes/changes.xml Thu Mar 13 19:42:32 2008 @@ -219,6 +219,7 @@ <action issue="LOGCXX-246">Config refresh hangs a client application that uses TelnetAppender</action> <action issue="LOGCXX-247">MSVC project has wrong additional include directories</action> <action issue="LOGCXX-248">ODBCAppender has unicode issues</action> +<action issue="LOGCXX-251">NDC::cloneStack and NDC::inherit missing in 0.10.0 RC2</action> </release> <release version="0.9.7" date="2004-05-10"> <action type="fix">Fixed examples source code in the "Short introduction to log4cxx".</action> Modified: logging/log4cxx/trunk/src/main/cpp/ndc.cpp URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/cpp/ndc.cpp?rev=636964&r1=636963&r2=636964&view=diff ============================================================================== --- logging/log4cxx/trunk/src/main/cpp/ndc.cpp (original) +++ logging/log4cxx/trunk/src/main/cpp/ndc.cpp Thu Mar 13 19:42:32 2008 @@ -58,6 +58,26 @@ } } +NDC::Stack* NDC::cloneStack() +{ + ThreadSpecificData* data = ThreadSpecificData::getCurrentData(); + if (data != 0) { + Stack& stack = data->getStack(); + if (!stack.empty()) { + return new Stack(stack); + } + } + return new Stack(); +} + +void NDC::inherit(NDC::Stack * stack) { + if (stack != NULL) { + ThreadSpecificData::inherit(*stack); + delete stack; + } +} + + bool NDC::get(LogString& dest) { ThreadSpecificData* data = ThreadSpecificData::getCurrentData(); Modified: logging/log4cxx/trunk/src/main/cpp/threadspecificdata.cpp URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/cpp/threadspecificdata.cpp?rev=636964&r1=636963&r2=636964&view=diff ============================================================================== --- logging/log4cxx/trunk/src/main/cpp/threadspecificdata.cpp (original) +++ logging/log4cxx/trunk/src/main/cpp/threadspecificdata.cpp Thu Mar 13 19:42:32 2008 @@ -104,6 +104,16 @@ } } +void ThreadSpecificData::inherit(const NDC::Stack& src) { + ThreadSpecificData* data = getCurrentData(); + if (data == 0) { + data = createCurrentData(); + } + if (data != 0) { + data->getStack() = src; + } +} + ThreadSpecificData* ThreadSpecificData::createCurrentData() { Modified: logging/log4cxx/trunk/src/main/include/log4cxx/helpers/threadspecificdata.h URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/include/log4cxx/helpers/threadspecificdata.h?rev=636964&r1=636963&r2=636964&view=diff ============================================================================== --- logging/log4cxx/trunk/src/main/include/log4cxx/helpers/threadspecificdata.h (original) +++ logging/log4cxx/trunk/src/main/include/log4cxx/helpers/threadspecificdata.h Thu Mar 13 19:42:32 2008 @@ -48,6 +48,7 @@ static void put(const LogString& key, const LogString& val); static void push(const LogString& val); + static void inherit(const log4cxx::NDC::Stack& stack); log4cxx::NDC::Stack& getStack(); log4cxx::MDC::Map& getMap(); Modified: logging/log4cxx/trunk/src/main/include/log4cxx/ndc.h URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/main/include/log4cxx/ndc.h?rev=636964&r1=636963&r2=636964&view=diff ============================================================================== --- logging/log4cxx/trunk/src/main/include/log4cxx/ndc.h (original) +++ logging/log4cxx/trunk/src/main/include/log4cxx/ndc.h Thu Mar 13 19:42:32 2008 @@ -128,6 +128,32 @@ */ static void clear(); + /** + Clone the diagnostic context for the current thread. + <p>Internally a diagnostic context is represented as a stack. A + given thread can supply the stack (i.e. diagnostic context) to a + child thread so that the child can inherit the parent thread's + diagnostic context. + <p>The child thread uses the #inherit method to + inherit the parent's diagnostic context. + <p>If not passed to #inherit, returned stack should be deleted by caller. + @return Stack A clone of the current thread's diagnostic context, will not be null. + */ + static Stack * cloneStack(); + + /** + Inherit the diagnostic context of another thread. + <p>The parent thread can obtain a reference to its diagnostic + context using the #cloneStack method. It should + communicate this information to its child so that it may inherit + the parent's diagnostic context. + <p>The parent's diagnostic context is cloned before being + inherited. In other words, once inherited, the two diagnostic + contexts can be managed independently. + @param stack The diagnostic context of the parent thread, + will be deleted during call. If NULL, NDC will not be modified. + */ + static void inherit(Stack * stack); /** * Get the current value of the NDC of the Modified: logging/log4cxx/trunk/src/test/cpp/ndctestcase.cpp URL: http://svn.apache.org/viewvc/logging/log4cxx/trunk/src/test/cpp/ndctestcase.cpp?rev=636964&r1=636963&r2=636964&view=diff ============================================================================== --- logging/log4cxx/trunk/src/test/cpp/ndctestcase.cpp (original) +++ logging/log4cxx/trunk/src/test/cpp/ndctestcase.cpp Thu Mar 13 19:42:32 2008 @@ -36,6 +36,7 @@ LOGUNIT_TEST_SUITE(NDCTestCase); LOGUNIT_TEST(testPushPop); LOGUNIT_TEST(test1); + LOGUNIT_TEST(testInherit); LOGUNIT_TEST_SUITE_END(); public: @@ -83,6 +84,21 @@ LOG4CXX_WARN(logger, "m3"); LOG4CXX_ERROR(logger, "m4"); LOG4CXX_FATAL(logger, "m5"); + } + + void testInherit() { + NDC::push("hello"); + NDC::push("world"); + NDC::Stack* clone = NDC::cloneStack(); + NDC::clear(); + NDC::push("discard"); + NDC::inherit(clone); + LogString expected1(LOG4CXX_STR("world")); + LOGUNIT_ASSERT_EQUAL(expected1, NDC::pop()); + LogString expected2(LOG4CXX_STR("hello")); + LOGUNIT_ASSERT_EQUAL(expected2, NDC::pop()); + LogString expected3; + LOGUNIT_ASSERT_EQUAL(expected3, NDC::pop()); } };