15
15
#include " json.h"
16
16
#include " xml_expr.h"
17
17
#include " json_expr.h"
18
+ #include " json_stream.h"
18
19
#include " cout_message.h"
19
20
#include " cmdline.h"
20
21
22
+ ui_message_handlert::ui_message_handlert ()
23
+ : _ui(uit::PLAIN),
24
+ time(timestampert::make(timestampert::clockt::NONE)),
25
+ out(std::cout),
26
+ json_stream(nullptr )
27
+ {
28
+ }
29
+
21
30
ui_message_handlert::ui_message_handlert (
22
31
uit __ui,
23
32
const std::string &program,
24
33
timestampert::clockt clock_type)
25
- : _ui(__ui), time(timestampert::make(clock_type))
34
+ : _ui(__ui),
35
+ time(timestampert::make(clock_type)),
36
+ out(std::cout),
37
+ json_stream(nullptr )
26
38
{
27
- switch (__ui )
39
+ switch (_ui )
28
40
{
29
41
case uit::PLAIN:
30
42
break ;
31
43
32
44
case uit::XML_UI:
33
- std::cout << " <?xml version=\" 1.0\" encoding=\" UTF-8\" ?>" << " \n " ;
34
- std::cout << " <cprover>" << " \n " ;
45
+ out << " <?xml version=\" 1.0\" encoding=\" UTF-8\" ?>"
46
+ << " \n " ;
47
+ out << " <cprover>"
48
+ << " \n " ;
35
49
36
50
{
37
51
xmlt program_xml;
38
52
program_xml.name =" program" ;
39
53
program_xml.data =program;
40
54
41
- std::cout << program_xml;
55
+ out << program_xml;
42
56
}
43
57
break ;
44
58
45
59
case uit::JSON_UI:
46
60
{
47
- std::cout << " [\n " ;
48
- json_objectt json_program;
49
- json_program[" program" ] = json_stringt (program);
50
- std::cout << json_program;
61
+ if (!json_stream)
62
+ {
63
+ json_stream =
64
+ std::unique_ptr<json_stream_arrayt>(new json_stream_arrayt (out));
65
+ }
66
+
67
+ INVARIANT (json_stream, " JSON stream must be initialized before use" );
68
+ json_stream->push_back ().make_object ()[" program" ] = json_stringt (program);
51
69
}
52
70
break ;
53
71
}
@@ -76,11 +94,15 @@ ui_message_handlert::~ui_message_handlert()
76
94
switch (get_ui ())
77
95
{
78
96
case uit::XML_UI:
79
- std::cout << " </cprover>" << " \n " ;
97
+
98
+ out << " </cprover>"
99
+ << " \n " ;
80
100
break ;
81
101
82
102
case uit::JSON_UI:
83
- std::cout << " \n ]\n " ;
103
+ INVARIANT (json_stream, " JSON stream must be initialized before use" );
104
+ json_stream->close ();
105
+ out << ' \n ' ;
84
106
break ;
85
107
86
108
case uit::PLAIN:
@@ -140,7 +162,7 @@ void ui_message_handlert::print(
140
162
INVARIANT (false , " Cannot print xml data on PLAIN UI" );
141
163
break ;
142
164
case uit::XML_UI:
143
- std::cout << data << ' \n ' ;
165
+ out << data << ' \n ' ;
144
166
flush (level);
145
167
break ;
146
168
case uit::JSON_UI:
@@ -165,7 +187,8 @@ void ui_message_handlert::print(
165
187
INVARIANT (false , " Cannot print json data on XML UI" );
166
188
break ;
167
189
case uit::JSON_UI:
168
- std::cout << ' ,' << ' \n ' << data;
190
+ INVARIANT (json_stream, " JSON stream must be initialized before use" );
191
+ json_stream->push_back (data);
169
192
flush (level);
170
193
break ;
171
194
}
@@ -249,8 +272,8 @@ void ui_message_handlert::xml_ui_msg(
249
272
if (!timestamp.empty ())
250
273
result.set_attribute (" timestamp" , timestamp);
251
274
252
- std::cout << result;
253
- std::cout << ' \n ' ;
275
+ out << result;
276
+ out << ' \n ' ;
254
277
}
255
278
256
279
void ui_message_handlert::json_ui_msg (
@@ -259,7 +282,8 @@ void ui_message_handlert::json_ui_msg(
259
282
const std::string &msg2,
260
283
const source_locationt &location)
261
284
{
262
- json_objectt result;
285
+ INVARIANT (json_stream, " JSON stream must be initialized before use" );
286
+ json_objectt &result = json_stream->push_back ().make_object ();
263
287
264
288
if (location.is_not_nil () &&
265
289
!location.get_file ().empty ())
@@ -291,7 +315,7 @@ void ui_message_handlert::flush(unsigned level)
291
315
case uit::XML_UI:
292
316
case uit::JSON_UI:
293
317
{
294
- std::cout << std::flush;
318
+ out << std::flush;
295
319
}
296
320
break ;
297
321
}
0 commit comments