#include <windows.h>
#include <iostream>
#include <fstream>
#include <boost/shared_ptr.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/core/core.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/expressions/formatter.hpp>
#include <boost/log/expressions/formatters/date_time.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/expressions/keyword.hpp>
#include <boost/log/trivial.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_channel_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_file_backend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/sinks/text_multifile_backend.hpp>
#include <boost/log/sinks/syslog_backend.hpp>
#include <boost/log/sinks/debug_output_backend.hpp>
#include <boost/log/sinks/event_log_backend.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/utility/value_ref.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/utility/empty_deleter.hpp>
#include <assert.h>
namespace logging = boost::log;
namespace expr = logging::expressions;
namespace keywords = logging::keywords;
enum sev_level{ normal, notification, warning, error, critical };
logging::sources::severity_logger< sev_level > slg;
enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
struct colorSet
{
HANDLE hCon;
Color col;
colorSet( HANDLE h, Color c): hCon( h )
, col( c )
{
}
};
std::basic_ostream< char > &operator << ( std::basic_ostream< char > &s, const colorSet &cs )
{
SetConsoleTextAttribute(cs.hCon, cs.col);
return s;
}
void init_console_logging()
{
boost::shared_ptr< logging::core > core = logging::core::get();
//Console sink
typedef logging::sinks::synchronous_sink< logging::sinks::text_ostream_backend > text_sink;
boost::shared_ptr< text_sink > consolebackend = boost::make_shared< text_sink >();
boost::shared_ptr< std::ostream > consolestrmr = boost::shared_ptr< std::ostream >( &std::cout, logging::empty_deleter() );
consolebackend->locked_backend()->add_stream( consolestrmr );
HANDLE h = GetStdHandle( STD_ERROR_HANDLE );
logging::formatter fmtconsole = expr::stream << colorSet( h, BLUE )<< "DEBUG:" << expr::smessage << colorSet( h, GRAY ) << "\n";
consolebackend->set_formatter( fmtconsole );
core->add_sink( consolebackend );
}
/**
*
*/
void logging_function()
{
init_console_logging();
logging::add_common_attributes();
HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
std::cout << colorSet( h, DARKTEAL )<<" I am coloured!!" << colorSet( h, GRAY ) << std::endl;
BOOST_LOG_SEV( slg, normal ) << "A regular message";
BOOST_LOG_SEV( slg, warning ) << "Something bad is going on but I can handle it";
BOOST_LOG_SEV( slg, critical ) << "Everything crumbles, shoot me now!";
BOOST_LOG_TRIVIAL( trace ) << "Trace message";
BOOST_LOG_TRIVIAL( fatal ) << "A fatal message";
}
int main( int argc, const char* argv )
{
logging_function();
}