2015-01-27 11:44:46 -05:00
|
|
|
#ifndef SIMPLE_LOGGER_HPP
|
|
|
|
#define SIMPLE_LOGGER_HPP
|
|
|
|
|
|
|
|
#include <atomic>
|
|
|
|
#include <mutex>
|
|
|
|
#include <sstream>
|
|
|
|
|
|
|
|
enum LogLevel
|
|
|
|
{
|
|
|
|
logINFO,
|
|
|
|
logWARNING,
|
|
|
|
logDEBUG
|
|
|
|
};
|
|
|
|
|
2016-01-05 10:51:13 -05:00
|
|
|
namespace osrm
|
|
|
|
{
|
|
|
|
namespace util
|
|
|
|
{
|
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
class LogPolicy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
void Unmute();
|
|
|
|
|
|
|
|
void Mute();
|
|
|
|
|
|
|
|
bool IsMute() const;
|
|
|
|
|
|
|
|
static LogPolicy &GetInstance();
|
|
|
|
|
|
|
|
LogPolicy(const LogPolicy &) = delete;
|
Takes care of proper special member generation globally, fixes #1689
Phew, a lot of classes were affected by this. The rationale for the
changes are as follows:
- When a type X declares any constructor, the default constructor is
not declared, so there is no need for X() = delete there. In fact,
there is brutal difference between those two: deleted members
participate in overload resolution, but not-declared members do not!
- When a type X wants to be non-copyable (e.g. to be only movable, like
threads, unique_ptrs, and so on), you can either do it by inheriting
from boost::noncopyable (the old way), or better declare both (!) the
copy constructor _and_ the copy assignment operator as deleted:
X(X const&) = delete;
X& operator=(X const&) = delete;
We had tons of types with deleted copy constructors that were lacking
a corresponding deleted copy assignment operator, making them still
copyable and you wouldn't even notice (read: scary)!
References:
- http://accu.org/content/conf2014/Howard_Hinnant_Accu_2014.pdf
- http://www.boost.org/doc/libs/master/libs/core/doc/html/core/noncopyable.html
Note: I know, I'm quoting Hinnant's extraordinary slides a lot, but
getting the sematic right here is so incredibly important.
2016-01-27 05:20:55 -05:00
|
|
|
LogPolicy &operator=(const LogPolicy &) = delete;
|
2015-01-27 11:44:46 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
LogPolicy() : m_is_mute(true) {}
|
|
|
|
std::atomic<bool> m_is_mute;
|
|
|
|
};
|
|
|
|
|
|
|
|
class SimpleLogger
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SimpleLogger();
|
|
|
|
|
|
|
|
virtual ~SimpleLogger();
|
|
|
|
std::mutex &get_mutex();
|
2015-01-28 04:09:46 -05:00
|
|
|
std::ostringstream &Write(LogLevel l = logINFO) noexcept;
|
2015-01-27 11:44:46 -05:00
|
|
|
|
|
|
|
private:
|
|
|
|
std::ostringstream os;
|
|
|
|
LogLevel level;
|
|
|
|
};
|
2016-01-05 10:51:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-27 11:44:46 -05:00
|
|
|
#endif /* SIMPLE_LOGGER_HPP */
|