Skip to content

Instantly share code, notes, and snippets.

@rchatsiri
Created October 25, 2011 16:08
Show Gist options
  • Save rchatsiri/1313280 to your computer and use it in GitHub Desktop.
Save rchatsiri/1313280 to your computer and use it in GitHub Desktop.
logger from boost-logging.
#ifndef INIT_LOGGER_HPP_
#define INIT_LOGGER_HPP_
#include <fstream>
#include <string>
#include <iostream>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/utility/init/to_file.hpp>
#include <boost/log/utility/init/from_stream.hpp>
#include <boost/log/utility/init/to_console.hpp>
#include <boost/log/utility/init/common_attributes.hpp>
#include <boost/log/sources/basic_logger.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/severity_feature.hpp>
#include <boost/log/sources/record_ostream.hpp>
#include <boost/program_options/parsers.hpp>
#include <boost/program_options/options_description.hpp>
#include <boost/log/utility/init/from_stream.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/log/core.hpp>
#include <boost/log/utility/init/filter_parser.hpp>
#include <boost/log/utility/init/formatter_parser.hpp>
#include <boost/log/attributes/clock.hpp>
#include <boost/log/attributes/constant.hpp>
#include <boost/log/sources/severity_logger.hpp>
#include <boost/log/sources/global_logger_storage.hpp>
#include <boost/log/utility/init/from_stream.hpp>
#include <boost/log/utility/empty_deleter.hpp>
#include <boost/log/utility/scoped_attribute.hpp>
#include <boost/log/filters/has_attr.hpp>
#include <boost/log/filters.hpp>
#include <boost/thread.hpp>
BOOST_LOG_DECLARE_GLOBAL_LOGGER(getlogger, boost::log::sources::severity_logger_mt< >)
namespace logging = boost::log;
namespace clamav_logger{
class logger
{
public:
enum serverity_level{
trace, debug, info, warn, error
};
bool initLogger(const std::string& filepath);
bool isInit()
{
return isInit_;
}
// singleton
static logger& instance()
{
static logger instance_;
return instance_;
}
private:
logger();
~logger();
bool isInit_;
};
logger::logger(): isInit_(false)
{
std::cout<<"### init logger status = ["<<isInit_<<"] ###"<<std::endl;
}
logger::~logger()
{
std::cout<<"### ~destory logger ###"<<std::endl;
}
bool logger::initLogger(const std::string& filepath)
{
if(isInit_)
return isInit_;
std::ifstream file(filepath.c_str());
if(!file.is_open())
{
std::cout<<"Open log configure file = ["<<filepath<<"]"<<std::endl;
boost::log::init_log_to_console(std::cout);
}
else
{
logging::init_from_stream(file);
}
logging::add_common_attributes();
isInit_ = true;
return isInit_;
}
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
#define AT __FILE__ ":" TOSTRING(__LINE__)
#define TID pthread_self()
#define PID getpid()
#define CLLOG_TRACE(str) {BOOST_LOG_SEV(getlogger::get(),clamav_logger::logger::trace) <<"CLAMAV_CPP = "<< AT << "|" << TID << "|-" << str;}
#define CLLOG_DEBUG(str) {BOOST_LOG_SEV(getlogger::get(),clamav_logger::logger::debug) <<"CLAMAV_CPP = "<< AT << "|" << TID << "|-" << str;}
#define CLLOG_INFO(str) {BOOST_LOG_SEV(getlogger::get(),clamav_logger::logger::info) <<"CLAMAV_CPP = "<< AT << "|" << TID << "|-" << str;}
#define CLLOG_WARN(str) {BOOST_LOG_SEV(getlogger::get(),clamav_logger::logger::warn)<<"CLAMAV_CPP = " << AT << "|" << TID << "|-" << str;}
#define CLLOG_ERROR(str) {BOOST_LOG_SEV(getlogger::get(),clamav_logger::logger::error)<<"CLAMAV_CPP = " << AT << "|" << TID << "|-" << str;}
}
#endif /* INIT_LOGGER_HPP_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment