4747#include < sstream>
4848#include < vector>
4949#include " ce/cedt.h"
50+ #include < mutex>
51+ #include < filesystem>
5052
5153#if defined(CE_WINDOWS)
5254 #include < windows.h>
@@ -69,6 +71,8 @@ class ceLog{
6971 double m_expiry_days;
7072 std::string m_extension;
7173 bool m_enPrintf;
74+ std::mutex _mtxLog;
75+ uint8_t _log_level;
7276public:
7377 ceLog ();
7478 ceLog (std::string path,double expdays);
@@ -90,6 +94,12 @@ class ceLog{
9094 int Print (std::string mes); // write to file and echo on std output
9195 bool GetEnPrintf ();
9296 void SetEnPrintf (bool en);
97+ void SetLogLevel (uint8_t l);
98+ uint8_t GetLogLevel ();
99+ 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
100+ int Info (std::string mes); // write to file and echo on std output with log level 0
101+ int Debug (std::string mes); // write to file and echo on std output with log level 1
102+ int ChkDir (std::string path); // check if the log dir exists, mkdir if it doesn't exist
93103};
94104
95105// ///////////////////////////////////////////////////////////////////////////
@@ -101,6 +111,8 @@ inline ceLog::ceLog()
101111 SetExpiry (30 );
102112 SetExtension (" .log" );
103113 SetEnPrintf (false );
114+ SetLogLevel (0 );
115+ ChkDir (LOG_PATH);
104116}
105117
106118inline ceLog::ceLog (std::string path, double expdays)
@@ -109,6 +121,8 @@ inline ceLog::ceLog(std::string path, double expdays)
109121 SetExpiry (expdays);
110122 SetExtension (" .log" );
111123 SetEnPrintf (true );
124+ SetLogLevel (0 );
125+ ChkDir (path);
112126}
113127
114128inline void ceLog::SetPath (std::string path)
@@ -171,12 +185,23 @@ inline double ceLog::GetTimezone()//get time zone
171185 return this ->m_dt .tz ();
172186}
173187
188+ inline void ceLog::SetLogLevel (uint8_t l)// set local time zone
189+ {
190+ this ->_log_level = l;
191+ }
192+
193+ inline uint8_t ceLog::GetLogLevel ()// get time zone
194+ {
195+ return this ->_log_level ;
196+ }
197+
174198inline int ceLog::Write (std::string mes)
175199{
176200 std::ofstream wfile;
177201 int r = -1 ;
178202 this ->m_dt .Set2Now ();
179203 std::string logpath = this ->m_path + " L" + this ->m_dt .ToString (" %yyyy-%mm-%dd" ) + " .log" ;
204+ _mtxLog.lock ();
180205 try {
181206 wfile.open (logpath.c_str (), std::fstream::out | std::fstream::app);
182207 if (wfile.is_open ()) {
@@ -188,6 +213,7 @@ inline int ceLog::Write(std::string mes)
188213 catch (...) {
189214 perror (" ceLog error in writing" );
190215 }
216+ _mtxLog.unlock ();
191217 return r;
192218}
193219
@@ -200,6 +226,25 @@ inline int ceLog::Print(std::string mes)
200226 return Write (mes);
201227}
202228
229+ // write to file and echo on std output if level is smaller than or equal to _log_level
230+ inline int ceLog::Print (uint8_t level, std::string mes)
231+ {
232+ if (level <= (this ->_log_level )) {
233+ return this ->Print (mes);
234+ }
235+ return 0 ;
236+ }
237+
238+ // write to file and echo on std output with log level 0
239+ inline int ceLog::Info (std::string mes) {
240+ return this ->Print (0 , mes);
241+ }
242+
243+ // write to file and echo on std output with log level 1
244+ inline int ceLog::Debug (std::string mes) {
245+ return this ->Print (1 , mes);
246+ }
247+
203248inline void ceLog::Clean (std::string path, std::string extension, double expiry_seconds)
204249{
205250 std::vector<std::string> filenames;
@@ -317,6 +362,31 @@ inline double ceLog::LastModified(const std::string& name)
317362 return dt.jd ();
318363}
319364
365+ inline int ceLog::ChkDir (std::string path) {
366+ int r = -1 ;
367+ if (std::filesystem::exists (path)) {
368+ if (std::filesystem::is_regular_file (path)) {
369+ perror (" ceLog error: path should be a dir, not a file\n " );
370+ }
371+ else if (std::filesystem::is_directory (path)) {
372+ r = 0 ;
373+ }
374+ else {
375+ perror (" ceLog error: path is not a dir\n " );
376+ }
377+ }
378+ else {
379+ // Directory does not exist, so create it
380+ if (std::filesystem::create_directory (path)) {
381+ printf (" ceLog: Directory %s created successfully.\n " ,path.c_str ());
382+ r = 1 ;
383+ } else {
384+ perror (" ceLog error: fail to create dir\n " );
385+ }
386+ }
387+ return r;
388+ }
389+
320390// ///////////////////////////////////////////////////////////////////////////
321391
322392} // namespace ce
0 commit comments