migrate SimpleLogger to C++11

This commit is contained in:
Dennis Luxen 2014-05-07 12:18:13 +02:00
parent c09e897dab
commit a0a835bbe6

View File

@ -25,20 +25,24 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
#ifndef SIMPLE_LOGGER_H_ #ifndef SIMPLE_LOGGER_H
#define SIMPLE_LOGGER_H_ #define SIMPLE_LOGGER_H
#include <boost/assert.hpp> #include <boost/assert.hpp>
#include <boost/noncopyable.hpp>
#include <boost/thread/mutex.hpp>
#include <cstdio> #include <cstdio>
#include <ostream> #include <ostream>
#include <iostream> #include <iostream>
#include <mutex>
enum LogLevel
{ logINFO,
logWARNING,
logDEBUG };
static std::mutex logger_mutex;
enum LogLevel { logINFO, logWARNING, logDEBUG };
static boost::mutex logger_mutex;
const char COL_RESET[] = "\x1b[0m"; const char COL_RESET[] = "\x1b[0m";
const char RED[] = "\x1b[31m"; const char RED[] = "\x1b[31m";
const char GREEN[] = "\x1b[32m"; const char GREEN[] = "\x1b[32m";
@ -47,26 +51,23 @@ const char BLUE[] = "\x1b[34m";
const char MAGENTA[] = "\x1b[35m"; const char MAGENTA[] = "\x1b[35m";
const char CYAN[] = "\x1b[36m"; const char CYAN[] = "\x1b[36m";
class LogPolicy : boost::noncopyable { class LogPolicy : boost::noncopyable
{
public: public:
void Unmute() { m_is_mute = false; }
void Unmute() { void Mute() { m_is_mute = true; }
m_is_mute = false;
}
void Mute() { bool IsMute() const { return m_is_mute; }
m_is_mute = true;
}
bool IsMute() const { static LogPolicy &GetInstance()
return m_is_mute; {
}
static LogPolicy & GetInstance() {
static LogPolicy runningInstance; static LogPolicy runningInstance;
return runningInstance; return runningInstance;
} }
LogPolicy(const LogPolicy &) = delete;
private: private:
LogPolicy() : m_is_mute(true) {} LogPolicy() : m_is_mute(true) {}
bool m_is_mute; bool m_is_mute;
@ -81,7 +82,7 @@ public:
{ {
try try
{ {
boost::mutex::scoped_lock lock(logger_mutex); std::lock_guard<std::mutex> lock(logger_mutex);
level = l; level = l;
os << "["; os << "[";
switch (level) switch (level)
@ -107,7 +108,8 @@ public:
return os; return os;
} }
virtual ~SimpleLogger() { virtual ~SimpleLogger()
{
if (!LogPolicy::GetInstance().IsMute()) if (!LogPolicy::GetInstance().IsMute())
{ {
const bool is_terminal = isatty(fileno(stdout)); const bool is_terminal = isatty(fileno(stdout));
@ -117,11 +119,13 @@ public:
std::cout << os.str() << (is_terminal ? COL_RESET : "") << std::endl; std::cout << os.str() << (is_terminal ? COL_RESET : "") << std::endl;
break; break;
case logWARNING: case logWARNING:
std::cerr << (is_terminal ? RED : "") << os.str() << (is_terminal ? COL_RESET : "") << std::endl; std::cerr << (is_terminal ? RED : "") << os.str() << (is_terminal ? COL_RESET : "")
<< std::endl;
break; break;
case logDEBUG: case logDEBUG:
#ifndef NDEBUG #ifndef NDEBUG
std::cout << (is_terminal ? YELLOW : "") << os.str() << (is_terminal ? COL_RESET : "") << std::endl; std::cout << (is_terminal ? YELLOW : "") << os.str()
<< (is_terminal ? COL_RESET : "") << std::endl;
#endif #endif
break; break;
default: default:
@ -136,4 +140,4 @@ private:
std::ostringstream os; std::ostringstream os;
}; };
#endif /* SIMPLE_LOGGER_H_ */ #endif /* SIMPLE_LOGGER_H */