update LogFatal, print more readable error message when errors happened

This commit is contained in:
yanyiwu 2015-05-06 17:20:15 +08:00
parent 56c524f7a8
commit b70875f412
4 changed files with 28 additions and 15 deletions

View File

@ -27,13 +27,13 @@ class WorkerThread: public ITask {
void run() { void run() {
do { do {
if(!_setsockopt(_sockfd)) { if(!_setsockopt(_sockfd)) {
LogFatal("_getsockopt failed."); LogError("_getsockopt failed.");
break; break;
} }
string strSnd, strRetByHandler; string strSnd, strRetByHandler;
HttpReqInfo httpReq; HttpReqInfo httpReq;
if(!_receive(_sockfd, httpReq)) { if(!_receive(_sockfd, httpReq)) {
LogFatal("_receive failed."); LogError("_receive failed.");
break; break;
} }

View File

@ -47,7 +47,10 @@ namespace CppJieba
bool init(const string& dictPath, const string& userDictPath = "") bool init(const string& dictPath, const string& userDictPath = "")
{ {
assert(!_trie); if(_trie != NULL)
{
LogFatal("trie already initted");
}
_loadDict(dictPath); _loadDict(dictPath);
_calculateWeight(_nodeInfos); _calculateWeight(_nodeInfos);
_minWeight = _findMinWeight(_nodeInfos); _minWeight = _findMinWeight(_nodeInfos);
@ -104,7 +107,10 @@ namespace CppJieba
void _loadUserDict(const string& filePath, double defaultWeight, const string& defaultTag) void _loadUserDict(const string& filePath, double defaultWeight, const string& defaultTag)
{ {
ifstream ifs(filePath.c_str()); ifstream ifs(filePath.c_str());
assert(ifs.is_open()); if(!ifs.is_open())
{
LogFatal("file %s open failed.", filePath.c_str());
}
string line; string line;
DictUnit nodeInfo; DictUnit nodeInfo;
vector<string> buf; vector<string> buf;
@ -113,7 +119,10 @@ namespace CppJieba
{ {
buf.clear(); buf.clear();
split(line, buf, " "); split(line, buf, " ");
assert(buf.size() >= 1); if(buf.size() < 1)
{
LogFatal("split [%s] result illegal", line.c_str());
}
if(!TransCode::decode(buf[0], nodeInfo.word)) if(!TransCode::decode(buf[0], nodeInfo.word))
{ {
LogError("line[%u:%s] illegal.", lineno, line.c_str()); LogError("line[%u:%s] illegal.", lineno, line.c_str());
@ -132,7 +141,10 @@ namespace CppJieba
void _loadDict(const string& filePath) void _loadDict(const string& filePath)
{ {
ifstream ifs(filePath.c_str()); ifstream ifs(filePath.c_str());
assert(ifs.is_open()); if(!ifs.is_open())
{
LogFatal("file %s open failed.", filePath.c_str());
}
string line; string line;
vector<string> buf; vector<string> buf;
@ -140,7 +152,10 @@ namespace CppJieba
for(size_t lineno = 0; getline(ifs, line); lineno++) for(size_t lineno = 0; getline(ifs, line); lineno++)
{ {
split(line, buf, " "); split(line, buf, " ");
assert(buf.size() == DICT_COLUMN_NUM); if(buf.size() != DICT_COLUMN_NUM)
{
LogFatal("split result illegal, line: %s, result size: %u", line.c_str(), buf.size());
}
if(!TransCode::decode(buf[0], nodeInfo.word)) if(!TransCode::decode(buf[0], nodeInfo.word))
{ {

View File

@ -91,10 +91,9 @@ namespace CppJieba
void _loadIdfDict(const string& idfPath) void _loadIdfDict(const string& idfPath)
{ {
ifstream ifs(idfPath.c_str()); ifstream ifs(idfPath.c_str());
if(!ifs) if(!ifs.is_open())
{ {
LogError("open %s failed.", idfPath.c_str()); LogFatal("open %s failed.", idfPath.c_str());
assert(false);
} }
string line ; string line ;
vector<string> buf; vector<string> buf;
@ -127,10 +126,9 @@ namespace CppJieba
void _loadStopWordDict(const string& filePath) void _loadStopWordDict(const string& filePath)
{ {
ifstream ifs(filePath.c_str()); ifstream ifs(filePath.c_str());
if(!ifs) if(!ifs.is_open())
{ {
LogError("open %s failed.", filePath.c_str()); LogFatal("open %s failed.", filePath.c_str());
assert(false);
} }
string line ; string line ;
while(getline(ifs, line)) while(getline(ifs, line))

View File

@ -22,7 +22,7 @@
#define LogInfo(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_INFO, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__) #define LogInfo(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_INFO, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
#define LogWarn(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_WARN, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__) #define LogWarn(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_WARN, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
#define LogError(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_ERROR, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__) #define LogError(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_ERROR, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__)
#define LogFatal(fmt, ...) Limonp::Logger::LoggingF(Limonp::LL_FATAL, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__) #define LogFatal(fmt, ...) {Limonp::Logger::LoggingF(Limonp::LL_FATAL, FILE_BASENAME, __LINE__, fmt, ## __VA_ARGS__); abort();}
namespace Limonp { namespace Limonp {
using namespace std; using namespace std;