update cppcommon

This commit is contained in:
gwdwyy 2013-07-09 17:22:06 +08:00
parent 83a8474a1d
commit 4078887be5
4 changed files with 31 additions and 4 deletions

View File

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

View File

@ -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<<endl;
cerr<<_cStrBuf<<endl;
}
if(_logFile && level >= _logFileLevel)
{
@ -62,7 +62,7 @@ namespace CPPCOMMON
}
#ifdef UNIT_TEST
#ifdef LOGGER_UT
using namespace CPPCOMMON;
int main()
{

View File

@ -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<string>& src, const string& connectorStr)
{
string res;
@ -328,6 +351,7 @@ int main()
// //cout<<strlen(utf8str);
// cout<<utf8str<<endl;
//}
cout<<string_format("hehe%s11asd%dasf","[here]",2);
ifstream ifile("testdata/dict.txt");
string line;
while(getline(ifile, line))

View File

@ -8,10 +8,13 @@
#include <cctype>
#include <map>
#include <stdint.h>
#include <stdio.h>
#include <stdarg.h>
#include "typedefs.h"
namespace CPPCOMMON
{
using namespace std;
std::string string_format(const std::string fmt, ...) ;
string joinStr(const vector<string>& source, const string& connector);
vector<string> splitStr(const string& source, const string& pattern = " \t\n");
void splitStr(const string& source, vector<string>& out_vec, const string& pattern = " \t\n");