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.

251 lines
5.5 KiB

  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: internal.h
  4. // Created: 1/2009
  5. // Author: Vaclav Haisman
  6. //
  7. //
  8. // Copyright (C) 2009-2010, 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. //! The default value of the \param alloc is false for Win32 DLL builds
  122. //! since per thread data are already initialized by DllMain().
  123. inline
  124. per_thread_data *
  125. get_ptd (bool alloc
  126. #if defined (_WIN32) && defined (LOG4CPLUS_BUILD_DLL)
  127. = false
  128. #else
  129. = true
  130. #endif
  131. )
  132. {
  133. if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
  134. return alloc_ptd ();
  135. // The assert() does not belong here. get_ptd() might be called by
  136. // cleanup code that can handle the returned NULL pointer.
  137. //assert (ptd);
  138. return ptd;
  139. }
  140. #else // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  141. inline
  142. void
  143. set_ptd (per_thread_data * p)
  144. {
  145. thread::impl::tls_set_value (tls_storage_key, p);
  146. }
  147. inline
  148. per_thread_data *
  149. get_ptd (bool alloc = true)
  150. {
  151. per_thread_data * ptd
  152. = reinterpret_cast<per_thread_data *>(
  153. thread::impl::tls_get_value (tls_storage_key));
  154. if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
  155. return alloc_ptd ();
  156. return ptd;
  157. }
  158. #endif // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  159. inline
  160. tstring &
  161. get_thread_name_str ()
  162. {
  163. return get_ptd ()->thread_name;
  164. }
  165. inline
  166. tstring &
  167. get_thread_name2_str ()
  168. {
  169. return get_ptd ()->thread_name2;
  170. }
  171. inline
  172. gft_scratch_pad &
  173. get_gft_scratch_pad ()
  174. {
  175. return get_ptd ()->gft_sp;
  176. }
  177. inline
  178. appender_sratch_pad &
  179. get_appender_sp ()
  180. {
  181. return get_ptd ()->appender_sp;
  182. }
  183. } // namespace internal {
  184. namespace detail
  185. {
  186. LOG4CPLUS_EXPORT void clear_tostringstream (tostringstream &);
  187. } // namespace detail
  188. } // namespace log4cplus {
  189. #endif // LOG4CPLUS_INTERNAL_INTERNAL_HEADER_