File: crn_colorized_console.cpp

package info (click to toggle)
crunch-dxtc 0.55.5-2
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 3,624 kB
  • sloc: cpp: 64,979; ansic: 633; python: 321; makefile: 116
file content (109 lines) | stat: -rw-r--r-- 2,823 bytes parent folder | download | duplicates (2)
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
// File: crn_colorized_console.cpp
// See Copyright Notice and license at the end of inc/crnlib.h
#include "crn_core.h"
#include "crn_colorized_console.h"
#ifdef CRNLIB_USE_WIN32_API
#include "crn_winhdr.h"
#endif

namespace crnlib {
void colorized_console::init() {
  console::init();
  console::add_console_output_func(console_output_func, NULL);
}

void colorized_console::deinit() {
  console::remove_console_output_func(console_output_func);
  console::deinit();
}

void colorized_console::tick() {
}

#ifdef CRNLIB_USE_WIN32_API
bool colorized_console::console_output_func(eConsoleMessageType type, const char* pMsg, void*) {
  if (console::get_output_disabled())
    return true;

  HANDLE cons = GetStdHandle(STD_OUTPUT_HANDLE);

  DWORD attr = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
  switch (type) {
    case cDebugConsoleMessage:
      attr = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
      break;
    case cMessageConsoleMessage:
      attr = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
      break;
    case cWarningConsoleMessage:
      attr = FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY;
      break;
    case cErrorConsoleMessage:
      attr = FOREGROUND_RED | FOREGROUND_INTENSITY;
      break;
    default:
      break;
  }

  if (INVALID_HANDLE_VALUE != cons)
    SetConsoleTextAttribute(cons, (WORD)attr);

  if ((console::get_prefixes()) && (console::get_at_beginning_of_line())) {
    switch (type) {
      case cDebugConsoleMessage:
        printf("Debug: %s", pMsg);
        break;
      case cWarningConsoleMessage:
        printf("Warning: %s", pMsg);
        break;
      case cErrorConsoleMessage:
        printf("Error: %s", pMsg);
        break;
      default:
        printf("%s", pMsg);
        break;
    }
  } else {
    printf("%s", pMsg);
  }

  if (console::get_crlf())
    printf("\n");

  if (INVALID_HANDLE_VALUE != cons)
    SetConsoleTextAttribute(cons, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);

  return true;
}
#else
bool colorized_console::console_output_func(eConsoleMessageType type, const char* pMsg, void*) {
  if (console::get_output_disabled())
    return true;

  if ((console::get_prefixes()) && (console::get_at_beginning_of_line())) {
    switch (type) {
      case cDebugConsoleMessage:
        printf("Debug: %s", pMsg);
        break;
      case cWarningConsoleMessage:
        printf("Warning: %s", pMsg);
        break;
      case cErrorConsoleMessage:
        printf("Error: %s", pMsg);
        break;
      default:
        printf("%s", pMsg);
        break;
    }
  } else {
    printf("%s", pMsg);
  }

  if (console::get_crlf())
    printf("\n");

  return true;
}
#endif

}  // namespace crnlib