fork(1) download
  1. #include <windows.h>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. #include <boost/shared_ptr.hpp>
  6. #include <boost/date_time/posix_time/posix_time.hpp>
  7. #include <boost/log/core/core.hpp>
  8. #include <boost/log/sources/severity_feature.hpp>
  9. #include <boost/log/sources/severity_logger.hpp>
  10. #include <boost/log/utility/setup/file.hpp>
  11. #include <boost/log/expressions.hpp>
  12. #include <boost/log/expressions/formatter.hpp>
  13. #include <boost/log/expressions/formatters/date_time.hpp>
  14. #include <boost/log/support/date_time.hpp>
  15. #include <boost/log/expressions/keyword.hpp>
  16. #include <boost/log/trivial.hpp>
  17.  
  18. #include <boost/log/attributes.hpp>
  19. #include <boost/log/sources/basic_logger.hpp>
  20. #include <boost/log/sources/severity_logger.hpp>
  21. #include <boost/log/sources/severity_channel_logger.hpp>
  22. #include <boost/log/sources/record_ostream.hpp>
  23.  
  24. #include <boost/log/sinks/sync_frontend.hpp>
  25. #include <boost/log/sinks/text_file_backend.hpp>
  26. #include <boost/log/sinks/text_ostream_backend.hpp>
  27. #include <boost/log/sinks/text_multifile_backend.hpp>
  28. #include <boost/log/sinks/syslog_backend.hpp>
  29. #include <boost/log/sinks/debug_output_backend.hpp>
  30. #include <boost/log/sinks/event_log_backend.hpp>
  31.  
  32. #include <boost/log/attributes/scoped_attribute.hpp>
  33. #include <boost/log/utility/value_ref.hpp>
  34. #include <boost/log/utility/setup/common_attributes.hpp>
  35. #include <boost/log/utility/empty_deleter.hpp>
  36. #include <assert.h>
  37.  
  38. namespace logging = boost::log;
  39. namespace expr = logging::expressions;
  40. namespace keywords = logging::keywords;
  41.  
  42. enum sev_level{ normal, notification, warning, error, critical };
  43.  
  44. logging::sources::severity_logger< sev_level > slg;
  45.  
  46. enum Color { DARKBLUE = 1, DARKGREEN, DARKTEAL, DARKRED, DARKPINK, DARKYELLOW, GRAY, DARKGRAY, BLUE, GREEN, TEAL, RED, PINK, YELLOW, WHITE };
  47.  
  48. struct colorSet
  49. {
  50. HANDLE hCon;
  51. Color col;
  52.  
  53. colorSet( HANDLE h, Color c): hCon( h )
  54. , col( c )
  55. {
  56.  
  57. }
  58. };
  59.  
  60. std::basic_ostream< char > &operator << ( std::basic_ostream< char > &s, const colorSet &cs )
  61. {
  62. SetConsoleTextAttribute(cs.hCon, cs.col);
  63. return s;
  64. }
  65.  
  66. void init_console_logging()
  67. {
  68. boost::shared_ptr< logging::core > core = logging::core::get();
  69.  
  70. //Console sink
  71. typedef logging::sinks::synchronous_sink< logging::sinks::text_ostream_backend > text_sink;
  72. boost::shared_ptr< text_sink > consolebackend = boost::make_shared< text_sink >();
  73. boost::shared_ptr< std::ostream > consolestrmr = boost::shared_ptr< std::ostream >( &std::cout, logging::empty_deleter() );
  74. consolebackend->locked_backend()->add_stream( consolestrmr );
  75.  
  76. HANDLE h = GetStdHandle( STD_ERROR_HANDLE );
  77. logging::formatter fmtconsole = expr::stream << colorSet( h, BLUE )<< "DEBUG:" << expr::smessage << colorSet( h, GRAY ) << "\n";
  78.  
  79. consolebackend->set_formatter( fmtconsole );
  80.  
  81. core->add_sink( consolebackend );
  82.  
  83. }
  84.  
  85. /**
  86.  *
  87.  */
  88.  
  89. void logging_function()
  90. {
  91. init_console_logging();
  92.  
  93. logging::add_common_attributes();
  94.  
  95. HANDLE h = GetStdHandle( STD_OUTPUT_HANDLE );
  96. std::cout << colorSet( h, DARKTEAL )<<" I am coloured!!" << colorSet( h, GRAY ) << std::endl;
  97.  
  98. BOOST_LOG_SEV( slg, normal ) << "A regular message";
  99. BOOST_LOG_SEV( slg, warning ) << "Something bad is going on but I can handle it";
  100. BOOST_LOG_SEV( slg, critical ) << "Everything crumbles, shoot me now!";
  101.  
  102. BOOST_LOG_TRIVIAL( trace ) << "Trace message";
  103. BOOST_LOG_TRIVIAL( fatal ) << "A fatal message";
  104.  
  105. }
  106.  
  107. int main( int argc, const char* argv )
  108. {
  109. logging_function();
  110. }
  111.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:1:21: fatal error: windows.h: No such file or directory
 #include <windows.h>
                     ^
compilation terminated.
stdout
Standard output is empty