/**************************************************************************** ** ** Copyright (C) 2017 liushixiongcpp@163.com ** All rights reserved. ** ****************************************************************************/ #ifndef log4cpphelper_h #define log4cpphelper_h #include <string> #include <stdarg.h> using std::string; #include "singleton.h" #include <log4cpp/Appender.hh> #include <log4cpp/Category.hh> #include <log4cpp/Appender.hh> #include <log4cpp/OstreamAppender.hh> #include <log4cpp/Layout.hh> #include <log4cpp/BasicLayout.hh> #include <log4cpp/Priority.hh> #include <log4cpp/FileAppender.hh> #include <log4cpp/RollingFileAppender.hh> #include <log4cpp/PatternLayout.hh> #include <log4cpp/NDC.hh> #define LOG_INFO_ARGS(formatStr, ...) {\ singleton::Singleton<Log4cppHelper>::Instance().LogInfo(log4cpp::Priority::INFO, formatStr, __VA_ARGS__); \ } #define LOG_NOTICE_ARGS(formatStr, ...) {\ singleton::Singleton<Log4cppHelper>::Instance().LogInfo(log4cpp::Priority::NOTICE, formatStr, __VA_ARGS__); \ } #define LOG_WARN_ARGS(formatStr, ...) {\ singleton::Singleton<Log4cppHelper>::Instance().LogInfo(log4cpp::Priority::WARN, formatStr, __VA_ARGS__); \ } #define LOG_ERROR_ARGS(formatStr, ...) {\ singleton::Singleton<Log4cppHelper>::Instance().LogInfo(log4cpp::Priority::ERROR, formatStr, __VA_ARGS__); \ } #define LOG_CRIT_ARGS(formatStr, ...) {\ singleton::Singleton<Log4cppHelper>::Instance().LogInfo(log4cpp::Priority::CRIT, formatStr, __VA_ARGS__); \ } #define LOG_ALTER_ARGS(formatStr, ...) {\ singleton::Singleton<Log4cppHelper>::Instance().LogInfo(log4cpp::Priority::ALERT, formatStr, __VA_ARGS__); \ } #define LOG_INFO(msg) \ singleton::Singleton<Log4cppHelper>::Instance().GetLogCategory() << \ log4cpp::Priority::INFO << __FILE__ << " [" << __LINE__ << "] " << msg; #define LOG_NOTICE(msg) \ singleton::Singleton<Log4cppHelper>::Instance().GetLogCategory() << \ log4cpp::Priority::NOTICE << __FILE__ << " [" << __LINE__ << "] " << msg; #define LOG_WARN(msg) \ singleton::Singleton<Log4cppHelper>::Instance().GetLogCategory() << \ log4cpp::Priority::WARN << __FILE__ << " [" << __LINE__ << "] " << msg; #define LOG_ERROR(msg) \ singleton::Singleton<Log4cppHelper>::Instance().GetLogCategory() << \ log4cpp::Priority::ERROR << __FILE__ << " [" << __LINE__ << "] " << \ msg; #define LOG_CRIT(msg) \ singleton::Singleton<Log4cppHelper>::Instance().GetLogCategory() << \ log4cpp::Priority::CRIT << __FILE__ << " [" << __LINE__ << "] " << msg; #define LOG_ALERT(msg) \ singleton::Singleton<Log4cppHelper>::Instance().GetLogCategory() << \ log4cpp::Priority::ALERT << __FILE__ << " [" << __LINE__ << "] " << \ msg; class Log4cppHelper : public singleton::Singleton<Log4cppHelper> { public: static Log4cppHelper & Instance(); public: ~Log4cppHelper(); log4cpp::Category& GetLogCategory(); void LogInfo(log4cpp::Priority::Value priority, const char* stringFormat, ...); private: Log4cppHelper(); Log4cppHelper(const Log4cppHelper &); Log4cppHelper& operator=(const Log4cppHelper &); private: friend singleton::Singleton<Log4cppHelper>; }; #endif // log4cpphelper_h
#include "log4cpphelper.h" #include "timstamp.h" #define LOG_FILE_NAME "defualt_name" Log4cppHelper::Log4cppHelper(const Log4cppHelper &) {} Log4cppHelper& Log4cppHelper::operator=(const Log4cppHelper &) { return *this; } Log4cppHelper::Log4cppHelper() { log4cpp::Appender *appender = new log4cpp::RollingFileAppender("default", string(LOG_FILE_NAME)+ Timestamp::now().toFormattedString( Timestamp::STRING_STYLE) + string(".log") ); log4cpp::PatternLayout *patternlayout = new log4cpp::PatternLayout(); patternlayout->setConversionPattern("%d [%p] %m%n"); appender->setLayout(patternlayout); log4cpp::Category& log = log4cpp::Category::getInstance( std::string(LOG_FILE_NAME)); log.addAppender(appender); } Log4cppHelper::~Log4cppHelper() { log4cpp::Category::shutdown(); } void Log4cppHelper::LogInfo(log4cpp::Priority::Value priority, const char* stringFormat, ...) { va_list va; va_start(va, stringFormat); log4cpp::Category::getInstance(std::string(LOG_FILE_NAME)).logva( priority, stringFormat, va); va_end(va); } log4cpp::Category& Log4cppHelper::GetLogCategory() { return log4cpp::Category::getInstance(std::string(LOG_FILE_NAME)); }
#include <iostream> #include "timstamp.h" #include "singleton.h" #include "log4cpphelper.h" int main(int argc, char ** argv[]) { int i = 0; // LOG_INFO("test info"); LOG_INFO_ARGS("%s [%d] test %d %s", __FILE__, __LINE__ , 5, "linux"); LOG_NOTICE("test NOTICE"); LOG_WARN("test warn"); LOG_ERROR("test error"); LOG_CRIT("test crit"); LOG_ALERT("test alter"); getchar(); return 0; }
https://files.cnblogs.com/files/L-Lune/test.7zlinux
注意編譯的時候,vs編譯選項選擇debug x86模式ios