#include "config.h" #include "ConfigCheck.h" #include "lib/utils/bts_export.h" #include "lib/utils/TimeStamp.h" #include #include #include #include #include #include namespace proto = google::protobuf; namespace bts { namespace config { namespace interop { std::string get_macro_string(const char* name); } } } using namespace bts; void fill_in_defaults(google::protobuf::Message* msg) { const proto::Descriptor* d = msg->GetDescriptor(); const proto::Reflection* r = msg->GetReflection(); for (int x = 0; x < d->field_count(); ++x) { auto* fd = d->field(x); auto type = fd->cpp_type(); if (!fd->is_repeated()) { if (type == proto::FieldDescriptor::CPPTYPE_MESSAGE) { fill_in_defaults(r->MutableMessage(msg, fd)); } else if (fd->has_default_value() && !r->HasField(*msg, fd)) { switch (type) { case proto::FieldDescriptor::CPPTYPE_INT32: r->SetInt32(msg, fd, fd->default_value_int32()); break; case proto::FieldDescriptor::CPPTYPE_INT64: r->SetInt64(msg, fd, fd->default_value_int64()); break; case proto::FieldDescriptor::CPPTYPE_UINT32: r->SetUInt32(msg, fd, fd->default_value_uint32()); break; case proto::FieldDescriptor::CPPTYPE_UINT64: r->SetUInt64(msg, fd, fd->default_value_uint64()); break; case proto::FieldDescriptor::CPPTYPE_DOUBLE: r->SetDouble(msg, fd, fd->default_value_double()); break; case proto::FieldDescriptor::CPPTYPE_FLOAT: r->SetFloat(msg, fd, fd->default_value_float()); break; case proto::FieldDescriptor::CPPTYPE_BOOL: r->SetBool(msg, fd, fd->default_value_bool()); break; case proto::FieldDescriptor::CPPTYPE_ENUM: r->SetEnum(msg, fd, fd->default_value_enum()); break; case proto::FieldDescriptor::CPPTYPE_STRING: r->SetString(msg, fd, fd->default_value_string()); break; default: break; } } } } } extern "C" BTS_EXPORT void configcheck(int argc, const char** argv) { std::vector args(argv, argv+argc); char buffer[4096] = {0}; utils::TimeStamp start = utils::TimeStamp::now(); bool success = config::initialize_nothrow(buffer, sizeof(buffer)); double time = (utils::TimeStamp::now() - start).seconds(); if (success) { success = config::internal::check_rules(buffer, sizeof(buffer)); } if (!success) { std::cout << buffer << std::endl; } std::cout << "Load time> " << std::floor(time * 1000) << " ms" << std::endl; for (size_t x = 0; x < args.size(); x++) { std::string key = args[x]; if (key[0] == '$') { // macro std::string result = config::interop::get_macro_string(key.c_str()); std::cout << key << ": " << result; } else { const google::protobuf::Message* message = config::interop::get_named(key.c_str()); if (message) { google::protobuf::Message* mut = message->New(); mut->CopyFrom(*message); if (args.size() > 1) std::cout << key << ":" << std::endl; fill_in_defaults(mut); mut->PrintDebugString(); } else { std::cout << "Cannot find a config key by the name: " << key; } } std::cout << std::endl; } }