/usr/include/log4cplus/ndc.h is in liblog4cplus-dev 1.0.4-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 | // Module: Log4CPLUS
// File: ndc.h
// Created: 6/2001
// Author: Tad E. Smith
//
//
// Copyright 2001-2010 Tad E. Smith
//
// Licensed 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.
/** @file
* This header defined the NDC class.
*/
#ifndef _LO4CPLUS_NDC_HEADER_
#define _LO4CPLUS_NDC_HEADER_
#include <log4cplus/config.hxx>
#include <log4cplus/tstring.h>
#include <log4cplus/helpers/logloguser.h>
#include <map>
#include <stack>
#if (defined(__MWERKS__) && defined(__MACOS__))
using std::size_t;
#endif
namespace log4cplus {
// Forward declarations
class NDC;
struct DiagnosticContext;
typedef std::stack<DiagnosticContext> DiagnosticContextStack;
#if defined (_MSC_VER) || defined (__HP_aCC)
LOG4CPLUS_EXPORT NDC& getNDC();
#endif
/**
* The NDC class implements <i>nested diagnostic contexts</i> as
* defined by Neil Harrison in the article "Patterns for Logging
* Diagnostic Messages" part of the book "<i>Pattern Languages of
* Program Design 3</i>" edited by Martin et al.
*
* A Nested Diagnostic Context, or NDC in short, is an instrument
* to distinguish interleaved log output from different sources. Log
* output is typically interleaved when a server handles multiple
* clients near-simultaneously.
*
* Interleaved log output can still be meaningful if each log entry
* from different contexts had a distinctive stamp. This is where NDCs
* come into play.
*
* <em><b>Note that NDCs are managed on a per thread
* basis</b></em>. NDC operations such as {@link #push}, {@link
* #pop}, {@link #clear}, {@link #getDepth} and {@link #setMaxDepth}
* affect the NDC of the <em>current</em> thread only. NDCs of other
* threads remain unaffected.
*
* For example, a server can build a per client request NDC
* consisting the clients host name and other information contained in
* the the request. <em>Cookies</em> are another source of distinctive
* information. To build an NDC one uses the {@link #push}
* operation. Simply put,
*
* <ul>
* <li>Contexts can be nested.
*
* <li>When entering a context, call <code>getNDC().push()</code>. As a
* side effect, if there is no nested diagnostic context for the
* current thread, this method will create it.
*
* <li>When leaving a context, call <code>getNDC().pop()</code>.
*
* <li><b>When exiting a thread make sure to call {@link #remove
* NDC.remove()}</b>.
* </ul>
*
* There is no penalty for forgetting to match each
* <code>push</code> operation with a corresponding <code>pop</code>,
* except the obvious mismatch between the real application context
* and the context set in the NDC. Use of the {@link NDCContextCreator}
* class can automate this process and make your code exception-safe.
*
* If configured to do so, {@link log4cplus::PatternLayout} and {@link
* log4cplus::TTCCLayout} instances automatically retrieve the nested diagnostic
* context for the current thread without any user intervention.
* Hence, even if a server is serving multiple clients
* simultaneously, the logs emanating from the same code (belonging to
* the same logger) can still be distinguished because each client
* request will have a different NDC tag.
*
* Heavy duty systems should call the {@link #remove} method when
* leaving the run method of a thread. This ensures that the memory
* used by the thread can be freed.
*
* A thread may inherit the nested diagnostic context of another
* (possibly parent) thread using the {@link #inherit inherit}
* method. A thread may obtain a copy of its NDC with the {@link
* #cloneStack cloneStack} method and pass the reference to any other
* thread, in particular to a child.
*/
class LOG4CPLUS_EXPORT NDC : protected log4cplus::helpers::LogLogUser {
public:
/**
* Clear any nested diagnostic information if any. This method is
* useful in cases where the same thread can be potentially used
* over and over in different unrelated contexts.
*
* This method is equivalent to calling the {@link #setMaxDepth}
* method with a zero <code>maxDepth</code> argument.
*/
void clear();
/**
* Clone the diagnostic context for the current thread.
*
* 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.
*
* The child thread uses the {@link #inherit inherit} method to
* inherit the parent's diagnostic context.
*
* @return Stack A clone of the current thread's diagnostic context.
*/
DiagnosticContextStack cloneStack();
/**
* Inherit the diagnostic context of another thread.
*
* The parent thread can obtain a reference to its diagnostic
* context using the {@link #cloneStack} method. It should
* communicate this information to its child so that it may inherit
* the parent's diagnostic context.
*
* 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.
*/
void inherit(const DiagnosticContextStack& stack);
/**
* Used when printing the diagnostic context.
*/
log4cplus::tstring get();
/**
* Get the current nesting depth of this diagnostic context.
*
* @see #setMaxDepth
*/
size_t getDepth();
/**
* Clients should call this method before leaving a diagnostic
* context.
*
* The returned value is the value that was pushed last. If no
* context is available, then the empty string "" is
* returned. If each call to push() is paired with a call to
* pop() (even in presence of thrown exceptions), the last
* pop() call frees the memory used by NDC for this
* thread. Otherwise, remove() must be called at the end of
* the thread to free the memory used by NDC for the thread.
*
* @return String The innermost diagnostic context.
*
* @see NDCContextCreator, remove(), push()
*/
log4cplus::tstring pop();
/**
* Looks at the last diagnostic context at the top of this NDC
* without removing it.
*
* The returned value is the value that was pushed last. If no
* context is available, then the empty string "" is returned.
*
* @return String The innermost diagnostic context.
*/
log4cplus::tstring peek();
/**
* Push new diagnostic context information for the current thread.
*
* The contents of the <code>message</code> parameter is
* determined solely by the client. Each call to push() should
* be paired with a call to pop().
*
* @param message The new diagnostic context information.
*
* @see NDCContextCreator, pop(), remove()
*/
void push(const log4cplus::tstring& message);
/**
* Remove the diagnostic context for this thread.
*
* Each thread that created a diagnostic context by calling
* push() should call this method before exiting. Otherwise,
* the memory used by the thread cannot be reclaimed. It is
* possible to omit this call if and only if each push() call
* is always paired with a pop() call (even in presence of
* thrown exceptions). Then the memory used by NDC will be
* returned by the last pop() call and a call to remove() will
* be no-op.
*/
void remove();
/**
* Set maximum depth of this diagnostic context. If the current
* depth is smaller or equal to <code>maxDepth</code>, then no
* action is taken.
*
* This method is a convenient alternative to multiple {@link
* #pop} calls. Moreover, it is often the case that at the end of
* complex call sequences, the depth of the NDC is
* unpredictable. The <code>setMaxDepth</code> method circumvents
* this problem.
*
* For example, the combination
* <pre>
* void foo() {
* size_t depth = NDC.getDepth();
*
* ... complex sequence of calls
*
* NDC.setMaxDepth(depth);
* }
* </pre>
*
* ensures that between the entry and exit of foo the depth of the
* diagnostic stack is conserved.
*
* <b>Note:</b> Use of the {@link NDCContextCreator} class will solve
* this particular problem.
*
* @see #getDepth
*/
void setMaxDepth(size_t maxDepth);
// Dtor
~NDC();
private:
// Methods
DiagnosticContextStack* getPtr();
// Data
LOG4CPLUS_THREAD_LOCAL_TYPE threadLocal;
// Disallow construction (and copying) except by getNDC()
NDC();
NDC(const NDC&);
NDC& operator=(const NDC&);
// Friends
#if defined (_MSC_VER) || defined (__HP_aCC)
friend LOG4CPLUS_EXPORT NDC& getNDC();
#else
friend NDC& getNDC();
#endif
};
/**
* Return a reference to the singleton object.
*/
LOG4CPLUS_EXPORT NDC& getNDC();
/**
* This is the internal object that is stored on the NDC stack.
*/
struct LOG4CPLUS_EXPORT DiagnosticContext {
// Ctors
DiagnosticContext(const log4cplus::tstring& message, DiagnosticContext *parent);
DiagnosticContext(const log4cplus::tstring& message);
// Data
log4cplus::tstring message; /*!< The message at this context level. */
log4cplus::tstring fullMessage; /*!< The entire message stack. */
};
/**
* This class ensures that a {@link NDC#push} call is always matched with
* a {@link NDC#pop} call even in the face of exceptions.
*/
class LOG4CPLUS_EXPORT NDCContextCreator {
public:
/** Pushes <code>msg</code> onto the NDC stack. */
NDCContextCreator(const log4cplus::tstring& msg);
/** Pops the NDC stack. */
~NDCContextCreator();
};
} // end namespace log4cplus
#endif // _LO4CPLUS_NDC_HEADER_
|