g3log/example/main_contract.cpp
Andreas Schönle 123977f106 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
2017-05-09 10:26:48 -06:00

65 lines
2.4 KiB
C++

/** ==========================================================================
* 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.
*
* For more information see g3log/LICENSE or refer refer to http://unlicense.org
* ============================================================================*/
#include <g3log/g3log.hpp>
#include <g3log/logworker.hpp>
#include <iomanip>
#include <thread>
#include <iostream>
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 worker = g3::LogWorker::createLogWorker();
auto handle= worker->addDefaultLogger(argv[0], path_to_log_file);
g3::initializeLogging(worker.get());
std::future<std::string> log_file_name = handle->call(&g3::FileSink::fileName);
std::cout << "* This is an example of g3log. 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 g3log/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(G3LOG_DEBUG) << "Test SLOG DEBUG";
LOG(INFO) << "one: " << 1;
LOG(INFO) << "two: " << 2;
LOG(INFO) << "one and two: " << 1 << " and " << 2;
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
int smaller = 1;
int larger = 2;
example_fatal::killWithContractIfNonEqual(smaller, larger);
}