Changeset 243 in pacpusframework
- Timestamp:
- Dec 2, 2013, 9:42:47 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/PacpusLib/ColorSeverityFormatter.hpp
r242 r243 23 23 #include <boost/log/expressions.hpp> 24 24 #include <boost/log/utility/functional/bind.hpp> 25 #include <cstdlib> 25 26 #include <sstream> 26 27 … … 130 131 #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 131 132 133 bool shouldUseColor(bool stdoutIsTty) 134 { 135 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 136 return true; 137 #else 138 // On non-Windows platforms, we rely on the TERM variable. 139 const char* const term = std::getenv("TERM"); 140 const bool termSupportsColor = (term == "xterm") 141 || (term, "xterm-color") 142 || (term, "xterm-256color") 143 || (term, "screen") 144 || (term, "linux") 145 || (term, "cygwin"); 146 return stdoutIsTty && termSupportsColor; 147 #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 148 } 149 132 150 template < typename CharT > 133 151 struct ColorFormatter 134 152 { 135 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE)136 153 ColorFormatter() 154 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 137 155 : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE)) 138 { 139 } 140 #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 156 #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 157 { 158 mShouldUseColor = shouldUseColor(/*stdoutIsTty*/ true); 159 } 141 160 142 161 void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& sev) 143 162 { 163 if (!mShouldUseColor) { 164 return; 165 } 144 166 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 145 167 // Gets the current text color. … … 158 180 } 159 181 182 bool mShouldUseColor; 160 183 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 161 184 const HANDLE mStreamHandle; … … 167 190 struct DefaultFormatter 168 191 { 169 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 170 DefaultFormatter() //(WORD savedConsoleBufferInfo) 192 DefaultFormatter() 193 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 194 //DefaultFormatter(WORD savedConsoleBufferInfo) 171 195 : mStreamHandle(GetStdHandle(STD_OUTPUT_HANDLE)) 172 196 //, mSavedConsoleBufferInfo(savedConsoleBufferInfo) 173 197 , 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) 198 #endif // defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 199 { 200 mShouldUseColor = shouldUseColor(/*stdoutIsTty*/ true); 201 } 177 202 178 203 void operator()(boost::log::basic_formatting_ostream<CharT>& strm, SeverityLevel const& /*sev*/) 179 204 { 205 if (!mShouldUseColor) { 206 return; 207 } 180 208 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 181 209 fflush(stdout); … … 187 215 } 188 216 217 bool mShouldUseColor; 189 218 #if defined(PACPUS_OS_WINDOWS) && !defined(PACPUS_OS_WINDOWS_MOBILE) 190 219 const HANDLE mStreamHandle;
Note:
See TracChangeset
for help on using the changeset viewer.