tiny_dnn 1.0.0
A header only, dependency-free deep learning framework in C++11
Loading...
Searching...
No Matches
colored_print.h
1/*
2 Copyright (c) 2016, Taiga Nomi
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the <organization> nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27#pragma once
28#include "tiny_dnn/config.h"
29
30#ifdef CNN_WINDOWS
31#ifndef NOMINMAX
32#define NOMINMAX
33#endif // ifdef NOMINMAX
34#include <Windows.h>
35#endif
36
37namespace tiny_dnn {
38
39enum class Color {
40 RED,
41 GREEN,
42 BLUE,
43 YELLOW
44};
45
46#ifdef CNN_WINDOWS
47inline WORD getColorAttr(Color c) {
48 switch (c) {
49 case Color::RED: return FOREGROUND_RED;
50 case Color::GREEN: return FOREGROUND_GREEN;
51 case Color::BLUE: return FOREGROUND_BLUE;
52 case Color::YELLOW: return FOREGROUND_GREEN|FOREGROUND_RED;
53 default: assert(0); return 0;
54 }
55}
56#else
57inline const char* getColorEscape(Color c) {
58 switch (c) {
59 case Color::RED: return "\033[31m";
60 case Color::GREEN: return "\033[32m";
61 case Color::BLUE: return "\033[34m";
62 case Color::YELLOW: return "\033[33m";
63 default: assert(0); return "";
64 }
65}
66#endif
67
68inline void coloredPrint(Color c, const char* fmt, ...) {
69 va_list args;
70 va_start(args, fmt);
71
72#ifdef CNN_WINDOWS
73 const HANDLE std_handle = ::GetStdHandle(STD_OUTPUT_HANDLE);
74
75 CONSOLE_SCREEN_BUFFER_INFO buffer_info;
76 ::GetConsoleScreenBufferInfo(std_handle, &buffer_info);
77 const WORD old_color = buffer_info.wAttributes;
78 const WORD new_color = getColorAttr(c) | FOREGROUND_INTENSITY;
79
80 fflush(stdout);
81 ::SetConsoleTextAttribute(std_handle, new_color);
82
83 vprintf(fmt, args);
84
85 fflush(stdout);
86 ::SetConsoleTextAttribute(std_handle, old_color);
87#else
88 printf("%s", getColorEscape(c));
89 vprintf(fmt, args);
90 printf("\033[m");
91#endif
92 va_end(args);
93}
94
95inline void coloredPrint(Color c, const std::string& msg) {
96 coloredPrint(c, msg.c_str());
97}
98
99} // tiny_dnn