properties.cxx 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // properties.cxx
  3. // logging
  4. //
  5. // Created by Sam Jaffe on 4/1/19.
  6. //
  7. #include "logger/properties.h"
  8. #include "../../../../paradigm/declarative/expect/include/expect/expect.hpp"
  9. #include "logger/exception.h"
  10. /*
  11. {
  12. {"pattern",{{},"%d{%h:%M:%s.%_ms} [%t] %-5.5p %.36c - %m%n"}}
  13. },
  14. "PatternLayout"
  15. }
  16. */
  17. using namespace logging::property;
  18. namespace logging {
  19. /*
  20. * %d{%h:%M:%s.%_ms} [%t] %-5.5p %.36c - %m%n
  21. *
  22. * %d{fmt} := date(format)
  23. * %t := thread
  24. * %-5.5p := level (max 5char, align left)
  25. * %.36c := logsource (max 36char)
  26. * %m := log message
  27. * %n := platform-specific line separator
  28. *
  29. */
  30. extern properties const DEFAULT_APPENDER_SCHEMA;
  31. extern properties const DEFAULT_LOGGER_SCHEMA;
  32. properties const DEFAULT_APPENDER_SCHEMA{_obj({
  33. {"appenders", _obj({
  34. {"Console", _obj({
  35. {"target", _v("SYSTEM_OUT")},
  36. {"PatternLayout", _obj({
  37. {"pattern", _v("%d{%h:%M:%s.%_ms} [%t] %-5.5p %.36c - %m%n")}
  38. })}
  39. })}
  40. })}
  41. })};
  42. properties const DEFAULT_LOGGER_SCHEMA{_obj({
  43. {"loggers", _obj({
  44. {"root", _obj({
  45. {"level", _v("Error")},
  46. {"appenders", _obj({
  47. {"ref", _v("Console")}
  48. })}
  49. })}
  50. })}
  51. })};
  52. static properties mergeKey(std::string const & key,
  53. properties const & defVal,
  54. properties const & other) {
  55. return other.contains(key) ? defVal.mergedWith(other[key]) : defVal;
  56. }
  57. properties properties::mergedWith(properties const & other) const {
  58. if (data.is<object_t>()) {
  59. properties out;
  60. for (auto & pair : object()) {
  61. auto & to = out.data.get<object_t>()[pair.first];
  62. to = mergeKey(pair.first, pair.second, other);
  63. }
  64. return out;
  65. } else {
  66. return other.data.valid() ? other : *this;
  67. }
  68. }
  69. bool properties::contains(std::string const & key) const {
  70. return data.is<object_t>() && object().count(key);
  71. }
  72. bool properties::contains(std::size_t idx) const {
  73. return data.is<array_t>() && array().size() < idx;
  74. }
  75. object_t const & properties::object() const {
  76. expects(data.is<object_t>(), invalid_property_type, "expected OBJECT");
  77. return data.get<object_t>();
  78. }
  79. array_t const & properties::array() const {
  80. expects(data.is<array_t>(), invalid_property_type, "expected ARRAY");
  81. return data.get<array_t>();
  82. }
  83. properties const & properties::operator[](std::string const & key) const {
  84. expects(data.is<object_t>(), invalid_property_type, "expected OBJECT");
  85. expects(contains(key), missing_property, "Missing key: " + key);
  86. return object().at(key);
  87. }
  88. properties const & properties::operator[](std::size_t idx) const {
  89. expects(data.is<array_t>(), invalid_property_type, "expected ARRAY");
  90. expects(contains(idx), missing_property,
  91. "Out of bounds: " + std::to_string(idx));
  92. return array().at(idx);
  93. }
  94. properties::operator std::string const &() const {
  95. expects(data.is<std::string>(), invalid_property_type, "expected STRING");
  96. return data.get<std::string>();
  97. }
  98. properties::operator int() const {
  99. expects(data.is<int>(), invalid_property_type, "expected INT");
  100. return data.get<int>();
  101. }
  102. properties::operator bool() const {
  103. expects(data.is<bool>(), invalid_property_type, "expected BOOL");
  104. return data.get<bool>();
  105. }
  106. std::string const & properties::str() const { return *this; }
  107. }