From 4078887be5fcb2f49e1990932020a55b6fdbe4bd Mon Sep 17 00:00:00 2001 From: gwdwyy Date: Tue, 9 Jul 2013 17:22:06 +0800 Subject: [PATCH] update cppcommon --- cppcommon/Makefile | 2 +- cppcommon/logger.cpp | 6 +++--- cppcommon/str_functs.cpp | 24 ++++++++++++++++++++++++ cppcommon/str_functs.h | 3 +++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/cppcommon/Makefile b/cppcommon/Makefile index 891e7b0..2b01f8c 100644 --- a/cppcommon/Makefile +++ b/cppcommon/Makefile @@ -33,7 +33,7 @@ vec_functs.test: vec_functs.cpp vec_functs.h vec_functs.tcc g++ -o $@ $< -DTEST_VEC_FUNCTS logger.ut: logger.cpp logger.h file_functs.cpp file_functs.h - g++ -o $@ $< file_functs.cpp -DUNIT_TEST + g++ -o $@ $< file_functs.cpp -DLOGGER_UT config.ut: config.cpp config.h g++ -o $@ $< -DCONFIG_UT $(CMLIB) diff --git a/cppcommon/logger.cpp b/cppcommon/logger.cpp index 383aaae..7823749 100644 --- a/cppcommon/logger.cpp +++ b/cppcommon/logger.cpp @@ -32,7 +32,7 @@ namespace CPPCOMMON createDir(_logDir); } _logFile.open((string(_logDir) + string(_logName)).c_str(), ios::app); - + } bool Logger::Logging(unsigned int level, const string& msg, const string& fileName, const int& lineNo) { @@ -47,7 +47,7 @@ namespace CPPCOMMON sprintf(_cStrBuf, _logFormat, timeStr.c_str(), fileName.c_str(), lineNo, _logLevel[level], msg.c_str()); if(_isCoutOpen && level >= _logCoutLevel) { - cout<<_cStrBuf<= _logFileLevel) { @@ -62,7 +62,7 @@ namespace CPPCOMMON } -#ifdef UNIT_TEST +#ifdef LOGGER_UT using namespace CPPCOMMON; int main() { diff --git a/cppcommon/str_functs.cpp b/cppcommon/str_functs.cpp index 9ef3b92..8cf2170 100644 --- a/cppcommon/str_functs.cpp +++ b/cppcommon/str_functs.cpp @@ -2,6 +2,29 @@ namespace CPPCOMMON { + //http://stackoverflow.com/questions/2342162/stdstring-formatting-like-sprintf + std::string string_format(const std::string fmt, ...) + { + int size = 100; + 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); + va_end(ap); + if (n > -1 && n < size) { + str.resize(n); + return str; + } + if (n > -1) + size = n + 1; + else + size *= 2; + } + return str; + } + string joinStr(const vector& src, const string& connectorStr) { string res; @@ -328,6 +351,7 @@ int main() // //cout< #include #include +#include +#include #include "typedefs.h" namespace CPPCOMMON { using namespace std; + std::string string_format(const std::string fmt, ...) ; string joinStr(const vector& source, const string& connector); vector splitStr(const string& source, const string& pattern = " \t\n"); void splitStr(const string& source, vector& out_vec, const string& pattern = " \t\n");