You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

243 lines
5.3 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: internal.h
  4. // Created: 1/2009
  5. // Author: Vaclav Haisman
  6. //
  7. //
  8. // Copyright (C) 2009-2013, Vaclav Haisman. All rights reserved.
  9. //
  10. // Redistribution and use in source and binary forms, with or without modifica-
  11. // tion, are permitted provided that the following conditions are met:
  12. //
  13. // 1. Redistributions of source code must retain the above copyright notice,
  14. // this list of conditions and the following disclaimer.
  15. //
  16. // 2. Redistributions in binary form must reproduce the above copyright notice,
  17. // this list of conditions and the following disclaimer in the documentation
  18. // and/or other materials provided with the distribution.
  19. //
  20. // THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
  21. // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  22. // FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. // APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  24. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
  25. // DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. // OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  27. // ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  29. // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. /** @file
  31. * This header contains declaration internal to log4cplus. They must never be
  32. * visible from user accesible headers or exported in DLL/shared libray.
  33. */
  34. #ifndef LOG4CPLUS_INTERNAL_INTERNAL_HEADER_
  35. #define LOG4CPLUS_INTERNAL_INTERNAL_HEADER_
  36. #include <log4cplus/config.hxx>
  37. #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
  38. #pragma once
  39. #endif
  40. #if ! defined (INSIDE_LOG4CPLUS)
  41. # error "This header must not be be used outside log4cplus' implementation files."
  42. #endif
  43. #include <memory>
  44. #include <vector>
  45. #include <sstream>
  46. #include <cstdio>
  47. #include <log4cplus/tstring.h>
  48. #include <log4cplus/streams.h>
  49. #include <log4cplus/ndc.h>
  50. #include <log4cplus/mdc.h>
  51. #include <log4cplus/spi/loggingevent.h>
  52. #include <log4cplus/thread/impl/tls.h>
  53. #include <log4cplus/helpers/snprintf.h>
  54. namespace log4cplus {
  55. namespace internal {
  56. //! Canonical empty string. It is used when the need to return empty string
  57. //! by reference arises.
  58. extern log4cplus::tstring const empty_str;
  59. struct gft_scratch_pad
  60. {
  61. gft_scratch_pad ();
  62. ~gft_scratch_pad ();
  63. void
  64. reset ()
  65. {
  66. uc_q_str_valid = false;
  67. q_str_valid = false;
  68. s_str_valid = false;
  69. ret.clear ();
  70. }
  71. log4cplus::tstring q_str;
  72. log4cplus::tstring uc_q_str;
  73. log4cplus::tstring s_str;
  74. log4cplus::tstring ret;
  75. log4cplus::tstring fmt;
  76. log4cplus::tstring tmp;
  77. std::vector<tchar> buffer;
  78. bool uc_q_str_valid;
  79. bool q_str_valid;
  80. bool s_str_valid;
  81. };
  82. struct appender_sratch_pad
  83. {
  84. appender_sratch_pad ();
  85. ~appender_sratch_pad ();
  86. tostringstream oss;
  87. tstring str;
  88. std::string chstr;
  89. };
  90. //! Per thread data.
  91. struct per_thread_data
  92. {
  93. per_thread_data ();
  94. ~per_thread_data ();
  95. tostringstream macros_oss;
  96. tostringstream layout_oss;
  97. DiagnosticContextStack ndc_dcs;
  98. MappedDiagnosticContextMap mdc_map;
  99. log4cplus::tstring thread_name;
  100. log4cplus::tstring thread_name2;
  101. gft_scratch_pad gft_sp;
  102. appender_sratch_pad appender_sp;
  103. log4cplus::tstring faa_str;
  104. log4cplus::tstring ll_str;
  105. spi::InternalLoggingEvent forced_log_ev;
  106. std::FILE * fnull;
  107. log4cplus::helpers::snprintf_buf snprintf_buf;
  108. };
  109. per_thread_data * alloc_ptd ();
  110. // TLS key whose value is pointer struct per_thread_data.
  111. extern log4cplus::thread::impl::tls_key_type tls_storage_key;
  112. #if ! defined (LOG4CPLUS_SINGLE_THREADED) \
  113. && defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  114. extern LOG4CPLUS_THREAD_LOCAL_VAR per_thread_data * ptd;
  115. inline
  116. void
  117. set_ptd (per_thread_data * p)
  118. {
  119. ptd = p;
  120. }
  121. inline
  122. per_thread_data *
  123. get_ptd (bool alloc = true)
  124. {
  125. if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
  126. return alloc_ptd ();
  127. // The assert() does not belong here. get_ptd() might be called by
  128. // cleanup code that can handle the returned NULL pointer.
  129. //assert (ptd);
  130. return ptd;
  131. }
  132. #else // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  133. inline
  134. void
  135. set_ptd (per_thread_data * p)
  136. {
  137. thread::impl::tls_set_value (tls_storage_key, p);
  138. }
  139. inline
  140. per_thread_data *
  141. get_ptd (bool alloc = true)
  142. {
  143. per_thread_data * ptd
  144. = reinterpret_cast<per_thread_data *>(
  145. thread::impl::tls_get_value (tls_storage_key));
  146. if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
  147. return alloc_ptd ();
  148. return ptd;
  149. }
  150. #endif // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  151. inline
  152. tstring &
  153. get_thread_name_str ()
  154. {
  155. return get_ptd ()->thread_name;
  156. }
  157. inline
  158. tstring &
  159. get_thread_name2_str ()
  160. {
  161. return get_ptd ()->thread_name2;
  162. }
  163. inline
  164. gft_scratch_pad &
  165. get_gft_scratch_pad ()
  166. {
  167. return get_ptd ()->gft_sp;
  168. }
  169. inline
  170. appender_sratch_pad &
  171. get_appender_sp ()
  172. {
  173. return get_ptd ()->appender_sp;
  174. }
  175. } // namespace internal {
  176. namespace detail
  177. {
  178. LOG4CPLUS_EXPORT void clear_tostringstream (tostringstream &);
  179. } // namespace detail
  180. } // namespace log4cplus {
  181. #endif // LOG4CPLUS_INTERNAL_INTERNAL_HEADER_