Changeset 236 in pacpusframework for trunk/src


Ignore:
Timestamp:
Nov 28, 2013, 5:07:16 PM (11 years ago)
Author:
Marek Kurdej
Message:

Experimental: using global logger object instead of trivial logger. Added: stub for colored log output on Linux.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PacpusLib/Log.cpp

    r231 r236  
    1111#include <boost/log/detail/date_time_format_parser.hpp>
    1212#include <boost/log/expressions.hpp>
     13#include <boost/log/expressions/formatters/if.hpp>
    1314#include <boost/log/sinks/text_file_backend.hpp>
    1415#include <boost/log/sinks/text_ostream_backend.hpp>
    1516#include <boost/log/sources/severity_logger.hpp>
    16 #include <boost/log/sources/record_ostream.hpp>
    1717#include <boost/log/support/date_time.hpp>
    1818#include <boost/log/utility/setup/common_attributes.hpp>
    1919#include <boost/log/utility/setup/file.hpp>
    2020#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
    2226#include <ostream>
    2327#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
    2446
    2547template< typename CharT, typename TraitsT >
     
    3658namespace pacpus
    3759{
    38    
     60
     61BOOST_LOG_ATTRIBUTE_KEYWORD(severity, "Severity", ::pacpus::SeverityLevel)
     62
     63enum 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.
     93WORD 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.
     112const 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
     143const char* getAnsiColorCodeRestoreDefault()
     144{
     145    return "\033[0m";
     146}
     147
     148#endif // PACPUS_OS_WINDOWS && !PACPUS_OS_WINDOWS_MOBILE
     149
    39150void init_log_factories()
    40151{
     
    78189        "ThreadID",
    79190        attrs::current_thread_id());
     191    //logging::core::get()->add_global_attribute(
     192    //    "Scope",
     193    //    attrs::named_scope());
    80194
    81195    logging::core::get()->set_filter
     
    83197#ifdef NDEBUG
    84198        // release
    85         logging::trivial::severity >= logging::trivial::debug
     199        severity >= debug
    86200#else
    87201        // 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
    93208    logging::add_file_log
    94209    (
     
    103218                //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "]"
    104219                << " [" << 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 << ">"
    106222                << " <" << expr::attr< attrs::current_process_id::value_type >("ProcessID")
    107223                << ":" << expr::attr< attrs::current_thread_id::value_type >("ThreadID") << ">"
     
    109225        )
    110226    );
     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
    111238
    112239    // 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 >();
    114242    backend->add_stream(
    115243        shared_ptr< std::ostream >(&std::clog, empty_deleter())
     
    125253    sink->set_filter
    126254    (
    127         logging::trivial::severity >= logging::trivial::info
     255        severity >= info
    128256    );
    129257
     
    131259    (
    132260        expr::stream
     261#if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX
     262//            << getAnsiColorCode(color)
     263#endif
    133264            << std::setfill('0') << std::setw(6) << expr::attr< unsigned int >("LineID")
    134265            //<< " [" << expr::format_date_time< posix_time::ptime >("TimeStamp", date_time::iso_extended_format) << "] "
    135266            << " [" << 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 << ">"
    137269            << " "
    138270            << expr::smessage
     271#if PACPUS_LOG_COLORED_OUTPUT && PACPUS_OS_LINUX
     272            << getAnsiColorCodeRestoreDefault() // Resets the terminal to default.
     273#endif
    139274    );
    140275
Note: See TracChangeset for help on using the changeset viewer.