Skip to content

Commit ca9f682

Browse files
committed
抛弃原始的log使用可变参接口来输出日志, 这是不安全的。
使用boost.format来完成该功能. kbengine#26
1 parent c8279d6 commit ca9f682

81 files changed

Lines changed: 878 additions & 793 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎kbe/src/examples/testnetwork/main.cpp‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,8 @@ class MyPacketReceiver : public InputNotificationHandler
216216
// is len weird?
217217
if (len == 0)
218218
{
219-
WARNING_MSG("PacketReceiver::processPendingEvents: "
220-
"Throwing REASON_GENERAL_NETWORK (1)- %s\n",
219+
WARNING_MSG(boost::format("PacketReceiver::processPendingEvents: "
220+
"Throwing REASON_GENERAL_NETWORK (1)- %1%\n") %
221221
kbe_strerror());
222222

223223
this->dispatcher().errorReporter().reportException(
@@ -288,12 +288,12 @@ class MyPacketReceiver : public InputNotificationHandler
288288

289289
// ok, I give up, something's wrong
290290
#ifdef _WIN32
291-
WARNING_MSG("PacketReceiver::processPendingEvents: "
292-
"Throwing REASON_GENERAL_NETWORK - %d\n",
291+
WARNING_MSG(boost::format("PacketReceiver::processPendingEvents: "
292+
"Throwing REASON_GENERAL_NETWORK - %1%\n") %
293293
wsaErr);
294294
#else
295-
WARNING_MSG("PacketReceiver::processPendingEvents: "
296-
"Throwing REASON_GENERAL_NETWORK - %s\n",
295+
WARNING_MSG(boost::format("PacketReceiver::processPendingEvents: "
296+
"Throwing REASON_GENERAL_NETWORK - %1%\n") %
297297
kbe_strerror());
298298
#endif
299299
this->dispatcher().errorReporter().reportException(
@@ -341,8 +341,8 @@ struct DialogOption
341341

342342
void init_network(void)
343343
{
344-
boost::format s = boost::format("writing %1%, x=%2% : %3%-th try") % "toto" % 40.23 % 50;
345-
std::string ss = s.str();
344+
345+
std::string ss = boost::str(boost::format("writing %1%, x=%2% : %3%-th try") % "toto" % 40.23 % 50);
346346
Mercury::g_trace_packet = 3;
347347

348348
mysocket.close();
@@ -389,7 +389,7 @@ void init_network(void)
389389
mysocket.convertAddress(ip.c_str(), address );
390390
if(mysocket.connect(htons(port), address) == -1)
391391
{
392-
ERROR_MSG("NetworkInterface::recreateListeningSocket: connect server is error(%s)!\n", kbe_strerror());
392+
ERROR_MSG(boost::format("NetworkInterface::recreateListeningSocket: connect server is error(%1%)!\n") % kbe_strerror());
393393
port = 0;
394394
continue;
395395
}
@@ -1184,7 +1184,7 @@ int main(int argc, char* argv[])
11841184
g_kbeSrvConfig.loadConfig("server/kbengine.xml");
11851185
Mercury::FixedMessages::getSingleton().loadConfig("../../res/server/fixed_mercury_messages.xml");
11861186
DebugHelper::initHelper(UNKNOWN_COMPONENT_TYPE);
1187-
INFO_MSG("你好,log4cxx---%d!---%s", 1, __FUNCTION__);
1187+
INFO_MSG(boost::format("你好,log4cxx---%1%!---%2%") % 1 % __FUNCTION__);
11881188
//LOG4CXX_INFO("Attempted to " << " in MemoryStream (pos:" << 111 << "size: " << 222 << ").\n");
11891189
init_network();
11901190
gdispatcher.processUntilBreak();

‎kbe/src/lib/client_lib/clientapp.cpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,8 @@ void ClientApp::onChannelDeregister(Mercury::Channel * pChannel)
144144
//-------------------------------------------------------------------------------------
145145
void ClientApp::onChannelTimeOut(Mercury::Channel * pChannel)
146146
{
147-
INFO_MSG( "ClientApp::onChannelTimeOut: "
148-
"Channel %s timed out.\n", pChannel->c_str());
147+
INFO_MSG(boost::format("ClientApp::onChannelTimeOut: "
148+
"Channel %1% timed out.\n") % pChannel->c_str());
149149

150150
networkInterface_.deregisterChannel(pChannel);
151151
pChannel->destroy();

‎kbe/src/lib/client_lib/kbemain.hpp‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ int kbeMainT(int argc, char * argv[], COMPONENT_TYPE componentType,
5454
return -1;
5555
}
5656

57-
INFO_MSG( "---- %s is running ----\n", COMPONENT_NAME_EX(componentType));
57+
INFO_MSG(boost::format("---- %1% is running ----\n") % COMPONENT_NAME_EX(componentType));
5858
int ret = app.run();
5959
app.finalise();
60-
INFO_MSG("%s has shut down.\n", COMPONENT_NAME_EX(componentType));
60+
INFO_MSG(boost::format("%1% has shut down.\n") % COMPONENT_NAME_EX(componentType));
6161
return ret;
6262
}
6363

‎kbe/src/lib/cstdkbe/memorystream.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class MemoryStreamException
7575

7676
void PrintPosError() const
7777
{
78-
ERROR_MSG("Attempted to %s in MemoryStream (pos:%d size: %d).\n" , (_m_add ? "put" : "get"), _m_pos, _m_size);
78+
ERROR_MSG(boost::format("Attempted to %1% in MemoryStream (pos:%2% size: %3%).\n") %
79+
(_m_add ? "put" : "get") % _m_pos % _m_size);
7980
}
8081
private:
8182
bool _m_add;

‎kbe/src/lib/db_mysql/db_interface_mysql.cpp‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,9 @@ bool DBInterfaceMysql::attach(const char* databaseName)
6363
{
6464
if(mysql_select_db(mysql(), databaseName) != 0)
6565
{
66-
ERROR_MSG( "DBInterfaceMysql::attach: Could not set active db[%s]\n", databaseName);
66+
ERROR_MSG(boost::format("DBInterfaceMysql::attach: Could not set active db[%1%]\n") %
67+
databaseName);
68+
6769
return false;
6870
}
6971
}
@@ -74,12 +76,12 @@ bool DBInterfaceMysql::attach(const char* databaseName)
7476

7577
if (mysql_set_character_set(mysql(), "utf8") != 0)
7678
{
77-
ERROR_MSG( "DBInterfaceMysql::attach: Could not set client connection character set to UTF-8\n" );
79+
ERROR_MSG("DBInterfaceMysql::attach: Could not set client connection character set to UTF-8\n" );
7880
}
7981
}
8082
catch (std::exception& e)
8183
{
82-
ERROR_MSG("DBInterfaceMysql::attach: %s\n", e.what() );
84+
ERROR_MSG(boost::format("DBInterfaceMysql::attach: %1%\n") % e.what());
8385
hasLostConnection_ = true;
8486
return false;
8587
}
@@ -110,7 +112,7 @@ bool DBInterfaceMysql::dropEntityTableFromDB(const char* tablename)
110112
{
111113
KBE_ASSERT(tablename != NULL);
112114

113-
DEBUG_MSG("DBInterfaceMysql::dropEntityTableFromDB: %s.\n", tablename);
115+
DEBUG_MSG(boost::format("DBInterfaceMysql::dropEntityTableFromDB: %1%.\n") % tablename);
114116

115117
char sql_str[MAX_BUF];
116118
kbe_snprintf(sql_str, MAX_BUF, "Drop table if exists %s;", tablename);
@@ -122,7 +124,8 @@ bool DBInterfaceMysql::dropEntityTableItemFromDB(const char* tablename, const ch
122124
{
123125
KBE_ASSERT(tablename != NULL && tableItemName != NULL);
124126

125-
DEBUG_MSG("DBInterfaceMysql::dropEntityTableItemFromDB: %s %s.\n", tablename, tableItemName);
127+
DEBUG_MSG(boost::format("DBInterfaceMysql::dropEntityTableItemFromDB: %1% %2%.\n") %
128+
tablename % tableItemName);
126129

127130
char sql_str[MAX_BUF];
128131
kbe_snprintf(sql_str, MAX_BUF, "alter table %s drop column %s;", tablename, tableItemName);
@@ -146,8 +149,8 @@ bool DBInterfaceMysql::query(const char* strCommand, uint32 size, bool showexeci
146149
{
147150
if(showexecinfo)
148151
{
149-
ERROR_MSG("DBInterfaceMysql::query: mysql is error(%d:%s)!\n",
150-
mysql_errno(pMysql_), mysql_error(pMysql_));
152+
ERROR_MSG(boost::format("DBInterfaceMysql::query: mysql is error(%1%:%2%)!\n") %
153+
mysql_errno(pMysql_) % mysql_error(pMysql_));
151154
}
152155
return false;
153156
}

‎kbe/src/lib/db_mysql/entity_table_mysql.cpp‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ void EntityTableMysql::init_db_item_name()
165165
//-------------------------------------------------------------------------------------
166166
bool EntityTableMysql::syncToDB(DBInterface* dbi)
167167
{
168-
DEBUG_MSG("EntityTableMysql::syncToDB(): %s.\n", tableName());
168+
DEBUG_MSG(boost::format("EntityTableMysql::syncToDB(): %1%.\n") % tableName());
169169

170170
char sql_str[MAX_BUF];
171171
std::string exItems = "";
@@ -353,7 +353,7 @@ DBID EntityTableMysql::writeTable(DBInterface* dbi, DBID dbid, MemoryStream* s,
353353
EntityTableItem* pTableItem = this->findItem(pid);
354354
if(pTableItem == NULL)
355355
{
356-
ERROR_MSG("EntityTable::writeTable: not found item[%u].\n", pid);
356+
ERROR_MSG(boost::format("EntityTable::writeTable: not found item[%1%].\n") % pid);
357357
return dbid;
358358
}
359359

@@ -515,7 +515,7 @@ bool EntityTableItemMysql_VECTOR2::isSameKey(std::string key)
515515
//-------------------------------------------------------------------------------------
516516
bool EntityTableItemMysql_VECTOR2::syncToDB(DBInterface* dbi)
517517
{
518-
DEBUG_MSG("EntityTableItemMysql_VECTOR2::syncToDB(): %s.\n", itemName());
518+
DEBUG_MSG(boost::format("EntityTableItemMysql_VECTOR2::syncToDB(): %1%.\n") % itemName());
519519

520520
if(!sync_item_to_db(dbi, itemDBType_.c_str(), tableName_.c_str(), db_item_names_[0]))
521521
return false;
@@ -601,7 +601,7 @@ bool EntityTableItemMysql_VECTOR3::isSameKey(std::string key)
601601
//-------------------------------------------------------------------------------------
602602
bool EntityTableItemMysql_VECTOR3::syncToDB(DBInterface* dbi)
603603
{
604-
DEBUG_MSG("EntityTableItemMysql_VECTOR3::syncToDB(): %s.\n", itemName());
604+
DEBUG_MSG(boost::format("EntityTableItemMysql_VECTOR3::syncToDB(): %1%.\n") % itemName());
605605

606606
if(!sync_item_to_db(dbi, itemDBType_.c_str(), tableName_.c_str(), db_item_names_[0]))
607607
return false;
@@ -690,7 +690,7 @@ bool EntityTableItemMysql_VECTOR4::isSameKey(std::string key)
690690
//-------------------------------------------------------------------------------------
691691
bool EntityTableItemMysql_VECTOR4::syncToDB(DBInterface* dbi)
692692
{
693-
DEBUG_MSG("EntityTableItemMysql_VECTOR4::syncToDB(): %s.\n", itemName());
693+
DEBUG_MSG(boost::format("EntityTableItemMysql_VECTOR4::syncToDB(): %1%.\n") % itemName());
694694

695695
if(!sync_item_to_db(dbi, itemDBType_.c_str(), tableName_.c_str(), db_item_names_[0]))
696696
return false;
@@ -770,7 +770,7 @@ void EntityTableItemMysql_VECTOR4::getReadSqlItem(DB_OP_TABLE_ITEM_DATA_BOX& opT
770770
//-------------------------------------------------------------------------------------
771771
bool EntityTableItemMysql_MAILBOX::syncToDB(DBInterface* dbi)
772772
{
773-
DEBUG_MSG("EntityTableItemMysql_MAILBOX::syncToDB(): %s.\n", itemName());
773+
DEBUG_MSG(boost::format("EntityTableItemMysql_MAILBOX::syncToDB(): %1%.\n") % itemName());
774774
return true;
775775
}
776776

@@ -869,7 +869,8 @@ bool EntityTableItemMysql_ARRAY::initialize(const PropertyDescription* pProperty
869869
//-------------------------------------------------------------------------------------
870870
bool EntityTableItemMysql_ARRAY::syncToDB(DBInterface* dbi)
871871
{
872-
DEBUG_MSG("EntityTableItemMysql_ARRAY::syncToDB(): %s.\n", itemName());
872+
DEBUG_MSG(boost::format("EntityTableItemMysql_ARRAY::syncToDB(): %1%.\n") % itemName());
873+
873874
if(pChildTable_)
874875
return pChildTable_->syncToDB(dbi);
875876

@@ -998,7 +999,7 @@ bool EntityTableItemMysql_FIXED_DICT::initialize(const PropertyDescription* pPro
998999
//-------------------------------------------------------------------------------------
9991000
bool EntityTableItemMysql_FIXED_DICT::syncToDB(DBInterface* dbi)
10001001
{
1001-
DEBUG_MSG("EntityTableItemMysql_FIXED_DICT::syncToDB(): %s.\n", itemName());
1002+
DEBUG_MSG(boost::format("EntityTableItemMysql_FIXED_DICT::syncToDB(): %1%.\n") % itemName());
10021003

10031004
EntityTableItemMysql_FIXED_DICT::FIXEDDICT_KEYTYPES::iterator iter = keyTypes_.begin();
10041005
for(; iter != keyTypes_.end(); iter++)
@@ -1068,7 +1069,7 @@ bool EntityTableItemMysqlBase::initialize(const PropertyDescription* pPropertyDe
10681069
//-------------------------------------------------------------------------------------
10691070
bool EntityTableItemMysql_DIGIT::syncToDB(DBInterface* dbi)
10701071
{
1071-
DEBUG_MSG("EntityTableItemMysql_DIGIT::syncToDB(): %s.\n", itemName());
1072+
DEBUG_MSG(boost::format("EntityTableItemMysql_DIGIT::syncToDB(): %1%.\n") % itemName());
10721073

10731074
if(datalength_ == 0)
10741075
{
@@ -1242,7 +1243,7 @@ void EntityTableItemMysql_DIGIT::getReadSqlItem(DB_OP_TABLE_ITEM_DATA_BOX& opTab
12421243
//-------------------------------------------------------------------------------------
12431244
bool EntityTableItemMysql_STRING::syncToDB(DBInterface* dbi)
12441245
{
1245-
DEBUG_MSG("EntityTableItemMysql_STRING::syncToDB(): %s.\n", itemName());
1246+
DEBUG_MSG(boost::format("EntityTableItemMysql_STRING::syncToDB(): %1%.\n") % itemName());
12461247

12471248
uint32 length = pPropertyDescription_->getDatabaseLength();
12481249
char sql_str[MAX_BUF];
@@ -1304,7 +1305,7 @@ void EntityTableItemMysql_STRING::getReadSqlItem(DB_OP_TABLE_ITEM_DATA_BOX& opTa
13041305
//-------------------------------------------------------------------------------------
13051306
bool EntityTableItemMysql_UNICODE::syncToDB(DBInterface* dbi)
13061307
{
1307-
DEBUG_MSG("EntityTableItemMysql_UNICODE::syncToDB(): %s.\n", itemName());
1308+
DEBUG_MSG(boost::format("EntityTableItemMysql_UNICODE::syncToDB(): %1%.\n") % itemName());
13081309

13091310
uint32 length = pPropertyDescription_->getDatabaseLength();
13101311
char sql_str[MAX_BUF];
@@ -1366,7 +1367,7 @@ void EntityTableItemMysql_UNICODE::getReadSqlItem(DB_OP_TABLE_ITEM_DATA_BOX& opT
13661367
//-------------------------------------------------------------------------------------
13671368
bool EntityTableItemMysql_BLOB::syncToDB(DBInterface* dbi)
13681369
{
1369-
DEBUG_MSG("EntityTableItemMysql_BLOB::syncToDB(): %s.\n", itemName());
1370+
DEBUG_MSG(boost::format("EntityTableItemMysql_BLOB::syncToDB(): %1%.\n") % itemName());
13701371
return sync_item_to_db(dbi, itemDBType_.c_str(), tableName_.c_str(), db_item_name());
13711372
}
13721373

‎kbe/src/lib/db_mysql/sqlstatement.hpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class SqlStatementInsert : public SqlStatement
141141
bool ret = SqlStatement::query(dbi);
142142
if(!ret)
143143
{
144-
ERROR_MSG("SqlStatementInsert::query: %s\n", (dbi != NULL ? dbi : dbi_)->getstrerror());
144+
ERROR_MSG(boost::format("SqlStatementInsert::query: %1%\n") % (dbi != NULL ? dbi : dbi_)->getstrerror());
145145
return false;
146146
}
147147

‎kbe/src/lib/dbmgr_lib/db_interface.cpp‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ DBInterface* DBUtil::createInterface(bool showinfo)
9797

9898
if(!dbinterface->attach(DBUtil::dbname()))
9999
{
100-
ERROR_MSG("DBUtil::createInterface: can't attach to database!\n\tdbinterface=%p\n\targs=%s",
101-
&dbinterface, dbinterface->c_str());
100+
ERROR_MSG(boost::format("DBUtil::createInterface: can't attach to database!\n\tdbinterface=%1%\n\targs=%2%") %
101+
&dbinterface % dbinterface->c_str());
102102

103103
delete dbinterface;
104104
return NULL;
@@ -107,7 +107,7 @@ DBInterface* DBUtil::createInterface(bool showinfo)
107107
{
108108
if(showinfo)
109109
{
110-
INFO_MSG("DBUtil::createInterface[%p]: %s\n", &dbinterface, dbinterface->c_str());
110+
INFO_MSG(boost::format("DBUtil::createInterface[%1%]: %2%\n") % &dbinterface % dbinterface->c_str());
111111
}
112112
}
113113

‎kbe/src/lib/dbmgr_lib/entity_table.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ DBID EntityTable::writeTable(DBInterface* dbi, DBID dbid, MemoryStream* s, Scrip
5959
EntityTableItem* pTableItem = this->findItem(pid);
6060
if(pTableItem == NULL)
6161
{
62-
ERROR_MSG("EntityTable::writeTable: not found item[%u].\n", pid);
62+
ERROR_MSG(boost::format("EntityTable::writeTable: not found item[%1%].\n") % pid);
6363
return dbid;
6464
}
6565

‎kbe/src/lib/entitydef/datatype.cpp‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -949,8 +949,8 @@ bool FixedArrayType::initialize(XmlPlus* xmlplus, TiXmlNode* node)
949949
}
950950
else
951951
{
952-
ERROR_MSG("FixedArrayType::initialize: can't found type[%s] by key[%s].\n",
953-
strType.c_str(), "ARRAY");
952+
ERROR_MSG(boost::format("FixedArrayType::initialize: can't found type[%1%] by key[%2%].\n") %
953+
strType.c_str() % "ARRAY");
954954

955955
return false;
956956
}
@@ -1027,7 +1027,9 @@ PyObject* FixedArrayType::createFromStream(MemoryStream* mstream)
10271027
{
10281028
if(mstream->opsize() == 0)
10291029
{
1030-
ERROR_MSG("FixedArrayType::createFromStream: invalid(size=%u), stream no space!\n", size);
1030+
ERROR_MSG(boost::format("FixedArrayType::createFromStream: invalid(size=%1%), stream no space!\n") %
1031+
size);
1032+
10311033
break;
10321034
}
10331035

@@ -1181,8 +1183,8 @@ bool FixedDictType::initialize(XmlPlus* xmlplus, TiXmlNode* node)
11811183
}
11821184
else
11831185
{
1184-
ERROR_MSG("FixedDictType::initialize: can't found array type[%s] by key[%s].\n",
1185-
strType.c_str(), typeName.c_str());
1186+
ERROR_MSG(boost::format("FixedDictType::initialize: can't found array type[%1%] by key[%2%].\n") %
1187+
strType.c_str() % typeName.c_str());
11861188

11871189
delete pDictItemDataType;
11881190
return false;
@@ -1206,8 +1208,8 @@ bool FixedDictType::initialize(XmlPlus* xmlplus, TiXmlNode* node)
12061208
}
12071209
else
12081210
{
1209-
ERROR_MSG("FixedDictType::initialize: can't found type[%s] by key[%s].\n",
1210-
strType.c_str(), typeName.c_str());
1211+
ERROR_MSG(boost::format("FixedDictType::initialize: can't found type[%1%] by key[%2%].\n") %
1212+
strType.c_str() % typeName.c_str());
12111213

12121214
delete pDictItemDataType;
12131215
return false;
@@ -1244,8 +1246,9 @@ bool FixedDictType::loadImplModule(std::string moduleName)
12441246

12451247
if(res_.size() != 2)
12461248
{
1247-
ERROR_MSG("FixedDictType::loadImplModule: %s impl is error! like:[moduleName.inst]\n",
1249+
ERROR_MSG(boost::format("FixedDictType::loadImplModule: %1% impl is error! like:[moduleName.inst]\n") %
12481250
moduleName.c_str());
1251+
12491252
return false;
12501253
}
12511254

@@ -1307,7 +1310,7 @@ PyObject* FixedDictType::impl_createObjFromDict(PyObject* dictData)
13071310
if(pyRet == NULL || !impl_isSameType(pyRet))
13081311
{
13091312
SCRIPT_ERROR_CHECK();
1310-
ERROR_MSG("FixedDictType::impl_createObjFromDict: %s.isSameType() is failed!\n",
1313+
ERROR_MSG(boost::format("FixedDictType::impl_createObjFromDict: %1%.isSameType() is failed!\n") %
13111314
moduleName_.c_str());
13121315

13131316
Py_RETURN_NONE;

0 commit comments

Comments
 (0)