log4cplus  2.0.0
internal.h
Go to the documentation of this file.
00001 // -*- C++ -*-
00002 // Module:  Log4CPLUS
00003 // File:    internal.h
00004 // Created: 1/2009
00005 // Author:  Vaclav Haisman
00006 //
00007 //
00008 //  Copyright (C) 2009-2015, Vaclav Haisman. All rights reserved.
00009 //  
00010 //  Redistribution and use in source and binary forms, with or without modifica-
00011 //  tion, are permitted provided that the following conditions are met:
00012 //  
00013 //  1. Redistributions of  source code must  retain the above copyright  notice,
00014 //     this list of conditions and the following disclaimer.
00015 //  
00016 //  2. Redistributions in binary form must reproduce the above copyright notice,
00017 //     this list of conditions and the following disclaimer in the documentation
00018 //     and/or other materials provided with the distribution.
00019 //  
00020 //  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
00021 //  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
00022 //  FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
00023 //  APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
00024 //  INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
00025 //  DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026 //  OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
00027 //  ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
00028 //  (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
00029 //  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00037 #ifndef LOG4CPLUS_INTERNAL_INTERNAL_HEADER_
00038 #define LOG4CPLUS_INTERNAL_INTERNAL_HEADER_
00039 
00040 #include <log4cplus/config.hxx>
00041 
00042 #if defined (LOG4CPLUS_HAVE_PRAGMA_ONCE)
00043 #pragma once
00044 #endif
00045 
00046 #if ! defined (INSIDE_LOG4CPLUS)
00047 #  error "This header must not be be used outside log4cplus' implementation files."
00048 #endif
00049 
00050 #include <memory>
00051 #include <vector>
00052 #include <sstream>
00053 #include <cstdio>
00054 #include <log4cplus/tstring.h>
00055 #include <log4cplus/streams.h>
00056 #include <log4cplus/ndc.h>
00057 #include <log4cplus/mdc.h>
00058 #include <log4cplus/spi/loggingevent.h>
00059 #include <log4cplus/thread/impl/tls.h>
00060 #include <log4cplus/helpers/snprintf.h>
00061 
00062 
00063 namespace log4cplus {
00064 
00065 namespace internal {
00066 
00067 
00070 extern log4cplus::tstring const empty_str;
00071 
00072 
00073 struct gft_scratch_pad
00074 {
00075     gft_scratch_pad ();
00076     ~gft_scratch_pad ();
00077     
00078     void
00079     reset ()
00080     {
00081         uc_q_str_valid = false;
00082         q_str_valid = false;
00083         s_str_valid = false;
00084         ret.clear ();
00085     }
00086     
00087     log4cplus::tstring q_str;
00088     log4cplus::tstring uc_q_str;
00089     log4cplus::tstring s_str;
00090     log4cplus::tstring ret;
00091     log4cplus::tstring fmt;
00092     log4cplus::tstring tmp;
00093     std::vector<tchar> buffer;
00094     bool uc_q_str_valid;
00095     bool q_str_valid;
00096     bool s_str_valid;
00097 };
00098 
00099 
00100 struct appender_sratch_pad
00101 {
00102     appender_sratch_pad ();
00103     ~appender_sratch_pad ();
00104 
00105     tostringstream oss;
00106     tstring str;
00107     std::string chstr;
00108 };
00109 
00110 
00112 struct per_thread_data
00113 {
00114     per_thread_data ();
00115     ~per_thread_data ();
00116 
00117     tstring macros_str;
00118     tostringstream macros_oss;
00119     tostringstream layout_oss;
00120     DiagnosticContextStack ndc_dcs;
00121     MappedDiagnosticContextMap mdc_map;
00122     log4cplus::tstring thread_name;
00123     log4cplus::tstring thread_name2;
00124     gft_scratch_pad gft_sp;
00125     appender_sratch_pad appender_sp;
00126     log4cplus::tstring faa_str;
00127     log4cplus::tstring ll_str;
00128     spi::InternalLoggingEvent forced_log_ev;
00129     std::FILE * fnull;
00130     log4cplus::helpers::snprintf_buf snprintf_buf;
00131 };
00132 
00133 
00134 per_thread_data * alloc_ptd ();
00135 
00136 // TLS key whose value is pointer struct per_thread_data.
00137 extern log4cplus::thread::impl::tls_key_type tls_storage_key;
00138 
00139 
00140 #if ! defined (LOG4CPLUS_SINGLE_THREADED) \
00141     && defined (LOG4CPLUS_THREAD_LOCAL_VAR)
00142 
00143 extern LOG4CPLUS_THREAD_LOCAL_VAR per_thread_data * ptd;
00144 
00145 
00146 inline
00147 void
00148 set_ptd (per_thread_data * p)
00149 {
00150     ptd = p;
00151 }
00152 
00153 
00154 inline
00155 per_thread_data *
00156 get_ptd (bool alloc = true)
00157 {
00158     if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
00159         return alloc_ptd ();
00160 
00161     // The assert() does not belong here. get_ptd() might be called by
00162     // cleanup code that can handle the returned NULL pointer.
00163     //assert (ptd);
00164 
00165     return ptd;
00166 }
00167 
00168 
00169 #else // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
00170 
00171 
00172 inline
00173 void
00174 set_ptd (per_thread_data * p)
00175 {
00176     thread::impl::tls_set_value (tls_storage_key, p);
00177 }
00178 
00179 
00180 inline
00181 per_thread_data *
00182 get_ptd (bool alloc = true)
00183 {
00184     per_thread_data * ptd
00185         = reinterpret_cast<per_thread_data *>(
00186             thread::impl::tls_get_value (tls_storage_key));
00187 
00188     if (LOG4CPLUS_UNLIKELY (! ptd && alloc))
00189         return alloc_ptd ();
00190 
00191     return ptd;
00192 }
00193 
00194 
00195 #endif // defined (LOG4CPLUS_THREAD_LOCAL_VAR)
00196 
00197 
00198 inline
00199 tstring &
00200 get_thread_name_str ()
00201 {
00202     return get_ptd ()->thread_name;
00203 }
00204 
00205 
00206 inline
00207 tstring &
00208 get_thread_name2_str ()
00209 {
00210     return get_ptd ()->thread_name2;
00211 }
00212 
00213 
00214 inline
00215 gft_scratch_pad &
00216 get_gft_scratch_pad ()
00217 {
00218     return get_ptd ()->gft_sp;
00219 }
00220 
00221 
00222 inline
00223 appender_sratch_pad &
00224 get_appender_sp ()
00225 {
00226     return get_ptd ()->appender_sp;
00227 }
00228 
00229 
00230 } // namespace internal {
00231 
00232 
00233 namespace detail
00234 {
00235 
00236 LOG4CPLUS_EXPORT void clear_tostringstream (tostringstream &);
00237 
00238 } // namespace detail
00239 
00240 
00241 } // namespace log4cplus { 
00242 
00243 
00244 #endif // LOG4CPLUS_INTERNAL_INTERNAL_HEADER_