Skip to content

Commit

Permalink
add spdlog
Browse files Browse the repository at this point in the history
  • Loading branch information
dan4ik605743 committed Feb 15, 2023
1 parent 5eea726 commit 2ae158a
Show file tree
Hide file tree
Showing 13 changed files with 89 additions and 50 deletions.
6 changes: 5 additions & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
[submodule "modules/fmt"]
path = modules/fmt
url = https://github.com/fmtlib/fmt
url = https://github.com/dan4ik605743/fmt

[submodule "modules/spdlog"]
path = modules/spdlog
url = https://github.com/gabime/spdlog
2 changes: 2 additions & 0 deletions modules/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,6 @@ function(initialize_submodule DIRECTORY)
endfunction(initialize_submodule)

initialize_submodule(modules/fmt)
initialize_submodule(modules/spdlog)
add_subdirectory(fmt)
add_subdirectory(spdlog)
1 change: 1 addition & 0 deletions modules/spdlog
Submodule spdlog added at da1425
3 changes: 2 additions & 1 deletion sqcli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ file(GLOB_RECURSE SRC_LIST *.cpp)

set(
LIB_LIST
PRIVATE fmt
PRIVATE fmt::fmt-header-only
PRIVATE spdlog::spdlog_header_only
${SQLite3_LIBRARIES}
${Boost_LIBRARIES}
)
Expand Down
2 changes: 1 addition & 1 deletion sqcli/enum.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ inline void insert_value_to_enum(std::uint8_t value, T& enum_obj) {
return;
}

throw std::invalid_argument("SQCli error: unknown choice selected\n");
throw std::invalid_argument("Unknown choice selected");
}

#endif // ENUM_HPP
85 changes: 51 additions & 34 deletions sqcli/io_controller/io_controller.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include "io_controller/io_controller.hpp"

#include <fmt/core.h>
#include <fmt/format.h>
#include <boost/process.hpp>

#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>

#include <exception>
#include <iostream>

Expand All @@ -19,6 +22,18 @@ void refresh_stdin() {
}
} // namespace

