154 lines
4.2 KiB
C++
154 lines
4.2 KiB
C++
#ifndef CONFIG_H_
|
|
#define CONFIG_H_
|
|
|
|
#include "config.h"
|
|
#include "../os/os.h"
|
|
#include "ConfigurationState.h"
|
|
#include "lib/collections/Contains.h"
|
|
|
|
#include <google/protobuf/descriptor.h>
|
|
#include <google/protobuf/descriptor.pb.h>
|
|
#include <google/protobuf/dynamic_message.h>
|
|
#include <google/protobuf/text_format.h>
|
|
#include <string>
|
|
|
|
//#include <boost/thread.hpp>
|
|
|
|
namespace proto = google::protobuf;
|
|
|
|
namespace bts {
|
|
namespace config {
|
|
|
|
using std::string;
|
|
using std::int32_t;
|
|
using LogLevel = bts::log::Level;
|
|
|
|
internal::ConfigurationState config_state;
|
|
|
|
//////////////////////////////////////////
|
|
// Public API
|
|
//////////////////////////////////////////
|
|
|
|
extern "C" BTS_EXPORT void initialize(
|
|
const char *config_source, int32_t console_level, int32_t syslog_level) {
|
|
initialize(
|
|
string{config_source}, log::log_level_from_int32(console_level),
|
|
log::log_level_from_int32(syslog_level));
|
|
}
|
|
|
|
void initialize(
|
|
const string &config_source,
|
|
LogLevel console_level,
|
|
LogLevel syslog_level) {
|
|
config_state.initialize_settings(config_source, console_level, syslog_level);
|
|
}
|
|
|
|
extern "C" BTS_EXPORT bool initialize_nothrow(
|
|
const char *config_source,
|
|
int32_t console_level,
|
|
int32_t syslog_level,
|
|
char *error_message,
|
|
std::size_t length) {
|
|
try {
|
|
initialize(config_source, console_level, syslog_level);
|
|
} catch (std::exception &e) {
|
|
std::string msg = e.what();
|
|
if (msg.size() < length) {
|
|
os::strncpy(error_message, msg.c_str(), length);
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// extern "C"
|
|
const proto::Message &
|
|
get_message(const google::protobuf::Descriptor *descriptor) {
|
|
return config_state.get_message(descriptor);
|
|
}
|
|
|
|
std::vector<const google::protobuf::FieldDescriptor *> get_fields(
|
|
const std::vector<std::string> keys,
|
|
const std::vector<std::string> excluded_keys) {
|
|
return config_state.get_fields(keys, excluded_keys);
|
|
}
|
|
|
|
std::vector<const google::protobuf::Message *> get_messages(
|
|
const std::vector<std::string> keys,
|
|
const std::vector<std::string> excluded_keys,
|
|
std::vector<std::string> *unknown_keys) {
|
|
return config_state.get_messages(keys, excluded_keys, unknown_keys);
|
|
}
|
|
|
|
namespace interop {
|
|
|
|
//////////////////////////////////////////////////
|
|
// Functions used for interop to other languages
|
|
/////////////////////////////////////////////////
|
|
|
|
std::string getbuffer_serialized_buffer;
|
|
extern "C" BTS_EXPORT const char *
|
|
get_buffer(const char *key_name, int &out_length) {
|
|
google::protobuf::DescriptorPool const *pool =
|
|
google::protobuf::DescriptorPool::generated_pool();
|
|
const proto::Descriptor *message_desc =
|
|
pool->FindMessageTypeByName(key_name);
|
|
|
|
const proto::Message &message = get_message(message_desc);
|
|
|
|
getbuffer_serialized_buffer.clear();
|
|
message.SerializePartialToString(&getbuffer_serialized_buffer);
|
|
|
|
out_length = getbuffer_serialized_buffer.size();
|
|
return getbuffer_serialized_buffer.c_str();
|
|
}
|
|
|
|
extern "C" BTS_EXPORT const proto::Message *get_named(const char *key_name) {
|
|
return config_state.try_get_message_or_default(key_name);
|
|
}
|
|
|
|
BTS_EXPORT std::string get_macro_string(const char *name) {
|
|
return config_state.get_macro_string(name);
|
|
}
|
|
|
|
} // namespace interop
|
|
|
|
namespace internal {
|
|
|
|
//////////////////////////////////////////
|
|
// Functions used in testing
|
|
//////////////////////////////////////////
|
|
|
|
extern "C" BTS_EXPORT void initialize_for_test(
|
|
const char *config, int32_t console_level, int32_t syslog_level) {
|
|
initialize_for_test(
|
|
string(config), bts::log::log_level_from_int32(console_level),
|
|
bts::log::log_level_from_int32(syslog_level));
|
|
}
|
|
|
|
void initialize_for_test(
|
|
const string &values, LogLevel console_level, LogLevel syslog_level) {
|
|
config_state.reset();
|
|
config_state.load_string(values, console_level, syslog_level);
|
|
}
|
|
|
|
extern "C" BTS_EXPORT void reset() { config_state.reset(); }
|
|
|
|
extern "C" BTS_EXPORT bool check_rules(char *error_msg, size_t len) {
|
|
try {
|
|
config_state.check_rules();
|
|
} catch (std::exception &e) {
|
|
std::string msg = e.what();
|
|
if (msg.size() < len) {
|
|
os::strncpy(error_msg, msg.c_str(), len);
|
|
}
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
} // namespace internal
|
|
} // namespace config
|
|
} // namespace bts
|
|
#endif
|