Effective implementation of algorithms (Master Thesis)
Effective and error-free implementation of algorithms
src/utils/benchmark/color.h
Go to the documentation of this file.
00001 #ifndef H_UTILS_BENCHMARK_COLOR
00002 #define H_UTILS_BENCHMARK_COLOR
00003 
00006 #include <stdarg.h>
00007 
00008 #ifdef _WIN32
00009   #include <windows.h>
00010   #include <wincon.h>
00011   #include <io.h>
00012   namespace color {
00016     enum Color {
00017       BLUE = FOREGROUND_BLUE,
00018       PINK = FOREGROUND_BLUE | FOREGROUND_RED,
00019       CYAN = FOREGROUND_BLUE | FOREGROUND_GREEN,
00020     };
00021 
00029     void colorPrintf(Color color, const char* fmt, ...) {
00030       // This function is copied from GTEST!
00031 
00032       va_list args;
00033       va_start(args, fmt);
00034 
00035       const HANDLE stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
00036 
00037       // Gets the current text color.
00038       CONSOLE_SCREEN_BUFFER_INFO buffer_info;
00039       GetConsoleScreenBufferInfo(stdout_handle, &buffer_info);
00040       const WORD old_color_attrs = buffer_info.wAttributes;
00041 
00042       // We need to flush the stream buffers into the console before each
00043       // SetConsoleTextAttribute call lest it affect the text that is already
00044       // printed but has not yet reached the console.
00045       fflush(stdout);
00046       SetConsoleTextAttribute(stdout_handle,
00047                               color | FOREGROUND_INTENSITY);
00048       vprintf(fmt, args);
00049 
00050       fflush(stdout);
00051       // Restores the text color.
00052       SetConsoleTextAttribute(stdout_handle, old_color_attrs);
00053     }
00054 
00055   }
00056 #else
00057   namespace color {
00061     enum Color {
00062       BLUE = 34,
00063       PINK = 35,
00064       CYAN = 36,
00065     };
00066 
00074     void colorPrintf(Color color, const char* fmt, ...) {
00075       va_list args;
00076       va_start(args, fmt);
00077       printf("%c[1;%dm", 27, (int) color);
00078       vprintf(fmt, args);
00079       printf("%c[0m", 27);
00080     }
00081 
00082   }
00083 #endif
00084 
00085 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines