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