namespace sqcli {
io_controller::io_controller() noexcept {
const auto sqcli_log = spdlog::stdout_color_mt("SQCli");
const auto sql_log = spdlog::stdout_color_mt("SQL");

// disable eol
sqcli_log->set_formatter(std::make_unique<spdlog::pattern_formatter>(
"[%n] [%^%l%$] %v", spdlog::pattern_time_type::local, std::string("")));
sql_log->set_formatter(std::make_unique<spdlog::pattern_formatter>(
"[%n] [%^%l%$] %v", spdlog::pattern_time_type::local, std::string("")));
}

void io_controller::menu() {
if (path_db_.empty()) {
clrscr();
Expand All @@ -31,6 +46,14 @@ void io_controller::menu() {
}
}

if (std::cin.eof()) {
clrscr();
spdlog::get("SQCli")->info("Exit successfully\n\n");

on_exit_();
return;
}

fmt::print(
"Выберите действие которое хотите выполнить над '{}'\n\n "
"1)\tВыбрать "
Expand All @@ -47,16 +70,10 @@ void io_controller::menu() {
"12)\tПереименовать поле из таблицы\n "
"13)\tВыполнить SQL команду вручную\n "
"14)\tЗавершить работу "
"с базой данных\n\nВыбранная таблица: '{}'\n\n: ",
path_db_, name_table_.empty() ? "NONE" : name_table_);

if (std::cin.eof()) {
clrscr();
fmt::print("SQCli: exit successfully\n");

on_exit_();
return;
}
"с базой данных\n\n",
path_db_);
spdlog::get("SQCli")->info("Выбранная таблица: '{}'\n\n: ",
name_table_.empty() ? "NONE" : name_table_);

try {
if (std::cin.fail()) {
Expand All @@ -69,7 +86,7 @@ void io_controller::menu() {
}
} catch (const std::exception& ex) {
clrscr();
fmt::print(stderr, "{}\n", ex.what());
spdlog::get("SQCli")->error("{}\n\n", ex.what());
return;
}

Expand Down Expand Up @@ -116,12 +133,10 @@ void io_controller::menu() {
break;
case menu_options::ON_EXIT:
clrscr();
fmt::print("SQCli: exit successfully\n");
spdlog::get("SQCli")->info("Exit successfully\n\n");
on_exit_();
break;
default:
clrscr();
fmt::print("SQCli error: unknown method selected\n\n");
break;
}
}
Expand All @@ -139,11 +154,11 @@ void io_controller::cli_command_process() {

if (obj.empty()) {
clrscr();
fmt::print("Вывод команды:\n\n NONE\n\n");
spdlog::get("SQCli")->info("Вывод команды:\n\n NONE\n\n");
status_check_process();
} else {
clrscr();
fmt::print("Вывод команды:\n\n");
spdlog::get("SQCli")->info("Вывод команды:\n\n");
const auto size_obj = obj.size();
for (std::size_t i = 0; i != size_obj; ++i) {
for (const auto& [field_name, argv] : obj.at(i)) {
Expand All @@ -161,16 +176,16 @@ void io_controller::cli_command_process() {

bool io_controller::status_check_process(bool open_database) {
if (open_database && on_status_check_()) {
fmt::print("SQL: open database successfully\n\n");
spdlog::get("SQL")->info("Open database successfully\n\n");
return true;
}

if (on_status_check_()) {
fmt::print("SQL: successfully\n\n");
spdlog::get("SQL")->info("Successfully\n\n");
return true;
}

fmt::print(stderr, "SQL error: {}\n\n", sqlite3_errmsg(on_get_db_()));
spdlog::get("SQL")->error("{}\n\n", sqlite3_errmsg(on_get_db_()));
return false;
}

Expand All @@ -180,7 +195,8 @@ void io_controller::field_list_process(bool clear_screen) {
clrscr();
}

fmt::print("Поля созданные в таблице: '{}'\n\n", name_table_);
spdlog::get("SQCli")->info("Поля созданные в таблице: '{}'\n\n",
name_table_);

on_field_list_(name_table_);
status_check_process();
Expand All @@ -203,7 +219,7 @@ bool io_controller::table_list_process(bool clear_screen) {
clrscr();
}

fmt::print("Таблицы созданные в базе данных:\n\n");
spdlog::get("SQCli")->info("Таблицы созданные в базе данных:\n\n");

on_table_list_();
const auto& obj = on_get_list_tables_();
Expand Down Expand Up @@ -302,7 +318,7 @@ void io_controller::insert_data_process() {
insert_value_to_enum(it, choice_variant_);
} catch (const std::exception& ex) {
clrscr();
fmt::print(stderr, "{}\n", ex.what());
spdlog::get("SQCli")->error("{}\n\n", ex.what());
return;
}

Expand All @@ -328,7 +344,7 @@ void io_controller::insert_data_process() {

if (insert_fields.empty()) {
clrscr();
fmt::print("SQCli error: no field found\n\n");
spdlog::get("SQCli")->error("No field found\n\n");
return;
}

Expand Down Expand Up @@ -375,7 +391,7 @@ void io_controller::update_data_process() {
status_check_process();
} else {
clrscr();
fmt::print("SQCli error: unknown field selected\n\n");
spdlog::get("SQCli")->error("Unknown field selected\n\n");
}
}
}
Expand Down Expand Up @@ -423,7 +439,7 @@ void io_controller::add_field_process(bool create_table) {
insert_value_to_enum(it, notnull);
} catch (const std::exception& ex) {
clrscr();
fmt::print(stderr, "{}\n", ex.what());
spdlog::get("SQCli")->error("{}\n\n", ex.what());
return;
}
fmt::print("\n");
Expand All @@ -436,7 +452,7 @@ void io_controller::add_field_process(bool create_table) {
insert_value_to_enum(it, pk);
} catch (const std::exception& ex) {
clrscr();
fmt::print(stderr, "{}\n", ex.what());
spdlog::get("SQCli")->error("{}\n\n", ex.what());
return;
}
fmt::print("\n");
Expand All @@ -450,7 +466,7 @@ void io_controller::add_field_process(bool create_table) {
insert_value_to_enum(it, ai);
} catch (const std::exception& ex) {
clrscr();
fmt::print(stderr, "{}\n", ex.what());
spdlog::get("SQCli")->error("{}\n\n", ex.what());
return;
}
fmt::print("\n");
Expand Down Expand Up @@ -486,7 +502,7 @@ void io_controller::rename_field_process() {
status_check_process();
} else {
clrscr();
fmt::print("SQCli error: unknown field selected\n\n");
spdlog::get("SQCli")->error("Unknown field selected\n\n");
}
}
}
Expand All @@ -511,7 +527,7 @@ void io_controller::create_table_process() {
clrscr();

name_table_.clear();
fmt::print(stderr, "{}\n", ex.what());
spdlog::get("SQCli")->error("{}\n\n", ex.what());

return;
}
Expand All @@ -537,7 +553,7 @@ bool io_controller::list_data_process(bool clear_screen) {
clrscr();
}

fmt::print("Данные из таблицы: '{}'\n\n", name_table_);
spdlog::get("SQCli")->info("Данные из таблицы: '{}'\n\n", name_table_);

on_list_data_(name_table_);
status_check_process();
Expand Down Expand Up @@ -572,18 +588,18 @@ void io_controller::select_table_process() {
name_table_ = tmp;

clrscr();
fmt::print("SQCli: successfully\n\n");
spdlog::get("SQCli")->info("Successfully\n\n");
} else {
clrscr();
fmt::print("SQCli error: unknown choice selected\n\n");
spdlog::get("SQCli")->error("Unknown choice selected\n\n");
}
}
}

bool io_controller::check_selected_table() const noexcept {
if (name_table_.empty()) {
clrscr();
fmt::print("SQCli error: table not select\n\n");
spdlog::get("SQCli")->error("Table not select\n\n");

return false;
}
Expand Down Expand Up @@ -692,3 +708,4 @@ void io_controller::set_on_status_check(on_status_check on_status_check) {
void io_controller::set_on_select_table(on_select_table on_select_table) {
on_select_table_ = std::move(on_select_table);
}
} // namespace sqcli
19 changes: 13 additions & 6 deletions sqcli/io_controller/io_controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <string>
#include <vector>

namespace sqcli {
class io_controller {
public:
using on_start = std::function<void(std::string& path_db)>;
Expand Down Expand Up @@ -52,6 +53,13 @@ class io_controller {
using on_get_list_fields = std::function<options::c_ref_other_data()>;
using on_get_cli_info = std::function<options::c_ref_other_data()>;

io_controller() noexcept;
io_controller(const io_controller&) = delete;
io_controller& operator=(const io_controller&) = delete;
io_controller& operator=(io_controller&&) = delete;
io_controller(io_controller&&) = delete;
~io_controller() noexcept = default;

void menu();

void set_on_start(on_start on_start);
Expand All @@ -78,13 +86,12 @@ class io_controller {
void set_on_get_cli_info(on_get_cli_info on_get_cli_info);

private:
std::uint16_t menu_it_;
std::uint16_t menu_it_ = 0;
std::string path_db_;
std::string name_table_;
bool select_table_;

menu_options menu_options_;
choice_variant choice_variant_;
menu_options menu_options_ = menu_options::ON_EXIT;
choice_variant choice_variant_ = choice_variant::NO;

on_start on_start_;
on_exit on_exit_;
Expand Down Expand Up @@ -127,5 +134,5 @@ class io_controller {
bool table_list_process(bool clear_screen = true);
bool status_check_process(bool open_database = false);
};

#endif // IO_CONTROLLER_HPP
} // namespace sqcli
#endif // IO_CONTROLLER_HPP
4 changes: 2 additions & 2 deletions sqcli/main.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <fmt/core.h>
#include <fmt/format.h>
#include <boost/program_options.hpp>

#include <iostream>
Expand Down Expand Up @@ -40,7 +40,7 @@ int main(int argc, char** argv) {
return 1;
}

sqcli sqcli(db_path);
sqcli::sqcli sqcli(db_path);
sqcli.run();

return 0;
Expand Down
8 changes: 5 additions & 3 deletions sqcli/options/options.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "options/options.hpp"

#include <fmt/core.h>
#include <fmt/format.h>

#include <algorithm>

Expand All @@ -10,15 +10,15 @@ int callback_data_list(void* data,
char** argv,
[[maybe_unused]] char** field_name) {
for (std::int32_t i = 0; i < argc; i++) {
static_cast<options::ptr_data_list>(data)->emplace_back(
static_cast<sqcli::options::ptr_data_list>(data)->emplace_back(
argv[i] != nullptr ? argv[i] : "NULL");
}

return 0;
}

int callback_other_data(void* data, int argc, char** argv, char** field_name) {
auto* obj = static_cast<options::ptr_other_data>(data);
auto* obj = static_cast<sqcli::options::ptr_other_data>(data);
obj->emplace_back();

for (std::int32_t i = 0; i < argc; ++i) {
Expand All @@ -30,6 +30,7 @@ int callback_other_data(void* data, int argc, char** argv, char** field_name) {
}
} // namespace

namespace sqcli {
options::options(const std::string& path_db) noexcept
: status_{sqlite3_open(path_db.c_str(), &db_)} {}

Expand Down Expand Up @@ -234,3 +235,4 @@ options::c_ref_other_data options::get_list_fields() const noexcept {
options::c_ref_other_data options::get_list_cli_info() const noexcept {
return list_cli_info_;
}
} // namespace sqcli
Loading

0 comments on commit 2ae158a

Please sign in to comment.