Pcannon Debug 1.0.0-build
Simple yet powerful debug manager for C++
Loading...
Searching...
No Matches
pd::Debug Class Reference

#include <Debug.hpp>

Public Member Functions

 Debug (const std::string &_id, const pd::DebugSettings &_settings)
 Construct the debugger with specified ID and settings Call the inner __init() function to Initialize.
 ~Debug ()
 Call the pd::Debug::close() function to free all allocated memory.
bool close ()
 Free all the allocated memory and resources, return true if it succeeded, return false if it did not.
std::pair< bool, std::string > constructInfo ()
 Construct debug information You can use it if it failed to construct properly to get the error message.
std::fstream * getFile () const
 Get file for more control over the processing and for more actions.
std::string getID () const
 Return the ID of the created Debug object.
DebugSettings getSettings () const
 Get the settings set for the created Debug object.
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.

Private Member Functions

bool __init ()
 Initialize the Debug object that is going (or was) to be constructed.

Private Attributes

bool constructed
std::string constructError
std::fstream * file
std::string id
const DebugSettings settings

Detailed Description

Definition at line 45 of file Debug.hpp.

Constructor & Destructor Documentation

◆ Debug()

pd::Debug::Debug ( const std::string & _id,
const pd::DebugSettings & _settings )

Construct the debugger with specified ID and settings Call the inner __init() function to Initialize.

Parameters
_idSet the ID of this object
_settingsSet the settings of this object

Definition at line 42 of file Debug.cpp.

43 : id(_id), settings(_settings)
44 {
45 this->constructed = this->__init();
46
47 if (!this->constructed)
48 return;
49 }
const DebugSettings settings
Definition Debug.hpp:52
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
bool constructed
Definition Debug.hpp:54

References __init(), constructed, id, and settings.

◆ ~Debug()

pd::Debug::~Debug ( )

Call the pd::Debug::close() function to free all allocated memory.

Definition at line 51 of file Debug.cpp.

52 {
53 this->close();
54 }
bool close()
Free all the allocated memory and resources, return true if it succeeded, return false if it did not.
Definition Debug.cpp:123

References close().

Member Function Documentation

◆ __init()

bool pd::Debug::__init ( )
private

Initialize the Debug object that is going (or was) to be constructed.

Returns
bool Did it fail? True = Success, False = Failiture

Definition at line 17 of file Debug.cpp.

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 }
std::string constructError
Definition Debug.hpp:55
std::fstream * file
Definition Debug.hpp:50

References constructError, file, id, and settings.

Referenced by Debug().

◆ close()

bool pd::Debug::close ( )

Free all the allocated memory and resources, return true if it succeeded, return false if it did not.

Returns
bool True if success, false if failiture

Definition at line 123 of file Debug.cpp.

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 }

References file.

Referenced by ~Debug().

◆ constructInfo()

std::pair< bool, std::string > pd::Debug::constructInfo ( )

Construct debug information You can use it if it failed to construct properly to get the error message.

Returns
std::pair<bool, std::string> .first: Did it succeed? .second: Error reason

Definition at line 137 of file Debug.cpp.

138 { return { this->constructed, this->constructError }; }

References constructed, and constructError.

◆ getFile()

std::fstream * pd::Debug::getFile ( ) const

Get file for more control over the processing and for more actions.

Returns
std::fstream Allocated file to Debug object

Definition at line 143 of file Debug.cpp.

144 { return this->file; }

References file.

◆ getID()

std::string pd::Debug::getID ( ) const

Return the ID of the created Debug object.

Returns
std::string The ID of this object

Definition at line 140 of file Debug.cpp.

141 { return this->id; }

References id.

◆ getSettings()

DebugSettings pd::Debug::getSettings ( ) const

Get the settings set for the created Debug object.

Returns
pd::DebugSettings Debug settings to return

Definition at line 146 of file Debug.cpp.

147 { return this->settings; }

References settings.

◆ log()

std::string pd::Debug::log ( const std::string & _type,
const std::string & _msg,
unsigned int _level = 0 )

Log the message with specific custom type, message and log level.

Parameters
_typeSet the type of the debug message (Ex: Information, Success, Warning, Error, ...)
_msgSet the message for debugging
_levelSet the debug level level of the log (default as 0, non-debug-level specific)
Returns
The total string of the total debug message

Definition at line 56 of file Debug.cpp.

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 }

References file, id, and settings.

Member Data Documentation

◆ constructed

bool pd::Debug::constructed
private

Definition at line 54 of file Debug.hpp.

Referenced by Debug(), and constructInfo().

◆ constructError

std::string pd::Debug::constructError
private

Definition at line 55 of file Debug.hpp.

Referenced by __init(), and constructInfo().

◆ file

std::fstream* pd::Debug::file
private

Definition at line 50 of file Debug.hpp.

Referenced by __init(), close(), getFile(), and log().

◆ id

std::string pd::Debug::id
private

Definition at line 48 of file Debug.hpp.

Referenced by Debug(), __init(), getID(), and log().

◆ settings

const DebugSettings pd::Debug::settings
private

Definition at line 52 of file Debug.hpp.

Referenced by Debug(), __init(), getSettings(), and log().


The documentation for this class was generated from the following files: