added printing debug error messages
33742aa parent: 190870d modified
app/include/database.hpp +8 -7 | @@ -4,10 +4,12 @@ | ||
| 4 | 4 | #include <string> |
| 5 | 5 | |
| 6 | 6 | #include <game.hpp> |
| 7 | +#include <output.hpp> | |
| 7 | 8 | |
| 8 | 9 | class Database |
| 9 | 10 | { |
| 10 | 11 | private: |
| 12 | + Output output{}; | |
| 11 | 13 | sqlite3 *database; |
| 12 | 14 | |
| 13 | 15 | std::string database_initialization_command = |
| @@ -19,7 +21,7 @@ private: | ||
| 19 | 21 | |
| 20 | 22 | auto initializeDatabase() -> void { |
| 21 | 23 | executeSqlCommand(database_initialization_command); |
| 22 | - //printMessage("Database Initialized"); | |
| 24 | + output.printDebugMessage("Database Initialized"); | |
| 23 | 25 | } |
| 24 | 26 | |
| 25 | 27 | public: |
| @@ -38,7 +40,8 @@ public: | ||
| 38 | 40 | exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError); |
| 39 | 41 | |
| 40 | 42 | if (exit != SQLITE_OK) { |
| 41 | - //printMessage("Error with command: " + command + " " + messageError); | |
| 43 | + | |
| 44 | + output.printDebugMessage("Error with command: " + command + " " + messageError); | |
| 42 | 45 | sqlite3_free(messageError); |
| 43 | 46 | throw std::exception(); |
| 44 | 47 | } |
| @@ -53,12 +56,11 @@ public: | ||
| 53 | 56 | exit = sqlite3_open(databasePath.c_str(), &database); |
| 54 | 57 | |
| 55 | 58 | if (exit) { |
| 56 | - std::cerr << "Error opening database" << sqlite3_errmsg(database) | |
| 57 | - << std::endl; | |
| 59 | + output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); | |
| 58 | 60 | throw std::exception(); |
| 59 | 61 | } |
| 60 | 62 | |
| 61 | - //printMessage("Opened database successfully!"); | |
| 63 | + output.printDebugMessage("Opened database successfully!"); | |
| 62 | 64 | |
| 63 | 65 | if (newCreation) { |
| 64 | 66 | initializeDatabase(); |
| @@ -71,8 +73,7 @@ public: | ||
| 71 | 73 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| 72 | 74 | |
| 73 | 75 | if (exit) { |
| 74 | - std::cerr << "Error retrieving data" << sqlite3_errmsg(database) | |
| 75 | - << std::endl; | |
| 76 | + output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); | |
| 76 | 77 | sqlite3_close(database); |
| 77 | 78 | return {}; |
| 78 | 79 | } |
| @@ -4,10 +4,12 @@ | |||
| 4 | #include <string> | 4 | #include <string> |
| 5 | 5 | ||
| 6 | #include <game.hpp> | 6 | #include <game.hpp> |
| 7 | +#include <output.hpp> | ||
| 7 | 8 | ||
| 8 | class Database | 9 | class Database |
| 9 | { | 10 | { |
| 10 | private: | 11 | private: |
| 12 | + Output output{}; | ||
| 11 | sqlite3 *database; | 13 | sqlite3 *database; |
| 12 | 14 | ||
| 13 | std::string database_initialization_command = | 15 | std::string database_initialization_command = |
| @@ -19,7 +21,7 @@ private: | |||
| 19 | 21 | ||
| 20 | auto initializeDatabase() -> void { | 22 | auto initializeDatabase() -> void { |
| 21 | executeSqlCommand(database_initialization_command); | 23 | executeSqlCommand(database_initialization_command); |
| 22 | - //printMessage("Database Initialized"); | 24 | + output.printDebugMessage("Database Initialized"); |
| 23 | } | 25 | } |
| 24 | 26 | ||
| 25 | public: | 27 | public: |
| @@ -38,7 +40,8 @@ public: | |||
| 38 | exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError); | 40 | exit = sqlite3_exec(database, command.c_str(), nullptr, nullptr, &messageError); |
| 39 | 41 | ||
| 40 | if (exit != SQLITE_OK) { | 42 | if (exit != SQLITE_OK) { |
| 41 | - //printMessage("Error with command: " + command + " " + messageError); | 43 | + |
| 44 | + output.printDebugMessage("Error with command: " + command + " " + messageError); | ||
| 42 | sqlite3_free(messageError); | 45 | sqlite3_free(messageError); |
| 43 | throw std::exception(); | 46 | throw std::exception(); |
| 44 | } | 47 | } |
| @@ -53,12 +56,11 @@ public: | |||
| 53 | exit = sqlite3_open(databasePath.c_str(), &database); | 56 | exit = sqlite3_open(databasePath.c_str(), &database); |
| 54 | 57 | ||
| 55 | if (exit) { | 58 | if (exit) { |
| 56 | - std::cerr << "Error opening database" << sqlite3_errmsg(database) | 59 | + output.printDebugError("Error opening database" + std::string{sqlite3_errmsg(database)}); |
| 57 | - << std::endl; | ||
| 58 | throw std::exception(); | 60 | throw std::exception(); |
| 59 | } | 61 | } |
| 60 | 62 | ||
| 61 | - //printMessage("Opened database successfully!"); | 63 | + output.printDebugMessage("Opened database successfully!"); |
| 62 | 64 | ||
| 63 | if (newCreation) { | 65 | if (newCreation) { |
| 64 | initializeDatabase(); | 66 | initializeDatabase(); |
| @@ -71,8 +73,7 @@ public: | |||
| 71 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); | 73 | exit = sqlite3_prepare_v2(database, select_all_games_command.c_str(), -1, &stmt, nullptr); |
| 72 | 74 | ||
| 73 | if (exit) { | 75 | if (exit) { |
| 74 | - std::cerr << "Error retrieving data" << sqlite3_errmsg(database) | 76 | + output.printDebugError("Error retrieving data" + std::string{sqlite3_errmsg(database)}); |
| 75 | - << std::endl; | ||
| 76 | sqlite3_close(database); | 77 | sqlite3_close(database); |
| 77 | return {}; | 78 | return {}; |
| 78 | } | 79 | } |
modified
app/include/output.hpp +8 -1 | @@ -4,8 +4,15 @@ | ||
| 4 | 4 | #include <string> |
| 5 | 5 | |
| 6 | 6 | class Output { |
| 7 | +private: | |
| 8 | + bool debugRun = false; | |
| 7 | 9 | public: |
| 8 | 10 | auto printDebugMessage(std::string message) -> void { |
| 9 | - std::cout << message << std::endl; | |
| 11 | + if (debugRun) | |
| 12 | + std::cout << message << std::endl; | |
| 13 | + } | |
| 14 | + auto printDebugError(std::string message) -> void { | |
| 15 | + if (debugRun) | |
| 16 | + std::cerr << message << std::endl; | |
| 10 | 17 | } |
| 11 | 18 | }; |
| \ No newline at end of file | ||
| @@ -4,8 +4,15 @@ | |||
| 4 | #include <string> | 4 | #include <string> |
| 5 | 5 | ||
| 6 | class Output { | 6 | class Output { |
| 7 | +private: | ||
| 8 | + bool debugRun = false; | ||
| 7 | public: | 9 | public: |
| 8 | auto printDebugMessage(std::string message) -> void { | 10 | auto printDebugMessage(std::string message) -> void { |
| 9 | - std::cout << message << std::endl; | 11 | + if (debugRun) |
| 12 | + std::cout << message << std::endl; | ||
| 13 | + } | ||
| 14 | + auto printDebugError(std::string message) -> void { | ||
| 15 | + if (debugRun) | ||
| 16 | + std::cerr << message << std::endl; | ||
| 10 | } | 17 | } |
| 11 | }; | 18 | }; |
| \ No newline at end of file | \ No newline at end of file | ||