From b9882f8297bfaff3ecbd8a7a5c3c342f4f76b852 Mon Sep 17 00:00:00 2001 From: wyy Date: Thu, 12 Sep 2013 00:31:01 +0800 Subject: [PATCH] update cppcommon for var_args in loggingF --- cppcommon/logger.cpp | 28 ++++++++++++++++++++++++++++ cppcommon/logger.h | 17 ++++++++++++----- cppcommon/str_functs.cpp | 6 +++--- cppcommon/str_functs.h | 2 +- 4 files changed, 44 insertions(+), 9 deletions(-) diff --git a/cppcommon/logger.cpp b/cppcommon/logger.cpp index bd5e45e..93792bf 100644 --- a/cppcommon/logger.cpp +++ b/cppcommon/logger.cpp @@ -21,10 +21,33 @@ namespace CPPCOMMON { } + bool Logger::LoggingF(uint level, const char* fileName, int lineNo, const string& fmt, ...) + { + int size = 256; + string msg; + va_list ap; + while (1) { + msg.resize(size); + va_start(ap, fmt); + int n = vsnprintf((char *)msg.c_str(), size, fmt.c_str(), ap); + va_end(ap); + if (n > -1 && n < size) { + msg.resize(n); + break; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return Logging(level, msg, fileName, lineNo); + } + bool Logger::Logging(uint level, const string& msg, const char * fileName, int lineNo) { return Logging(level, msg.c_str(), fileName, lineNo); } + bool Logger::Logging(uint level, const char * msg, const char* fileName, int lineNo) { @@ -61,8 +84,13 @@ int main() LogDebug("debug log!"); LogInfo("test info log"); LogWarn("warning log"); + LogInfo("str[%s] int[%d]", "str1"); + LogInfo("str[%s] int[%d]", "str1",15); LogError("error log"); LogFatal("fatal !!!!"); + LogFatal("str[%s] int[%d]", "str1"); + LogFatal("str[%s] int[%d]", "str1", 17,16); + LogFatal("str"); return 0; } #endif diff --git a/cppcommon/logger.h b/cppcommon/logger.h index 7c6d28f..049a018 100644 --- a/cppcommon/logger.h +++ b/cppcommon/logger.h @@ -10,6 +10,7 @@ #include #include #include +#include #include "file_functs.h" #include "str_functs.h" #include "typedefs.h" @@ -23,12 +24,17 @@ #define CSTR_BUFFER_SIZE 1024 -#define LogDebug(msg) Logger::Logging(LL_DEBUG, msg, __FILE__, __LINE__) -#define LogInfo(msg) Logger::Logging(LL_INFO, msg, __FILE__, __LINE__) -#define LogWarn(msg) Logger::Logging(LL_WARN, msg, __FILE__, __LINE__) -#define LogError(msg) Logger::Logging(LL_ERROR, msg, __FILE__, __LINE__) -#define LogFatal(msg) Logger::Logging(LL_FATAL, msg, __FILE__, __LINE__) +//#define LogDebug(msg) Logger::Logging(LL_DEBUG, msg, __FILE__, __LINE__) +//#define LogInfo(msg) Logger::Logging(LL_INFO, msg, __FILE__, __LINE__) +//#define LogWarn(msg) Logger::Logging(LL_WARN, msg, __FILE__, __LINE__) +//#define LogError(msg) Logger::Logging(LL_ERROR, msg, __FILE__, __LINE__) +//#define LogFatal(msg) Logger::Logging(LL_FATAL, msg, __FILE__, __LINE__) +#define LogDebug(fmt, ...) Logger::LoggingF(LL_DEBUG, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogInfo(fmt, ...) Logger::LoggingF(LL_INFO, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogWarn(fmt, ...) Logger::LoggingF(LL_WARN, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogError(fmt, ...) Logger::LoggingF(LL_ERROR, __FILE__, __LINE__, fmt, ## __VA_ARGS__) +#define LogFatal(fmt, ...) Logger::LoggingF(LL_FATAL, __FILE__, __LINE__, fmt, ## __VA_ARGS__) namespace CPPCOMMON @@ -42,6 +48,7 @@ namespace CPPCOMMON public: static bool Logging(uint level, const string& msg, const char* fileName, int lineNo); static bool Logging(uint level, const char * msg, const char* fileName, int lineNo); + static bool LoggingF(uint level, const char* fileName, int lineNo, const string& fmt, ...); private: static char _cStrBuf[CSTR_BUFFER_SIZE]; static const char * _logLevel[LEVEL_ARRAY_SIZE]; diff --git a/cppcommon/str_functs.cpp b/cppcommon/str_functs.cpp index 1396aaa..f23afb6 100644 --- a/cppcommon/str_functs.cpp +++ b/cppcommon/str_functs.cpp @@ -7,15 +7,15 @@ namespace CPPCOMMON { //http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf - string string_format(const string fmt, ...) + string string_format(const char* fmt, ...) { - int size = 100; + int size = 256; std::string str; va_list ap; while (1) { str.resize(size); va_start(ap, fmt); - int n = vsnprintf((char *)str.c_str(), size, fmt.c_str(), ap); + int n = vsnprintf((char *)str.c_str(), size, fmt, ap); va_end(ap); if (n > -1 && n < size) { str.resize(n); diff --git a/cppcommon/str_functs.h b/cppcommon/str_functs.h index bc88da7..0dc38f3 100644 --- a/cppcommon/str_functs.h +++ b/cppcommon/str_functs.h @@ -21,7 +21,7 @@ namespace CPPCOMMON { using namespace std; - string string_format(const string fmt, ...) ; + string string_format(const char*, ...) ; string joinStr(const vector& source, const string& connector); vector splitStr(const string& source, const string& pattern = " \t\n"); bool splitStr(const string& source, vector& res, const string& pattern = " \t\n");