Skip to content

Commit a68125c

Browse files
committed
Base JSON UI message handler upon json_streamt
1 parent 35ac459 commit a68125c

File tree

3 files changed

+73
-21
lines changed

3 files changed

+73
-21
lines changed

src/util/message.h

+15
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Author: Daniel Kroening, [email protected]
1919
#include "source_location.h"
2020
#include "xml.h"
2121

22+
class json_stream_arrayt;
23+
2224
class message_handlert
2325
{
2426
public:
@@ -33,6 +35,12 @@ class message_handlert
3335
// no-op by default
3436
}
3537

38+
/// Return the underlying JSON stream
39+
virtual json_stream_arrayt &get_json_stream()
40+
{
41+
UNREACHABLE;
42+
}
43+
3644
virtual void print(unsigned level, const jsont &json)
3745
{
3846
// no-op by default
@@ -232,6 +240,13 @@ class messaget
232240
return func(*this);
233241
}
234242

243+
/// Returns a reference to the top-level JSON array stream
244+
json_stream_arrayt &json_stream()
245+
{
246+
*this << eom; // force end of previous message
247+
return message.message_handler->get_json_stream();
248+
}
249+
235250
private:
236251
void assign_from(const mstreamt &other)
237252
{

src/util/ui_message.cpp

+41-17
Original file line numberDiff line numberDiff line change
@@ -15,39 +15,57 @@ Author: Daniel Kroening, [email protected]
1515
#include "json.h"
1616
#include "xml_expr.h"
1717
#include "json_expr.h"
18+
#include "json_stream.h"
1819
#include "cout_message.h"
1920
#include "cmdline.h"
2021

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+
2130
ui_message_handlert::ui_message_handlert(
2231
uit __ui,
2332
const std::string &program,
2433
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)
2638
{
27-
switch(__ui)
39+
switch(_ui)
2840
{
2941
case uit::PLAIN:
3042
break;
3143

3244
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";
3549

3650
{
3751
xmlt program_xml;
3852
program_xml.name="program";
3953
program_xml.data=program;
4054

41-
std::cout << program_xml;
55+
out << program_xml;
4256
}
4357
break;
4458

4559
case uit::JSON_UI:
4660
{
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);
5169
}
5270
break;
5371
}
@@ -76,11 +94,15 @@ ui_message_handlert::~ui_message_handlert()
7694
switch(get_ui())
7795
{
7896
case uit::XML_UI:
79-
std::cout << "</cprover>" << "\n";
97+
98+
out << "</cprover>"
99+
<< "\n";
80100
break;
81101

82102
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';
84106
break;
85107

86108
case uit::PLAIN:
@@ -140,7 +162,7 @@ void ui_message_handlert::print(
140162
INVARIANT(false, "Cannot print xml data on PLAIN UI");
141163
break;
142164
case uit::XML_UI:
143-
std::cout << data << '\n';
165+
out << data << '\n';
144166
flush(level);
145167
break;
146168
case uit::JSON_UI:
@@ -165,7 +187,8 @@ void ui_message_handlert::print(
165187
INVARIANT(false, "Cannot print json data on XML UI");
166188
break;
167189
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);
169192
flush(level);
170193
break;
171194
}
@@ -249,8 +272,8 @@ void ui_message_handlert::xml_ui_msg(
249272
if(!timestamp.empty())
250273
result.set_attribute("timestamp", timestamp);
251274

252-
std::cout << result;
253-
std::cout << '\n';
275+
out << result;
276+
out << '\n';
254277
}
255278

256279
void ui_message_handlert::json_ui_msg(
@@ -259,7 +282,8 @@ void ui_message_handlert::json_ui_msg(
259282
const std::string &msg2,
260283
const source_locationt &location)
261284
{
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();
263287

264288
if(location.is_not_nil() &&
265289
!location.get_file().empty())
@@ -291,7 +315,7 @@ void ui_message_handlert::flush(unsigned level)
291315
case uit::XML_UI:
292316
case uit::JSON_UI:
293317
{
294-
std::cout << std::flush;
318+
out << std::flush;
295319
}
296320
break;
297321
}

src/util/ui_message.h

+17-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@ Author: Daniel Kroening, [email protected]
1010
#ifndef CPROVER_UTIL_UI_MESSAGE_H
1111
#define CPROVER_UTIL_UI_MESSAGE_H
1212

13+
#include <memory>
14+
1315
#include "message.h"
16+
#include "json_stream.h"
1417
#include "timestamper.h"
1518

1619
class ui_message_handlert : public message_handlert
@@ -25,10 +28,8 @@ class ui_message_handlert : public message_handlert
2528

2629
ui_message_handlert(const class cmdlinet &, const std::string &program);
2730

28-
ui_message_handlert()
29-
: _ui(uit::PLAIN), time(timestampert::make(timestampert::clockt::NONE))
30-
{
31-
}
31+
/// Default constructor; implementation is in .cpp file
32+
ui_message_handlert();
3233

3334
virtual ~ui_message_handlert();
3435

@@ -40,13 +41,25 @@ class ui_message_handlert : public message_handlert
4041
void set_ui(uit __ui)
4142
{
4243
_ui=__ui;
44+
if(_ui == uit::JSON_UI && !json_stream)
45+
{
46+
json_stream =
47+
std::unique_ptr<json_stream_arrayt>(new json_stream_arrayt(out));
48+
}
4349
}
4450

4551
virtual void flush(unsigned level) override;
4652

53+
json_stream_arrayt &get_json_stream() override
54+
{
55+
return *json_stream;
56+
}
57+
4758
protected:
4859
uit _ui;
4960
std::unique_ptr<const timestampert> time;
61+
std::ostream &out;
62+
std::unique_ptr<json_stream_arrayt> json_stream;
5063

5164
virtual void print(
5265
unsigned level,

0 commit comments

Comments
 (0)