2424#include < boost/date_time/posix_time/posix_time.hpp>
2525#include < iomanip>
2626#include < ored/utilities/log.hpp>
27+ #include < ored/utilities/to_string.hpp>
2728#include < ql/errors.hpp>
2829
2930using namespace boost ::posix_time;
@@ -219,11 +220,11 @@ LoggerStream::~LoggerStream() {
219220}
220221
221222string StructuredMessage::json () const {
222- string msg =
223- " { \" category \" : \" " + category_ + " \" , \" group \" : \" " + group_ + " \" , " + " \" message \" : \" " + jsonify (message_) ;
223+ string msg = " { \" category \" : \" " + category_ + " \" , \" group \" : \" " + group_ + " \" , " + " \" message \" : \" " +
224+ jsonify (message_) + " \" " ;
224225
225226 if (!subFields_.empty ()) {
226- msg += " \" , \" sub_fields\" : [ " ;
227+ msg += " , \" sub_fields\" : [ " ;
227228 QuantLib::Size i = 0 ;
228229 for (const auto & p : subFields_) {
229230 // Only include subFields that are non-empty.
@@ -249,5 +250,50 @@ string StructuredMessage::jsonify(const string& s) const {
249250 boost::replace_all (str, " \n " , " \\ n" );
250251 return str;
251252}
253+
254+ string EventMessage::json () const {
255+ string msg = " { \" exception_message\" :\" " + jsonify (message_) + " \" " ;
256+
257+ if (!data_.empty ()) {
258+ msg += " , " ;
259+ QuantLib::Size i = 0 ;
260+ for (const auto & p : data_) {
261+ string value;
262+ if (p.second .type () == typeid (string)) {
263+ value = " \" " + boost::any_cast<string>(p.second ) + " \" " ;
264+ } else if (p.second .type () == typeid (boost::posix_time::ptime)) {
265+ auto time = boost::any_cast<boost::posix_time::ptime>(p.second );
266+ value = boost::posix_time::to_iso_extended_string (time);
267+ } else if (p.second .type () == typeid (int )) {
268+ value = to_string (boost::any_cast<int >(p.second ));
269+ } else if (p.second .type () == typeid (QuantLib::Real)) {
270+ value = to_string (boost::any_cast<QuantLib::Real>(p.second ));
271+ } else if (p.second .type () == typeid (bool )) {
272+ value = to_string (boost::any_cast<bool >(p.second ));
273+ } else {
274+ WLOG (StructuredMessage (" Error" , " Event Logging" , " Unrecognised value type for key '" + p.first + " '" ,
275+ std::pair<string, string>()));
276+ }
277+
278+ if (i > 0 )
279+ msg += " , " ;
280+ msg += " \" " + p.first + " \" : " + value;
281+ i++;
282+ }
283+ }
284+ msg += " }" ;
285+
286+ return msg;
287+ }
288+
289+ string EventMessage::jsonify (const string& s) const {
290+ string str = s;
291+ boost::replace_all (str, " \\ " , " \\\\ " ); // do this before the below otherwise we get \\"
292+ boost::replace_all (str, " \" " , " \\\" " );
293+ boost::replace_all (str, " \r " , " \\ r" );
294+ boost::replace_all (str, " \n " , " \\ n" );
295+ return str;
296+ }
297+
252298} // namespace data
253299} // namespace ore
0 commit comments