CBMC
Loading...
Searching...
No Matches
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 // messaget::quote_begin and messaget::quote_end render as a single
74 // quote on every UI; we agree with ui_message_handlert here on what
75 // '<' / '>' mean. If quote_begin / quote_end are ever re-encoded to
76 // values outside the SGR range (see comment in message.cpp), this
77 // arm needs to be updated in lock-step.
78 if(c == '<' || c == '>')
79 return "'";
80
81 if(!use_SGR)
82 return std::string();
83
84 return "\x1b[" + std::to_string(c) + 'm';
85}
86
88 unsigned level,
89 const std::string &message)
90{
91 message_handlert::print(level, message);
92
93 if(verbosity<level)
94 return;
95
96 #ifdef _WIN32
99
100 // We use UTF16 when we write to the console,
101 // but we write UTF8 otherwise.
102
105 {
106 // writing to the console
107 std::wstring wide_message=widen(message);
108
110
112 out_handle, wide_message.c_str(),
114
116 }
117 else
118 {
119 // writing to a file
120
121 if(level>=4)
122 {
123 std::cout << message << '\n';
124 }
125 else
126 std::cerr << message << '\n';
127 }
128 #else
129 // Messages level 3 or lower go to cerr, messages level 4 or
130 // above go to cout.
131
132 if(level>=4)
133 {
134 std::cout << message << '\n';
135 }
136 else
137 std::cerr << message << '\n';
138 #endif
139}
140
142{
143 // We flush after messages of level 6 or lower.
144 // We don't for messages of level 7 or higher to improve performance,
145 // in particular when writing to NFS.
146 if(level>=4)
147 {
148 if(level <= 6 || always_flush)
149 std::cout << std::flush;
150 }
151 else
152 std::cerr << std::flush;
153}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:566
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