update cppcommon for var_args in loggingF

This commit is contained in:
wyy 2013-09-12 00:31:01 +08:00
parent c140cbdfc1
commit b9882f8297
4 changed files with 44 additions and 9 deletions

View File

@ -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

View File

@ -10,6 +10,7 @@
#include <fstream>
#include <string>
#include <stdio.h>
#include <stdarg.h>
#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];

View File

@ -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);

View File

@ -21,7 +21,7 @@
namespace CPPCOMMON
{
using namespace std;
string string_format(const string fmt, ...) ;
string string_format(const char*, ...) ;
string joinStr(const vector<string>& source, const string& connector);
vector<string> splitStr(const string& source, const string& pattern = " \t\n");
bool splitStr(const string& source, vector<string>& res, const string& pattern = " \t\n");