NVBIO
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
console.cpp
Go to the documentation of this file.
1 /*
2  * nvbio
3  * Copyright (c) 2011-2014, NVIDIA CORPORATION. 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 NVIDIA CORPORATION 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
17  * ANY 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 NVIDIA CORPORATION 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 
28 #include <nvbio/basic/console.h>
29 #include <string>
30 
31 std::string retokenize(const char* format, const char* prefix)
32 {
33  // check if the string starts with a carriage return
34  bool carriage_return = false;
35  const char* start_p = format;
36 
37  for (const char* p = format; *p == '\r' || *p == ' '; ++p)
38  {
39  if (*p == '\r')
40  {
41  carriage_return = true;
42  start_p = p;
43  break;
44  }
45  }
46 
47  std::string new_format;
48  if (carriage_return == false)
49  new_format = std::string( prefix );
50 
51  for (const char* p = start_p; *p != '\0'; ++p)
52  {
53  if (*p == '\n')
54  {
55  new_format.append( 1u,'\n' );
56  if (*(p+1) != '\0')
57  new_format += prefix;
58  }
59  else if (*p == '\r')
60  {
61  new_format.append( 1u,'\r' );
62  if (*(p+1) != '\0')
63  new_format += prefix;
64  }
65  else
66  new_format.append( 1u,*p );
67  }
68  return new_format;
69 }
70 
71 #if WIN32
72 #include <nvbio/basic/threads.h>
73 #include <windows.h>
74 
75 unsigned int TEXT_BLUE = FOREGROUND_BLUE;
76 unsigned int TEXT_RED = FOREGROUND_RED;
77 unsigned int TEXT_GREEN = FOREGROUND_GREEN;
78 unsigned int TEXT_BRIGHT = FOREGROUND_INTENSITY;
79 
80 namespace { nvbio::Mutex s_mutex; }
81 
83 
84 void set_verbosity(Verbosity level)
85 {
86  s_verbosity = level;
87 }
88 
89 static void textcolor(unsigned int color)
90 {
91  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Get handle to standard output
92  SetConsoleTextAttribute(hConsole,color); // set the text attribute of the previous handle
93 }
94 
95 void log_visible(FILE* stream, const char* format, ...)
96 {
97  nvbio::ScopedLock lock( &s_mutex );
98 
99  if (s_verbosity >= V_VISIBLE)
100  {
101  const std::string new_format = retokenize( format, "visible : " );
103  va_list args;
104  va_start(args, format);
105  vfprintf(stream, new_format.c_str(), args);
106  va_end(args);
108  }
109 }
110 void log_info(FILE* stream, const char* format, ...)
111 {
112  nvbio::ScopedLock lock( &s_mutex );
113 
114  if (s_verbosity >= V_INFO)
115  {
116  const std::string new_format = retokenize( format, "info : " );
118  va_list args;
119  va_start(args, format);
120  vfprintf(stream, new_format.c_str(), args);
121  va_end(args);
123  }
124 }
125 void log_stats(FILE* stream, const char* format, ...)
126 {
127  if (s_verbosity >= V_STATS)
128  {
129  const std::string new_format = retokenize( format, "stats : " );
130  textcolor( TEXT_BLUE | TEXT_BRIGHT/*| TEXT_GREEN*/ );
131  va_list args;
132  va_start(args, format);
133  vfprintf(stream, new_format.c_str(), args);
134  va_end(args);
136  }
137 }
138 void log_verbose(FILE* stream, const char* format, ...)
139 {
140  nvbio::ScopedLock lock( &s_mutex );
141 
142  if (s_verbosity >= V_VERBOSE)
143  {
144  const std::string new_format = retokenize( format, "verbose : " );
146  va_list args;
147  va_start(args, format);
148  vfprintf(stream, new_format.c_str(), args);
149  va_end(args);
151  }
152 }
153 void log_debug(FILE* stream, const char* format, ...)
154 {
155  nvbio::ScopedLock lock( &s_mutex );
156 
157  if (s_verbosity >= V_DEBUG)
158  {
159  const std::string new_format = retokenize( format, "debug : " );
160  textcolor( TEXT_RED );
161  va_list args;
162  va_start(args, format);
163  vfprintf(stream, new_format.c_str(), args);
164  va_end(args);
166  }
167 }
168 void log_warning(FILE* stream, const char* format, ...)
169 {
170  nvbio::ScopedLock lock( &s_mutex );
171 
172  if (s_verbosity >= V_ERROR)
173  {
174  const std::string new_format = retokenize( format, "warning : " );
176  va_list args;
177  va_start(args, format);
178  vfprintf(stream, new_format.c_str(), args);
179  va_end(args);
181  }
182 }
183 void log_error(FILE* stream, const char* format, ...)
184 {
185  nvbio::ScopedLock lock( &s_mutex );
186 
187  if (s_verbosity >= V_ERROR)
188  {
189  const std::string new_format = retokenize( format, "error : " );
191  va_list args;
192  va_start(args, format);
193  vfprintf(stream, new_format.c_str(), args);
194  va_end(args);
196  }
197 }
198 
199 void log_visible_cont(FILE* stream, const char* format, ...)
200 {
201  nvbio::ScopedLock lock( &s_mutex );
202 
203  if (s_verbosity >= V_VISIBLE)
204  {
206  va_list args;
207  va_start(args, format);
208  vfprintf(stream, format, args);
209  va_end(args);
211  }
212 }
213 void log_info_cont(FILE* stream, const char* format, ...)
214 {
215  nvbio::ScopedLock lock( &s_mutex );
216 
217  if (s_verbosity >= V_INFO)
218  {
220  va_list args;
221  va_start(args, format);
222  vfprintf(stream, format, args);
223  va_end(args);
225  }
226 }
227 void log_stats_cont(FILE* stream, const char* format, ...)
228 {
229  nvbio::ScopedLock lock( &s_mutex );
230 
231  if (s_verbosity >= V_STATS)
232  {
233  const std::string new_format = retokenize( format, "stats : " );
234  textcolor( TEXT_BLUE | TEXT_BRIGHT/*| TEXT_GREEN*/ );
235  va_list args;
236  va_start(args, format);
237  vfprintf(stream, new_format.c_str(), args);
238  va_end(args);
240  }
241 }
242 void log_verbose_cont(FILE* stream, const char* format, ...)
243 {
244  nvbio::ScopedLock lock( &s_mutex );
245 
246  if (s_verbosity >= V_VERBOSE)
247  {
249  va_list args;
250  va_start(args, format);
251  vfprintf(stream, format, args);
252  va_end(args);
254  }
255 }
256 void log_debug_cont(FILE* stream, const char* format, ...)
257 {
258  nvbio::ScopedLock lock( &s_mutex );
259 
260  if (s_verbosity >= V_DEBUG)
261  {
262  textcolor( TEXT_RED );
263  va_list args;
264  va_start(args, format);
265  vfprintf(stream, format, args);
266  va_end(args);
268  }
269 }
270 void log_warning_cont(FILE* stream, const char* format, ...)
271 {
272  nvbio::ScopedLock lock( &s_mutex );
273 
274  if (s_verbosity >= V_ERROR)
275  {
277  va_list args;
278  va_start(args, format);
279  vfprintf(stream, format, args);
280  va_end(args);
282  }
283 }
284 void log_error_cont(FILE* stream, const char* format, ...)
285 {
286  nvbio::ScopedLock lock( &s_mutex );
287 
288  if (s_verbosity >= V_ERROR)
289  {
291  va_list args;
292  va_start(args, format);
293  vfprintf(stream, format, args);
294  va_end(args);
296  }
297 }
298 
299 #else
300 #include <stdarg.h>
301 
302 const char* TEXT_BRIGHT_BLUE = "\033[01;34m";
303 const char* TEXT_BRIGHT_MAGENTA = "\033[01;35m";
304 const char* TEXT_BRIGHT_RED = "\033[01;31m";
305 const char* TEXT_CYAN = "\033[22;36m";
306 const char* TEXT_BLUE = "\033[22;34m";
307 const char* TEXT_RED = "\033[22;31m";
308 const char* TEXT_GREEN = "\033[22;32m";
309 const char* TEXT_BRIGHT = "\033[01;37m";
310 const char* TEXT_NORMAL = "\033[22;37m";
311 
313 
315 {
316  s_verbosity = level;
317 }
318 
319 void log_visible(FILE* stream, const char* format, ...)
320 {
321  if (s_verbosity >= V_VISIBLE)
322  {
323  const std::string new_format = retokenize( format, "visible : " );
324  char col_format[2048];
325  sprintf( col_format, "%s%s", TEXT_BRIGHT, new_format.c_str() );
326  va_list args;
327  va_start(args, format);
328  vfprintf(stream, col_format, args);
329  va_end(args);
330  }
331 }
332 void log_info(FILE* stream, const char* format, ...)
333 {
334  if (s_verbosity >= V_INFO)
335  {
336  const std::string new_format = retokenize( format, "info : " );
337  char col_format[2048];
338  sprintf( col_format, "%s%s", TEXT_NORMAL, new_format.c_str() );
339  va_list args;
340  va_start(args, format);
341  vfprintf(stream, col_format, args);
342  va_end(args);
343  }
344 }
345 void log_stats(FILE* stream, const char* format, ...)
346 {
347  if (s_verbosity >= V_STATS)
348  {
349  const std::string new_format = retokenize( format, "stats : " );
350  char col_format[2048];
351  sprintf( col_format, "%s%s", TEXT_CYAN, new_format.c_str() );
352  va_list args;
353  va_start(args, format);
354  vfprintf(stream, col_format, args);
355  va_end(args);
356  }
357 }
358 void log_verbose(FILE* stream, const char* format, ...)
359 {
360  if (s_verbosity >= V_VERBOSE)
361  {
362  const std::string new_format = retokenize( format, "verbose : " );
363  char col_format[2048];
364  sprintf( col_format, "%s%s", TEXT_GREEN, new_format.c_str() );
365  va_list args;
366  va_start(args, format);
367  vfprintf(stream, col_format, args);
368  va_end(args);
369  }
370 }
371 void log_debug(FILE* stream, const char* format, ...)
372 {
373  if (s_verbosity >= V_DEBUG)
374  {
375  const std::string new_format = retokenize( format, "debug : " );
376  char col_format[2048];
377  sprintf( col_format, "%s%s", TEXT_RED, new_format.c_str() );
378  va_list args;
379  va_start(args, format);
380  vfprintf(stream, col_format, args);
381  va_end(args);
382  }
383 }
384 void log_warning(FILE* stream, const char* format, ...)
385 {
386  if (s_verbosity >= V_ERROR)
387  {
388  const std::string new_format = retokenize( format, "warning : " );
389  char col_format[2048];
390  sprintf( col_format, "%s%s", TEXT_BRIGHT_MAGENTA, new_format.c_str() );
391  va_list args;
392  va_start(args, format);
393  vfprintf(stream, col_format, args);
394  va_end(args);
395  }
396 }
397 void log_error(FILE* stream, const char* format, ...)
398 {
399  if (s_verbosity >= V_ERROR)
400  {
401  const std::string new_format = retokenize( format, "error : " );
402  char col_format[2048];
403  sprintf( col_format, "%s%s", TEXT_BRIGHT_RED, new_format.c_str() );
404  va_list args;
405  va_start(args, format);
406  vfprintf(stream, col_format, args);
407  va_end(args);
408  }
409 }
410 
411 void log_visible_cont(FILE* stream, const char* format, ...)
412 {
413  if (s_verbosity >= V_VISIBLE)
414  {
415  char col_format[2048];
416  sprintf( col_format, "%s%s", TEXT_BRIGHT, format );
417  va_list args;
418  va_start(args, format);
419  vfprintf(stream, col_format, args);
420  va_end(args);
421  }
422 }
423 void log_info_cont(FILE* stream, const char* format, ...)
424 {
425  if (s_verbosity >= V_INFO)
426  {
427  char col_format[2048];
428  sprintf( col_format, "%s%s", TEXT_NORMAL, format );
429  va_list args;
430  va_start(args, format);
431  vfprintf(stream, col_format, args);
432  va_end(args);
433  }
434 }
435 void log_stats_cont(FILE* stream, const char* format, ...)
436 {
437  if (s_verbosity >= V_STATS)
438  {
439  char col_format[2048];
440  sprintf( col_format, "%s%s", TEXT_CYAN, format );
441  va_list args;
442  va_start(args, format);
443  vfprintf(stream, col_format, args);
444  va_end(args);
445  }
446 }
447 void log_verbose_cont(FILE* stream, const char* format, ...)
448 {
449  if (s_verbosity >= V_VERBOSE)
450  {
451  char col_format[2048];
452  sprintf( col_format, "%s%s", TEXT_GREEN, format );
453  va_list args;
454  va_start(args, format);
455  vfprintf(stream, col_format, args);
456  va_end(args);
457  }
458 }
459 void log_debug_cont(FILE* stream, const char* format, ...)
460 {
461  if (s_verbosity >= V_DEBUG)
462  {
463  char col_format[2048];
464  sprintf( col_format, "%s%s", TEXT_RED, format );
465  va_list args;
466  va_start(args, format);
467  vfprintf(stream, col_format, args);
468  va_end(args);
469  }
470 }
471 void log_warning_cont(FILE* stream, const char* format, ...)
472 {
473  if (s_verbosity >= V_ERROR)
474  {
475  char col_format[2048];
476  sprintf( col_format, "%s%s", TEXT_BRIGHT_MAGENTA, format );
477  va_list args;
478  va_start(args, format);
479  vfprintf(stream, col_format, args);
480  va_end(args);
481  }
482 }
483 void log_error_cont(FILE* stream, const char* format, ...)
484 {
485  if (s_verbosity >= V_ERROR)
486  {
487  char col_format[2048];
488  sprintf( col_format, "%s%s", TEXT_BRIGHT_RED, format );
489  va_list args;
490  va_start(args, format);
491  vfprintf(stream, col_format, args);
492  va_end(args);
493  }
494 }
495 
496 #endif
497 
498 void log_visible_nl(FILE* stream)
499 {
500  if (s_verbosity >= V_VISIBLE)
501  fprintf(stream, "\n");
502 }
503 void log_info_nl(FILE* stream)
504 {
505  if (s_verbosity >= V_INFO)
506  fprintf(stream, "\n");
507 }
508 void log_stats_nl(FILE* stream)
509 {
510  if (s_verbosity >= V_STATS)
511  fprintf(stream, "\n");
512 }
513 void log_verbose_nl(FILE* stream)
514 {
515  if (s_verbosity >= V_VERBOSE)
516  fprintf(stream, "\n");
517 }
518 void log_debug_nl(FILE* stream)
519 {
520  if (s_verbosity >= V_DEBUG)
521  fprintf(stream, "\n");
522 }
523 void log_warning_nl(FILE* stream)
524 {
525  if (s_verbosity >= V_ERROR)
526  fprintf(stream, "\n");
527 }
528 void log_error_nl(FILE* stream)
529 {
530  if (s_verbosity >= V_ERROR)
531  fprintf(stream, "\n");
532 }