CBMC
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
cout_message.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module:
4
5Author: Daniel Kroening, kroening@kroening.com
6
7\*******************************************************************/
8
9#include "cout_message.h"
10
11#include <iostream>
12
13// clang-format off
14// clang-format must not re-order includes here to avoid pragma_push/pragma_pop
15// being moved around
16#ifdef _WIN32
17#include <util/pragma_push.def>
18#ifdef _MSC_VER
19#pragma warning(disable:4668)
20 // using #if/#elif on undefined macro
21#pragma warning(disable : 5039)
22// pointer or reference to potentially throwing function passed to extern C
23#endif
24#include <windows.h>
25#include <fcntl.h>
26#include <io.h>
27#include <cstdio>
28#include <util/pragma_pop.def>
29#include "unicode.h"
30// clang-format on
31#else
32#include <unistd.h>
33#endif
34
39
42{
43}
44
46 : always_flush(_always_flush), is_a_tty(false), use_SGR(false)
47{
48#ifdef _WIN32
50
53 {
54 is_a_tty = true;
55
56#ifdef ENABLE_VIRTUAL_TERMINAL_PROCESSING
59 use_SGR = true;
60#endif
61 }
62#else
65#endif
66}
67
71std::string console_message_handlert::command(unsigned c) const
72{
73 if(!use_SGR)
74 return std::string();
75
76 return "\x1b[" + std::to_string(c) + 'm';
77}
78
80 unsigned level,
81 const std::string &message)
82{
83 message_handlert::print(level, message);
84
85 if(verbosity<level)
86 return;
87
88 #ifdef _WIN32
91
92 // We use UTF16 when we write to the console,
93 // but we write UTF8 otherwise.
94
97 {
98 // writing to the console
99 std::wstring wide_message=widen(message);
100
102
104 out_handle, wide_message.c_str(),
106
108 }
109 else
110 {
111 // writing to a file
112
113 if(level>=4)
114 {
115 std::cout << message << '\n';
116 }
117 else
118 std::cerr << message << '\n';
119 }
120 #else
121 // Messages level 3 or lower go to cerr, messages level 4 or
122 // above go to cout.
123
124 if(level>=4)
125 {
126 std::cout << message << '\n';
127 }
128 else
129 std::cerr << message << '\n';
130 #endif
131}
132
134{
135 // We flush after messages of level 6 or lower.
136 // We don't for messages of level 7 or higher to improve performance,
137 // in particular when writing to NFS.
138 if(level>=4)
139 {
140 if(level <= 6 || always_flush)
141 std::cout << std::flush;
142 }
143 else
144 std::cerr << std::flush;
145}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
bool use_SGR
true if we use ECMA-48 SGR to render colors
bool is_a_tty
true if we are outputting to a proper console
std::string command(unsigned c) const override
Create an ECMA-48 SGR (Select Graphic Rendition) command with given code.
void print(unsigned, const xmlt &) override
virtual void flush(unsigned level) override
virtual void print(unsigned level, const std::string &message)=0
Definition message.cpp:60
unsigned verbosity
Definition message.h:71
STL namespace.
std::wstring widen(const char *s)
Definition unicode.cpp:49