Skip to content

Commit 3a1cfd4

Browse files
Base JSON UI message handler upon json_streamt
1 parent 7c41491 commit 3a1cfd4

File tree

3 files changed

+53
-26
lines changed

3 files changed

+53
-26
lines changed

src/util/message.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Author: Daniel Kroening, [email protected]
1616

1717
#include "invariant.h"
1818
#include "json.h"
19+
#include "json_stream.h"
1920
#include "source_location.h"
2021
#include "xml.h"
2122

@@ -38,6 +39,11 @@ class message_handlert
3839
// no-op by default
3940
}
4041

42+
virtual json_stream_arrayt &get_json_stream()
43+
{
44+
UNREACHABLE;
45+
}
46+
4147
virtual void print(
4248
unsigned level,
4349
const std::string &message,
@@ -232,6 +238,12 @@ class messaget
232238
return func(*this);
233239
}
234240

241+
json_stream_arrayt &json_stream()
242+
{
243+
*this << eom; // force end of previous message
244+
return message.message_handler->get_json_stream();
245+
}
246+
235247
private:
236248
void assign_from(const mstreamt &other)
237249
{

src/util/ui_message.cpp

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,36 @@ 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

2122
ui_message_handlert::ui_message_handlert(
22-
uit __ui, const std::string &program):_ui(__ui)
23+
uit __ui, const std::string &program):ui_message_handlert()
2324
{
24-
switch(__ui)
25+
set_ui(__ui);
26+
switch(_ui)
2527
{
2628
case uit::PLAIN:
2729
break;
2830

2931
case uit::XML_UI:
30-
std::cout << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << "\n";
31-
std::cout << "<cprover>" << "\n";
32+
out << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" << "\n";
33+
out << "<cprover>" << "\n";
3234

3335
{
3436
xmlt program_xml;
3537
program_xml.name="program";
3638
program_xml.data=program;
3739

38-
std::cout << program_xml;
40+
out << program_xml;
3941
}
4042
break;
4143

4244
case uit::JSON_UI:
4345
{
44-
std::cout << "[\n";
45-
json_objectt json_program;
46-
json_program["program"] = json_stringt(program);
47-
std::cout << json_program;
46+
INVARIANT(json_stream, "JSON stream must be initialized before use");
47+
json_stream->push_back().make_object()["program"] = json_stringt(program);
4848
}
4949
break;
5050
}
@@ -61,16 +61,24 @@ ui_message_handlert::ui_message_handlert(
6161
{
6262
}
6363

64+
ui_message_handlert::ui_message_handlert():
65+
_ui(uit::PLAIN), out(std::cout), json_stream(nullptr)
66+
{
67+
}
68+
6469
ui_message_handlert::~ui_message_handlert()
6570
{
6671
switch(get_ui())
6772
{
6873
case uit::XML_UI:
69-
std::cout << "</cprover>" << "\n";
74+
75+
out << "</cprover>" << "\n";
7076
break;
7177

7278
case uit::JSON_UI:
73-
std::cout << "\n]\n";
79+
INVARIANT(json_stream, "JSON stream must be initialized before use");
80+
json_stream->close();
81+
out << '\n';
7482
break;
7583

7684
case uit::PLAIN:
@@ -127,7 +135,7 @@ void ui_message_handlert::print(
127135
INVARIANT(false, "Cannot print xml data on PLAIN UI");
128136
break;
129137
case uit::XML_UI:
130-
std::cout << data << '\n';
138+
out << data << '\n';
131139
flush(level);
132140
break;
133141
case uit::JSON_UI:
@@ -152,7 +160,8 @@ void ui_message_handlert::print(
152160
INVARIANT(false, "Cannot print json data on XML UI");
153161
break;
154162
case uit::JSON_UI:
155-
std::cout << ',' << '\n' << data;
163+
INVARIANT(json_stream, "JSON stream must be initialized before use");
164+
json_stream->push_back(data);
156165
flush(level);
157166
break;
158167
}
@@ -233,8 +242,8 @@ void ui_message_handlert::xml_ui_msg(
233242
result.new_element("text").data=msg1;
234243
result.set_attribute("type", type);
235244

236-
std::cout << result;
237-
std::cout << '\n';
245+
out << result;
246+
out << '\n';
238247
}
239248

240249
void ui_message_handlert::json_ui_msg(
@@ -243,19 +252,15 @@ void ui_message_handlert::json_ui_msg(
243252
const std::string &msg2,
244253
const source_locationt &location)
245254
{
246-
json_objectt result;
255+
INVARIANT(json_stream, "JSON stream must be initialized before use");
256+
json_objectt &result = json_stream->push_back().make_object();
247257

248258
if(location.is_not_nil() &&
249259
!location.get_file().empty())
250260
result["sourceLocation"] = json(location);
251261

252262
result["messageType"] = json_stringt(type);
253263
result["messageText"] = json_stringt(msg1);
254-
255-
// By convention a leading comma is created by every new array entry.
256-
// The first entry is generated in the constructor and does not have
257-
// a trailing comma.
258-
std::cout << ",\n" << result;
259264
}
260265

261266
void ui_message_handlert::flush(unsigned level)
@@ -272,7 +277,7 @@ void ui_message_handlert::flush(unsigned level)
272277
case uit::XML_UI:
273278
case uit::JSON_UI:
274279
{
275-
std::cout << std::flush;
280+
out << std::flush;
276281
}
277282
break;
278283
}

src/util/ui_message.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@ Author: Daniel Kroening, [email protected]
1212

1313
#include "message.h"
1414

15+
class json_stream_arrayt;
16+
1517
class ui_message_handlert:public message_handlert
1618
{
1719
public:
1820
enum class uit { PLAIN, XML_UI, JSON_UI };
1921

2022
ui_message_handlert(uit, const std::string &program);
2123
ui_message_handlert(const class cmdlinet &, const std::string &program);
22-
ui_message_handlert():
23-
_ui(uit::PLAIN)
24-
{
25-
}
24+
ui_message_handlert();
2625

2726
virtual ~ui_message_handlert();
2827

@@ -34,12 +33,23 @@ class ui_message_handlert:public message_handlert
3433
void set_ui(uit __ui)
3534
{
3635
_ui=__ui;
36+
if(_ui == uit::JSON_UI && !json_stream)
37+
{
38+
json_stream = std::unique_ptr<json_stream_arrayt>(new json_stream_arrayt(out));
39+
}
3740
}
3841

3942
virtual void flush(unsigned level) override;
4043

44+
json_stream_arrayt &get_json_stream() override
45+
{
46+
return *json_stream;
47+
}
48+
4149
protected:
4250
uit _ui;
51+
std::ostream &out;
52+
std::unique_ptr<json_stream_arrayt> json_stream;
4353

4454
virtual void print(
4555
unsigned level,

0 commit comments

Comments
 (0)