NDC.hh 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * NDC.hh
  3. *
  4. * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved.
  5. * Copyright 2000, Bastiaan Bakker. All rights reserved.
  6. *
  7. * See the COPYING file for the terms of usage and distribution.
  8. */
  9. #ifndef _LOG4CPP_NDC_HH
  10. #define _LOG4CPP_NDC_HH
  11. #include <log4cpp/Portability.hh>
  12. #include <string>
  13. #include <vector>
  14. LOG4CPP_NS_BEGIN
  15. /**
  16. The NDC class implements <i>nested diagnostic contexts</i> as
  17. defined by Neil Harrison in the article "Patterns for Logging
  18. Diagnostic Messages" part of the book "<i>Pattern Languages of
  19. Program Design 3</i>" edited by Martin et al.
  20. <p>A Nested Diagnostic Context, or NDC in short, is an instrument
  21. to distinguish interleaved log output from different sources. Log
  22. output is typically interleaved when a server handles multiple
  23. clients near-simulatanously.
  24. <p>Interleaved log output can still be meaningful if each log entry
  25. from different contexts had a distinctive stamp. This is where NDCs
  26. come into play.
  27. <p><em><b>Note that NDCs are managed on a per thread
  28. basis</b></em>. NDC operations such as <code>push</code>, <code>
  29. pop</code>, <code>clear</code>, <code>getDepth</code> and <code>
  30. setMaxDepth</code> affect the NDC of the <em>current</em> thread only.
  31. NDCs of other threads remain unaffected.
  32. <p>To build an NDC one uses the <code>push</code> operation.
  33. Simply put,
  34. <p><ul>
  35. <li>Contexts can be nested.
  36. <p><li>When entering a context, call <code>NDC.push</code>. As a
  37. side effect, if there is no nested diagnostic context for the
  38. current thread, this method will create it.
  39. <p><li>When leaving a context, call <code>NDC.pop</code>.
  40. </ul>
  41. <p>There is no penalty for forgetting to match each
  42. <code>push</code> operation with a corresponding <code>pop</code>,
  43. except the obvious mismatch between the real application context
  44. and the context set in the NDC.
  45. <p>Custom Layouts may include the nested diagnostic context for the
  46. current thread in log messages, without any user intervention.
  47. Hence, even if a server is serving multiple clients
  48. simultaneously, the logs emanating from the same code (belonging to
  49. the same category) can still be distinguished because each client
  50. request will have a different NDC tag.
  51. <p><em>Unfortunately, unlike Java, C++ does not have platform
  52. independent multithreading support. Therefore, currently log4cpp is
  53. not multithread aware, it implicitly assumes only one thread exists,
  54. the main process thread. </em>
  55. **/
  56. class LOG4CPP_EXPORT NDC {
  57. static bool isUsedNDC;
  58. static const std::string emptyString;
  59. public:
  60. struct DiagnosticContext {
  61. DiagnosticContext(const std::string& message);
  62. DiagnosticContext(const std::string& message,
  63. const DiagnosticContext& parent);
  64. std::string message;
  65. std::string fullMessage;
  66. };
  67. //! cleans up
  68. static void shutdown();
  69. typedef std::vector<DiagnosticContext> ContextStack;
  70. /**
  71. Clear any nested disgnostic information if any. This method is
  72. useful in cases where the same thread can be potentially used
  73. over and over in different unrelated contexts.
  74. <p>This method is equivalent to calling the <code>setMaxDepth</code>
  75. method with a zero <code>maxDepth</code> argument.
  76. **/
  77. static void clear();
  78. /**
  79. Clone the diagnostic context for the current thread.
  80. <p>Internally a diagnostic context is represented as a stack. A
  81. given thread can supply the stack (i.e. diagnostic context) to a
  82. child thread so that the child can inherit the parent thread's
  83. diagnostic context.
  84. <p>The child thread uses the <code>inherit</code> method to
  85. inherit the parent's diagnostic context.
  86. @return Stack A clone of the current thread's diagnostic context.
  87. **/
  88. static ContextStack* cloneStack();
  89. /**
  90. Get the current diagnostic context string.
  91. @return the context string.
  92. **/
  93. static const std::string& get();
  94. /**
  95. Get the current nesting depth of this diagnostic context.
  96. @return the nesting depth
  97. **/
  98. static size_t getDepth();
  99. static void inherit(ContextStack* stack);
  100. /**
  101. Clients should call this method before leaving a diagnostic
  102. context.
  103. <p>The returned value is the value that was pushed last. If no
  104. context is available, then the empty string "" is returned.
  105. @return String The innermost diagnostic context.
  106. **/
  107. static std::string pop();
  108. /**
  109. Push new diagnostic context information for the current thread.
  110. <p>The contents of the <code>message</code> parameter is
  111. determined solely by the client.
  112. @param message The new diagnostic context information.
  113. **/
  114. static void push(const std::string& message);
  115. /**
  116. Set the maximum nesting depth for the current NDC. Curently NDCs
  117. do not enforce a maximum depth and consequentially this method
  118. has no effect.
  119. @param maxDepth the maximum nesting depth
  120. **/
  121. static void setMaxDepth(int maxDepth);
  122. /**
  123. Return the NDC for the current thread.
  124. @return the NDC for the current thread
  125. **/
  126. static NDC& getNDC();
  127. NDC();
  128. virtual ~NDC();
  129. public:
  130. virtual void _clear();
  131. virtual ContextStack* _cloneStack();
  132. virtual const std::string& _get() const;
  133. virtual size_t _getDepth() const;
  134. virtual void _inherit(ContextStack* stack);
  135. virtual std::string _pop();
  136. virtual void _push(const std::string& message);
  137. virtual void _setMaxDepth(int maxDepth);
  138. ContextStack _stack;
  139. };
  140. LOG4CPP_NS_END
  141. #endif // _LOG4CPP_NDC_HH