Changeset 236 in pacpusframework for trunk/src/PacpusLib
- Timestamp:
- Nov 28, 2013, 5:07:16 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/PacpusLib/Log.cpp
r231 r236 11 11 #include <boost/log/detail/date_time_format_parser.hpp> 12 12 #include <boost/log/expressions.hpp> 13 #include <boost/log/expressions/formatters/if.hpp> 13 14 #include <boost/log/sinks/text_file_backend.hpp> 14 15 #include <boost/log/sinks/text_ostream_backend.hpp> 15 16 #include <boost/log/sources/severity_logger.hpp> 16 #include <boost/log/sources/record_ostream.hpp>17 17 #include <boost/log/support/date_time.hpp> 18 18 #include <boost/log/utility/setup/common_attributes.hpp> 19 19 #include <boost/log/utility/setup/file.hpp> 20 20 #include <boost/log/utility/setup/formatter_parser.hpp> 21 #include <boost/utility/empty_deleter.hpp> 21 #if BOOST_VERSION >= 105500 // header exists from 1.55 22 # include <boost/utility/empty_deleter.hpp> 23 #else 24 # include <boost/log/utility/empty_deleter.hpp> 25 #endif 22 26 #include <ostream> 23 27 #include <QString> 28 29 // could use Boost.Predef with Boost >= 1.55 30 #if defined(WIN32) || defined(_WINDOWS) 31 # define PACPUS_OS_WINDOWS 1 32 #elif defined(__unix) || defined(__unix__) 33 # define PACPUS_OS_UNIX 1 34 # if defined(__linux) || defined(__linux__) 35 # define PACPUS_OS_LINUX 1 36 # endif 37 #elif defined(__APPLE__) || defined(__MACH__) || defined(Macintosh) || defined(macintosh) 38 # define PACPUS_OS_MACOS 1 39 #else 40 // unknown system 41 #endif 42 43 #if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_WINDOWS 44 # include <Windows.h> 45 #endif 24 46 25 47 template< typename CharT, typename TraitsT > … … 36 58 namespace pacpus 37 59 { 38 60 61 BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel) 62 63 enum Color { 64 COLOR_DEFAULT, 65 COLOR_BLACK, 66 COLOR_RED, 67 COLOR_GREEN, 68 COLOR_YELLOW, 69 COLOR_BLUE, 70 COLOR_MAGENTA, 71 COLOR_CYAN, 72 COLOR_WHITE 73 }; 74 75 //Color getColor(boost::log::trivial::severity_type const& sev) 76 //{ 77 // using namespace boost::log::trivial; 78 79 // if (sev >= error) { 80 // return COLOR_RED; 81 // } else if (sev == warning) { 82 // return COLOR_YELLOW; 83 // } else if (sev == info) { 84 // return COLOR_GREEN; 85 // } else { 86 // return COLOR_DEFAULT; 87 // } 88 //} 89 90 #if PACPUS_OS_WINDOWS //&& !PACPUS_OS_WINDOWS_MOBILE 91 92 // Returns the character attribute for the given color. 93 WORD getColorAttribute(Color color) 94 { 95 switch (color) { 96 case COLOR_BLACK: return 0; 97 case COLOR_RED: return FOREGROUND_RED; 98 case COLOR_GREEN: return FOREGROUND_GREEN; 99 case COLOR_YELLOW: return FOREGROUND_RED | FOREGROUND_GREEN; 100 case COLOR_BLUE: return FOREGROUND_BLUE; 101 case COLOR_MAGENTA: return FOREGROUND_RED | FOREGROUND_BLUE; 102 case COLOR_CYAN: return FOREGROUND_GREEN | FOREGROUND_BLUE; 103 case COLOR_WHITE: return FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE; 104 default: return 0; 105 } 106 } 107 108 #else 109 110 /// @returns the ANSI color code for the given color. COLOR_DEFAULT is 111 /// an invalid input. 112 const char* getAnsiColorCode(Color color) 113 { 114 const char* kEscapeSequence = "\033["; 115 // const char* kBackground = ""; 116 const char* kFontRegular = "0;"; 117 // const char* kFontBold = "1;"; 118 // const char* kFontWeak = "2;"; 119 // const char* kFontStrong = "3;"; 120 // const char* kFontUnderline = "4;"; 121 122 std::stringstream ss; 123 ss << kEscapeSequence << kFontRegular; 124 125 switch (color) { 126 case COLOR_BLACK: ss << "30"; break; 127 case COLOR_RED: ss << "31"; break; 128 case COLOR_GREEN: ss << "32"; break; 129 case COLOR_YELLOW: ss << "33"; break; 130 case COLOR_BLUE: ss << "34"; break; 131 case COLOR_MAGENTA: ss << "35"; break; 132 case COLOR_CYAN: ss << "36"; break; 133 case COLOR_WHITE: ss << "37"; break; 134 default: return ""; 135 }; 136 137 const char* kPostfix = "m"; 138 ss << kPostfix; 139 140 return ss.str().c_str(); 141 } 142 143 const char* getAnsiColorCodeRestoreDefault() 144 { 145 return "\033[0m"; 146 } 147 148 #endif // PACPUS_OS_WINDOWS && !PACPUS_OS_WINDOWS_MOBILE 149 39 150 void init_log_factories() 40 151 { … … 78 189 "ThreadID", 79 190 attrs::current_thread_id()); 191 //logging::core::get()->add_global_attribute( 192 // "Scope", 193 // attrs::named_scope()); 80 194 81 195 logging::core::get()->set_filter … … 83 197 #ifdef NDEBUG 84 198 // release 85 logging::trivial::severity >= logging::trivial::debug199 severity >= debug 86 200 #else 87 201 // debug 88 logging::trivial::severity >= logging::trivial::trace 89 #endif 90 ); 91 92 // Add a file log 202 severity >= trace 203 #endif 204 ); 205 206 //////////////////////////////////////////////////////////////////////////////// 207 // FILE 93 208 logging::add_file_log 94 209 ( … … 103 218 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]" 104 219 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]" 105 << " <" << logging::trivial::severity << ">" 220 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">" 221 << " <" << severity << ">" 106 222 << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID") 107 223 << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">" … … 109 225 ) 110 226 ); 227 228 //////////////////////////////////////////////////////////////////////////////// 229 // CONSOLE 230 #if PACPUS_LOG_COLORED_OUTPUT 231 Color color = COLOR_GREEN;// = getColor(expr::attr<logging::trivial::severity_level>(logging::trivial::severity.get_name())); 232 #endif 233 234 #if BOOST_VERSION >= 105500 235 #else 236 using logging::empty_deleter; 237 #endif 111 238 112 239 // Create a backend and attach a couple of streams to it 113 shared_ptr< sinks::text_ostream_backend > backend = make_shared< sinks::text_ostream_backend >(); 240 boost::shared_ptr< sinks::text_ostream_backend > backend = 241 make_shared< sinks::text_ostream_backend >(); 114 242 backend->add_stream( 115 243 shared_ptr< std::ostream >(&std::clog, empty_deleter()) … … 125 253 sink->set_filter 126 254 ( 127 logging::trivial::severity >= logging::trivial::info255 severity >= info 128 256 ); 129 257 … … 131 259 ( 132 260 expr::stream 261 #if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX 262 // << getAnsiColorCode(color) 263 #endif 133 264 << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID") 134 265 //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] " 135 266 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "] " 136 << "<" << logging::trivial::severity << ">" 267 //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">" 268 << "<" << severity << ">" 137 269 << " " 138 270 << expr::smessage 271 #if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX 272 << getAnsiColorCodeRestoreDefault() // Resets the terminal to default. 273 #endif 139 274 ); 140 275
Note:
See TracChangeset
for help on using the changeset viewer.