00001 /* 00002 * NDC.cpp 00003 * 00004 * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved. 00005 * Copyright 2000, Bastiaan Bakker. All rights reserved. 00006 * 00007 * See the COPYING file for the terms of usage and distribution. 00008 */ 00009 00010 #include "PortabilityImpl.hh" 00011 #include <log4cpp/NDC.hh> 00012 #include <log4cpp/threading/Threading.hh> 00013 00014 namespace log4cpp { 00015 00016 NDC::DiagnosticContext::DiagnosticContext(const std::string& message) : 00017 message(message), 00018 fullMessage(message) { 00019 } 00020 00021 NDC::DiagnosticContext::DiagnosticContext(const std::string& message, 00022 const DiagnosticContext& parent) : 00023 message(message), 00024 fullMessage(parent.fullMessage + " " + message) { 00025 } 00026 00027 namespace { 00028 threading::ThreadLocalDataHolder<NDC> _nDC; 00029 } 00030 00031 void NDC::clear() { 00032 getNDC()._clear(); 00033 } 00034 00035 NDC::ContextStack* NDC::cloneStack() { 00036 return getNDC()._cloneStack(); 00037 } 00038 00039 const std::string& NDC::get() { 00040 return getNDC()._get(); 00041 } 00042 00043 size_t NDC::getDepth() { 00044 return getNDC()._getDepth(); 00045 } 00046 00047 void NDC::inherit(NDC::ContextStack* stack) { 00048 getNDC()._inherit(stack); 00049 } 00050 00051 std::string NDC::pop() { 00052 return getNDC()._pop(); 00053 } 00054 00055 void NDC::push(const std::string& message) { 00056 getNDC()._push(message); 00057 } 00058 00059 void NDC::setMaxDepth(int maxDepth) { 00060 getNDC()._setMaxDepth(maxDepth); 00061 } 00062 00063 NDC& NDC::getNDC() { 00064 NDC* nDC = _nDC.get(); 00065 00066 if (!nDC) { 00067 nDC = new NDC(); 00068 _nDC.reset(nDC); 00069 } 00070 00071 return *nDC; 00072 } 00073 00074 NDC::NDC() { 00075 } 00076 00077 NDC::~NDC() { 00078 } 00079 00080 void NDC::_clear() { 00081 _stack.clear(); 00082 } 00083 00084 NDC::ContextStack* NDC::_cloneStack() { 00085 return new ContextStack(_stack); 00086 } 00087 00088 const std::string& NDC::_get() const { 00089 static std::string empty = ""; 00090 00091 return (_stack.empty() ? empty : _stack.back().fullMessage); 00092 } 00093 00094 size_t NDC::_getDepth() const { 00095 return _stack.size(); 00096 } 00097 00098 void NDC::_inherit(NDC::ContextStack* stack) { 00099 _stack = *stack; 00100 } 00101 00102 std::string NDC::_pop() { 00103 std::string result = _stack.back().message; 00104 _stack.pop_back(); 00105 return result; 00106 } 00107 00108 void NDC::_push(const std::string& message) { 00109 if (_stack.empty()) { 00110 _stack.push_back(DiagnosticContext(message)); 00111 } else { 00112 _stack.push_back(DiagnosticContext(message, _stack.back())); 00113 } 00114 } 00115 00116 void NDC::_setMaxDepth(int maxDepth) { 00117 // XXX no maximum 00118 } 00119 00120 }