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,115 +25,119 @@ 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";
const char YELLOW[] = "\x1b[33m"; const char YELLOW[] = "\x1b[33m";
const char BLUE[] = "\x1b[34m"; 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 runningInstance;
return runningInstance;
static LogPolicy & GetInstance() {
static LogPolicy runningInstance;
return runningInstance;
} }
private: LogPolicy(const LogPolicy &) = delete;
LogPolicy() : m_is_mute(true) { }
bool m_is_mute; private:
LogPolicy() : m_is_mute(true) {}
bool m_is_mute;
}; };
class SimpleLogger class SimpleLogger
{ {
public: public:
SimpleLogger() : level(logINFO) { } SimpleLogger() : level(logINFO) {}
std::ostringstream& Write(LogLevel l = logINFO) std::ostringstream &Write(LogLevel l = logINFO)
{ {
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)
{ {
case logINFO: case logINFO:
os << "info"; os << "info";
break; break;
case logWARNING: case logWARNING:
os << "warn"; os << "warn";
break; break;
case logDEBUG: case logDEBUG:
#ifndef NDEBUG #ifndef NDEBUG
os << "debug"; os << "debug";
#endif #endif
break; break;
default: default:
BOOST_ASSERT_MSG(false, "should not happen"); BOOST_ASSERT_MSG(false, "should not happen");
break; break;
} }
os << "] "; os << "] ";
} }
catch (...) { } catch (...) {}
return os; return os;
} }
virtual ~SimpleLogger() { virtual ~SimpleLogger()
if(!LogPolicy::GetInstance().IsMute()) {
{ if (!LogPolicy::GetInstance().IsMute())
const bool is_terminal = isatty(fileno(stdout)); {
switch(level) const bool is_terminal = isatty(fileno(stdout));
{ switch (level)
case logINFO: {
std::cout << os.str() << (is_terminal ? COL_RESET : "") << std::endl; case logINFO:
break; std::cout << os.str() << (is_terminal ? COL_RESET : "") << std::endl;
case logWARNING: break;
std::cerr << (is_terminal ? RED : "") << os.str() << (is_terminal ? COL_RESET : "") << std::endl; case logWARNING:
break; std::cerr << (is_terminal ? RED : "") << os.str() << (is_terminal ? COL_RESET : "")
case logDEBUG: << std::endl;
break;
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:
BOOST_ASSERT_MSG(false, "should not happen"); BOOST_ASSERT_MSG(false, "should not happen");
break; break;
} }
} }
} }
private: private:
LogLevel level; LogLevel level;
std::ostringstream os; std::ostringstream os;
}; };
#endif /* SIMPLE_LOGGER_H_ */ #endif /* SIMPLE_LOGGER_H */