SWC-DB  v0.5.12 C++ documentations
SWC-DB© (Super Wide Column Database) - High Performance Scalable Database (https://github.com/kashirin-alex/swc-db)
main.cc
Go to the documentation of this file.
1 /*
2  * SWC-DB© Copyright since 2019 Alex Kashirin <kashirin.alex@gmail.com>
3  * License details at <https://github.com/kashirin-alex/swc-db/#license>
4  */
5 
6 
7 #include "swcdb/utils/Settings.h"
8 #include <dlfcn.h>
9 
10 namespace SWC { namespace Utils {
11 
12 
13 int run(const std::string& cmd, bool custom=false) {
14 
15  std::string lib_path(Env::Config::settings()->get_str("lib-path"));
16  if(custom) {
17  lib_path.append(Env::Config::settings()->get_str("lib"));
18  } else {
19  lib_path.reserve(lib_path.length() + 18 + cmd.length());
20  lib_path.append("libswcdb_utils_");
21  lib_path.append(cmd);
22  lib_path.append(SWC_DSO_EXT);// {lib-path}/libswcdb_utils_shell.so
23  }
24 
25  const char* err = dlerror();
26  void* handle = dlopen(lib_path.c_str(), RTLD_NOW | RTLD_LAZY | RTLD_LOCAL);
27  if (handle == nullptr || err != nullptr)
29  "Shared Lib %s, open fail: %s\n", lib_path.c_str(), err);
30 
31  err = dlerror();
32  std::string handler_name = "swcdb_utils_apply_cfg";
33  void* f_cfg_ptr = dlsym(handle, handler_name.c_str());
34  if(err || !f_cfg_ptr)
36  "Shared Lib %s, link(%s) fail: %s handle=%p\n",
37  lib_path.c_str(), handler_name.c_str(), err, handle);
38  reinterpret_cast<swcdb_utils_apply_cfg_t*>(f_cfg_ptr)(Env::Config::get());
39 
40  err = dlerror();
41  handler_name = "swcdb_utils_run";
42  void* f_new_ptr = dlsym(handle, handler_name.c_str());
43  if(err || !f_new_ptr)
45  "Shared Lib %s, link(%s) fail: %s handle=%p\n",
46  lib_path.c_str(), handler_name.c_str(), err, handle);
47  int res = reinterpret_cast<swcdb_utils_run_t*>(f_new_ptr)();
48 
49  SWC_CAN_QUICK_EXIT(res);
50  reinterpret_cast<swcdb_utils_apply_cfg_t*>(f_cfg_ptr)(nullptr);
51  dlclose(handle);
52  return res;
53 }
54 
55 
56 
57 int not_implemented(const std::string& cmd) {
58  SWC_LOGF(LOG_ERROR, "Not implemented, Utils command=%s", cmd.c_str());
59  return 1;
60 }
61 
62 }} // namespace SWC::Utils
63 
64 
65 
66 int main(int argc, char** argv) {
68  argc,
69  argv,
72  );
73 
74  int res;
75  {
76  const auto& command = SWC::Env::Config::settings()->get_str("command");
77 
78  if(SWC::Condition::str_case_eq(command.data(), "shell", command.size())) {
79  res = SWC::Utils::run(command);
81  command.data(), "custom", command.size())) {
82  res = SWC::Utils::run("command", true);
83  } else {
85  }
86  }
87 
89  return res;
90 }
main
int main(int argc, char **argv)
Definition: main.cc:68
SWC::Env::Config::reset
static void reset() noexcept
Definition: Settings.h:133
swcdb_utils_apply_cfg_t
void swcdb_utils_apply_cfg_t(SWC::Env::Config::Ptr env)
Definition: Settings.h:16
SWC_LOGF
#define SWC_LOGF(priority, fmt,...)
Definition: Logger.h:188
SWC::Env::Config::settings
static SWC::Config::Settings::Ptr & settings()
Definition: Settings.h:128
SWC::Config::init_app_options
void init_app_options(Settings *settings)
Definition: load_generator.cc:166
SWC::Env::Config::init
static SWC_SHOULD_NOT_INLINE void init(int argc, char **argv, SWC::Config::Settings::init_option_t init_app_options, SWC::Config::Settings::init_option_t init_post_cmd_args)
Definition: Settings.h:110
swcdb_utils_run_t
int swcdb_utils_run_t()
Definition: Settings.h:15
SWC::Utils::not_implemented
int not_implemented(const std::string &cmd)
Definition: main.cc:57
SWC
The SWC-DB C++ namespace 'SWC'.
Definition: main.cc:12
SWC_DSO_EXT
#define SWC_DSO_EXT
Definition: Compat.h:185
SWC::Env::Config::get
static Ptr & get()
Definition: Settings.h:122
SWC_CAN_QUICK_EXIT
#define SWC_CAN_QUICK_EXIT(_CODE_)
Definition: Compat.h:191
SWC_THROWF
#define SWC_THROWF(_code_, _fmt_,...)
Definition: Exception.h:136
Settings.h
SWC::LOG_ERROR
@ LOG_ERROR
Definition: Logger.h:32
SWC::Utils::run
int run(const std::string &cmd, bool custom=false)
Definition: main.cc:13
SWC::Error::CONFIG_BAD_VALUE
@ CONFIG_BAD_VALUE
Definition: Error.h:81
SWC::Config::init_post_cmd_args
void init_post_cmd_args(Settings *settings)
Definition: Settings.h:64
command
uint16_t command
Request command number.
Definition: Header.h:60
SWC::Condition::str_case_eq
bool str_case_eq(const char *s1, const char *s2, size_t count) noexcept SWC_ATTRIBS((SWC_ATTRIB_O3))
Definition: Comparators_basic.h:257