Changeset 236 in pacpusframework


Ignore:
Timestamp:
11/28/13 17:07:16 (11 years ago)
Author:
Marek Kurdej
Message:

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

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/CMakeLists.txt

    r187 r236  
    3434set(PACPUS_SOURCE_DIR       ${PACPUS_ROOT_DIR}/src)
    3535set(IS_BUILDING_PACPUS      TRUE)
     36
     37set(PACPUS_DEFINITIONS "")
    3638
    3739################################################################################
  • trunk/cmake/PacpusConfiguration.cmake

    r187 r236  
    1919# Global configuration
    2020# ========================================
    21 set(PACPUS_USE_LOG      TRUE  CACHE BOOL "Logging using log4cxx, the library is required")
     21set(PACPUS_USE_LOG      TRUE  CACHE BOOL "Logging using Boost.Log, the library is required")
     22set(PACPUS_LOG_COLORED_OUTPUT FALSE CACHE BOOL "Logging in color.")
    2223set(PACPUS_INSTALL_3RD  FALSE CACHE BOOL "Installation of the 3rd party")
    2324set(PACPUS_BUILD_DOC    FALSE CACHE BOOL "Whether build the documentation - requires Doxygen")
     
    3233    endif()
    3334endif(PACPUS_USE_SOLUTION_FOLDERS)
     35
     36if(PACPUS_LOG_COLORED_OUTPUT)
     37    list(APPEND PACPUS_DEFINITIONS " -DPACPUS_LOG_COLORED_OUTPUT=1 ")
     38endif()
  • trunk/cmake/PacpusDependencies.cmake

    r234 r236  
    1010################################################################################
    1111# OUTPUT
    12 set(PACPUS_DEFINITIONS "")
    1312set(PACPUS_DEPENDENCIES_INC "")
    1413set(PACPUS_DEPENDENCIES_LIB "")
     
    120119            log
    121120            log_setup
     121            system
    122122    )
    123123    include_directories(${Boost_INCLUDE_DIRS})
  • trunk/cmake/PacpusUtilities.cmake

    r235 r236  
    345345        TARGETS ${ARGV}
    346346        RUNTIME DESTINATION ${PACPUS_INSTALL_DIR}/bin
    347         LIBRARY DESTINATION ${PACPUS_INSTALL_DIR}/lib
     347        LIBRARY DESTINATION ${PACPUS_INSTALL_DIR}/bin
    348348        ARCHIVE DESTINATION ${PACPUS_INSTALL_DIR}/lib
    349349    )
  • trunk/include/Pacpus/kernel/Log.h

    r196 r236  
    1717#include <Pacpus/kernel/PacpusLibConfig.h>
    1818
    19 namespace pacpus {
     19namespace pacpus
     20{
    2021
    2122/// Static log facility initializer
     
    3435
    3536#if defined(PACPUS_USE_LOG)
     37#   include <boost/log/common.hpp>
    3638#   include <boost/log/attributes/named_scope.hpp>
    37 #   include <boost/log/trivial.hpp>
     39#   include <boost/log/core.hpp>
     40#   include <boost/log/sources/record_ostream.hpp>
     41#   include <boost/log/sources/severity_logger.hpp>
    3842#   include <iosfwd>
     43
     44namespace pacpus
     45{
     46    /// Trivial severity levels
     47    /*PACPUSLIB_API*/ enum SeverityLevel
     48    {
     49        trace,
     50        debug,
     51        info,
     52        warning,
     53        error,
     54        fatal
     55    };
     56
     57    BOOST_LOG_INLINE_GLOBAL_LOGGER_DEFAULT(gLogger, ::boost::log::sources::severity_logger_mt< ::pacpus::SeverityLevel>)
     58
     59} // namespace pacpus
    3960
    4061class QString;
     
    4566#   define PACPUS_LOG_FUNCTION()              BOOST_LOG_FUNCTION()
    4667#   define PACPUS_LOG_NAMED_SCOPE(name)       BOOST_LOG_NAMED_SCOPE(name)
    47 #   define PACPUS_LOG(level, message)         BOOST_LOG_TRIVIAL(level) << message
     68#   define PACPUS_LOG(level, message)         BOOST_LOG_SEV( ::pacpus::gLogger::get(), level) << message
    4869#else
    4970/// @param name Name of the logger, displayed when logging a message.
     
    5576
    5677/// Logs a message at TRACE level using default logger
    57 #define LOG_TRACE(message)          PACPUS_LOG(trace, message)
     78#define LOG_TRACE(message)          PACPUS_LOG( ::pacpus::trace, message)
    5879/// Logs a message at DEBUG level using default logger
    59 #define LOG_DEBUG(message)          PACPUS_LOG(debug, message)
     80#define LOG_DEBUG(message)          PACPUS_LOG( ::pacpus::debug, message)
    6081/// Logs a message at INFO level using default logger
    61 #define LOG_INFO(message)           PACPUS_LOG(info, message)
     82#define LOG_INFO(message)           PACPUS_LOG( ::pacpus::info, message)
    6283/// Logs a message at WARN level using default logger
    63 #define LOG_WARN(message)           PACPUS_LOG(warning, message)
     84#define LOG_WARN(message)           PACPUS_LOG( ::pacpus::warning, message)
    6485/// Logs a message at ERROR level using default logger
    65 #define LOG_ERROR(message)          PACPUS_LOG(error, message)
     86#define LOG_ERROR(message)          PACPUS_LOG( ::pacpus::error, message)
    6687/// Logs a message at FATAL level using default logger
    67 #define LOG_FATAL(message)          PACPUS_LOG(fatal, message)
     88#define LOG_FATAL(message)          PACPUS_LOG( ::pacpus::fatal, message)
    6889
    6990#endif // DEF_PACPUS_LOG_H
  • 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.