Add semaphore for work wit multithread

testing
marcelb 1 year ago
parent 152c8b6719
commit c2a5995e7e
  1. 3
      .vscode/settings.json
  2. 3
      example/2023-07-27.log
  3. 2
      lib/log.hpp
  4. 2
      src/log.cpp

@ -4,6 +4,7 @@
"iosfwd": "cpp", "iosfwd": "cpp",
"fstream": "cpp", "fstream": "cpp",
"*.tcc": "cpp", "*.tcc": "cpp",
"ostream": "cpp" "ostream": "cpp",
"mutex": "cpp"
} }
} }

@ -0,0 +1,3 @@
18:29:30 [EVENT] Start loging
18:29:44 [EVENT] Start loging
18:29:47 [EVENT] Start loging

@ -7,6 +7,7 @@
#include <fstream> #include <fstream>
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <mutex>
using namespace std; using namespace std;
@ -19,6 +20,7 @@ class log {
struct tm * moment; struct tm * moment;
uint day; uint day;
string path; string path;
mutex io;
log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false); log (string _dir, bool _isKeepOpen = true, bool _printInConsole = false);

@ -43,6 +43,7 @@ void log::setMoment() {
} }
void log::put(string logline) { void log::put(string logline) {
io.lock();
if (printInConsole) { if (printInConsole) {
cout << logline << endl; cout << logline << endl;
} }
@ -74,6 +75,7 @@ void log::put(string logline) {
if (!isKeepOpen && logfile.is_open()) { if (!isKeepOpen && logfile.is_open()) {
loose(); loose();
} }
io.unlock();
} }

Loading…
Cancel
Save