// // properties.cxx // logging // // Created by Sam Jaffe on 4/1/19. // #include "logger/properties.h" #include "../../../../paradigm/declarative/expect/include/expect/expect.hpp" #include "logger/exception.h" /* { {"pattern",{{},"%d{%h:%M:%s.%_ms} [%t] %-5.5p %.36c - %m%n"}} }, "PatternLayout" } */ using namespace logging::property; namespace logging { /* * %d{%h:%M:%s.%_ms} [%t] %-5.5p %.36c - %m%n * * %d{fmt} := date(format) * %t := thread * %-5.5p := level (max 5char, align left) * %.36c := logsource (max 36char) * %m := log message * %n := platform-specific line separator * */ extern properties const DEFAULT_APPENDER_SCHEMA; extern properties const DEFAULT_LOGGER_SCHEMA; properties const DEFAULT_APPENDER_SCHEMA{_obj({ {"appenders", _obj({ {"Console", _obj({ {"target", _v("SYSTEM_OUT")}, {"PatternLayout", _obj({ {"pattern", _v("%d{%h:%M:%s.%_ms} [%t] %-5.5p %.36c - %m%n")} })} })} })} })}; properties const DEFAULT_LOGGER_SCHEMA{_obj({ {"loggers", _obj({ {"root", _obj({ {"level", _v("Error")}, {"appenders", _obj({ {"ref", _v("Console")} })} })} })} })}; static properties mergeKey(std::string const & key, properties const & defVal, properties const & other) { return other.contains(key) ? defVal.mergedWith(other[key]) : defVal; } properties properties::mergedWith(properties const & other) const { if (data.is()) { properties out; for (auto & pair : object()) { auto & to = out.data.get()[pair.first]; to = mergeKey(pair.first, pair.second, other); } return out; } else { return other.data.valid() ? other : *this; } } bool properties::contains(std::string const & key) const { return data.is() && object().count(key); } bool properties::contains(std::size_t idx) const { return data.is() && array().size() < idx; } object_t const & properties::object() const { expects(data.is(), invalid_property_type, "expected OBJECT"); return data.get(); } array_t const & properties::array() const { expects(data.is(), invalid_property_type, "expected ARRAY"); return data.get(); } properties const & properties::operator[](std::string const & key) const { expects(data.is(), invalid_property_type, "expected OBJECT"); expects(contains(key), missing_property, "Missing key: " + key); return object().at(key); } properties const & properties::operator[](std::size_t idx) const { expects(data.is(), invalid_property_type, "expected ARRAY"); expects(contains(idx), missing_property, "Out of bounds: " + std::to_string(idx)); return array().at(idx); } properties::operator std::string const &() const { expects(data.is(), invalid_property_type, "expected STRING"); return data.get(); } properties::operator int() const { expects(data.is(), invalid_property_type, "expected INT"); return data.get(); } properties::operator bool() const { expects(data.is(), invalid_property_type, "expected BOOL"); return data.get(); } std::string const & properties::str() const { return *this; } }