From 8d9f1bfea2ea1b76d2b3b3a21f51e686b6b62641 Mon Sep 17 00:00:00 2001 From: KjellKod Date: Thu, 19 Dec 2013 22:51:47 -0700 Subject: [PATCH] split FATAL examples into a SIGSEGV and a CONTRACT examplew --- g2log/CMakeLists.txt | 18 ++++-- g2log/test_example/main_contract.cpp | 59 +++++++++++++++++ g2log/test_example/main_sigsegv.cpp | 95 ++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 7 deletions(-) create mode 100644 g2log/test_example/main_contract.cpp create mode 100644 g2log/test_example/main_sigsegv.cpp diff --git a/g2log/CMakeLists.txt b/g2log/CMakeLists.txt index 91ad698..8c8b6a7 100644 --- a/g2log/CMakeLists.txt +++ b/g2log/CMakeLists.txt @@ -27,7 +27,7 @@ # 3. cmake -DCMAKE_BUILD_TYPE=Release -G "Visual Studio 11" .. # the "Visual Studio 11" .. does not require just::thread! # 4. msbuild g2log_by_kjellkod.sln /p:Configuration=Release -# 5. Release\g2log-FATAL-example.exe +# 5. Release\g2log-FATAL-contract.exe # # # >>. LINUX:To try this out from folder g2log: @@ -132,7 +132,7 @@ ENDIF(MSVC) option (USE_DYNAMIC_LOGGING_LEVELS "Turn ON/OFF log levels. An disabled level will not push logs of that level to the sink. By default dynamic logging is disabled" OFF) - # Do 'cmake -DUSE_DYNAMIC_LOGGING_LEVELS=ON' to enable this + # Do 'cmake -DUSE_DYNAMIC_LOGGING_LEVELS=ON -DCMAKE_BUILD_TYPE=Release .. ' to enable this # (or change the default option above) if(USE_DYNAMIC_LOGGING_LEVELS) add_definitions(-DG2_DYNAMIC_LOGGING) @@ -151,10 +151,12 @@ ENDIF(MSVC) # before it can be "cmake'd" and compiled --- leaving it as OFF for now # ============================================================================ - # 1. a simple test example 'g2log-FATAL-example' - option (USE_SIMPLE_EXAMPLE "Simple (fatal-crash) example " ON) + # 1. a simple test example 'g2log-FATAL-*' + # Do 'cmake -DUSE_SIMPLE_EXAMPLE=ON' to enable this + option (USE_SIMPLE_EXAMPLE "Simple (fatal-crash/contract) examples " ON) # 2. performance test (average + worst case) for KjellKod's g2log + # Do 'cmake -DUSE_G2LOG_PERFORMANCE=ON' to enable this option (USE_G2LOG_PERFORMANCE "g2log performance test" OFF) # 3. performance test for Google's glog @@ -177,10 +179,12 @@ ENDIF(MSVC) # 1. create the the example EXECUTABLE - hook in the test_example's CMakeLists.txt file # ========================= if (USE_SIMPLE_EXAMPLE) - MESSAGE(" g2log-FATAL-example option ON") + MESSAGE(" g2log-FATAL-* examples option ON") include_directories (${DIR_EXAMPLE}) - add_executable(g2log-FATAL-example ${DIR_EXAMPLE}/main.cpp) - target_link_libraries(g2log-FATAL-example lib_g3logger ${PLATFORM_LINK_LIBRIES}) + add_executable(g2log-FATAL-contract ${DIR_EXAMPLE}/main_contract.cpp) + add_executable(g2log-FATAL-sigsegv ${DIR_EXAMPLE}/main_sigsegv.cpp) + target_link_libraries(g2log-FATAL-contract lib_g3logger ${PLATFORM_LINK_LIBRIES}) + target_link_libraries(g2log-FATAL-sigsegv lib_g3logger ${PLATFORM_LINK_LIBRIES}) endif (USE_SIMPLE_EXAMPLE) diff --git a/g2log/test_example/main_contract.cpp b/g2log/test_example/main_contract.cpp new file mode 100644 index 0000000..46da868 --- /dev/null +++ b/g2log/test_example/main_contract.cpp @@ -0,0 +1,59 @@ +/** ========================================================================== +* 2011 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. +* ============================================================================*/ + +#include "g2logworker.hpp" +#include "g2log.hpp" +#include +#include +#include +namespace +{ +#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) +const std::string path_to_log_file = "./"; +#else +const std::string path_to_log_file = "/tmp/"; +#endif +} + +namespace example_fatal +{ + void killWithContractIfNonEqual(int first, int second) + { + CHECK(first == second) << "Test to see if contract works: onetwothree: " << 123 << ". This should be at the end of the log, and will exit this example"; + } +} // example fatal + +int main(int argc, char** argv) +{ + double pi_d = 3.1415926535897932384626433832795; + float pi_f = 3.1415926535897932384626433832795f; + + auto logger_n_handle = g2::LogWorker::createWithDefaultLogger(argv[0], path_to_log_file); + g2::initializeLogging(logger_n_handle.worker.get()); + std::future log_file_name = logger_n_handle.sink->call(&g2::FileSink::fileName); + std::cout << "* This is an example of g2log. It WILL exit by a failed CHECK(...)" << std::endl; + std::cout << "* that acts as a FATAL trigger. Please see the generated log and " << std::endl; + std::cout << "* compare to the code at:\n* \t g2log/test_example/main_contract.cpp" << std::endl; + std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n" << std::endl; + + LOGF(INFO, "Hi log %d", 123); + LOG(INFO) << "Test SLOG INFO"; + LOG(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; + LOGF(INFO, "pi float printf:%f", pi_f); + + // FATAL SECTION + int smaller = 1; + int larger = 2; + example_fatal::killWithContractIfNonEqual(smaller, larger); +} + diff --git a/g2log/test_example/main_sigsegv.cpp b/g2log/test_example/main_sigsegv.cpp new file mode 100644 index 0000000..bcd33c8 --- /dev/null +++ b/g2log/test_example/main_sigsegv.cpp @@ -0,0 +1,95 @@ +/** ========================================================================== +* 2011 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. +* ============================================================================*/ + +#include "g2logworker.hpp" +#include "g2log.hpp" +#include +#include +#include +namespace +{ +#if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__)) +const std::string path_to_log_file = "./"; +#else +const std::string path_to_log_file = "/tmp/"; +#endif +} + +namespace example_fatal +{ + // on Ubunti this caused get a compiler warning with gcc4.6 + // from gcc 4.7.2 (at least) it causes a crash (as expected) + // On windows it'll probably crash too. + void tryToKillWithIllegalPrintout() + { + std::cout << "\n\n***** Be ready this last example may 'abort' if on Windows/Linux_gcc4.7 " << std::endl << std::flush; + 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()); + } + + + // The function above 'tryToKillWithIllegalPrintout' IS system / compiler dependent. Older compilers sometimes did NOT generate a SIGSEGV + // fault as expected by the illegal printf-format usage. just in case we exit by zero division" + void killByZeroDivision(int value) + { + int zero = 0; // trying to fool the compiler to automatically warn + LOG(INFO) << "This is a bad operation [value/zero] : " << value/zero; + } +} // example fatal + + + +int main(int argc, char** argv) +{ + double pi_d = 3.1415926535897932384626433832795; + float pi_f = 3.1415926535897932384626433832795f; + + + auto logger_n_handle = g2::LogWorker::createWithDefaultLogger(argv[0], path_to_log_file); + g2::initializeLogging(logger_n_handle.worker.get()); + std::future log_file_name = logger_n_handle.sink->call(&g2::FileSink::fileName); + std::cout << "* This is an example of g2log. It WILL exit by a FATAL trigger" << std::endl; + std::cout << "* Please see the generated log and compare to the code at" << std::endl; + std::cout << "* g2log/test_example/main.cpp" << std::endl; + std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n" << std::endl; + + + LOGF(INFO, "Hi log %d", 123); + LOG(INFO) << "Test SLOG INFO"; + LOG(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; + LOGF(INFO, "pi float printf:%f", pi_f); + + // + // START: LOG Entris that were in the CodeProject article + // + //LOG(UNKNOWN_LEVEL) << "This log attempt will cause a compiler error"; + + LOG(INFO) << "Simple to use with streaming syntax, easy as abc or " << 123; + LOGF(WARNING, "Printf-style syntax is also %s", "available"); + 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"); + + // 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) + // On windows itll probably crash + //example_fatal::tryToKillWithIllegalPrintout(); + + int value = 1; // system dependent but it SHOULD never reach this line + example_fatal::killByZeroDivision(value); + return 0; +}