Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

initialization reorganization #6

Merged
merged 2 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ cmake_minimum_required(VERSION 3.20.0)
target_sources(vgraph
PRIVATE
main.cpp
arguments.cpp
manager.cpp
PUBLIC
arguments.h
manager.h
)

add_subdirectory(telemetry)
Expand Down
64 changes: 64 additions & 0 deletions src/arguments.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "arguments.h"
#include "utils/argument_parser.h"

namespace vgraph {
namespace key {
std::string help("help");
std::string debug("debug");
std::string telemetry("telemetry");
}

utils::argument_parser prepare_parser()
{
utils::argument_parser parser("vgraph");

parser.add_argument(key::help, utils::argument().flag().option("-h").option("--help").description("Print this help message"));
parser.add_argument(key::debug, utils::argument().flag().option("-d").option("--debug").description("Enable debug logs"));
parser.add_argument(key::telemetry, utils::argument().option("-t").option("--telemetry").description("Telemetry file path"));

return parser;
}

arguments read_args(utils::argument_parser parser, utils::logging::logger& log) {
arguments a;

a.debug = parser.get<bool>(key::debug);

if (parser.count(key::telemetry) != 1) {
log.error("Expected 1 telemetry file to be provided, got {}", parser.count(key::telemetry));
exit(1);
}
a.telemetry = parser.get<std::string>(key::telemetry);

return std::move(a);
}

arguments arguments::parse(int argc, char* argv[])
{
utils::logging::logger log{"arguments::parse()"};

try {
auto parser = prepare_parser();

parser.parse(argc, argv);
if (parser.get<bool>(key::help)) {
parser.print_help();
exit(0); //ok case
}

return std::move(read_args(parser, log));

} catch (utils::argument_exception e) {
log.error(e.what());
exit(1);
} catch (std::exception e) {
log.error("Unexpected error: {}", e.what());
exit(1);
}

//shall never reach
log.error("Execution error");
exit(2);
}

}
17 changes: 17 additions & 0 deletions src/arguments.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef ARGUMENTS_H
#define ARGUMENTS_H

#include <string>

namespace vgraph {

struct arguments {
bool debug = false;
std::string telemetry = "";

static arguments parse(int argc, char* argv[]);
};

} // namespace vgraph

#endif // ARGUMENTS_H
22 changes: 4 additions & 18 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,10 @@
#include <iostream>
#include "manager.h"

#include "utils/argument_parser.h"
#include <iostream>

int main(int argc, char* argv[])
{
vgraph::utils::argument_parser parser("vgraph");

{
using vgraph::utils::argument;
parser.add_argument("help", argument().option("-h").option("--help").flag().description("Print this help message"));
parser.add_argument("debug", argument().option("-d").option("--debug").flag().description("Enable debug logs"));
parser.add_argument("telemetry", argument().option("-t").option("--telemetry").description("Telemetry file path"));
}

parser.parse(argc, argv);

if (parser.get<bool>("help")) {
parser.print_help();
return 0;
}

vgraph::manager m;
m.init(argc, argv);
return 0;
}
29 changes: 29 additions & 0 deletions src/manager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include "manager.h"

#include "utils/logging/backend.h"
#include "utils/logging/stream_sink.h"

#include <iostream>

namespace vgraph {

void manager::init(int argc, char* argv[])
{
enable_logging();

args = arguments::parse(argc, argv);
set_log_level(args.debug ? utils::logging::ELogLevel::Debug : utils::logging::ELogLevel::Info);
}

void manager::enable_logging()
{
log_sink = std::make_shared<utils::logging::stream_sink>(std::cout);
utils::logging::backend::get_instance().add_sink(log_sink);
}

void manager::set_log_level(utils::logging::ELogLevel level)
{
utils::logging::backend::get_instance().set_log_level(log_sink, level);
}

} // namespace vgraph
29 changes: 29 additions & 0 deletions src/manager.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef MANAGER_H
#define MANAGER_H

#include "arguments.h"
#include "utils/logging/logger.h"
#include "utils/logging/sink.h"
#include "utils/logging/log_level.h"

namespace vgraph {

class manager {
public:
manager() = default;
void init(int argc, char* argv[]);

private:
void enable_logging();
void set_log_level(utils::logging::ELogLevel level);

utils::logging::logger log{"manager"};

std::shared_ptr<utils::logging::sink> log_sink;
arguments args;

};

} // namespace vgraph

#endif // MANAGER_H
17 changes: 15 additions & 2 deletions src/utils/argument_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ std::string to_upper(std::string str)
} // namespace helper

argument_exception::argument_exception(
const std::string& msg): msg_(msg) {}
const std::string& msg): msg_(std::format("Argument exception: {}", msg)) {}

const char* argument_exception::what() const noexcept
{
return std::format("Argument exception: {}", msg_).c_str();
return msg_.c_str();
}

