Changeset 239 in pacpusframework for trunk/src


Ignore:
Timestamp:
Nov 29, 2013, 6:39:11 PM (11 years ago)
Author:
Marek Kurdej
Message:

Added: possibility to use color log output (needs more testing).

Location:
trunk/src/PacpusLib
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PacpusLib/CMakeLists.txt

    r185 r239  
    3434set(PROJECT_HDRS
    3535    ${EXPORT_HDR}
     36
     37    ColorSeverityFormatter.hpp
    3638
    3739    ${PACPUS_INCLUDE_DIR}/Pacpus/kernel/cstdint.h
  • trunk/src/PacpusLib/Log.cpp

    r236 r239  
    77#include <Pacpus/kernel/Log.h>
    88
    9 #ifdef PACPUS_USE_LOG
     9#if defined(PACPUS_USE_LOG)
     10
     11#if defined(PACPUS_LOG_COLORED_OUTPUT)
     12#   include "ColorSeverityFormatter.hpp"
     13#endif
    1014
    1115#include <boost/log/detail/date_time_format_parser.hpp>
     
    2731#include <QString>
    2832
    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
    46 
    47 template< typename CharT, typename TraitsT >
    48 std::basic_ostream< CharT, TraitsT >& operator<< (std::basic_ostream< CharT, TraitsT >& strm, QString const& s)
     33// specialization for char
     34template <>
     35PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s)
    4936{
    5037    strm << s.toStdString();
     
    5239}
    5340
    54 // explicit instantiation
    55 template
    56 PACPUSLIB_API std::basic_ostream<char>& operator<< (std::basic_ostream<char>& strm, QString const& s);
     41// specialization for wchar_t
     42template <>
     43PACPUSLIB_API std::basic_ostream<wchar_t>& operator<< (std::basic_ostream<wchar_t>& strm, QString const& s)
     44{
     45    strm << s.toStdWString();
     46    return strm;
     47}
    5748
    5849namespace pacpus
     
    6051
    6152BOOST_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
    14953
    15054void init_log_factories()
     
    217121                << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
    218122                //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
    219                 << " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
     123                << " [" << boost::log::expressions::format_date_time< posix_time::ptime >("TimeStamp", "%Y-%m-%d %T.%f") << "]"
    220124                //<< " [" << std::setw(20) << expr::attr<std::string>("Scope") << ">"
    221125                << " <" << severity << ">"
     
    228132    ////////////////////////////////////////////////////////////////////////////////
    229133    // 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
     134    // Create a backend and attach a couple of streams to it
     135#if ! (BOOST_VERSION >= 105500)
    236136    using logging::empty_deleter;
    237137#endif
    238 
    239     // Create a backend and attach a couple of streams to it
    240138    boost::shared_ptr< sinks::text_ostream_backend > backend =
    241139        make_shared< sinks::text_ostream_backend >();
     
    259157    (
    260158        expr::stream
    261 #if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX
    262 //            << getAnsiColorCode(color)
     159#if defined(PACPUS_LOG_COLORED_OUTPUT)
     160            << formatSeverityWithColors< SeverityLevel >("Severity")
    263161#endif
    264162            << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
     
    269167            << " "
    270168            << expr::smessage
    271 #if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX
    272             << getAnsiColorCodeRestoreDefault() // Resets the terminal to default.
     169#if defined(PACPUS_LOG_COLORED_OUTPUT)
     170            << formatSeverityWithColors< SeverityLevel >("Severity", /*restoreDefault=*/ true) // Resets the terminal to default.
    273171#endif
    274172    );
Note: See TracChangeset for help on using the changeset viewer.