summaryrefslogtreecommitdiff
path: root/boost/test/utils/setcolor.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'boost/test/utils/setcolor.hpp')
-rw-r--r--boost/test/utils/setcolor.hpp201
1 files changed, 195 insertions, 6 deletions
diff --git a/boost/test/utils/setcolor.hpp b/boost/test/utils/setcolor.hpp
index c70209038a..91b068ae6f 100644
--- a/boost/test/utils/setcolor.hpp
+++ b/boost/test/utils/setcolor.hpp
@@ -24,6 +24,15 @@
#include <boost/test/detail/suppress_warnings.hpp>
+#ifdef _WIN32
+ #include <windows.h>
+
+ #if defined(__MINGW32__) && !defined(COMMON_LVB_UNDERSCORE)
+ // mingw badly mimicking windows.h
+ #define COMMON_LVB_UNDERSCORE 0x8000
+ #endif
+#endif
+
//____________________________________________________________________________//
namespace boost {
@@ -64,6 +73,7 @@ struct term_color { enum _ {
// ************** setcolor ************** //
// ************************************************************************** //
+#ifndef _WIN32
class setcolor {
public:
// Constructor
@@ -77,7 +87,10 @@ public:
friend std::ostream&
operator<<( std::ostream& os, setcolor const& sc )
{
- return os.write( sc.m_control_command, sc.m_command_size );
+ if (&os == &std::cout || &os == &std::cerr) {
+ return os.write( sc.m_control_command, sc.m_command_size );
+ }
+ return os;
}
private:
@@ -86,10 +99,151 @@ private:
int m_command_size;
};
+#else
+
+class setcolor {
+
+protected:
+ void set_console_color(std::ostream& os, WORD *attributes = NULL) const {
+ DWORD console_type;
+ if (&os == &std::cout) {
+ console_type = STD_OUTPUT_HANDLE;
+ }
+ else if (&os == &std::cerr) {
+ console_type = STD_ERROR_HANDLE;
+ }
+ else {
+ return;
+ }
+ HANDLE hConsole = GetStdHandle(console_type);
+
+ if(hConsole == INVALID_HANDLE_VALUE || hConsole == NULL )
+ return;
+
+ if(attributes != NULL) {
+ SetConsoleTextAttribute(hConsole, *attributes);
+ return;
+ }
+
+ CONSOLE_SCREEN_BUFFER_INFO consoleInfo;
+ GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
+ //if(!has_written_console_ext) {
+ saved_attributes = consoleInfo.wAttributes;
+ //}
+
+ WORD fg_attr = 0;
+ switch(m_fg)
+ {
+ case term_color::WHITE:
+ fg_attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
+ break;
+ case term_color::BLACK:
+ fg_attr = 0;
+ break;
+ case term_color::RED:
+ fg_attr = FOREGROUND_RED;
+ break;
+ case term_color::GREEN:
+ fg_attr = FOREGROUND_GREEN;
+ break;
+ case term_color::CYAN:
+ fg_attr = FOREGROUND_GREEN | FOREGROUND_BLUE;
+ break;
+ case term_color::MAGENTA:
+ fg_attr = FOREGROUND_RED | FOREGROUND_BLUE;
+ break;
+ case term_color::BLUE:
+ fg_attr = FOREGROUND_BLUE;
+ break;
+ case term_color::YELLOW:
+ fg_attr = FOREGROUND_RED | FOREGROUND_GREEN;
+ break;
+ case term_color::ORIGINAL:
+ default:
+ fg_attr = saved_attributes & (FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
+ break;
+ }
+
+ WORD bg_attr = 0;
+ switch(m_bg)
+ {
+ case term_color::BLACK:
+ bg_attr = 0;
+ break;
+ case term_color::WHITE:
+ bg_attr = BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
+ break;
+ case term_color::RED:
+ bg_attr = BACKGROUND_RED;
+ break;
+ case term_color::GREEN:
+ bg_attr = BACKGROUND_GREEN;
+ break;
+ case term_color::BLUE:
+ bg_attr = BACKGROUND_BLUE;
+ break;
+ case term_color::ORIGINAL:
+ default:
+ bg_attr = saved_attributes & (BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
+ break;
+ }
+
+ WORD text_attr = 0;
+ switch(m_attr)
+ {
+ case term_attr::BRIGHT:
+ text_attr = FOREGROUND_INTENSITY;
+ break;
+ case term_attr::UNDERLINE:
+ text_attr = COMMON_LVB_UNDERSCORE;
+ break;
+ default:
+ break;
+ }
+
+ SetConsoleTextAttribute(hConsole, fg_attr | bg_attr | text_attr);
+
+ //has_written_console_ext = true;
+ return;
+ }
+
+public:
+ // Constructor
+ explicit setcolor(
+ term_attr::_ attr = term_attr::NORMAL,
+ term_color::_ fg = term_color::ORIGINAL,
+ term_color::_ bg = term_color::ORIGINAL )
+ : /*has_written_console_ext(false)
+ , */m_attr(attr)
+ , m_fg(fg)
+ , m_bg(bg)
+ {}
+
+ friend std::ostream&
+ operator<<( std::ostream& os, setcolor const& sc )
+ {
+ sc.set_console_color(os);
+ return os;
+ }
+
+private:
+ term_attr::_ m_attr;
+ term_color::_ m_fg;
+ term_color::_ m_bg;
+
+protected:
+ // Data members
+ mutable WORD saved_attributes;
+ //mutable bool has_written_console_ext;
+};
+
+#endif
// ************************************************************************** //
// ************** scope_setcolor ************** //
// ************************************************************************** //
+#ifndef _WIN32
+
struct scope_setcolor {
scope_setcolor() : m_os( 0 ) {}
explicit scope_setcolor( std::ostream& os,
@@ -106,15 +260,50 @@ struct scope_setcolor {
*m_os << setcolor();
}
private:
+ scope_setcolor(const scope_setcolor& r);
+ scope_setcolor& operator=(const scope_setcolor& r);
// Data members
std::ostream* m_os;
};
-#define BOOST_TEST_SCOPE_SETCOLOR( is_color_output, os, attr, color ) \
- utils::scope_setcolor const& sc = is_color_output \
- ? utils::scope_setcolor( os, utils::attr, utils::color ) \
- : utils::scope_setcolor(); \
- ut_detail::ignore_unused_variable_warning( sc ) \
+#else
+
+struct scope_setcolor : setcolor {
+ scope_setcolor() : m_os( 0 ) {}
+ explicit scope_setcolor(
+ std::ostream& os,
+ term_attr::_ attr = term_attr::NORMAL,
+ term_color::_ fg = term_color::ORIGINAL,
+ term_color::_ bg = term_color::ORIGINAL )
+ :
+ setcolor(attr, fg, bg),
+ m_os( &os )
+ {
+ os << *this;
+ }
+
+ ~scope_setcolor()
+ {
+ if (m_os) {
+ set_console_color(*m_os, &this->saved_attributes);
+ }
+ }
+private:
+ scope_setcolor(const scope_setcolor& r);
+ scope_setcolor& operator=(const scope_setcolor& r);
+ // Data members
+ std::ostream* m_os;
+};
+
+
+#endif
+
+#define BOOST_TEST_SCOPE_SETCOLOR( is_color_output, os, attr, color ) \
+ utils::scope_setcolor const sc( \
+ os, \
+ is_color_output ? utils::attr : utils::term_attr::NORMAL, \
+ is_color_output ? utils::color : utils::term_color::ORIGINAL);\
+ ut_detail::ignore_unused_variable_warning( sc ) \
/**/
} // namespace utils