|
1 | 1 | # debug.hpp |
2 | 2 | C++ Debug Library (header only) |
| 3 | + |
| 4 | +## Usage |
| 5 | + |
| 6 | +declare `_DEBUG` before including this library |
| 7 | + |
| 8 | +it automatically writes into a file called `debug_log.txt` |
| 9 | + |
| 10 | +## Public Methods |
| 11 | + |
| 12 | +All Methods are under the namespace Debug |
| 13 | + |
| 14 | +* When starting the program you can clear the previous debug output with this function: |
| 15 | + |
| 16 | + ```cpp |
| 17 | + Debug::clear(); |
| 18 | + ``` |
| 19 | + |
| 20 | +* You can log Messages, Warnings and Errors with these: |
| 21 | + |
| 22 | + ```cpp |
| 23 | + Debug::logMessage("This is a message"); |
| 24 | + Debug::logWarning("This is a warning"); |
| 25 | + Debug::logError("This is an error"); |
| 26 | + ``` |
| 27 | + |
| 28 | + The time of this message will automatically be added |
| 29 | + |
| 30 | +* you can pause the program by calling |
| 31 | + |
| 32 | + ```cpp |
| 33 | + Debug::pause(); |
| 34 | + ``` |
| 35 | + |
| 36 | +* When a critical Error occurs, you can call: |
| 37 | + |
| 38 | + ```cpp |
| 39 | + Debug::logCriticalError("A critical error occurred"); |
| 40 | + ``` |
| 41 | + |
| 42 | + This prints the error message to the console and debug output, |
| 43 | + then pauses. |
| 44 | + |
| 45 | + Even if debug is not enabled, this function exits after doing all of this |
| 46 | + |
| 47 | +* When you want to log a raw text, use this function: |
| 48 | + |
| 49 | + ```cpp |
| 50 | + Debug::logRaw("This is my raw message"); |
| 51 | + ``` |
| 52 | + |
| 53 | +## Use Example |
| 54 | + |
| 55 | +```cpp |
| 56 | +#define _DEBUG |
| 57 | + |
| 58 | +#include "debug.hpp" |
| 59 | +#include <iostream> |
| 60 | +#include <string> |
| 61 | +#include <fstream> |
| 62 | + |
| 63 | +int main(void) { |
| 64 | + // clear the debug output |
| 65 | + Debug::clear(); |
| 66 | + |
| 67 | + // log, that the program started |
| 68 | + Debug::logMessage("Program Started"); |
| 69 | + |
| 70 | + // open a file stream to text.txt |
| 71 | + std::ifstream file("text.txt", std::ios::in); |
| 72 | + |
| 73 | + // check if couldn't be opened |
| 74 | + if (!file) { |
| 75 | + // when the file can't be read, there is a critical error |
| 76 | + Debug::logCriticalError("File text.txt couldn't be opened...\nExiting."); |
| 77 | + } |
| 78 | + |
| 79 | + Debug::logMessage("Opened File successfully"); |
| 80 | + |
| 81 | + std::string first_line; |
| 82 | + |
| 83 | + std::getline(file, first_line); |
| 84 | + |
| 85 | + if (first_line.empty()) { |
| 86 | + Debug::logWarning("text.txt file is empty"); |
| 87 | + } |
| 88 | + |
| 89 | + std::cout << "First line of text.txt: " << first_line << "\n"; |
| 90 | + |
| 91 | + file.close(); |
| 92 | + |
| 93 | + Debug::logMessage("Program executed successfully"); |
| 94 | + return 0; |
| 95 | +} |
| 96 | +``` |
0 commit comments