/src/log4cplus/include/log4cplus/internal/internal.h
Line | Count | Source |
1 | | // -*- C++ -*- |
2 | | // Module: Log4CPLUS |
3 | | // File: internal.h |
4 | | // Created: 1/2009 |
5 | | // Author: Vaclav Haisman |
6 | | // |
7 | | // |
8 | | // Copyright (C) 2009-2017, 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 | | |
31 | | /** @file |
32 | | * This header contains declaration internal to log4cplus. They must never be |
33 | | * visible from user accesible headers or exported in DLL/shared libray. |
34 | | */ |
35 | | |
36 | | |
37 | | #ifndef LOG4CPLUS_INTERNAL_INTERNAL_HEADER_ |
38 | | #define LOG4CPLUS_INTERNAL_INTERNAL_HEADER_ |
39 | | |
40 | | #include <log4cplus/config.hxx> |
41 | | |
42 | | #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE) |
43 | | #pragma once |
44 | | #endif |
45 | | |
46 | | #if ! defined (INSIDE_LOG4CPLUS) |
47 | | # error "This header must not be be used outside log4cplus' implementation files." |
48 | | #endif |
49 | | |
50 | | #include <memory> |
51 | | #include <vector> |
52 | | #include <sstream> |
53 | | #include <cstdio> |
54 | | #include <log4cplus/tstring.h> |
55 | | #include <log4cplus/streams.h> |
56 | | #include <log4cplus/ndc.h> |
57 | | #include <log4cplus/mdc.h> |
58 | | #include <log4cplus/spi/loggingevent.h> |
59 | | #include <log4cplus/thread/impl/tls.h> |
60 | | #include <log4cplus/helpers/snprintf.h> |
61 | | |
62 | | |
63 | | namespace log4cplus { |
64 | | |
65 | | namespace internal { |
66 | | |
67 | | |
68 | | //! Canonical empty string. It is used when the need to return empty string |
69 | | //! by reference arises. |
70 | | extern log4cplus::tstring const empty_str; |
71 | | |
72 | | |
73 | | struct gft_scratch_pad |
74 | | { |
75 | | gft_scratch_pad (); |
76 | | ~gft_scratch_pad (); |
77 | | |
78 | | void |
79 | | reset () |
80 | 607k | { |
81 | 607k | uc_q_str_valid = false; |
82 | 607k | q_str_valid = false; |
83 | 607k | s_str_valid = false; |
84 | 607k | ret.clear (); |
85 | 607k | } |
86 | | |
87 | | log4cplus::tstring q_str; |
88 | | log4cplus::tstring uc_q_str; |
89 | | log4cplus::tstring s_str; |
90 | | log4cplus::tstring ret; |
91 | | log4cplus::tstring fmt; |
92 | | log4cplus::tstring tmp; |
93 | | std::vector<tchar> buffer; |
94 | | bool uc_q_str_valid; |
95 | | bool q_str_valid; |
96 | | bool s_str_valid; |
97 | | }; |
98 | | |
99 | | |
100 | | struct appender_sratch_pad |
101 | | { |
102 | | appender_sratch_pad (); |
103 | | ~appender_sratch_pad (); |
104 | | |
105 | | tostringstream oss; |
106 | | tstring str; |
107 | | std::string chstr; |
108 | | }; |
109 | | |
110 | | |
111 | | //! Per thread data. |
112 | | struct per_thread_data |
113 | | { |
114 | | per_thread_data (); |
115 | | ~per_thread_data (); |
116 | | |
117 | | tstring macros_str; |
118 | | tostringstream macros_oss; |
119 | | tostringstream layout_oss; |
120 | | DiagnosticContextStack ndc_dcs; |
121 | | MappedDiagnosticContextMap mdc_map; |
122 | | log4cplus::tstring thread_name; |
123 | | log4cplus::tstring thread_name2; |
124 | | gft_scratch_pad gft_sp; |
125 | | appender_sratch_pad appender_sp; |
126 | | log4cplus::tstring faa_str; |
127 | | log4cplus::tstring ll_str; |
128 | | spi::InternalLoggingEvent forced_log_ev; |
129 | | std::FILE * fnull; |
130 | | log4cplus::helpers::snprintf_buf snprintf_buf; |
131 | | }; |
132 | | |
133 | | |
134 | | per_thread_data * alloc_ptd (); |
135 | | |
136 | | // TLS key whose value is pointer struct per_thread_data. |
137 | | extern log4cplus::thread::impl::tls_key_type tls_storage_key; |
138 | | |
139 | | |
140 | | #if ! defined (LOG4CPLUS_SINGLE_THREADED) \ |
141 | | && defined (LOG4CPLUS_THREAD_LOCAL_VAR) |
142 | | |
143 | | extern LOG4CPLUS_THREAD_LOCAL_VAR per_thread_data * ptd; |
144 | | |
145 | | |
146 | | inline |
147 | | void |
148 | | set_ptd (per_thread_data * p) |
149 | 70 | { |
150 | 70 | ptd = p; |
151 | 70 | } |
152 | | |
153 | | |
154 | | inline |
155 | | per_thread_data * |
156 | | get_ptd (bool alloc = true) |
157 | 9.72M | { |
158 | 9.72M | if (LOG4CPLUS_UNLIKELY (! ptd && alloc)) |
159 | 70 | return alloc_ptd (); |
160 | | |
161 | | // The assert() does not belong here. get_ptd() might be called by |
162 | | // cleanup code that can handle the returned NULL pointer. |
163 | | //assert (ptd); |
164 | | |
165 | 9.72M | return ptd; |
166 | 9.72M | } |
167 | | |
168 | | |
169 | | #else // defined (LOG4CPLUS_THREAD_LOCAL_VAR) |
170 | | |
171 | | |
172 | | inline |
173 | | void |
174 | | set_ptd (per_thread_data * p) |
175 | | { |
176 | | thread::impl::tls_set_value (tls_storage_key, p); |
177 | | } |
178 | | |
179 | | |
180 | | inline |
181 | | per_thread_data * |
182 | | get_ptd (bool alloc = true) |
183 | | { |
184 | | per_thread_data * ptd |
185 | | = reinterpret_cast<per_thread_data *>( |
186 | | thread::impl::tls_get_value (tls_storage_key)); |
187 | | |
188 | | if (LOG4CPLUS_UNLIKELY (! ptd && alloc)) |
189 | | return alloc_ptd (); |
190 | | |
191 | | return ptd; |
192 | | } |
193 | | |
194 | | |
195 | | #endif // defined (LOG4CPLUS_THREAD_LOCAL_VAR) |
196 | | |
197 | | |
198 | | inline |
199 | | tstring & |
200 | | get_thread_name_str () |
201 | 607k | { |
202 | 607k | return get_ptd ()->thread_name; |
203 | 607k | } |
204 | | |
205 | | |
206 | | inline |
207 | | tstring & |
208 | | get_thread_name2_str () |
209 | 0 | { |
210 | 0 | return get_ptd ()->thread_name2; |
211 | 0 | } |
212 | | |
213 | | |
214 | | inline |
215 | | gft_scratch_pad & |
216 | | get_gft_scratch_pad () |
217 | 607k | { |
218 | 607k | return get_ptd ()->gft_sp; |
219 | 607k | } |
220 | | |
221 | | |
222 | | inline |
223 | | appender_sratch_pad & |
224 | | get_appender_sp () |
225 | 0 | { |
226 | 0 | return get_ptd ()->appender_sp; |
227 | 0 | } |
228 | | |
229 | | |
230 | | } // namespace internal { |
231 | | |
232 | | |
233 | | namespace detail |
234 | | { |
235 | | |
236 | | LOG4CPLUS_EXPORT void clear_tostringstream (tostringstream &); |
237 | | |
238 | | } // namespace detail |
239 | | |
240 | | |
241 | | } // namespace log4cplus { |
242 | | |
243 | | |
244 | | #endif // LOG4CPLUS_INTERNAL_INTERNAL_HEADER_ |