pmt: New features and improvements
- Added type getter and reboot function - Writed a garbage collector, so manually freeing memory and closing file descriptors is removed - Some other improvements
This commit is contained in:
@@ -27,62 +27,73 @@
|
||||
#include <libhelper/lib.hpp>
|
||||
|
||||
namespace Helper {
|
||||
Error::Error(const char *format, ...) {
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
|
||||
Error::Error(const char* format, ...)
|
||||
{
|
||||
char buf[1024];
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
vsnprintf(buf, sizeof(buf), format, args);
|
||||
va_end(args);
|
||||
_message = std::string(buf);
|
||||
LOGN(HELPER, ERROR) << _message << std::endl;
|
||||
}
|
||||
const char *Error::what() const noexcept {
|
||||
return _message.data();
|
||||
}
|
||||
|
||||
const char* Error::what() const noexcept
|
||||
{
|
||||
return _message.data();
|
||||
}
|
||||
Logger::Logger(const LogLevels level, const char *func, const char *file, const char *name, const char *sfile,
|
||||
const int line) : _level(level), _funcname(func), _logFile(file), _program_name(name), _file(sfile),
|
||||
_line(line) {
|
||||
}
|
||||
|
||||
Logger::Logger(const LogLevels level, const char* func, const char* file, const char* name, const char* sfile, const int line) : _level(level), _funcname(func), _logFile(file), _program_name(name), _file(sfile), _line(line) {}
|
||||
Logger::~Logger() {
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
static_cast<char>(_level),
|
||||
_program_name,
|
||||
basename(const_cast<char *>(_file)),
|
||||
_line,
|
||||
currentDate().data(),
|
||||
currentTime().data(),
|
||||
_funcname,
|
||||
_oss.str().data());
|
||||
|
||||
Logger::~Logger()
|
||||
{
|
||||
if (LoggingProperties::DISABLE) return;
|
||||
char str[1024];
|
||||
snprintf(str, sizeof(str), "<%c> [ <prog %s> <on %s:%d> %s %s] %s(): %s",
|
||||
static_cast<char>(_level),
|
||||
_program_name,
|
||||
basename(const_cast<char *>(_file)),
|
||||
_line,
|
||||
currentDate().data(),
|
||||
currentTime().data(),
|
||||
_funcname,
|
||||
_oss.str().data());
|
||||
|
||||
if (!isExists(_logFile)) {
|
||||
if (const int fd = open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS); fd != -1) close(fd);
|
||||
else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot create log file: " << _logFile << ": " << strerror(errno) << " New logging file: last_logs.log (this file)." << std::endl;
|
||||
if (!isExists(_logFile)) {
|
||||
if (const int fd = open(_logFile, O_WRONLY | O_CREAT, DEFAULT_EXTENDED_FILE_PERMS); fd != -1) close(fd);
|
||||
else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot create log file: " << _logFile << ": " << strerror(errno) <<
|
||||
" New logging file: last_logs.log (this file)." << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (FILE *fp = fopen(_logFile, "a"); fp != nullptr) {
|
||||
fprintf(fp, "%s", str);
|
||||
fclose(fp);
|
||||
} else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot write logs to log file: " << _logFile << ": " << strerror(errno) <<
|
||||
" Logging file setting up as: last_logs.log (this file)." << std::endl;
|
||||
}
|
||||
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
|
||||
if (FILE* fp = fopen(_logFile, "a"); fp != nullptr) {
|
||||
fprintf(fp, "%s", str);
|
||||
fclose(fp);
|
||||
} else {
|
||||
LoggingProperties::setLogFile("last_logs.log");
|
||||
LOGN(HELPER, INFO) << "Cannot write logs to log file: " << _logFile << ": " << strerror(errno) << " Logging file setting up as: last_logs.log (this file)." << std::endl;
|
||||
Logger &Logger::operator<<(std::ostream & (*msg)(std::ostream &)) {
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
if (LoggingProperties::PRINT) printf("%s", str);
|
||||
}
|
||||
|
||||
Logger& Logger::operator<<(std::ostream& (*msg)(std::ostream&))
|
||||
{
|
||||
_oss << msg;
|
||||
return *this;
|
||||
}
|
||||
garbageCollector::~garbageCollector() {
|
||||
for (const auto& ptr : _ptrs_c) delete[] ptr;
|
||||
for (const auto& ptr : _ptrs_u) delete[] ptr;
|
||||
for (const auto& fd : _fds) close(fd);
|
||||
for (const auto& fp : _fps) fclose(fp);
|
||||
}
|
||||
|
||||
void garbageCollector::delAfterProgress(char* &_ptr) { _ptrs_c.push_back(_ptr); }
|
||||
void garbageCollector::delAfterProgress(uint8_t *&_ptr) { _ptrs_u.push_back(_ptr); }
|
||||
void garbageCollector::closeAfterProgress(const int _fd) { _fds.push_back(_fd); }
|
||||
void garbageCollector::delAfterProgress(FILE* &_fp) { _fps.push_back(_fp); }
|
||||
} // namespace Helper
|
||||
|
||||
Reference in New Issue
Block a user