Logo
Fully annotated reference manual - version 1.8.12
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Attributes | Protected Member Functions | List of all members
StructuredMessage Class Reference

#include <ored/utilities/log.hpp>

+ Inheritance diagram for StructuredMessage:
+ Collaboration diagram for StructuredMessage:

Public Types

enum class  Category { Error , Warning , Unknown }
 
enum class  Group {
  Analytics , Configuration , Model , Curve ,
  Trade , Fixing , Logging , ReferenceData ,
  Unknown
}
 

Public Member Functions

 StructuredMessage (const Category &category, const Group &group, const std::string &message, const std::map< std::string, std::string > &subFields=std::map< std::string, std::string >())
 
 StructuredMessage (const Category &category, const Group &group, const std::string &message, const std::pair< std::string, std::string > &subField=std::pair< std::string, std::string >())
 
virtual ~StructuredMessage ()
 
std::string msg () const
 return a std::string for the log file More...
 
void emitLog () const
 generate Boost log record to pass to corresponding sinks More...
 
- Public Member Functions inherited from JSONMessage
virtual ~JSONMessage ()
 
virtual std::string msg () const =0
 return a std::string for the log file More...
 
void log () const
 generate Boost log record to pass to corresponding sinks More...
 
const std::string json () const
 create JSON-like output from the data More...
 
void set (const std::string &key, const boost::any &value)
 

Static Public Attributes

static constexpr const char * name = "StructuredMessage"
 

Protected Member Functions

void addSubFields (const std::map< std::string, std::string > &)
 
virtual void emitLog () const =0
 generate Boost log record - this method is called by log() More...
 

Additional Inherited Members

- Static Protected Member Functions inherited from JSONMessage
static std::string jsonify (const boost::any &)
 
- Protected Attributes inherited from JSONMessage
std::map< std::string, boost::any > data_
 

Detailed Description

Definition at line 677 of file log.hpp.

Member Enumeration Documentation

◆ Category

enum class Category
strong

◆ Group

enum class Group
strong

Constructor & Destructor Documentation

◆ StructuredMessage() [1/2]

StructuredMessage ( const Category category,
const Group group,
const std::string &  message,
const std::map< std::string, std::string > &  subFields = std::map<std::string, std::string>() 
)

Definition at line 555 of file log.cpp.

556 {
557 data_["category"] = to_string(category);
558 data_["group"] = to_string(group);
559 data_["message"] = message;
560
561 if (!subFields.empty()) {
562 vector<boost::any> subFieldsVector;
563 bool addedSubField = false;
564 for (const auto& sf : subFields) {
565 if (!sf.second.empty()) {
566 map<string, boost::any> subField({{"name", sf.first}, {"value", sf.second}});
567 subFieldsVector.push_back(subField);
568 addedSubField = true;
569 }
570 }
571 if (addedSubField)
572 data_["sub_fields"] = subFieldsVector;
573 }
574}
std::map< std::string, boost::any > data_
Definition: log.hpp:651
std::string to_string(const LocationInfo &l)
Definition: ast.cpp:28
+ Here is the call graph for this function:

◆ StructuredMessage() [2/2]

StructuredMessage ( const Category category,
const Group group,
const std::string &  message,
const std::pair< std::string, std::string > &  subField = std::pair<std::string, std::string>() 
)

Definition at line 686 of file log.hpp.

688 : StructuredMessage(category, group, message, std::map<std::string, std::string>({subField})) {}
StructuredMessage(const Category &category, const Group &group, const std::string &message, const std::map< std::string, std::string > &subFields=std::map< std::string, std::string >())
Definition: log.cpp:555

◆ ~StructuredMessage()

virtual ~StructuredMessage ( )
virtual

Definition at line 690 of file log.hpp.

690{}

Member Function Documentation

◆ msg()

std::string msg ( ) const
virtual

return a std::string for the log file

Implements JSONMessage.

Definition at line 695 of file log.hpp.

695{ return std::string(name) + std::string(" ") + json(); }
const std::string json() const
create JSON-like output from the data
Definition: log.hpp:643
static constexpr const char * name
Definition: log.hpp:692
+ Here is the call graph for this function:

◆ emitLog()

void emitLog ( ) const
virtual

generate Boost log record to pass to corresponding sinks

Implements JSONMessage.

Definition at line 576 of file log.cpp.

576 {
577 lsrc::severity_logger_mt<oreSeverity> lg;
578 lg.add_attribute(messageType.get_name(), lattr::constant<string>(name));
579
580 auto it = data_.find("category");
581 QL_REQUIRE(it != data_.end(), "StructuredMessage must have a 'category' key specified.");
582 QL_REQUIRE(it->second.type() == typeid(string), "StructuredMessage category must be a string.");
583
584 string category = boost::any_cast<string>(it->second);
586 BOOST_LOG_SEV(lg, oreSeverity::warning) << json();
587 } else if (category == to_string(StructuredMessage::Category::Error)) {
588 BOOST_LOG_SEV(lg, oreSeverity::alert) << json();
589 } else {
590 QL_FAIL("StructuredMessage::log() invalid category '" << category << "'");
591 }
592}
+ Here is the call graph for this function:

◆ addSubFields()

void addSubFields ( const std::map< std::string, std::string > &  subFields)
protected

Definition at line 594 of file log.cpp.

594 {
595 if (!subFields.empty()) {
596
597 // First check that there is at least one non-empty subfield
598 bool hasNonEmptySubField = false;
599 for (const auto& sf : subFields) {
600 if (!sf.second.empty()) {
601 hasNonEmptySubField = true;
602 break;
603 }
604 }
605 if (!hasNonEmptySubField)
606 return;
607
608 if (data_.find("sub_fields") == data_.end()) {
609 data_["sub_fields"] = vector<boost::any>();
610 }
611
612 for (const auto& sf : subFields) {
613 if (!sf.second.empty()) {
614 map<string, boost::any> subField({{"name", sf.first}, {"value", sf.second}});
615 boost::any_cast<vector<boost::any>&>(data_.at("sub_fields")).push_back(subField);
616 }
617 }
618
619 }
620}

Member Data Documentation

◆ name

constexpr const char* name = "StructuredMessage"
staticconstexpr

Definition at line 692 of file log.hpp.