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-07-16 09:55:23 +02: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>
|
2013-12-20 06:51:47 +01:00
|
|
|
#include <iomanip>
|
|
|
|
#include <thread>
|
|
|
|
#include <iostream>
|
2015-07-16 09:55:23 +02:00
|
|
|
|
|
|
|
|
2013-12-20 06:51:47 +01:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
#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
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace example_fatal
|
|
|
|
{
|
2015-07-16 09:55:23 +02:00
|
|
|
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";
|
|
|
|
}
|
2013-12-20 06:51:47 +01:00
|
|
|
} // example fatal
|
|
|
|
|
2015-07-16 09:55:23 +02:00
|
|
|
int main(int argc, char **argv)
|
2013-12-20 06:51:47 +01:00
|
|
|
{
|
2015-07-16 09:55:23 +02:00
|
|
|
double pi_d = 3.1415926535897932384626433832795;
|
|
|
|
float pi_f = 3.1415926535897932384626433832795f;
|
|
|
|
|
2015-08-19 18:08:41 +02:00
|
|
|
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);
|
2018-03-08 17:16:12 +01:00
|
|
|
|
|
|
|
// Exmple of overriding the default formatting of log entry
|
|
|
|
auto changeFormatting = handle->call(&g3::FileSink::overrideLogDetails, g3::LogMessage::FullLogDetailsToString);
|
|
|
|
const std::string newHeader = "\t\tLOG format: [YYYY/MM/DD hh:mm:ss uuu* LEVEL THREAD_ID FILE->FUNCTION:LINE] message\n\t\t(uuu*: microseconds fractions of the seconds value)\n\n";
|
|
|
|
// example of ovrriding the default formatting of header
|
|
|
|
auto changeHeader = handle->call(&g3::FileSink::overrideLogHeader, newHeader);
|
|
|
|
|
|
|
|
changeFormatting.wait();
|
|
|
|
changeHeader.wait();
|
|
|
|
|
|
|
|
|
2015-07-20 07:10:56 +02:00
|
|
|
std::cout << "* This is an example of g3log. It WILL exit by a failed CHECK(...)" << std::endl;
|
2015-07-16 09:55:23 +02:00
|
|
|
std::cout << "* that acts as a FATAL trigger. Please see the generated log and " << std::endl;
|
2015-07-20 07:10:56 +02:00
|
|
|
std::cout << "* compare to the code at:\n* \t g3log/test_example/main_contract.cpp" << std::endl;
|
2015-07-16 09:55:23 +02:00
|
|
|
std::cout << "*\n* Log file: [" << log_file_name.get() << "]\n\n" << std::endl;
|
|
|
|
|
|
|
|
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-07-16 09:55:23 +02: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-07-16 09:55:23 +02:00
|
|
|
LOGF(INFO, "pi float printf:%f", pi_f);
|
2013-12-20 06:51:47 +01:00
|
|
|
|
|
|
|
// FATAL SECTION
|
2015-07-16 09:55:23 +02:00
|
|
|
int smaller = 1;
|
|
|
|
int larger = 2;
|
|
|
|
example_fatal::killWithContractIfNonEqual(smaller, larger);
|
2013-12-20 06:51:47 +01:00
|
|
|
}
|
|
|
|
|