multithreaded initial support

This commit is contained in:
mihais 2018-06-05 11:59:04 +03:00 committed by Stefan Andritoiu
parent 4ed5da0098
commit 046ac70071

View File

@ -1,9 +1,11 @@
#pragma once #pragma once
#include <sstream> #include <sstream>
#include <fstream>
#include <string> #include <string>
#include <chrono> #include <chrono>
#include <stdio.h> #include <thread>
#include <mutex>
/* Helper macro for logger utility class. */ /* Helper macro for logger utility class. */
#define UPM_LOG(log_level) \ #define UPM_LOG(log_level) \
@ -14,6 +16,8 @@
<< __FUNCTION__ << " " \ << __FUNCTION__ << " " \
<< __LINE__ << ": " << __LINE__ << ": "
#define LOG_FILE "/var/log/upm.log"
namespace upm { namespace upm {
enum UpmLogLevel { enum UpmLogLevel {
@ -38,6 +42,9 @@ namespace upm {
virtual ~UPM_LOGGER(); virtual ~UPM_LOGGER();
std::ostringstream& log(UpmLogLevel level = LOG_ERROR); std::ostringstream& log(UpmLogLevel level = LOG_ERROR);
static UpmLogLevel& LogLevel(); static UpmLogLevel& LogLevel();
static std::ofstream logStream;
static std::mutex logMutex;
protected: protected:
std::ostringstream os; std::ostringstream os;
private: private:
@ -56,6 +63,18 @@ namespace upm {
return logLevelNames[level]; return logLevelNames[level];
} }
public:
static void write(std::ostringstream& os)
{
std::lock_guard<std::mutex> lock(logMutex);
logStream.open(LOG_FILE, std::ios_base::app);
logStream << os.str();
logStream.flush();
}
}; };
UPM_LOGGER::UPM_LOGGER() {} UPM_LOGGER::UPM_LOGGER() {}
@ -84,7 +103,7 @@ namespace upm {
UPM_LOGGER::~UPM_LOGGER() UPM_LOGGER::~UPM_LOGGER()
{ {
os << std::endl; os << std::endl;
fprintf(stderr, "%s", os.str().c_str());
fflush(stderr); write(os);
} }
} }