// Module: Log4CPLUS // File: ndc.cxx // Created: 6/2001 // Author: Tad E. Smith // // // Copyright (C) Tad E. Smith All rights reserved. // // This software is published under the terms of the Apache Software // License version 1.1, a copy of which has been included with this // distribution in the LICENSE.APL file. // // $Log: ndc.cxx,v $ // Revision 1.9 2003/09/10 06:48:29 tcsmith // The ctor once again initialized fullMessage in an initialization list. // // Revision 1.8 2003/08/08 05:56:23 tcsmith // Added a fix for the Borland compiler. // // Revision 1.7 2003/05/21 22:14:46 tcsmith // Fixed compiler warning: "conversion from 'size_t' to 'int', possible loss // of data". // // Revision 1.6 2003/05/04 01:37:22 tcsmith // Removed the static initializer class. // // Revision 1.5 2003/04/19 23:04:31 tcsmith // Fixed UNICODE support. // // Revision 1.4 2003/04/18 22:08:19 tcsmith // Converted from std::string to log4cplus::tstring. // // Revision 1.3 2003/04/03 01:06:41 tcsmith // Standardized the formatting. // #include #include #include #include #include #include using namespace log4cplus; using namespace log4cplus::helpers; /////////////////////////////////////////////////////////////////////////////// // public methods /////////////////////////////////////////////////////////////////////////////// NDC& log4cplus::getNDC() { static NDC singleton; return singleton; } /////////////////////////////////////////////////////////////////////////////// // log4cplus::DiagnosticContext ctors /////////////////////////////////////////////////////////////////////////////// DiagnosticContext::DiagnosticContext(const log4cplus::tstring& message, DiagnosticContext* parent) : message(message), fullMessage( ( (parent == NULL) ? message : parent->fullMessage + LOG4CPLUS_TEXT(" ") + message) ) { } DiagnosticContext::DiagnosticContext(const log4cplus::tstring& message) : message(message), fullMessage(message) { } /////////////////////////////////////////////////////////////////////////////// // log4cplus::NDC ctor and dtor /////////////////////////////////////////////////////////////////////////////// NDC::NDC() : threadLocal(LOG4CPLUS_THREAD_LOCAL_INIT) { } NDC::~NDC() { LOG4CPLUS_THREAD_LOCAL_CLEANUP( threadLocal ); } /////////////////////////////////////////////////////////////////////////////// // log4cplus::NDC public methods /////////////////////////////////////////////////////////////////////////////// void NDC::clear() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL) { delete ptr; LOG4CPLUS_SET_THREAD_LOCAL_VALUE( threadLocal, NULL ); } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::clear()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::clear()- exception occured")); } } DiagnosticContextStack NDC::cloneStack() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL) { return DiagnosticContextStack(*ptr); } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::cloneStack()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::cloneStack()- exception occured")); } return DiagnosticContextStack(); } void NDC::inherit(const DiagnosticContextStack& stack) { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL) { delete ptr; } ptr = new DiagnosticContextStack(stack); LOG4CPLUS_SET_THREAD_LOCAL_VALUE( threadLocal, ptr ); } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::inherit()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::inherit()- exception occured")); } } log4cplus::tstring NDC::get() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL && !ptr->empty()) { return ptr->top().fullMessage; } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::get()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::get()- exception occured")); } return LOG4CPLUS_TEXT(""); } size_t NDC::getDepth() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL) { return ptr->size(); } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::getDepth()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::getDepth()- exception occured")); } return 0; } log4cplus::tstring NDC::pop() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL && !ptr->empty()) { DiagnosticContext dc = ptr->top(); ptr->pop(); if(ptr->empty()) { // If the NDC stack is empty we will delete it so that we can avoid // most memory leaks if Threads don't call remove when exiting delete ptr; LOG4CPLUS_SET_THREAD_LOCAL_VALUE( threadLocal, NULL ); } return dc.message; } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::pop()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::pop()- exception occured")); } return LOG4CPLUS_TEXT(""); } log4cplus::tstring NDC::peek() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL && !ptr->empty()) { return ptr->top().message; } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::peek()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::peek()- exception occured")); } return LOG4CPLUS_TEXT(""); } void NDC::push(const log4cplus::tstring& message) { try { DiagnosticContextStack* ptr = getPtr(); if(ptr == NULL) { ptr = new DiagnosticContextStack(); LOG4CPLUS_SET_THREAD_LOCAL_VALUE( threadLocal, ptr ); } if(ptr->empty()) { ptr->push( DiagnosticContext(message, NULL) ); } else { DiagnosticContext dc = ptr->top(); ptr->push( DiagnosticContext(message, &dc) ); } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::push()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::push()- exception occured")); } } void NDC::remove() { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL) { delete ptr; } LOG4CPLUS_SET_THREAD_LOCAL_VALUE( threadLocal, NULL ); } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::remove()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::remove()- exception occured")); } } void NDC::setMaxDepth(size_t maxDepth) { try { DiagnosticContextStack* ptr = getPtr(); if(ptr != NULL) { while(maxDepth < ptr->size()) { ptr->pop(); } } } catch(std::exception& e) { getLogLog().error( LOG4CPLUS_TEXT("NDC::setMaxDepth()- exception occured: ") + LOG4CPLUS_C_STR_TO_TSTRING(e.what())); } catch(...) { getLogLog().error(LOG4CPLUS_TEXT("NDC::setMaxDepth()- exception occured")); } }