Changeset 242 in pacpusframework for trunk


Ignore:
Timestamp:
Dec 2, 2013, 9:25:38 AM (11 years ago)
Author:
Marek Kurdej
Message:

Minor: refactored code in ColorSeverityFormatter.hpp

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PacpusLib/ColorSeverityFormatter.hpp

    r240 r242  
    8888}
    8989
    90 template < typename CharT >
    91 struct ColorFormatter
    92 {
    93     ColorFormatter()
    94         : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE))
    95     {
    96     }
    97 
    98     void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& sev)
    99     {
    100         // Gets the current text color.
    101         CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
    102         GetConsoleScreenBufferInfo(mStreamHandle, &bufferInfo);
    103         mSavedConsoleBufferInfo = bufferInfo.wAttributes;
    104 
    105         // We need to flush the stream buffers into the console before each
    106         // SetConsoleTextAttribute call lest it affect the text that is already
    107         // printed but has not yet reached the console.
    108         fflush(stdout);
    109         SetConsoleTextAttribute(mStreamHandle, getColorAttribute(getColor(sev)) | FOREGROUND_INTENSITY);
    110     }
    111 
    112     const HANDLE mStreamHandle;
    113     WORD mSavedConsoleBufferInfo;
    114 };
    115 
    116 template < typename CharT >
    117 struct DefaultFormatter
    118 {
    119     DefaultFormatter() //(WORD savedConsoleBufferInfo)
    120         : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE))
    121         //, mSavedConsoleBufferInfo(savedConsoleBufferInfo)
    122         , mSavedConsoleBufferInfo(getColorAttribute(COLOR_WHITE) | FOREGROUND_INTENSITY) // FIXME: restore old value, not just reset
    123     {
    124     }
    125 
    126     void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& /*sev*/)
    127     {
    128         fflush(stdout);
    129         // Restores the text color.
    130         SetConsoleTextAttribute(mStreamHandle, mSavedConsoleBufferInfo);
    131     }
    132 
    133     const HANDLE mStreamHandle;
    134     WORD mSavedConsoleBufferInfo;
    135 };
    136 
    13790#else
    13891
     
    175128}
    176129
     130#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     131
    177132template < typename CharT >
    178133struct ColorFormatter
    179134{
     135#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     136    ColorFormatter()
     137        : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE))
     138    {
     139    }
     140#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     141
    180142    void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& sev)
    181143    {
     144#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     145        // Gets the current text color.
     146        CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
     147        GetConsoleScreenBufferInfo(mStreamHandle, &bufferInfo);
     148        mSavedConsoleBufferInfo = bufferInfo.wAttributes;
     149
     150        // We need to flush the stream buffers into the console before each
     151        // SetConsoleTextAttribute call lest it affect the text that is already
     152        // printed but has not yet reached the console.
     153        fflush(stdout);
     154        SetConsoleTextAttribute(mStreamHandle, getColorAttribute(getColor(sev)) | FOREGROUND_INTENSITY);
     155#else
    182156        strm << getAnsiColorCode(getColor(sev)).c_str();
    183     }
    184 };
     157#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     158    }
     159
     160#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     161    const HANDLE mStreamHandle;
     162    WORD mSavedConsoleBufferInfo;
     163#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     164};
     165
    185166template < typename CharT >
    186167struct DefaultFormatter
    187168{
     169#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     170    DefaultFormatter() //(WORD savedConsoleBufferInfo)
     171        : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE))
     172        //, mSavedConsoleBufferInfo(savedConsoleBufferInfo)
     173        , mSavedConsoleBufferInfo(getColorAttribute(COLOR_WHITE) | FOREGROUND_INTENSITY) // FIXME: restore old value, not just reset
     174    {
     175    }
     176#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     177
    188178    void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& /*sev*/)
    189179    {
     180#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     181        fflush(stdout);
     182        // Restores the text color.
     183        SetConsoleTextAttribute(mStreamHandle, mSavedConsoleBufferInfo);
     184#else
    190185        strm << getAnsiColorCodeRestoreDefault().c_str();
    191     }
    192 };
    193 
    194 #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     186#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     187    }
     188
     189#if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     190    const HANDLE mStreamHandle;
     191    WORD mSavedConsoleBufferInfo;
     192#endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)
     193};
    195194
    196195template< typename T, typename FallbackPolicyT, typename CharT >
Note: See TracChangeset for help on using the changeset viewer.