Merge pull request 'Add mutex for multithreaded output' (#2) from MrBesen/Log:mutex into master
Reviewed-on: https://git.okaestne.de/okaestne/Log/pulls/2
This commit is contained in:
commit
02aaf162ce
22
Log.cpp
22
Log.cpp
|
@ -6,6 +6,10 @@
|
||||||
#include <iostream> // std::ostream, std::cout, std::cin
|
#include <iostream> // std::ostream, std::cout, std::cin
|
||||||
#include <vector> // list of outputs
|
#include <vector> // list of outputs
|
||||||
|
|
||||||
|
#if LOG_USEMUTEX == 1
|
||||||
|
#include <mutex>
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Log {
|
namespace Log {
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -21,6 +25,11 @@ public:
|
||||||
virtual ~Output() {}
|
virtual ~Output() {}
|
||||||
|
|
||||||
virtual void log(Level lvl, std::stringbuf* sbuf) {
|
virtual void log(Level lvl, std::stringbuf* sbuf) {
|
||||||
|
//aquire lock
|
||||||
|
#if LOG_USEMUTEX == 1
|
||||||
|
std::unique_lock<std::mutex> lock(ostreamLock);
|
||||||
|
#endif
|
||||||
|
|
||||||
std::ostream* os = getOs(lvl);
|
std::ostream* os = getOs(lvl);
|
||||||
if (os) {
|
if (os) {
|
||||||
*os << sbuf << std::endl;
|
*os << sbuf << std::endl;
|
||||||
|
@ -34,6 +43,10 @@ protected:
|
||||||
// returns the correct ostream for the given log-level
|
// returns the correct ostream for the given log-level
|
||||||
// or returns nullptr if no ostream is set/enabled for this level
|
// or returns nullptr if no ostream is set/enabled for this level
|
||||||
virtual std::ostream* getOs(Level lvl) = 0; // abstract
|
virtual std::ostream* getOs(Level lvl) = 0; // abstract
|
||||||
|
|
||||||
|
#if LOG_USEMUTEX == 1
|
||||||
|
std::mutex ostreamLock; //used for both streams
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
// logging to stdout/stderr
|
// logging to stdout/stderr
|
||||||
|
@ -56,6 +69,11 @@ private:
|
||||||
// OFF FATAL ERROR WARN NOTE INFO DEBUG TRACE
|
// OFF FATAL ERROR WARN NOTE INFO DEBUG TRACE
|
||||||
static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"};
|
static const char* color_codes[] = {"", "1;31;40m", "31m", "33m", "96m", "32m", "0m", "0m"};
|
||||||
|
|
||||||
|
//aquire lock
|
||||||
|
#if LOG_USEMUTEX == 1
|
||||||
|
std::unique_lock<std::mutex> lock(ostreamLock);
|
||||||
|
#endif
|
||||||
|
|
||||||
std::ostream* os = getOs(lvl);
|
std::ostream* os = getOs(lvl);
|
||||||
|
|
||||||
if (os) {
|
if (os) {
|
||||||
|
@ -98,6 +116,10 @@ private:
|
||||||
std::ofstream ofs;
|
std::ofstream ofs;
|
||||||
Level lvl_min = FATAL;
|
Level lvl_min = FATAL;
|
||||||
|
|
||||||
|
#if LOG_USEMUTEX == 1
|
||||||
|
std::mutex ostreamLock; //used for both streams
|
||||||
|
#endif
|
||||||
|
|
||||||
virtual std::ostream* getOs(Level lvl) {
|
virtual std::ostream* getOs(Level lvl) {
|
||||||
if (lvl_min <= lvl && lvl <= lvl_max)
|
if (lvl_min <= lvl && lvl <= lvl_max)
|
||||||
return &ofs;
|
return &ofs;
|
||||||
|
|
4
Log.h
4
Log.h
|
@ -3,6 +3,10 @@
|
||||||
#include <sstream> // std::stringstream (buffer for log entries)
|
#include <sstream> // std::stringstream (buffer for log entries)
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
|
#ifndef LOG_USEMUTEX
|
||||||
|
#define LOG_USEMUTEX 0
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace Log {
|
namespace Log {
|
||||||
enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE };
|
enum Level { OFF = 0, FATAL, ERROR, WARN, NOTE, INFO, DEBUG, TRACE };
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue