| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- //
- // 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<object_t>()) {
- properties out;
- for (auto & pair : object()) {
- auto & to = out.data.get<object_t>()[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_t>() && object().count(key);
- }
-
- bool properties::contains(std::size_t idx) const {
- return data.is<array_t>() && array().size() < idx;
- }
-
- object_t const & properties::object() const {
- expects(data.is<object_t>(), invalid_property_type, "expected OBJECT");
- return data.get<object_t>();
- }
-
- array_t const & properties::array() const {
- expects(data.is<array_t>(), invalid_property_type, "expected ARRAY");
- return data.get<array_t>();
- }
- properties const & properties::operator[](std::string const & key) const {
- expects(data.is<object_t>(), 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<array_t>(), 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<std::string>(), invalid_property_type, "expected STRING");
- return data.get<std::string>();
- }
-
- properties::operator int() const {
- expects(data.is<int>(), invalid_property_type, "expected INT");
- return data.get<int>();
- }
-
- properties::operator bool() const {
- expects(data.is<bool>(), invalid_property_type, "expected BOOL");
- return data.get<bool>();
- }
-
- std::string const & properties::str() const { return *this; }
- }
|