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) bool Logger::Logging(uint level, const string& msg, const char * fileName, int lineNo)
{ {
return Logging(level, msg.c_str(), fileName, lineNo); return Logging(level, msg.c_str(), fileName, lineNo);
} }
bool Logger::Logging(uint level, const char * msg, const char* fileName, int lineNo) bool Logger::Logging(uint level, const char * msg, const char* fileName, int lineNo)
{ {
@ -61,8 +84,13 @@ int main()
LogDebug("debug log!"); LogDebug("debug log!");
LogInfo("test info log"); LogInfo("test info log");
LogWarn("warning log"); LogWarn("warning log");
LogInfo("str[%s] int[%d]", "str1");
LogInfo("str[%s] int[%d]", "str1",15);
LogError("error log"); LogError("error log");
LogFatal("fatal !!!!"); LogFatal("fatal !!!!");
LogFatal("str[%s] int[%d]", "str1");
LogFatal("str[%s] int[%d]", "str1", 17,16);
LogFatal("str");
return 0; return 0;
} }
#endif #endif

View File

@ -10,6 +10,7 @@
#include <fstream> #include <fstream>
#include <string> #include <string>
#include <stdio.h> #include <stdio.h>
#include <stdarg.h>
#include "file_functs.h" #include "file_functs.h"
#include "str_functs.h" #include "str_functs.h"
#include "typedefs.h" #include "typedefs.h"
@ -23,12 +24,17 @@
#define CSTR_BUFFER_SIZE 1024 #define CSTR_BUFFER_SIZE 1024
#define LogDebug(msg) Logger::Logging(LL_DEBUG, msg, __FILE__, __LINE__) //#define LogDebug(msg) Logger::Logging(LL_DEBUG, msg, __FILE__, __LINE__)
#define LogInfo(msg) Logger::Logging(LL_INFO, msg, __FILE__, __LINE__) //#define LogInfo(msg) Logger::Logging(LL_INFO, msg, __FILE__, __LINE__)
#define LogWarn(msg) Logger::Logging(LL_WARN, msg, __FILE__, __LINE__) //#define LogWarn(msg) Logger::Logging(LL_WARN, msg, __FILE__, __LINE__)
#define LogError(msg) Logger::Logging(LL_ERROR, msg, __FILE__, __LINE__) //#define LogError(msg) Logger::Logging(LL_ERROR, msg, __FILE__, __LINE__)
#define LogFatal(msg) Logger::Logging(LL_FATAL, 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 namespace CPPCOMMON
@ -42,6 +48,7 @@ namespace CPPCOMMON
public: public:
static bool Logging(uint level, const string& msg, const char* fileName, int lineNo); 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 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: private:
static char _cStrBuf[CSTR_BUFFER_SIZE]; static char _cStrBuf[CSTR_BUFFER_SIZE];
static const char * _logLevel[LEVEL_ARRAY_SIZE]; static const char * _logLevel[LEVEL_ARRAY_SIZE];

View File

@ -7,15 +7,15 @@
namespace CPPCOMMON namespace CPPCOMMON
{ {
//http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf //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; std::string str;
va_list ap; va_list ap;
while (1) { while (1) {
str.resize(size); str.resize(size);
va_start(ap, fmt); 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); va_end(ap);
if (n > -1 && n < size) { if (n > -1 && n < size) {
str.resize(n); str.resize(n);

View File

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