27 add handling debug flag #28
Merged@github-juk3n wants to merge
27-add-handling--d-debug-flag into mainMerged as
e100cdf733ea.3 files changed
modified
app/include/database.hpp +9 -7 | @@ -2,6 +2,7 @@ | ||
| 2 | 2 | |
| 3 | 3 | #include <sqlite3.h> |
| 4 | 4 | #include <string> |
| 5 | +#include <memory> | |
| 5 | 6 | |
| 6 | 7 | #include <game.hpp> |
| 7 | 8 | #include <output.hpp> |
| @@ -9,7 +10,7 @@ | ||
| 9 | 10 | class Database |
| 10 | 11 | { |
| 11 | 12 | private: |
| 12 | - Output output{}; | |
| 13 | + std::shared_ptr<Output> output{}; | |
| 13 | 14 | sqlite3 *database; |
| 14 | 15 | |
| 15 | 16 | std::string database_initialization_command = |
| @@ -21,12 +22,13 @@ private: | ||
| 21 | 22 | |
| 22 | 23 | auto initializeDatabase() -> void { |
| 23 | 24 | executeSqlCommand(database_initialization_command); |
| 24 | - output.printDebugMessage("Database Initialized"); | |
| 25 | + output->printDebugMessage("Database Initialized"); | |
| 25 | 26 | } |
| 26 | 27 | |
| 27 | 28 | public: |
| 28 | 29 | |
| 29 | - Database(std::filesystem::path databasePath) { | |
| 30 | + Database(std::filesystem::path databasePath, std::shared_ptr<Output> output) { | |
| 31 | + this->output = output; | |
| 30 | 32 | loadDatabase(databasePath); |
| 31 | 33 | } |
| 32 | 34 | |
| @@ -41,7 +43,7 @@ public: | ||
| 41 | 43 | |
| 42 | 44 | if (exit != SQLITE_OK) { |
| 43 | 45 | |
| 44 | - output.printDebugMessage("Error with command: " + command + " " + messageError); | |
| 46 | + output->printDebugMessage("Error with command: " + command + " " + messageError); | |
| 45 | 47 | sqlite3_free(messageError); |
| 46 | 48 | throw std::exception(); |
| 47 | 49 | } |
| @@ -56,11 +58,11 @@ public: | ||
| 56 | 58 | exit = sqlite3_open(databasePath.c_str(), &database); |
| 57 | 59 | |
| 58 | 60 | if (exit) { |
| 59 | - output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); | |
| 61 | + output->printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); | |
| 60 | 62 | throw std::exception(); |
| 61 | 63 | } |
| 62 | 64 | |
| 63 | - output.printDebugMessage("Opened database successfully!"); | |
| 65 | + output->printDebugMessage("Opened database successfully!"); | |
| 64 | 66 | |
| 65 | 67 | if (newCreation) { |
| 66 | 68 | initializeDatabase(); |
| @@ -73,7 +75,7 @@ public: | ||
| 73 | 75 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| 74 | 76 | |
| 75 | 77 | if (exit) { |
| 76 | - output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); | |
| 78 | + output->printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); | |
| 77 | 79 | sqlite3_close(database); |
| 78 | 80 | return {}; |
| 79 | 81 | } |
| @@ -2,6 +2,7 @@ | |||
| 2 | 2 | ||
| 3 | #include <sqlite3.h> | 3 | #include <sqlite3.h> |
| 4 | #include <string> | 4 | #include <string> |
| 5 | +#include <memory> | ||
| 5 | 6 | ||
| 6 | #include <game.hpp> | 7 | #include <game.hpp> |
| 7 | #include <output.hpp> | 8 | #include <output.hpp> |
| @@ -9,7 +10,7 @@ | |||
| 9 | class Database | 10 | class Database |
| 10 | { | 11 | { |
| 11 | private: | 12 | private: |
| 12 | - Output output{}; | 13 | + std::shared_ptr<Output> output{}; |
| 13 | sqlite3 *database; | 14 | sqlite3 *database; |
| 14 | 15 | ||
| 15 | std::string database_initialization_command = | 16 | std::string database_initialization_command = |
| @@ -21,12 +22,13 @@ private: | |||
| 21 | 22 | ||
| 22 | auto initializeDatabase() -> void { | 23 | auto initializeDatabase() -> void { |
| 23 | executeSqlCommand(database_initialization_command); | 24 | executeSqlCommand(database_initialization_command); |
| 24 | - output.printDebugMessage("Database Initialized"); | 25 | + output->printDebugMessage("Database Initialized"); |
| 25 | } | 26 | } |
| 26 | 27 | ||
| 27 | public: | 28 | public: |
| 28 | 29 | ||
| 29 | - Database(std::filesystem::path databasePath) { | 30 | + Database(std::filesystem::path databasePath, std::shared_ptr<Output> output) { |
| 31 | + this->output = output; | ||
| 30 | loadDatabase(databasePath); | 32 | loadDatabase(databasePath); |
| 31 | } | 33 | } |
| 32 | 34 | ||
| @@ -41,7 +43,7 @@ public: | |||
| 41 | 43 | ||
| 42 | if (exit != SQLITE_OK) { | 44 | if (exit != SQLITE_OK) { |
| 43 | 45 | ||
| 44 | - output.printDebugMessage("Error with command: " + command + " " + messageError); | 46 | + output->printDebugMessage("Error with command: " + command + " " + messageError); |
| 45 | sqlite3_free(messageError); | 47 | sqlite3_free(messageError); |
| 46 | throw std::exception(); | 48 | throw std::exception(); |
| 47 | } | 49 | } |
| @@ -56,11 +58,11 @@ public: | |||
| 56 | exit = sqlite3_open(databasePath.c_str(), &database); | 58 | exit = sqlite3_open(databasePath.c_str(), &database); |
| 57 | 59 | ||
| 58 | if (exit) { | 60 | if (exit) { |
| 59 | - output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); | 61 | + output->printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); |
| 60 | throw std::exception(); | 62 | throw std::exception(); |
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | - output.printDebugMessage("Opened database successfully!"); | 65 | + output->printDebugMessage("Opened database successfully!"); |
| 64 | 66 | ||
| 65 | if (newCreation) { | 67 | if (newCreation) { |
| 66 | initializeDatabase(); | 68 | initializeDatabase(); |
| @@ -73,7 +75,7 @@ public: | |||
| 73 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); | 75 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| 74 | 76 | ||
| 75 | if (exit) { | 77 | if (exit) { |
| 76 | - output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); | 78 | + output->printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); |
| 77 | sqlite3_close(database); | 79 | sqlite3_close(database); |
| 78 | return {}; | 80 | return {}; |
| 79 | } | 81 | } |
modified
app/include/output.hpp +5 -0 | @@ -7,6 +7,11 @@ class Output { | ||
| 7 | 7 | private: |
| 8 | 8 | bool debugRun = false; |
| 9 | 9 | public: |
| 10 | + auto turnOnDebug() -> void { | |
| 11 | + debugRun = true; | |
| 12 | + printDebugMessage("Debug mode on"); | |
| 13 | + } | |
| 14 | + | |
| 10 | 15 | auto printDebugMessage(std::string message) -> void { |
| 11 | 16 | if (debugRun) |
| 12 | 17 | std::cout << message << std::endl; |
| @@ -7,6 +7,11 @@ class Output { | |||
| 7 | private: | 7 | private: |
| 8 | bool debugRun = false; | 8 | bool debugRun = false; |
| 9 | public: | 9 | public: |
| 10 | + auto turnOnDebug() -> void { | ||
| 11 | + debugRun = true; | ||
| 12 | + printDebugMessage("Debug mode on"); | ||
| 13 | + } | ||
| 14 | + | ||
| 10 | auto printDebugMessage(std::string message) -> void { | 15 | auto printDebugMessage(std::string message) -> void { |
| 11 | if (debugRun) | 16 | if (debugRun) |
| 12 | std::cout << message << std::endl; | 17 | std::cout << message << std::endl; |
modified
app/src/main.cpp +14 -13 | @@ -15,6 +15,7 @@ | ||
| 15 | 15 | #include <vector> |
| 16 | 16 | #include <unistd.h> |
| 17 | 17 | #include <limits.h> |
| 18 | +#include <memory> | |
| 18 | 19 | |
| 19 | 20 | #include <game.hpp> |
| 20 | 21 | #include <database.hpp> |
| @@ -22,11 +23,10 @@ | ||
| 22 | 23 | |
| 23 | 24 | using namespace ftxui; |
| 24 | 25 | |
| 25 | -Output output{}; | |
| 26 | +std::shared_ptr<Output> output{}; | |
| 26 | 27 | std::string version{ "v0.1.2" }; |
| 27 | 28 | std::vector<Game> games; |
| 28 | 29 | |
| 29 | - | |
| 30 | 30 | auto addGame(Database& database, std::string name, std::string path) -> void { |
| 31 | 31 | std::string command = |
| 32 | 32 | "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES ("; |
| @@ -81,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a | ||
| 81 | 81 | runGame(gameName); |
| 82 | 82 | } |
| 83 | 83 | else if (command == "--version") { |
| 84 | - output.printMessage("fluffy " + version); | |
| 84 | + output->printMessage("fluffy " + version); | |
| 85 | 85 | } |
| 86 | 86 | else if (command == "--help") { |
| 87 | - output.printMessage("fluffy commands:\n \ | |
| 87 | + output->printMessage("fluffy commands:\n \ | |
| 88 | 88 | play <game_name> Runs a game\n \ |
| 89 | 89 | add <game_name> <game_path> Add a game library\n \ |
| 90 | 90 | show Show all games added to library\n \ |
| 91 | 91 | rm <game_name> Remove game from a library"); |
| 92 | 92 | } |
| 93 | 93 | else { |
| 94 | - output.printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'"); | |
| 94 | + output->printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'"); | |
| 95 | 95 | } |
| 96 | 96 | } |
| 97 | 97 | |
| @@ -147,11 +147,11 @@ auto runConsoleApp() -> void { | ||
| 147 | 147 | |
| 148 | 148 | auto handleFlags(std::vector<std::string> flags) -> void { |
| 149 | 149 | for (const auto& flag : flags) { |
| 150 | - if (flag == "-test") { | |
| 151 | - output.printMessage("hello!"); | |
| 150 | + if (flag == "-d") { | |
| 151 | + output->turnOnDebug(); | |
| 152 | 152 | } |
| 153 | 153 | else { |
| 154 | - output.printMessage("unkown option: '" + flag + "'"); | |
| 154 | + output->printMessage("unkown option: '" + flag + "'"); | |
| 155 | 155 | } |
| 156 | 156 | } |
| 157 | 157 | } |
| @@ -189,15 +189,16 @@ auto parseCommand(int argc, char const* argv[]) { | ||
| 189 | 189 | |
| 190 | 190 | auto main(int argc, char const *argv[]) -> int { |
| 191 | 191 | try { |
| 192 | + output = std::make_shared<Output>(); | |
| 193 | + auto flags = parseFlags(argc, argv); | |
| 194 | + handleFlags(flags); | |
| 195 | + | |
| 192 | 196 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; |
| 193 | - Database database{databasePath}; | |
| 197 | + Database database{databasePath, output}; | |
| 194 | 198 | games = database.getGames(); |
| 195 | 199 | |
| 196 | - if (argc > 1) { | |
| 197 | - auto flags = parseFlags(argc, argv); | |
| 200 | + if (argc - flags.size() > 1) { | |
| 198 | 201 | auto [command, arguments] = parseCommand(argc, argv); |
| 199 | - | |
| 200 | - handleFlags(flags); | |
| 201 | 202 | if (command != "") handleCommand(command, arguments, database); |
| 202 | 203 | } |
| 203 | 204 | else { |
| @@ -15,6 +15,7 @@ | |||
| 15 | #include <vector> | 15 | #include <vector> |
| 16 | #include <unistd.h> | 16 | #include <unistd.h> |
| 17 | #include <limits.h> | 17 | #include <limits.h> |
| 18 | +#include <memory> | ||
| 18 | 19 | ||
| 19 | #include <game.hpp> | 20 | #include <game.hpp> |
| 20 | #include <database.hpp> | 21 | #include <database.hpp> |
| @@ -22,11 +23,10 @@ | |||
| 22 | 23 | ||
| 23 | using namespace ftxui; | 24 | using namespace ftxui; |
| 24 | 25 | ||
| 25 | -Output output{}; | 26 | +std::shared_ptr<Output> output{}; |
| 26 | std::string version{ "v0.1.2" }; | 27 | std::string version{ "v0.1.2" }; |
| 27 | std::vector<Game> games; | 28 | std::vector<Game> games; |
| 28 | 29 | ||
| 29 | - | ||
| 30 | auto addGame(Database& database, std::string name, std::string path) -> void { | 30 | auto addGame(Database& database, std::string name, std::string path) -> void { |
| 31 | std::string command = | 31 | std::string command = |
| 32 | "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES ("; | 32 | "INSERT INTO games (GAME_NAME, GAME_PATH) VALUES ("; |
| @@ -81,17 +81,17 @@ auto handleCommand(const std::string& command, const std::vector<std::string>& a | |||
| 81 | runGame(gameName); | 81 | runGame(gameName); |
| 82 | } | 82 | } |
| 83 | else if (command == "--version") { | 83 | else if (command == "--version") { |
| 84 | - output.printMessage("fluffy " + version); | 84 | + output->printMessage("fluffy " + version); |
| 85 | } | 85 | } |
| 86 | else if (command == "--help") { | 86 | else if (command == "--help") { |
| 87 | - output.printMessage("fluffy commands:\n \ | 87 | + output->printMessage("fluffy commands:\n \ |
| 88 | play <game_name> Runs a game\n \ | 88 | play <game_name> Runs a game\n \ |
| 89 | add <game_name> <game_path> Add a game library\n \ | 89 | add <game_name> <game_path> Add a game library\n \ |
| 90 | show Show all games added to library\n \ | 90 | show Show all games added to library\n \ |
| 91 | rm <game_name> Remove game from a library"); | 91 | rm <game_name> Remove game from a library"); |
| 92 | } | 92 | } |
| 93 | else { | 93 | else { |
| 94 | - output.printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'"); | 94 | + output->printMessage("fluffy: '" + command + "' is not a fluffy command. See 'fluffy --help'"); |
| 95 | } | 95 | } |
| 96 | } | 96 | } |
| 97 | 97 | ||
| @@ -147,11 +147,11 @@ auto runConsoleApp() -> void { | |||
| 147 | 147 | ||
| 148 | auto handleFlags(std::vector<std::string> flags) -> void { | 148 | auto handleFlags(std::vector<std::string> flags) -> void { |
| 149 | for (const auto& flag : flags) { | 149 | for (const auto& flag : flags) { |
| 150 | - if (flag == "-test") { | 150 | + if (flag == "-d") { |
| 151 | - output.printMessage("hello!"); | 151 | + output->turnOnDebug(); |
| 152 | } | 152 | } |
| 153 | else { | 153 | else { |
| 154 | - output.printMessage("unkown option: '" + flag + "'"); | 154 | + output->printMessage("unkown option: '" + flag + "'"); |
| 155 | } | 155 | } |
| 156 | } | 156 | } |
| 157 | } | 157 | } |
| @@ -189,15 +189,16 @@ auto parseCommand(int argc, char const* argv[]) { | |||
| 189 | 189 | ||
| 190 | auto main(int argc, char const *argv[]) -> int { | 190 | auto main(int argc, char const *argv[]) -> int { |
| 191 | try { | 191 | try { |
| 192 | + output = std::make_shared<Output>(); | ||
| 193 | + auto flags = parseFlags(argc, argv); | ||
| 194 | + handleFlags(flags); | ||
| 195 | + | ||
| 192 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; | 196 | auto databasePath{ std::filesystem::path(getExecutablePath().parent_path().string() + "/data.db")}; |
| 193 | - Database database{databasePath}; | 197 | + Database database{databasePath, output}; |
| 194 | games = database.getGames(); | 198 | games = database.getGames(); |
| 195 | 199 | ||
| 196 | - if (argc > 1) { | 200 | + if (argc - flags.size() > 1) { |
| 197 | - auto flags = parseFlags(argc, argv); | ||
| 198 | auto [command, arguments] = parseCommand(argc, argv); | 201 | auto [command, arguments] = parseCommand(argc, argv); |
| 199 | - | ||
| 200 | - handleFlags(flags); | ||
| 201 | if (command != "") handleCommand(command, arguments, database); | 202 | if (command != "") handleCommand(command, arguments, database); |
| 202 | } | 203 | } |
| 203 | else { | 204 | else { |