argument& argument::option(const std::string& opt)
Expand Down Expand Up @@ -112,6 +112,14 @@ bool argument_parser::has(const std::string& key) const
return values_.contains(key);
}

int argument_parser::count(const std::string& key) const
{
if (!has(key))
return 0;

return values_.at(key).size();
}

template <>
std::string argument_parser::get(const std::string& key) const
{
Expand Down Expand Up @@ -146,6 +154,11 @@ std::vector<std::string> argument_parser::get(const std::string& key) const
return val;
}

std::map<std::string, std::vector<std::string>> argument_parser::get_raw() const
{
return values_;
}

template <>
bool argument_parser::get(const std::string& key) const
{
Expand Down
10 changes: 9 additions & 1 deletion src/utils/argument_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#include <map>
#include <optional>

#include "logging/logger.h"

namespace vgraph {
namespace utils {

Expand All @@ -33,20 +35,26 @@ class argument_parser {
public:
argument_parser(const std::string binary_name);
~argument_parser() = default;
argument_parser(const argument_parser&) = delete;
argument_parser(const argument_parser&) = default;
argument_parser(argument_parser&&) = default;
argument_parser& operator=(const argument_parser&) = default;
argument_parser& operator=(argument_parser&&) = default;

void add_argument(const std::string& key, const argument& arg);

void parse(int argc, char* argv[]);

bool has(const std::string& key) const;
int count(const std::string& key) const;

template <typename T>
T get(const std::string& key) const
{
throw argument_exception(std::format("Unsupported argument type requested for {} argument", key));
}

std::map<std::string, std::vector<std::string>> get_raw() const;

void print_help() const;

private:
Expand Down
10 changes: 9 additions & 1 deletion src/utils/logging/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,19 @@ const void backend::log(const std::string& logger_name, ELogLevel level, const s
}
}

void backend::add_sink(ELogLevel level, std::shared_ptr<sink> sink)
void backend::add_sink(std::shared_ptr<sink> sink, ELogLevel level)
{
sinks_.push_back(std::make_pair(level, sink));
}

void backend::set_log_level(std::shared_ptr<sink> sink, ELogLevel level)
{
std::for_each(sinks_.begin(), sinks_.end(), [sink, level](auto& s) {
if (s.second == sink)
s.first = level;
});
}

void backend::remove_sink(std::shared_ptr<sink> sink)
{
sinks_.erase(std::remove_if(sinks_.begin(), sinks_.end(), [sink](auto& s) {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/logging/backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class backend {

const void log(const std::string& logger_name, ELogLevel level, const std::string& msg);

void add_sink(ELogLevel level, std::shared_ptr<sink> sink);
void add_sink(std::shared_ptr<sink> sink, ELogLevel level = ELogLevel::Debug);
void set_log_level(std::shared_ptr<sink> sink, ELogLevel level);
void remove_sink(std::shared_ptr<sink> sink);

private:
Expand Down
1 change: 0 additions & 1 deletion test/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
int main(int argc, char **argv)
{
vgraph::utils::logging::backend::get_instance().add_sink(
vgraph::utils::logging::ELogLevel::Debug,
std::make_shared<vgraph::utils::logging::stream_sink>(std::cout));

testing::InitGoogleTest(&argc, argv);
Expand Down
16 changes: 12 additions & 4 deletions test/utils/argument_parser_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,21 @@ TEST_F(argument_parser_test, argument_without_options_throws)
EXPECT_THROW(uut.add_argument("test", argument()), argument_exception);
}

/* PARSING AND GETTING VALUES */

TEST_F(argument_parser_test, parsing_arguments)
{
argument_parser uut("binary");
uut.add_argument("help", argument().option("-h").option("--help").flag().description("help description"));
uut.add_argument("debug", argument().option("-d").option("--debug").flag().description("debug description"));
uut.add_argument("verbose", argument().option("-v").option("--verbose").flag().description("verbose description"));
uut.add_argument("file", argument().option("-f").description("file description"));
uut.add_argument("optional", argument().option("-o").option("--optional").description("optional description"));
}

/* PARSING AND GETTING VALUES */
create_args({"-d", "-f", "somefile", "-v"});
uut.parse(argc, argv);

auto args = uut.get_raw();
EXPECT_EQ(3, args.size());
}

TEST_F(argument_parser_test, string_value_parsing)
{
Expand All @@ -119,6 +125,8 @@ TEST_F(argument_parser_test, multiple_string_values_parsing)
uut.parse(argc, argv);

EXPECT_TRUE(uut.has("file"));
EXPECT_EQ(2, uut.count("file"));

EXPECT_TRUE(uut.get<bool>("file"));
EXPECT_THROW(uut.get<std::string>("file"), argument_exception);

Expand Down
Loading