Skip to content

Instantly share code, notes, and snippets.

@akira-okumura
Last active August 29, 2015 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save akira-okumura/8a7bfe7343b553b1291f to your computer and use it in GitHub Desktop.
Save akira-okumura/8a7bfe7343b553b1291f to your computer and use it in GitHub Desktop.
#ifndef MY_PROJECT_LOGGER_H_
#define MY_PROJECT_LOGGER_H_
#include <fstream>
#include <stdio.h>
#include <stdarg.h>
#include <string>
#include <boost/smart_ptr/shared_ptr.hpp>
#include <boost/smart_ptr/make_shared_object.hpp>
#include <boost/phoenix/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/log/core.hpp>
#include <boost/log/expressions.hpp>
#include <boost/log/attributes.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/log/sinks/sync_frontend.hpp>
#include <boost/log/sinks/text_ostream_backend.hpp>
#include <boost/log/attributes/scoped_attribute.hpp>
#include <boost/log/utility/value_ref.hpp>
#include <boost/log/utility/setup/common_attributes.hpp>
#include <boost/log/support/date_time.hpp>
#include <boost/log/attributes/mutable_constant.hpp>
namespace MyProject {
namespace logging = boost::log;
namespace expr = boost::log::expressions;
namespace sinks = boost::log::sinks;
namespace attrs = boost::log::attributes;
namespace src = boost::log::sources;
namespace keywords = boost::log::keywords;
typedef sinks::synchronous_sink<sinks::text_ostream_backend> text_sink;
class Logger
{
public:
enum ESeverityLevel {kDebug, kInfo, kWarning, kError, kFatal};
private:
src::severity_logger_mt<ESeverityLevel> fLogger;
boost::shared_ptr<text_sink> fSink;
void Log(ESeverityLevel level, const std::string& message);
void Log(ESeverityLevel level, const char* format, va_list ap);
public:
Logger(const std::string& fileName = "/dev/stderr");
virtual ~Logger() {};
void InitFormat();
void SetFilterLevel(ESeverityLevel level);
void Flush();
void Debug(const std::string& message);
void Debug(const char* format, ...);
void Info(const std::string& message);
void Info(const char* format, ...);
void Warning(const std::string& message);
void Warning(const char* format, ...);
void Error(const std::string& message);
void Error(const char* format, ...);
void Fatal(const std::string& message);
void Fatal(const char* format, ...);
};
extern Logger gLogger;
inline void Logger::Flush()
{
if(fSink.get()){
fSink->flush();
} // if
}
inline void Logger::Log(ESeverityLevel level, const std::string& message)
{
BOOST_LOG_SEV(fLogger, level) << message;
}
inline void Logger::Log(ESeverityLevel level, const char* format, va_list ap)
{
char str[500];
vsnprintf(str, 500, format, ap);
BOOST_LOG_SEV(fLogger, level) << str;
}
inline void Logger::Debug(const std::string& message)
{
Log(kDebug, message);
}
inline void Logger::Debug(const char* format, ...)
{
va_list args;
va_start(args, format);
Log(kDebug, format, args);
va_end(args);
}
inline void Logger::Info(const std::string& message)
{
Log(kInfo, message);
}
inline void Logger::Info(const char* format, ...)
{
va_list args;
va_start(args, format);
Log(kInfo, format, args);
va_end(args);
}
inline void Logger::Warning(const std::string& message)
{
Log(kWarning, message);
}
inline void Logger::Warning(const char* format, ...)
{
va_list args;
va_start(args, format);
Log(kWarning, format, args);
va_end(args);
}
inline void Logger::Error(const std::string& message)
{
Log(kError, message);
Flush();
}
inline void Logger::Error(const char* format, ...)
{
va_list args;
va_start(args, format);
Log(kError, format, args);
va_end(args);
Flush();
}
inline void Logger::Fatal(const std::string& message)
{
Log(kFatal, message);
Flush();
}
inline void Logger::Fatal(const char* format, ...)
{
va_list args;
va_start(args, format);
Log(kFatal, format, args);
va_end(args);
Flush();
}
inline std::ostream& operator<< (std::ostream& strm, Logger::ESeverityLevel level)
{
static const char* strings[] = {"DEBUG", "INFO", "WARNING", "ERROR", "FATAL"};
if (static_cast< std::size_t>(level) < sizeof(strings)/sizeof(*strings)){
strm << strings[level];
} else {
strm << static_cast<int>(level);
} // if
return strm;
}
} // MyProject
#endif // MY_PROJECT_LOGGER_H_
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment