-
Notifications
You must be signed in to change notification settings - Fork 3
/
czout.h
200 lines (157 loc) · 4.86 KB
/
czout.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/**
--------------------------------------------------------------------------------
- Module : czout.h
- Description : A stdout wrapper with a mutex to print sentences entirely
- so that in a multithreaded environment sentences do not
- mingle with each character.
- Author : Tim Zaman, 18-FEB-2016
--------------------------------------------------------------------------------
*/
/*
Copyright (c) 2016 Tim Zaman
Permission to use, copy, modify, distribute, and sell this software
for any purpose is hereby granted without fee, provided
that (i) the above copyright notices and this permission notice appear in
all copies of the software and related documentation.
THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*/
#ifndef CZOUT_h
#define CZOUT_h
#include <mutex>
#include <string>
#include <iostream> //cout, cerr, cin
#define RESET "\033[0m"
#define BOLD "\033[1m"
#define BLACK "\033[30m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define WHITE "\033[37m"
#define BGBLACK "\033[40m"
#define BGRED "\033[41m"
#define BGGREEN "\033[42m"
#define BGBROWN "\033[43m"
#define BGBLUE "\033[44m"
#define BGMAGENTA "\033[45m"
#define BGCYAN "\033[46m"
#define BGWHITE "\033[47m"
#define COLRESET "\033[0m"
#define endlr "\033[0m\r\n"
#define WARNING "\033[1m\033[31mWARNING:\033[0m "
/**
* Interface structure that can be used for printing with mutex locks so that one can make sure entire strings are
* printed without streams intertwining instead of just the characters as in 'cout'.
*/
struct czout_printer {
std::mutex mutex;
/**
* Prints a complete uninterruptable string without line ending
*/
void print(std::string str){
std::lock_guard<std::mutex> guard(mutex);
std::cout << str;
}
/**
* Prints a complete uninterruptable string with line ending
*/
void println(std::string str, int verbosity){
std::lock_guard<std::mutex> guard(mutex);
if (verbosity<=verbose){
std::cout << str << std::endl;
}
}
int verbose = 1; //Default verbosity of a call (0=very important)
};
/*!
* \brief czout
*
* Interface for clean verbose printing, need a printer to make this work, see czout_printer
* There can be many czout classes hooked into one printer.
* \author Tim Zaman
*
*/
class czout {
public:
czout(){
//Nothing. Make sure to set the printer though!
}
//Constructor that sets the printer immediatelly
czout(czout_printer * my_pzout_printer){
setPrinter(my_pzout_printer);
}
void setPrinter(czout_printer * my_pzout_printer){
this->t_pzout_printer = my_pzout_printer;
}
int verbose = 1; //Default verbosity of a call (0=very important)
//template<typename T> czout& operator << (T&& x) {
// //TODO
// cout << RED << " TODO: " << x << COLRESET << endl;
// return *this;
//}
czout& operator << (std::string data) {
allStr += data;
return *this;
};
czout& operator << (void * data) {
//allStr += std::to_string(data);
allStr += " ptr <0xTODO!>";
return *this;
};
czout& operator << (int data) {
allStr += std::to_string(data);
return *this;
};
czout& operator << (unsigned int data) {
allStr += std::to_string(data);
return *this;
};
czout& operator << (unsigned long data) {
allStr += std::to_string(data);
return *this;
};
czout& operator << (float data) {
allStr += std::to_string(data);
return *this;
};
czout& operator << (double data) {
allStr += std::to_string(data);
return *this;
};
//Call to 'std::endl' or 'endl'
czout& operator << (std::ostream&(*pManip)(std::ostream&)){
//Write and flush it out
//Construct the full string
std::string line = color + prefix + COLRESET + allStr;
t_pzout_printer->println(line, verbose);
//And reset the data
allStr = "";
}
void setPrefix(std::string myprefix, int id = -1){
this->prefix = myprefix;
if (id>=0){
setColor(id);
}
}
void setColor(int id){
std::vector<std::string> VECCOLSTR; //TODO: this can be more static, global and made generally less cumbersome
VECCOLSTR.resize(5);
VECCOLSTR[0] = CYAN;
VECCOLSTR[1] = GREEN;
VECCOLSTR[2] = YELLOW;
VECCOLSTR[3] = BLUE;
VECCOLSTR[4] = MAGENTA;
this->color = VECCOLSTR[id % VECCOLSTR.size()]; //Pick sequentially changing color per ID
}
private:
czout_printer * t_pzout_printer = NULL; //The interface that actually prints
std::string allStr; //All data is concatenated here
std::string prefix = "[UNSET]"; //The thing that's written before each string
std::string color = "";
};
//END CZOUT_h
#endif