Pcannon Debug 1.0.0-build
Simple yet powerful debug manager for C++
Loading...
Searching...
No Matches
Debug.cpp
Go to the documentation of this file.
1#include <iostream>
2#include <algorithm>
3#include <string>
4
5#ifndef __PD_NO_FULL_SUPPORT
6# include <filesystem>
7
8namespace fs = std::filesystem;
9#endif
10
11#include "../inc/Debug.hpp"
12#include "../vendor/cpp-time-utils/inc/Time.hpp"
13
14namespace pd
15{
16 // PRIVATE //
18 {
19 if (!this->settings.filePath.empty())
20 {
21 this->file = new std::fstream(this->settings.filePath, this->settings.openMode);
22
23 if (!this->file->is_open())
24 {
25 this->constructError = "File could not open : " + this->id;
26
27 return false;
28 }
29
30 if (!this->file->good())
31 {
32 this->constructError = "File is not good for opening : " + this->id;
33
34 return false;
35 }
36 }
37
38 return true;
39 }
40
41 // PUBLIC //
42 Debug::Debug(const std::string &_id, const pd::DebugSettings &_settings)
43 : id(_id), settings(_settings)
44 {
45 this->constructed = this->__init();
46
47 if (!this->constructed)
48 return;
49 }
50
52 {
53 this->close();
54 }
55
56 std::string Debug::log(const std::string &_type, const std::string &_msg, unsigned int _level)
57 {
58 std::string msg;
59 std::string type = _type;
60
61 std::transform(type.begin(), type.end(), type.begin(), ::toupper);
62
63 msg += this->settings.preStartMsg;
64
65 if (!this->settings.custom)
66 {
67 msg += "[ " + this->settings.startMsg + type + (this->settings.debugID ? + " | " + this->id : "");
68
69 // DATE AND TIME INFORMATION
70 if (this->settings.timeStamp)
71 {
72 timeUtils::TimeInfo timeInfo;
73 timeUtils::DateInfo dateInfo;
74 timeUtils::Time time("debug-time");
75
76 time.update(timeInfo);
77 time.update(dateInfo);
78
79 msg += " | " + std::to_string(dateInfo.year) + "-" + std::to_string(dateInfo.month) + "-" + std::to_string(dateInfo.day) + " | ";
80 msg += std::to_string(timeInfo.hour) + ":" + std::to_string(timeInfo.min) + ":" + std::to_string(timeInfo.sec) + "." + std::to_string(timeInfo.ms)
81 + (this->settings.timeZone ? " +" + std::to_string(time.getUTCOffset().hour) + ":" + std::to_string(time.getUTCOffset().min) : "");
82 }
83
84 else msg += " ";
85
86 // FINISG MSG
87 }
88
89 else msg = "[ " + this->settings.startMsg + this->settings.totalCustom;
90
91 msg += this->settings.endMsg + "] " + (this->settings.postEndMsg) + _msg + this->settings.totalEndMsg + "\n";
92
93 if (this->settings.output)
94 {
95 if ((!this->settings.logLevelIgnoreOutput && this->settings.logLevel == 0) ||
96 (this->settings.logLevel > 0 && _level <= this->settings.logLevel))
97 {
98 std::cout << msg;
99 }
100 }
101
102 if (!this->settings.blockedSave)
103 {
104 if ((!this->settings.logLevelIgnoreSave && this->settings.logLevel == 0) ||
105 (this->settings.logLevel > 0 && _level <= this->settings.logLevel))
106 {
107# ifndef __PD_NO_FULL_SUPPORT
108 if (!fs::exists(fs::path(this->settings.filePath).parent_path()))
109 {
110 fs::create_directories(fs::path(this->settings.filePath).parent_path());
111
112 this->file->close(); this->file->open(this->settings.filePath, this->settings.openMode);
113 }
114# endif
115
116 *this->file << msg;
117 }
118 }
119
120 return msg;
121 }
122
124 {
125 if (this->file)
126 {
127 this->file->close();
128
129 delete this->file; this->file = nullptr;
130 }
131
132 else return false;
133
134 return true;
135 }
136
137 std::pair<bool, std::string> Debug::constructInfo()
138 { return { this->constructed, this->constructError }; }
139
140 std::string Debug::getID() const
141 { return this->id; }
142
143 std::fstream *Debug::getFile() const
144 { return this->file; }
145
147 { return this->settings; }
148}
149
std::fstream * getFile() const
Get file for more control over the processing and for more actions.
Definition Debug.cpp:143
std::string constructError
Definition Debug.hpp:55
DebugSettings getSettings() const
Get the settings set for the created Debug object.
Definition Debug.cpp:146
~Debug()
Call the pd::Debug::close() function to free all allocated memory.
Definition Debug.cpp:51
std::string getID() const
Return the ID of the created Debug object.
Definition Debug.cpp:140
std::string log(const std::string &_type, const std::string &_msg, unsigned int _level=0)
Log the message with specific custom type, message and log level.
Definition Debug.cpp:56
const DebugSettings settings
Definition Debug.hpp:52
std::fstream * file
Definition Debug.hpp:50
bool __init()
Initialize the Debug object that is going (or was) to be constructed.
Definition Debug.cpp:17
std::string id
Definition Debug.hpp:48
Debug(const std::string &_id, const pd::DebugSettings &_settings)
Construct the debugger with specified ID and settings Call the inner __init() function to Initialize.
Definition Debug.cpp:42
bool constructed
Definition Debug.hpp:54
std::pair< bool, std::string > constructInfo()
Construct debug information You can use it if it failed to construct properly to get the error messag...
Definition Debug.cpp:137
bool close()
Free all the allocated memory and resources, return true if it succeeded, return false if it did not.
Definition Debug.cpp:123
Definition Debug.hpp:17
Set debug settings struct with set values, default are already set for the best output.
Definition Debug.hpp:22