Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions include/ce/celog.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include <sstream>
#include <vector>
#include "ce/cedt.h"
#include <mutex>
#include <filesystem>

#if defined(CE_WINDOWS)
#include <windows.h>
Expand All @@ -69,6 +71,8 @@ class ceLog{
double m_expiry_days;
std::string m_extension;
bool m_enPrintf;
std::mutex _mtxLog;
uint8_t _log_level;
public:
ceLog();
ceLog(std::string path,double expdays);
Expand All @@ -90,6 +94,12 @@ class ceLog{
int Print(std::string mes); // write to file and echo on std output
bool GetEnPrintf();
void SetEnPrintf(bool en);
void SetLogLevel(uint8_t l);
uint8_t GetLogLevel();
int Print(uint8_t level, std::string mes); // write to file and echo on std output if level is smaller than or equal to _log_level
int Info(std::string mes); // write to file and echo on std output with log level 0
int Debug(std::string mes); // write to file and echo on std output with log level 1
int ChkDir(std::string path); // check if the log dir exists, mkdir if it doesn't exist
};

/////////////////////////////////////////////////////////////////////////////
Expand All @@ -101,6 +111,8 @@ inline ceLog::ceLog()
SetExpiry(30);
SetExtension(".log");
SetEnPrintf(false);
SetLogLevel(0);
ChkDir(LOG_PATH);
}

inline ceLog::ceLog(std::string path, double expdays)
Expand All @@ -109,6 +121,8 @@ inline ceLog::ceLog(std::string path, double expdays)
SetExpiry(expdays);
SetExtension(".log");
SetEnPrintf(true);
SetLogLevel(0);
ChkDir(path);
}

inline void ceLog::SetPath(std::string path)
Expand Down Expand Up @@ -171,12 +185,23 @@ inline double ceLog::GetTimezone()//get time zone
return this->m_dt.tz();
}

inline void ceLog::SetLogLevel(uint8_t l)//set local time zone
{
this->_log_level = l;
}

inline uint8_t ceLog::GetLogLevel()//get time zone
{
return this->_log_level;
}

inline int ceLog::Write(std::string mes)
{
std::ofstream wfile;
int r = -1;
this->m_dt.Set2Now();
std::string logpath = this->m_path + "L" + this->m_dt.ToString("%yyyy-%mm-%dd") + ".log";
_mtxLog.lock();
try {
wfile.open(logpath.c_str(), std::fstream::out | std::fstream::app);
if (wfile.is_open()) {
Expand All @@ -188,6 +213,7 @@ inline int ceLog::Write(std::string mes)
catch (...) {
perror("ceLog error in writing");
}
_mtxLog.unlock();
return r;
}

Expand All @@ -200,6 +226,25 @@ inline int ceLog::Print(std::string mes)
return Write(mes);
}

// write to file and echo on std output if level is smaller than or equal to _log_level
inline int ceLog::Print(uint8_t level, std::string mes)
{
if (level <= (this->_log_level)) {
return this->Print(mes);
}
return 0;
}

// write to file and echo on std output with log level 0
inline int ceLog::Info(std::string mes) {
return this->Print(0, mes);
}

// write to file and echo on std output with log level 1
inline int ceLog::Debug(std::string mes) {
return this->Print(1, mes);
}

inline void ceLog::Clean(std::string path, std::string extension, double expiry_seconds)
{
std::vector<std::string> filenames;
Expand Down Expand Up @@ -317,6 +362,31 @@ inline double ceLog::LastModified(const std::string& name)
return dt.jd();
}

inline int ceLog::ChkDir(std::string path) {
int r = -1;
if (std::filesystem::exists(path)) {
if (std::filesystem::is_regular_file(path)) {
perror("ceLog error: path should be a dir, not a file\n");
}
else if (std::filesystem::is_directory(path)) {
r = 0;
}
else {
perror("ceLog error: path is not a dir\n");
}
}
else {
// Directory does not exist, so create it
if (std::filesystem::create_directory(path)) {
printf("ceLog: Directory %s created successfully.\n",path.c_str());
r = 1;
} else {
perror("ceLog error: fail to create dir\n");
}
}
return r;
}

/////////////////////////////////////////////////////////////////////////////

} // namespace ce
Expand Down