diff --git a/.gitignore b/.gitignore index d864b520d..652408787 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ obj/ msbuild.log *.orig vcxproj.user +launchSettings.json diff --git a/src/lib/config/ConfigCheck.cpp b/src/lib/config/ConfigCheck.cpp new file mode 100644 index 000000000..87c43ef7c --- /dev/null +++ b/src/lib/config/ConfigCheck.cpp @@ -0,0 +1,108 @@ +#include "config.h" +#include "lib/utils/bts_export.h" +#include "lib/utils/TimeStamp.h" + +#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; + } +} \ No newline at end of file