8000 Added LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION option. · log4cplus/log4cplus@2072828 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2072828

Browse files
MaksymBwilx
authored andcommitted
Added LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION option.
Switched off by default. In case this option is switched on, the library will throw an `std::logic_error` exception when used without initialization (e. g. using log4cplus::Initializer).
1 parent 56974af commit 2072828

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ set (log4cplus_postfix "")
3939

4040
option(LOG4CPLUS_BUILD_TESTING "Build the test suite." ON)
4141
option(LOG4CPLUS_BUILD_LOGGINGSERVER "Build the logging server." ON)
42+
option(LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION "Require explicit initialization (see log4cplus::Initializer)" OFF)
4243

4344
if(NOT LOG4CPLUS_SINGLE_THREADED)
4445
find_package (Threads)

src/CMakeLists.txt

Lin 8000 es changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ set (log4cplus_sources
4949
tls.cxx
5050
version.cxx)
5151

52+
if (LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION)
53+
set_property(SOURCE global-init.cxx
54+
APPEND PROPERTY COMPILE_DEFINITIONS LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION)
55+
endif(LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION)
56+
5257
#message (STATUS "Type: ${UNIX}|${CYGWIN}|${WIN32}")
5358

5459
if ("${UNIX}" OR "${CYGWIN}")

src/global-init.cxx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,10 +215,28 @@ alloc_dc ()
215215

216216
static
217217
DefaultContext *
218-
get_dc (bool alloc = 8000 true)
218+
get_dc(
219+
#ifdef LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION
220+
bool alloc = false
221+
#else
222+
bool alloc = true
223+
#endif
224+
)
219225
{
220-
if (LOG4CPLUS_UNLIKELY (! default_context && alloc))
221-
alloc_dc ();
226+
if (LOG4CPLUS_UNLIKELY(!default_context))
227+
{
228+
if (alloc)
229+
{
230+
alloc_dc();
231+
}
232+
else
233+
{
234+
#ifdef LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION
235+
tcerr << LOG4CPLUS_TEXT("ERROR: log4cplus is not initialized (see log4cplus::Initializer)") << std::endl;
236+
throw std::logic_error("log4cplus is not initialized");
237+
#endif
238+
}
239+
}
222240
return default_context;
223241
}
224242

@@ -630,11 +648,13 @@ thread_callback (LPVOID /*hinstDLL*/, DWORD fdwReason, LPVOID /*lpReserved*/)
630648
{
631649
case DLL_PROCESS_ATTACH:
632650
{
651+
#if !defined(LOG4CPLUS_REQUIRE_EXPLICIT_INITIALIZATION)
633652
// We cannot initialize log4cplus directly here. This is because
634653
// DllMain() is called under loader lock. When we are using C++11
635654
// threads and synchronization primitives then there is a deadlock
636655
// somewhere in internals of std::mutex::lock().
637656
queueLog4cplusInitializationThroughAPC ();
657+
#endif
638658
break;
639659
}
640660

0 commit comments

Comments
 (0)
0