unit tests running with windows shared dll (#178)

* unit tests running with windows shared dll

* CMake automatically uses DLL runtime - remove cmake entries modifying
compiler flags

* missing DBUG vs DEBUG issue in Linux

* generated header added

* correction in CMake: Set HEADER_FILES correctly

* added static library option

* switched to powershell and included WORKING_DIRECTORY

* powershell use in windows and WORKING_DIRECTORY instead of cd

* Fixed appveyor.yml to use Release configuration and run unit tests

* trying to make appveyor work again ...

* make sure ERRORLEVEL is 0 when unit tests run successfully

* Still trying to fix appveyor ...

* Defaulting to shared library in linux, too

* Removed runtime loading test when g3log is not a shared library in linux

* Run unit tests verbosly using ctest.exe

* Disabled AggressiveThreadCallsDuringShutdown test

* Revert "Run unit tests verbosly using ctest.exe" (accidental commit)

This reverts commit b30513450d.

* re-committing valid parts of reverted commit

* DBUG vs DEBUG fix moved to generated_definitions.hpp

* cleanup shared/static option

* clarify cmake build types regardless of type

* Added compile test for DEBUG and DBUG

* put back formatting

* Removed commented out /MD switches for MSVC
This commit is contained in:
Andreas Schönle 2017-05-09 18:26:48 +02:00 committed by Kjell Hedström
parent a1b5d97689
commit 123977f106
16 changed files with 152 additions and 146 deletions

View File

@ -12,6 +12,7 @@
SET(LOG_SRC ${g3log_SOURCE_DIR}/src)
include_directories(${LOG_SRC})
include_directories("${CMAKE_CURRENT_BINARY_DIR}/include")
SET(ACTIVE_CPP0xx_DIR "Release")
#cmake -DCMAKE_CXX_COMPILER=clang++ ..
@ -60,9 +61,6 @@ ELSEIF(${CMAKE_CXX_COMPILER_ID} STREQUAL "GNU")
ENDIF()
ELSEIF(MSVC)
set(PLATFORM_LINK_LIBRIES dbghelp)
set(CMAKE_CXX_FLAGS_RELEASE "/MT")
set(CMAKE_CXX_FLAGS_DEBUG "/MTd")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8") # source code already in utf-8, force it for compilers in non-utf8_windows_locale
# ERROR level conflicts with windows.h
ADD_DEFINITIONS (-DNOGDI)
@ -93,20 +91,32 @@ ENDIF()
file(GLOB SRC_FILES ${LOG_SRC}/g3log/*.h ${LOG_SRC}/g3log/*.hpp ${LOG_SRC}/*.cpp ${LOG_SRC}/*.ipp)
file(GLOB HEADER_FILES ${LOG_SRC}/g3log/*.hpp ${LOG_SRC}/*.hpp)
list( APPEND HEADER_FILES ${GENERATED_G3_DEFINITIONS} )
list( APPEND SRC_FILES ${GENERATED_G3_DEFINITIONS} )
IF (MSVC OR MINGW)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_unix.cpp)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_unix.cpp)
ELSE()
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_windows.cpp ${LOG_SRC}/g3log/stacktrace_windows.hpp ${LOG_SRC}/stacktrace_windows.cpp)
list(REMOVE_ITEM SRC_FILES ${LOG_SRC}/crashhandler_windows.cpp ${LOG_SRC}/g3log/stacktrace_windows.hpp ${LOG_SRC}/stacktrace_windows.cpp)
ENDIF (MSVC OR MINGW)
set(SRC_FILES ${SRC_FILES} ${SRC_PLATFORM_SPECIFIC})
# Create the g3log library
INCLUDE_DIRECTORIES(${LOG_SRC})
SET(G3LOG_LIBRARY g3logger)
SET(G3LOG_LIBRARY g3logger)
IF( G3_SHARED_LIB )
IF(NOT(CMAKE_VERSION LESS 3.4))
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
ELSE()
message( FATAL "Need CMake version >=3.4 to build shared windows library!" )
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} SHARED ${SRC_FILES})
ELSE()
ADD_LIBRARY(${G3LOG_LIBRARY} STATIC ${SRC_FILES})
ENDIF()
ADD_LIBRARY(${G3LOG_LIBRARY} SHARED ${SRC_FILES})
SET(${G3LOG_LIBRARY}_VERSION_STRING ${VERSION})
MESSAGE("Creating ${G3LOG_LIBRARY} VERSION: " ${VERSION})
SET_TARGET_PROPERTIES(g3logger PROPERTIES LINKER_LANGUAGE CXX SOVERSION ${VERSION})
@ -120,13 +130,6 @@ ENDIF()
set_target_properties(${G3LOG_LIBRARY} PROPERTIES MACOSX_RPATH TRUE)
ENDIF()
IF(NOT ${ADD_BUILD_WIN_SHARED}.x STREQUAL ".x")
IF(NOT(CMAKE_VERSION LESS 3.4) AND MSVC)
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
ENDIF()
ENDIF()
TARGET_LINK_LIBRARIES(${G3LOG_LIBRARY} ${PLATFORM_LINK_LIBRIES})
# Kjell: This is likely not necessary, except for Windows?

View File

@ -1,10 +1,10 @@
# ==========================================================================
# =============================================================================
# 2010 by KjellKod.cc. This is PUBLIC DOMAIN to use at your own risk and comes
# with no warranties. This code is yours to share, use and modify with no
# strings attached and no restrictions or obligations.
#
# For more information see g3log/LICENSE or refer refer to http://unlicense.org
# ============================================================================*/
# ==============================================================================
# Below are details for compiling on Windows and Linux
@ -49,13 +49,16 @@
cmake_minimum_required (VERSION 3.1)
ENABLE_LANGUAGE(CXX)
if(NOT CMAKE_BUILD_TYPE)
if(NOT CMAKE_BUILD_TYPE AND NOT (MSVC_IDE OR XCODE))
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type, one of: Release, Debug" FORCE)
endif()
message("Build type: ${CMAKE_BUILD_TYPE}")
project (g3log)
message("Build type: ${CMAKE_BUILD_TYPE}")
message("Configuration types: ${CMAKE_CONFIGURATION_TYPES}")
# Detect 64 or 32 bit
if (CMAKE_SIZEOF_VOID_P EQUAL 8)
# 64-bit project

View File

@ -21,7 +21,7 @@
MESSAGE("")
MESSAGE("COMPILE_DEFINITIONS:\n\t[${G3_DEFINITIONS}]")
MESSAGE("")
SET(GENERATED_G3_DEFINITIONS src/g3log/generated_definitions.hpp)
SET(GENERATED_G3_DEFINITIONS "${CMAKE_CURRENT_BINARY_DIR}/include/g3log/generated_definitions.hpp")
file(REMOVE ${GENERATED_G3_DEFINITIONS} )
FILE(WRITE ${GENERATED_G3_DEFINITIONS} "// AUTO GENERATED MACRO DEFINITIONS FOR G3LOG\n\n")
FILE(APPEND ${GENERATED_G3_DEFINITIONS} "${HEADER}\n")

View File

@ -35,7 +35,7 @@ IF(USE_DYNAMIC_LOGGING_LEVELS)
MESSAGE("\tDynamic logging levels is used")
MESSAGE("\tUse [g3::addLogLevel(LEVEL boolean)] to enable/disable logging on specified levels\n\n")
ELSE()
MESSAGE("-DUSE_DYNAMIC_LOGGING_LEVELS=OFF")
MESSAGE("-DUSE_DYNAMIC_LOGGING_LEVELS=OFF")
ENDIF(USE_DYNAMIC_LOGGING_LEVELS)
@ -47,9 +47,11 @@ option (CHANGE_G3LOG_DEBUG_TO_DBUG
"Use DBUG logging level instead of DEBUG. By default DEBUG is the debugging level" OFF)
IF(CHANGE_G3LOG_DEBUG_TO_DBUG)
LIST(APPEND G3_DEFINITIONS CHANGE_G3LOG_DEBUG_TO_DBUG)
LIST(APPEND G3_DEFINITIONS "G3LOG_DEBUG DBUG")
MESSAGE("-DCHANGE_G3LOG_DEBUG_TO_DBUG=ON DBUG instead of DEBUG logging level is used")
ELSE()
MESSAGE("-DCHANGE_G3LOG_DEBUG_TO_DBUG=OFF \t(Debuggin logging level is 'DEBUG')")
ELSE()
LIST(APPEND G3_DEFINITIONS "G3LOG_DEBUG DEBUG")
MESSAGE("-DCHANGE_G3LOG_DEBUG_TO_DBUG=OFF \t(Debuggin logging level is 'DEBUG')")
ENDIF(CHANGE_G3LOG_DEBUG_TO_DBUG)
@ -61,13 +63,21 @@ option (ENABLE_FATAL_SIGNALHANDLING
"Vectored exception / crash handling with improved stack trace" ON)
IF(NOT ENABLE_FATAL_SIGNALHANDLING)
LIST(APPEND G3_DEFINITIONS DISABLE_FATAL_SIGNALHANDLING)
LIST(APPEND G3_DEFINITIONS DISABLE_FATAL_SIGNALHANDLING)
MESSAGE("-DENABLE_FATAL_SIGNALHANDLING=OFF Fatal signal handler is disabled")
ELSE()
MESSAGE("-DENABLE_FATAL_SIGNALHANDLING=ON\tFatal signal handler is enabled")
MESSAGE("-DENABLE_FATAL_SIGNALHANDLING=OFF Fatal signal handler is disabled")
ELSE()
MESSAGE("-DENABLE_FATAL_SIGNALHANDLING=ON\tFatal signal handler is enabled")
ENDIF(NOT ENABLE_FATAL_SIGNALHANDLING)
# Option for building as a static or shared library in all platforms
option (G3_SHARED_LIB "Build shared library" ON)
IF(G3_SHARED_LIB)
MESSAGE("-DG3_SHARED_LIB=ON\tBuild shared library")
ELSE()
MESSAGE("-DG3_SHARED_LIB=ON\tBuild static library")
ENDIF()
# WINDOWS OPTIONS
IF (MSVC OR MINGW)
# -DENABLE_VECTORED_EXCEPTIONHANDLING=ON : defualt change the
@ -77,12 +87,12 @@ IF (MSVC OR MINGW)
option (ENABLE_VECTORED_EXCEPTIONHANDLING
"Vectored exception / crash handling with improved stack trace" ON)
IF(NOT ENABLE_VECTORED_EXCEPTIONHANDLING)
IF(NOT ENABLE_VECTORED_EXCEPTIONHANDLING)
LIST(APPEND G3_DEFINITIONS DISABLE_VECTORED_EXCEPTIONHANDLING)
MESSAGE("-DENABLE_VECTORED_EXCEPTIONHANDLING=OFF Vectored exception handling is disabled")
ELSE()
MESSAGE("-DENABLE_VECTORED_EXCEPTIONHANDLING=ON\t\t\tVectored exception handling is enabled")
ENDIF(NOT ENABLE_VECTORED_EXCEPTIONHANDLING)
ELSE()
MESSAGE("-DENABLE_VECTORED_EXCEPTIONHANDLING=ON\t\t\tVectored exception handling is enabled")
ENDIF(NOT ENABLE_VECTORED_EXCEPTIONHANDLING)
@ -92,22 +102,13 @@ IF (MSVC OR MINGW)
#
option (DEBUG_BREAK_AT_FATAL_SIGNAL
"Enable Visual Studio break point when receiving a fatal exception. In __DEBUG mode only" OFF)
IF(DEBUG_BREAK_AT_FATAL_SIGNAL)
LIST(APPEND G3_DEFINITIONS DEBUG_BREAK_AT_FATAL_SIGNAL)
MESSAGE("-DDEBUG_BREAK_AT_FATAL_SIGNAL=ON Break point for fatal signal is enabled for __DEBUG.")
ELSE()
MESSAGE("-DDEBUG_BREAK_AT_FATAL_SIGNAL=OFF\t\t\tBreak point for fatal signal is disabled")
ENDIF(DEBUG_BREAK_AT_FATAL_SIGNAL)
IF(DEBUG_BREAK_AT_FATAL_SIGNAL)
LIST(APPEND G3_DEFINITIONS DEBUG_BREAK_AT_FATAL_SIGNAL)
MESSAGE("-DDEBUG_BREAK_AT_FATAL_SIGNAL=ON Break point for fatal signal is enabled for __DEBUG.")
ELSE()
MESSAGE("-DDEBUG_BREAK_AT_FATAL_SIGNAL=OFF\t\t\tBreak point for fatal signal is disabled")
ENDIF(DEBUG_BREAK_AT_FATAL_SIGNAL)
# Option for adding windows shared library
option (ADD_BUILD_WIN_SHARED "Build shared library on Windows" OFF)
IF(ADD_BUILD_WIN_SHARED)
MESSAGE("-DADD_BUILD_WIN_SHARED=ON")
ELSE()
MESSAGE("-DADD_BUILD_WIN_SHARED=OFF")
ENDIF()
ENDIF (MSVC OR MINGW)
MESSAGE("\n\n\n")

View File

@ -18,7 +18,7 @@ install:
# by default, all script lines are interpreted as batch
build:
project: g3log\build\g3log.sln
project: c:\projects\g3log\build\g3log.sln
# scripts to run before build
before_build:
@ -28,11 +28,11 @@ before_build:
- cd c:\projects\g3log\
- mkdir build
- cd build
- cmake -DCMAKE_BUILD=Relese -G "Visual Studio 14 2015 Win64" -DADD_G3LOG_UNIT_TEST=ON ..
- msbuild g3log.sln
- cmake -G "Visual Studio 14 2015 Win64" -DADD_G3LOG_UNIT_TEST=ON -DUSE_DYNAMIC_LOGGING_LEVELS=ON -DCHANGE_G3LOG_DEBUG_TO_DBUG=ON ..
- msbuild g3log.sln /p:Configuration=Release
# scripts to run after build
after_build:
- cd c:\projects\g3log\build\Release
- g3log-FATAL-contract.exe
- cmd /c Release\g3log-FATAL-contract.exe || exit /B 0
- ctest -C Release --verbose

View File

@ -30,11 +30,6 @@ namespace example_fatal
}
} // example fatal
#ifdef CHANGE_G3LOG_DEBUG_TO_DBUG
#undef DEBUG
#define DEBUG DBUG
#endif
int main(int argc, char **argv)
{
double pi_d = 3.1415926535897932384626433832795;
@ -51,14 +46,14 @@ int main(int argc, char **argv)
LOGF(INFO, "Hi log %d", 123);
LOG(INFO) << "Test SLOG INFO";
LOG(DEBUG) << "Test SLOG DEBUG";
LOG(G3LOG_DEBUG) << "Test SLOG DEBUG";
LOG(INFO) << "one: " << 1;
LOG(INFO) << "two: " << 2;
LOG(INFO) << "one and two: " << 1 << " and " << 2;
LOG(DEBUG) << "float 2.14: " << 1000 / 2.14f;
LOG(DEBUG) << "pi double: " << pi_d;
LOG(DEBUG) << "pi float: " << pi_f;
LOG(DEBUG) << "pi float (width 10): " << std::setprecision(10) << pi_f;
LOG(G3LOG_DEBUG) << "float 2.14: " << 1000 / 2.14f;
LOG(G3LOG_DEBUG) << "pi double: " << pi_d;
LOG(G3LOG_DEBUG) << "pi float: " << pi_f;
LOG(G3LOG_DEBUG) << "pi float (width 10): " << std::setprecision(10) << pi_f;
LOGF(INFO, "pi float printf:%f", pi_f);
// FATAL SECTION

View File

@ -24,11 +24,6 @@
#define NOEXCEPT throw()
#endif
#ifdef CHANGE_G3LOG_DEBUG_TO_DBUG
#undef DEBUG
#define DEBUG DBUG
#endif
namespace
{
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
@ -46,33 +41,33 @@ namespace
void RaiseSIGABRT() {
raise(SIGABRT);
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
LOG(WARNING) << "Expected to have died by now...";
}
void RaiseSIGFPE() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
LOGF_IF(INFO, (false != true), "Exiting %s SIGFPE", "by");
raise(SIGFPE);
LOG(WARNING) << "Expected to have died by now...";
}
void RaiseSIGSEGV() {
LOG(DEBUG) << " trigger exit";
LOG(DEBUG) << "Exit by SIGSEGV";
LOG(G3LOG_DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << "Exit by SIGSEGV";
raise(SIGSEGV);
LOG(WARNING) << "Expected to have died by now...";
}
void RaiseSIGILL() {
LOG(DEBUG) << " trigger exit";
LOGF(DEBUG, "Exit by %s", "SIGILL");
LOG(G3LOG_DEBUG) << " trigger exit";
LOGF(G3LOG_DEBUG, "Exit by %s", "SIGILL");
raise(SIGILL);
LOG(WARNING) << "Expected to have died by now...";
}
void RAiseSIGTERM() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
LOGF_IF(INFO, (false != true), "Exiting %s SIGFPE", "by");
raise(SIGTERM);
LOG(WARNING) << "Expected to have died by now...";
@ -80,7 +75,7 @@ namespace
int gShouldBeZero = 1;
void DivisionByZero() {
LOG(DEBUG) << " trigger exit Executing DivisionByZero: gShouldBeZero: " << gShouldBeZero;
LOG(G3LOG_DEBUG) << " trigger exit Executing DivisionByZero: gShouldBeZero: " << gShouldBeZero;
LOG(INFO) << "Division by zero is a big no-no";
int value = 3;
auto test = value / gShouldBeZero;
@ -88,14 +83,14 @@ namespace
}
void IllegalPrintf() {
LOG(DEBUG) << " trigger exit";
LOG(DEBUG) << "Impending doom due to illeteracy";
LOG(G3LOG_DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << "Impending doom due to illeteracy";
LOGF(INFO, "2nd attempt at ILLEGAL PRINTF_SYNTAX %d EXAMPLE. %s %s", "hello", 1);
LOG(WARNING) << "Expected to have died by now...";
}
void OutOfBoundsArrayIndexing() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
std::vector<int> v;
v[0] = 5;
LOG(WARNING) << "Expected to have died by now...";
@ -103,7 +98,7 @@ namespace
void AccessViolation() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
char *ptr = 0;
LOG(INFO) << "Death by access violation is imminent";
*ptr = 0;
@ -111,12 +106,12 @@ namespace
}
void NoExitFunction() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
CHECK(false) << "This function should never be called";
}
void RaiseSIGABRTAndAccessViolation() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
auto f1 = std::async(std::launch::async, &RaiseSIGABRT);
auto f2 = std::async(std::launch::async, &AccessViolation);
@ -127,7 +122,7 @@ namespace
using deathfunc = void (*) (void);
void Death_x10000(deathfunc func, std::string funcname) NOEXCEPT {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
std::vector<std::future<void>> asyncs;
asyncs.reserve(10000);
for (auto idx = 0; idx < 10000; ++idx) {
@ -143,7 +138,7 @@ namespace
void Throw() NOEXCEPT {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
std::future<int> empty;
empty.get();
// --> thows future_error http://en.cppreference.com/w/cpp/thread/future_error
@ -162,7 +157,7 @@ namespace
}
void FailedCHECK() {
LOG(DEBUG) << " trigger exit";
LOG(G3LOG_DEBUG) << " trigger exit";
CHECK(false) << "This is fatal";
}
@ -177,7 +172,7 @@ namespace
void ExecuteDeathFunction(const bool runInNewThread, int fatalChoice) {
LOG(DEBUG) << "trigger exit";
LOG(G3LOG_DEBUG) << "trigger exit";
auto exitFunction = &NoExitFunction;
switch (fatalChoice) {
@ -286,7 +281,7 @@ void breakHere() {
std::ostringstream oss;
oss << "Fatal hook function: " << __FUNCTION__ << ":" << __LINE__ << " was called";
oss << " through g3::setFatalPreLoggingHook(). setFatalPreLoggingHook should be called AFTER g3::initializeLogging()" << std::endl;
LOG(DEBUG) << oss.str();
LOG(G3LOG_DEBUG) << oss.str();
#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
__debugbreak();
#endif
@ -306,7 +301,7 @@ int main(int argc, char **argv)
<< "The logfile is generated at: [" << log_file_name.get() << "]\n\n" << std::endl;
LOGF(DEBUG, "Fatal exit example starts now, it's as easy as %d", 123);
LOGF(G3LOG_DEBUG, "Fatal exit example starts now, it's as easy as %d", 123);
LOG(INFO) << "Feel free to read the source code also in g3log/example/main_fatal_choice.cpp";
while (true) {

View File

@ -22,11 +22,6 @@ namespace
#endif
}
#ifdef CHANGE_G3LOG_DEBUG_TO_DBUG
#undef DEBUG
#define DEBUG DBUG
#endif
namespace example_fatal
{
// on Ubunti this caused get a compiler warning with gcc4.6
@ -38,7 +33,7 @@ namespace example_fatal
std::cout << "************************************************************\n\n" << std::endl << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(1));
const std::string logging = "logging";
LOGF(DEBUG, "ILLEGAL PRINTF_SYNTAX EXAMPLE. WILL GENERATE compiler warning.\n\nbadly formatted message:[Printf-type %s is the number 1 for many %s]", logging.c_str());
LOGF(G3LOG_DEBUG, "ILLEGAL PRINTF_SYNTAX EXAMPLE. WILL GENERATE compiler warning.\n\nbadly formatted message:[Printf-type %s is the number 1 for many %s]", logging.c_str());
}
@ -84,14 +79,14 @@ int main(int argc, char **argv)
LOGF(INFO, "Hi log %d", 123);
LOG(INFO) << "Test SLOG INFO";
LOG(DEBUG) << "Test SLOG DEBUG";
LOG(G3LOG_DEBUG) << "Test SLOG DEBUG";
LOG(INFO) << "one: " << 1;
LOG(INFO) << "two: " << 2;
LOG(INFO) << "one and two: " << 1 << " and " << 2;
LOG(DEBUG) << "float 2.14: " << 1000 / 2.14f;
LOG(DEBUG) << "pi double: " << pi_d;
LOG(DEBUG) << "pi float: " << pi_f;
LOG(DEBUG) << "pi float (width 10): " << std::setprecision(10) << pi_f;
LOG(G3LOG_DEBUG) << "float 2.14: " << 1000 / 2.14f;
LOG(G3LOG_DEBUG) << "pi double: " << pi_d;
LOG(G3LOG_DEBUG) << "pi float: " << pi_f;
LOG(G3LOG_DEBUG) << "pi float (width 10): " << std::setprecision(10) << pi_f;
LOGF(INFO, "pi float printf:%f", pi_f);
//
@ -104,8 +99,8 @@ int main(int argc, char **argv)
LOG_IF(INFO, (1 < 2)) << "If true this text will be logged";
LOGF_IF(INFO, (1 < 2), "if %d<%d : then this text will be logged", 1, 2);
LOG_IF(FATAL, (2 > 3)) << "This message should NOT throw";
LOGF(DEBUG, "This API is popular with some %s", "programmers");
LOGF_IF(DEBUG, (1 < 2), "If true, then this %s will be logged", "message");
LOGF(G3LOG_DEBUG, "This API is popular with some %s", "programmers");
LOGF_IF(G3LOG_DEBUG, (1 < 2), "If true, then this %s will be logged", "message");
// OK --- on Ubunti this caused get a compiler warning with gcc4.6
// from gcc 4.7.2 (at least) it causes a crash (as expected)

View File

@ -90,14 +90,10 @@ namespace g3 {
} // g3
#if (defined(CHANGE_G3LOG_DEBUG_TO_DBUG))
const LEVELS DBUG {g3::kDebugValue, {"DEBUG"}},
#else
const LEVELS DEBUG {g3::kDebugValue, {"DEBUG"}},
#endif
INFO {g3::kInfoValue, {"INFO"}},
WARNING {g3::kWarningValue, {"WARNING"}},
FATAL {g3::kFatalValue, {"FATAL"}};
const LEVELS G3LOG_DEBUG{g3::kDebugValue, {"DEBUG"}},
INFO {g3::kInfoValue, {"INFO"}},
WARNING {g3::kWarningValue, {"WARNING"}},
FATAL {g3::kFatalValue, {"FATAL"}};

View File

@ -19,7 +19,7 @@ namespace g3 {
#ifdef G3_DYNAMIC_LOGGING
const std::map<int, LoggingLevel> g_log_level_defaults = {
{g3::kDebugValue, {DEBUG}},
{G3LOG_DEBUG.value,{G3LOG_DEBUG}},
{INFO.value, {INFO}},
{WARNING.value, {WARNING}},
{FATAL.value, {FATAL}}

View File

@ -64,12 +64,13 @@
set_target_properties(${test} PROPERTIES COMPILE_FLAGS "-isystem -pthread ")
ENDIF( NOT(MSVC))
target_link_libraries(${test} g3logger gtest_170_lib)
add_test( ${test} ${test} )
ENDFOREACH(test)
#
# Test for Linux, runtime loading of dynamic libraries
#
IF (NOT WIN32 AND NOT ("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang"))
IF (NOT WIN32 AND NOT ("${CMAKE_CXX_COMPILER_ID}" MATCHES ".*Clang") AND G3_SHARED_LIB)
add_library(tester_sharedlib SHARED ${DIR_UNIT_TEST}/tester_sharedlib.h ${DIR_UNIT_TEST}/tester_sharedlib.cpp)
target_link_libraries(tester_sharedlib ${G3LOG_LIBRARY})

View File

@ -49,11 +49,11 @@ using namespace testing_helpers;
#ifdef G3_DYNAMIC_LOGGING
TEST(Initialization, No_Logger_Initialized___LevelsAreONByDefault) {
EXPECT_FALSE(g3::internal::isLoggingInitialized());
EXPECT_TRUE(g3::logLevel(DEBUG));
EXPECT_TRUE(g3::logLevel(G3LOG_DEBUG));
EXPECT_TRUE(g3::logLevel(INFO));
EXPECT_TRUE(g3::logLevel(WARNING));
EXPECT_TRUE(g3::logLevel(FATAL));
EXPECT_EQ(DEBUG.value, 100);
EXPECT_EQ(G3LOG_DEBUG.value, 100);
EXPECT_EQ(INFO.value, 300);
EXPECT_EQ(WARNING.value, 500);
EXPECT_EQ(FATAL.value, 1000);
@ -64,7 +64,7 @@ TEST(Initialization, No_Logger_Initialized___Expecting_LOG_calls_to_be_Still_OKi
EXPECT_FALSE(g3::internal::isLoggingInitialized());
EXPECT_TRUE(g3::logLevel(INFO));
EXPECT_TRUE(g3::logLevel(FATAL));
EXPECT_TRUE(g3::logLevel(DEBUG));
EXPECT_TRUE(g3::logLevel(G3LOG_DEBUG));
EXPECT_TRUE(g3::logLevel(WARNING));
std::string err_msg1 = "Hey. I am not instantiated but I still should not crash. (I am g3logger)";
std::string err_msg3_ignored = "This uninitialized message should be ignored";
@ -90,7 +90,7 @@ TEST(Initialization, No_Logger_Initialized___Expecting_LOG_calls_to_be_Still_OKi
EXPECT_FALSE(g3::internal::isLoggingInitialized());
EXPECT_TRUE(g3::logLevel(INFO));
EXPECT_TRUE(g3::logLevel(FATAL));
EXPECT_TRUE(g3::logLevel(DEBUG));
EXPECT_TRUE(g3::logLevel(G3LOG_DEBUG));
EXPECT_TRUE(g3::logLevel(WARNING));
std::string err_msg1 = "Hey. I am not instantiated but I still should not crash. (I am g3logger)";
std::string err_msg3_ignored = "This uninitialized message should be ignored";
@ -237,7 +237,7 @@ TEST(LogTest, LOG_F) {
std::cout << "logfilename: " << logger.logFile() << std::flush << std::endl;
LOGF(INFO, std::string(t_info + "%d").c_str(), 123);
LOGF(DEBUG, std::string(t_debug + "%f").c_str(), 1.123456);
LOGF(G3LOG_DEBUG, std::string(t_debug + "%f").c_str(), 1.123456);
LOGF(WARNING, std::string(t_warning + "%s").c_str(), "yello");
logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
@ -257,7 +257,7 @@ TEST(LogTest, LOG) {
{
RestoreFileLogger logger(log_directory);
LOG(INFO) << t_info << 123;
LOG(DEBUG) << t_debug << std::setprecision(7) << 1.123456f;
LOG(G3LOG_DEBUG) << t_debug << std::setprecision(7) << 1.123456f;
LOG(WARNING) << t_warning << "yello";
logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
@ -274,7 +274,7 @@ TEST(LogTest, LOG_F_IF) {
{
RestoreFileLogger logger(log_directory);
LOGF_IF(INFO, (2 == 2), std::string(t_info + "%d").c_str(), 123);
LOGF_IF(DEBUG, (2 != 2), std::string(t_debug + "%f").c_str(), 1.123456);
LOGF_IF(G3LOG_DEBUG, (2 != 2), std::string(t_debug + "%f").c_str(), 1.123456);
logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
SCOPED_TRACE("LOG_IF"); // Scope exit be prepared for destructor failure
@ -289,7 +289,7 @@ TEST(LogTest, LOG_IF) {
{
RestoreFileLogger logger(log_directory);
LOG_IF(INFO, (2 == 2)) << t_info << 123;
LOG_IF(DEBUG, (2 != 2)) << t_debug << std::setprecision(7) << 1.123456f;
LOG_IF(G3LOG_DEBUG, (2 != 2)) << t_debug << std::setprecision(7) << 1.123456f;
logger.reset(); // force flush of logger
file_content = readFileToText(logger.logFile());
SCOPED_TRACE("LOG_IF"); // Scope exit be prepared for destructor failure
@ -573,7 +573,7 @@ namespace {
};
~RestoreDynamicLoggingLevels() {
g3::only_change_at_initialization::reset();
g3::only_change_at_initialization::addLogLevel(DEBUG, false);
g3::only_change_at_initialization::addLogLevel(G3LOG_DEBUG, false);
g3::only_change_at_initialization::addLogLevel(INFO, false);
g3::only_change_at_initialization::addLogLevel(WARNING, false);
g3::only_change_at_initialization::addLogLevel(FATAL, false);
@ -637,30 +637,30 @@ TEST(CustomLogLevels, AddANonFatal__DidtAddItToEnabledValue) {
TEST(DynamicLogging, DynamicLogging_IS_ENABLED) {
RestoreDynamicLoggingLevels raiiLevelRestore;
ASSERT_TRUE(g3::logLevel(DEBUG));
ASSERT_TRUE(g3::logLevel(G3LOG_DEBUG));
ASSERT_TRUE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(DEBUG, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
g3::only_change_at_initialization::addLogLevel(G3LOG_DEBUG, false);
ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
ASSERT_TRUE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(INFO, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(WARNING, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_FALSE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
g3::only_change_at_initialization::addLogLevel(FATAL, false);
ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
ASSERT_FALSE(g3::logLevel(INFO));
ASSERT_FALSE(g3::logLevel(WARNING));
ASSERT_FALSE(g3::logLevel(FATAL)); // Yes FATAL can be turned off. Thereby rendering it ineffective.
@ -668,7 +668,7 @@ TEST(DynamicLogging, DynamicLogging_IS_ENABLED) {
TEST(DynamicLogging, DynamicLogging_No_Logs_If_Disabled) {
{
RestoreFileLogger logger(log_directory);
ASSERT_TRUE(g3::logLevel(DEBUG));
ASSERT_TRUE(g3::logLevel(G3LOG_DEBUG));
ASSERT_TRUE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL));
@ -681,16 +681,16 @@ TEST(DynamicLogging, DynamicLogging_No_Logs_If_Disabled) {
try {
{
RestoreFileLogger logger(log_directory);
LOGF(DEBUG, msg_debugOn.c_str(), "msg", "log");
LOGF(G3LOG_DEBUG, msg_debugOn.c_str(), "msg", "log");
auto content = logger.resetAndRetrieveContent();
ASSERT_TRUE(verifyContent(content, "This msg SHOULD appear in the log")) << "Content: [" << content << "]";
}
{
RestoreFileLogger logger(log_directory);
g3::only_change_at_initialization::addLogLevel(DEBUG, false);
EXPECT_FALSE(g3::logLevel(DEBUG));
LOG(DEBUG) << msg_debugOff;
g3::only_change_at_initialization::addLogLevel(G3LOG_DEBUG, false);
EXPECT_FALSE(g3::logLevel(G3LOG_DEBUG));
LOG(G3LOG_DEBUG) << msg_debugOff;
auto content = logger.resetAndRetrieveContent();
ASSERT_FALSE(verifyContent(content, "This message should never appear in the log")) << "Content: [" << content << "]";
}
@ -702,7 +702,7 @@ TEST(DynamicLogging, DynamicLogging_No_Logs_If_Disabled) {
TEST(DynamicLogging, DynamicLogging_No_Fatal_If_Disabled) {
RestoreFileLogger logger(log_directory);
RestoreDynamicLoggingLevels raiiLevelRestore;
ASSERT_TRUE(g3::logLevel(DEBUG));
ASSERT_TRUE(g3::logLevel(G3LOG_DEBUG));
ASSERT_TRUE(g3::logLevel(INFO));
ASSERT_TRUE(g3::logLevel(WARNING));
ASSERT_TRUE(g3::logLevel(FATAL));
@ -752,9 +752,9 @@ TEST(DynamicLogging, DynamicLogging_Check_WillAlsoBeTurnedOffWhen_Fatal_Is_Disab
#else
TEST(DynamicLogging, DynamicLogging_IS_NOT_ENABLED) {
ASSERT_TRUE(g3::logLevel(DEBUG));
//g3::addLogLevel(DEBUG, false); this line will not compile since G3_DYNAMIC_LOGGING is not enabled. Kept for show.
//ASSERT_FALSE(g3::logLevel(DEBUG));
ASSERT_TRUE(g3::logLevel(G3LOG_DEBUG));
//g3::addLogLevel(G3LOG_DEBUG, false); this line will not compile since G3_DYNAMIC_LOGGING is not enabled. Kept for show.
//ASSERT_FALSE(g3::logLevel(G3LOG_DEBUG));
}
#endif // Dynamic logging

View File

@ -12,6 +12,7 @@
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <g3log/generated_definitions.hpp>
namespace {
// https://www.epochconverter.com/
@ -189,21 +190,31 @@ TEST(Message, localtime_formatted) {
#endif // timezone
#if defined(CHANGE_G3LOG_DEBUG_TO_DBUG)
TEST(Level, G3LogDebug_is_DBUG) {
LOG(DBUG) << "DBUG equals G3LOG_DEBUG";
LOG(G3LOG_DEBUG) << "G3LOG_DEBUG equals DBUG";
}
#else
TEST(Level, G3LogDebug_is_DEBUG) {
LOG(DEBUG) << "DEBUG equals G3LOG_DEBUG";
LOG(G3LOG_DEBUG) << "G3LOG_DEBUG equals DEBUG";
}
#endif
#ifdef G3_DYNAMIC_LOGGING
namespace {
using LevelsContainer = std::map<int, g3::LoggingLevel>;
const LevelsContainer g_test_log_level_defaults = {
{g3::kDebugValue, {DEBUG}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG}},
{INFO.value, {INFO}},
{WARNING.value, {WARNING}},
{FATAL.value, {FATAL}}
};
const LevelsContainer g_test_all_disabled = {
{g3::kDebugValue, {DEBUG, false}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG,false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, false}},
{FATAL.value, {FATAL, false}}
@ -239,7 +250,7 @@ TEST(Level, DefaultChanged_only_change_at_initialization) {
EXPECT_FALSE(mapCompare(defaults, g_test_log_level_defaults));
const LevelsContainer defaultsWithInfoChangged = {
{g3::kDebugValue, {DEBUG, true}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG, true}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
@ -259,7 +270,7 @@ TEST(Level, DefaultChanged_log_levels) {
EXPECT_FALSE(mapCompare(defaults, g_test_log_level_defaults));
const LevelsContainer defaultsWithInfoChangged = {
{g3::kDebugValue, {DEBUG, true}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG, true}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
@ -316,7 +327,7 @@ TEST(Level, setHighestLogLevel_high_end) {
LevelsContainer expected = {
{g3::kDebugValue, {DEBUG, false}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG, false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, false}},
{FATAL.value, {FATAL, true}}
@ -335,11 +346,11 @@ TEST(Level, setHighestLogLevel_low_end) {
g3::log_levels::disableAll();
g3::log_levels::setHighest(DEBUG);
g3::log_levels::setHighest(G3LOG_DEBUG);
LevelsContainer expected = {
{g3::kDebugValue, {DEBUG, true}},
{G3LOG_DEBUG.value,{G3LOG_DEBUG, true}},
{INFO.value, {INFO, true}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
@ -362,7 +373,7 @@ TEST(Level, setHighestLogLevel_middle) {
LevelsContainer expected = {
{g3::kDebugValue, {DEBUG, false}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG, false}},
{INFO.value, {INFO, false}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}
@ -382,7 +393,7 @@ TEST(Level, setHighestLogLevel_StepWiseDisableAll) {
});
LevelsContainer changing_levels = {
{g3::kDebugValue, {DEBUG, true}},
{G3LOG_DEBUG.value, {G3LOG_DEBUG, true}},
{INFO.value, {INFO, true}},
{WARNING.value, {WARNING, true}},
{FATAL.value, {FATAL, true}}

View File

@ -68,7 +68,7 @@ TEST(ConceptSink, OneHundredSinks) {
// ignore the handle
worker->addSink(std2::make_unique<ScopedSetTrue>(flag, count), &ScopedSetTrue::ReceiveMsg);
}
LOG(DEBUG) << "start message";
LOG(G3LOG_DEBUG) << "start message";
LogMessagePtr message1{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
LogMessagePtr message2{std2::make_unique<LogMessage>("test", 0, "test", DEBUG)};
auto& write1 = message1.get()->write();
@ -183,7 +183,7 @@ TEST(ConceptSink, CannotCallSpawnTaskOnNullptrWorker) {
EXPECT_ANY_THROW(failed.get());
}
TEST(ConceptSink, AggressiveThreadCallsDuringShutdown) {
TEST(ConceptSink, DISABLED_AggressiveThreadCallsDuringShutdown) {
std::atomic<bool> keepRunning{true};
std::vector<std::thread> threads;

View File

@ -11,6 +11,12 @@
#include <g3log/logworker.hpp>
#include "tester_sharedlib.h"
#if (defined(CHANGE_G3LOG_DEBUG_TO_DBUG))
#define DEBUG_NAME DBUG
#else
#define DEBUG_NAME DEBUG
#endif
struct RuntimeLoadedLib : public SomeLibrary {
RuntimeLoadedLib() {
@ -19,7 +25,7 @@ struct RuntimeLoadedLib : public SomeLibrary {
}
~RuntimeLoadedLib() {
LOG(DEBUG) << "Library destroyed";
LOG(DEBUG_NAME) << "Library destroyed";
}
void action() {

View File

@ -123,7 +123,7 @@ namespace testing_helpers {
#ifdef G3_DYNAMIC_LOGGING
g3::only_change_at_initialization::addLogLevel(INFO, true);
g3::only_change_at_initialization::addLogLevel(DEBUG, true);
g3::only_change_at_initialization::addLogLevel(G3LOG_DEBUG, true);
g3::only_change_at_initialization::addLogLevel(WARNING, true);
g3::only_change_at_initialization::addLogLevel(FATAL, true);
#endif