2013-12-20 06:51:47 +01:00
/** ==========================================================================
* 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 .
2015-02-02 08:31:43 +01:00
*
2014-07-03 23:42:19 +02:00
* For more information see g3log / LICENSE or refer refer to http : //unlicense.org
2013-12-20 06:51:47 +01:00
* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
2015-07-20 07:10:56 +02:00
# include <g3log/g3log.hpp>
# include <g3log/logworker.hpp>
2015-07-16 09:55:23 +02:00
2013-12-20 06:51:47 +01:00
# include <iomanip>
# include <iostream>
2014-02-03 07:49:24 +01:00
# include <memory>
2023-12-01 00:17:45 +01:00
# include <thread>
namespace {
2013-12-20 06:51:47 +01:00
# if (defined(WIN32) || defined(_WIN32) || defined(__WIN32__))
2015-07-16 09:55:23 +02:00
const std : : string path_to_log_file = " ./ " ;
2013-12-20 06:51:47 +01:00
# else
2015-07-16 09:55:23 +02:00
const std : : string path_to_log_file = " /tmp/ " ;
2013-12-20 06:51:47 +01:00
# endif
2023-12-01 00:17:45 +01:00
} // namespace
2013-12-20 06:51:47 +01:00
2023-12-01 00:17:45 +01:00
namespace example_fatal {
2015-07-16 09:55:23 +02:00
// 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.
2023-12-01 00:17:45 +01:00
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 ;
2015-07-16 09:55:23 +02:00
std : : this_thread : : sleep_for ( std : : chrono : : seconds ( 1 ) ) ;
const std : : string logging = " logging " ;
2017-05-09 18:26:48 +02:00
LOGF ( G3LOG_DEBUG , " ILLEGAL PRINTF_SYNTAX EXAMPLE. WILL GENERATE compiler warning. \n \n badly formatted message:[Printf-type %s is the number 1 for many %s] " , logging . c_str ( ) ) ;
2015-07-16 09:55:23 +02:00
}
2024-05-23 22:18:22 +02:00
// The function above 'tryToKillWithIllegalPrintout' IS system / compiler dependent. Older compilers sometimes did NOT generate a segmentation
2015-07-16 09:55:23 +02:00
// fault as expected by the illegal printf-format usage. just in case we exit by zero division"
2023-12-01 00:17:45 +01:00
void killByZeroDivision ( int value ) {
int zero = 0 ; // trying to fool the compiler to automatically warn
2015-07-16 09:55:23 +02:00
LOG ( INFO ) < < " This is a bad operation [value/zero] : " < < value / zero ;
}
2015-11-23 00:35:36 +01:00
void tryToKillWithAccessingIllegalPointer ( std : : unique_ptr < std : : string > badStringPtr ) {
auto badPtr = std : : move ( badStringPtr ) ;
2024-05-23 22:18:22 +02:00
LOG ( INFO ) < < " Function calls through a nullptr object will trigger segmentation fault " ;
2015-11-23 00:35:36 +01:00
badStringPtr - > append ( " crashing " ) ;
}
2023-12-01 00:17:45 +01:00
} // namespace example_fatal
2015-11-23 00:35:36 +01:00
2023-12-01 00:17:45 +01:00
int main ( int argc , char * * argv ) {
2015-02-02 08:31:43 +01:00
double pi_d = 3.1415926535897932384626433832795 ;
float pi_f = 3.1415926535897932384626433832795f ;
2015-07-20 07:10:56 +02:00
using namespace g3 ;
2015-02-02 08:31:43 +01:00
2023-12-01 00:17:45 +01:00
std : : unique_ptr < LogWorker > logworker { LogWorker : : createLogWorker ( ) } ;
2018-02-21 06:02:19 +01:00
auto sinkHandle = logworker - > addSink ( std : : make_unique < FileSink > ( argv [ 0 ] , path_to_log_file ) ,
2015-02-02 08:31:43 +01:00
& FileSink : : fileWrite ) ;
initializeLogging ( logworker . get ( ) ) ;
std : : future < std : : string > log_file_name = sinkHandle - > call ( & FileSink : : fileName ) ;
2015-07-20 07:10:56 +02:00
std : : cout < < " * This is an example of g3log. It WILL exit by a FATAL trigger " < < std : : endl ;
2015-02-02 08:31:43 +01:00
std : : cout < < " * Please see the generated log and compare to the code at " < < std : : endl ;
2015-07-20 07:10:56 +02:00
std : : cout < < " * g3log/test_example/main.cpp " < < std : : endl ;
2023-12-01 00:17:45 +01:00
std : : cout < < " * \n * Log file: [ " < < log_file_name . get ( ) < < " ] \n \n "
< < std : : endl ;
2015-02-02 08:31:43 +01:00
LOGF ( INFO , " Hi log %d " , 123 ) ;
LOG ( INFO ) < < " Test SLOG INFO " ;
2017-05-09 18:26:48 +02:00
LOG ( G3LOG_DEBUG ) < < " Test SLOG DEBUG " ;
2015-02-02 08:31:43 +01:00
LOG ( INFO ) < < " one: " < < 1 ;
LOG ( INFO ) < < " two: " < < 2 ;
LOG ( INFO ) < < " one and two: " < < 1 < < " and " < < 2 ;
2017-05-09 18:26:48 +02:00
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 ;
2015-02-02 08:31:43 +01:00
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 " ;
2017-05-09 18:26:48 +02:00
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 " ) ;
2015-02-02 08:31:43 +01:00
// 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
2015-11-23 00:35:36 +01:00
example_fatal : : tryToKillWithIllegalPrintout ( ) ;
// try 2
std : : unique_ptr < std : : string > badStringPtr ;
example_fatal : : tryToKillWithAccessingIllegalPointer ( std : : move ( badStringPtr ) ) ;
2015-02-02 08:31:43 +01:00
2023-12-01 00:17:45 +01:00
// what happened? OK. let us just exit with SIGFPE
int value = 1 ; // system dependent but it SHOULD never reach this line
2015-02-02 08:31:43 +01:00
example_fatal : : killByZeroDivision ( value ) ;
return 0 ;
2013-12-20 06:51:47 +01:00
}