internal.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. // -*- C++ -*-
  2. // Module: Log4CPLUS
  3. // File: internal.h
  4. // Created: 1/2009
  5. // Author: Vaclav Haisman
  6. //
  7. //
  8. // Copyright (C) 2009-2015, 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. tstring macros_str;
  96. tostringstream macros_oss;
  97. tostringstream layout_oss;
  98. DiagnosticContextStack ndc_dcs;
  99. MappedDiagnosticContextMap mdc_map;
  100. log4cplus::tstring thread_name;
  101. log4cplus::tstring thread_name2;
  102. gft_scratch_pad gft_sp;
  103. appender_sratch_pad appender_sp;
  104. log4cplus::tstring faa_str;
  105. log4cplus::tstring ll_str;
  106. spi::InternalLoggingEvent forced_log_ev;
  107. std::FILE * fnull;
  108. log4cplus::helpers::snprintf_buf snprintf_buf;
  109. };
  110. per_thread_data * alloc_ptd ();
  111. // TLS key whose value is pointer struct per_thread_data.
  112. extern log4cplus::thread::impl::tls_key_type tls_storage_key;
  113. #if ! defined (LOG4CPLUS_SINGLE_THREADED) \
  114. && defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  115. extern LOG4CPLUS_THREAD_LOCAL_VAR per_thread_data * ptd;
  116. inline
  117. void
  118. set_ptd (per_thread_data * p)
  119. {
  120. ptd = p;
  121. }
  122. inline
  123. per_thread_data *
  124. get_ptd (bool alloc = true)
  125. {
  126. if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
  127. return alloc_ptd ();
  128. // The assert() does not belong here. get_ptd() might be called by
  129. // cleanup code that can handle the returned NULL pointer.
  130. //assert (ptd);
  131. return ptd;
  132. }
  133. #else // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  134. inline
  135. void
  136. set_ptd (per_thread_data * p)
  137. {
  138. thread::impl::tls_set_value (tls_storage_key, p);
  139. }
  140. inline
  141. per_thread_data *
  142. get_ptd (bool alloc = true)
  143. {
  144. per_thread_data * ptd
  145. = reinterpret_cast<per_thread_data *>(
  146. thread::impl::tls_get_value (tls_storage_key));
  147. if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
  148. return alloc_ptd ();
  149. return ptd;
  150. }
  151. #endif // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
  152. inline
  153. tstring &
  154. get_thread_name_str ()
  155. {
  156. return get_ptd ()->thread_name;
  157. }
  158. inline
  159. tstring &
  160. get_thread_name2_str ()
  161. {
  162. return get_ptd ()->thread_name2;
  163. }
  164. inline
  165. gft_scratch_pad &
  166. get_gft_scratch_pad ()
  167. {
  168. return get_ptd ()->gft_sp;
  169. }
  170. inline
  171. appender_sratch_pad &
  172. get_appender_sp ()
  173. {
  174. return get_ptd ()->appender_sp;
  175. }
  176. } // namespace internal {
  177. namespace detail
  178. {
  179. LOG4CPLUS_EXPORT void clear_tostringstream (tostringstream &);
  180. } // namespace detail
  181. } // namespace log4cplus {
  182. #endif // LOG4CPLUS_INTERNAL_INTERNAL_HEADER_