Skip to content

Commit 7adfe92

Browse files
committed
继续完善messagelog。
1 parent 1228884 commit 7adfe92

10 files changed

Lines changed: 185 additions & 33 deletions

File tree

‎kbe/res/server/log4cxx_properties/messagelog.properties‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ log4j.appender.stdout=org.apache.log4j.ConsoleAppender
44
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
55

66
# Pattern to output the caller's file name and line number.
7-
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] [%d] - %m
7+
log4j.appender.stdout.layout.ConversionPattern=%m
88

99
log4j.appender.R=org.apache.log4j.RollingFileAppender
1010
log4j.appender.R.File=./messagelog.log
@@ -14,4 +14,4 @@ log4j.appender.R.MaxFileSize=100KB
1414
log4j.appender.R.MaxBackupIndex=10
1515

1616
log4j.appender.R.layout=org.apache.log4j.PatternLayout
17-
log4j.appender.R.layout.ConversionPattern=%5p %c [%t] [%d] - %m
17+
log4j.appender.R.layout.ConversionPattern=%m

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,20 @@ same license as the rest of the engine.
5252
#define DEFINE_IN_INTERFACE
5353
#include "loginapp/loginapp_interface.hpp"
5454

55+
#undef DEFINE_IN_INTERFACE
56+
#include "tools/message_log/messagelog_interface.hpp"
57+
#define DEFINE_IN_INTERFACE
58+
#include "tools/message_log/messagelog_interface.hpp"
59+
60+
#undef DEFINE_IN_INTERFACE
61+
#include "resourcemgr/resourcemgr_interface.hpp"
62+
#define DEFINE_IN_INTERFACE
63+
#include "resourcemgr/resourcemgr_interface.hpp"
64+
65+
#include "machine/machine_interface.hpp"
66+
#define DEFINE_IN_INTERFACE
67+
#include "machine/machine_interface.hpp"
68+
5569
#include "log4cxx/logger.h"
5670
#include "log4cxx/propertyconfigurator.h"
5771

‎kbe/src/lib/helper/debug_helper.cpp‎

Lines changed: 114 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
2424
#include "thread/threadguard.hpp"
2525
#include "network/channel.hpp"
2626
#include "resmgr/resmgr.hpp"
27+
#include "network/bundle.hpp"
28+
#include "network/event_dispatcher.hpp"
29+
#include "server/components.hpp"
2730

2831
#ifndef NO_USE_LOG4CXX
2932
#include "log4cxx/logger.h"
@@ -50,6 +53,8 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
5053
#endif
5154
#endif
5255

56+
#include "../../server/tools/message_log/messagelog_interface.hpp"
57+
5358
namespace KBEngine{
5459

5560
KBE_SINGLETON_INIT(DebugHelper);
@@ -96,13 +101,17 @@ _currFile(),
96101
_currFuncName(),
97102
_currLine(0),
98103
watcherChannels_(),
99-
logMutex()
104+
logMutex(),
105+
pBundle_(NULL),
106+
syncStarting_(false),
107+
pDispatcher_(NULL)
100108
{
101109
}
102110

103111
//-------------------------------------------------------------------------------------
104112
DebugHelper::~DebugHelper()
105113
{
114+
Mercury::Bundle::ObjPool().reclaimObject(pBundle_);
106115
}
107116

108117
//-------------------------------------------------------------------------------------
@@ -168,13 +177,94 @@ void DebugHelper::outTime()
168177
}
169178

170179
//-------------------------------------------------------------------------------------
171-
void DebugHelper::onMessage(LOG_TYPE logType, const char * str)
180+
void DebugHelper::sync()
172181
{
173-
/*
174-
int strlength = strlen(str);
175-
if(strlength <= 0)
182+
if(pBundle_ == NULL)
183+
return;
184+
185+
Components::COMPONENTS cts = Components::getSingleton().getComponents(MESSAGELOG_TYPE);
186+
Components::ComponentInfos* loginfos = NULL;
187+
188+
if(cts.size() > 0)
189+
loginfos = &(*cts.begin());
190+
191+
if(loginfos == NULL || loginfos->pChannel == NULL)
192+
{
193+
if(pBundle_->packets().size() > 32)
194+
{
195+
ERROR_MSG("DebugHelper::sync: can't found messagelog. packet size=%u.\n", pBundle_->packets().size());
196+
Mercury::Bundle::ObjPool().reclaimObject(pBundle_);
197+
pBundle_ = NULL;
198+
}
176199
return;
200+
}
177201

202+
int8 v = Mercury::g_trace_packet;
203+
Mercury::g_trace_packet = 0;
204+
loginfos->pChannel->send(pBundle_);
205+
Mercury::Bundle::ObjPool().reclaimObject(pBundle_);
206+
pBundle_ = NULL;
207+
Mercury::g_trace_packet = v;
208+
}
209+
210+
//-------------------------------------------------------------------------------------
211+
bool DebugHelper::process()
212+
{
213+
if(pBundle_ == NULL || pDispatcher_ == NULL)
214+
{
215+
syncStarting_ = false;
216+
return false;
217+
}
218+
219+
sync();
220+
return true;
221+
}
222+
223+
//-------------------------------------------------------------------------------------
224+
void DebugHelper::pDispatcher(Mercury:: EventDispatcher* dispatcher)
225+
{
226+
pDispatcher_ = dispatcher;
227+
if(syncStarting_)
228+
{
229+
pDispatcher_->addFrequentTask(this);
230+
}
231+
}
232+
233+
//-------------------------------------------------------------------------------------
234+
void DebugHelper::onMessage(int8 logType, const char * str, uint32 length)
235+
{
236+
if(g_componentType == MACHINE_TYPE ||
237+
g_componentType == CONSOLE_TYPE || g_componentType == MESSAGELOG_TYPE)
238+
return;
239+
240+
if(length <= 0)
241+
return;
242+
243+
if(pBundle_ == NULL)
244+
pBundle_ = Mercury::Bundle::ObjPool().createObject();
245+
246+
int8 v = Mercury::g_trace_packet;
247+
Mercury::g_trace_packet = 0;
248+
pBundle_->newMessage(MessagelogInterface::writeLog);
249+
250+
(*pBundle_) << logType;
251+
(*pBundle_) << g_componentType;
252+
(*pBundle_) << g_componentID;
253+
(*pBundle_) << g_componentOrder;
254+
255+
int64 t = time(NULL);
256+
(*pBundle_) << t;
257+
(*pBundle_) << str;
258+
259+
Mercury::g_trace_packet = v;
260+
if(!syncStarting_)
261+
{
262+
if(pDispatcher_)
263+
pDispatcher_->addFrequentTask(this);
264+
syncStarting_ = true;
265+
}
266+
267+
/*
178268
WATCH_CHANNELS::iterator iter = watcherChannels_.begin();
179269
for(; iter != watcherChannels_.end(); iter++)
180270
{
@@ -239,16 +329,17 @@ void DebugHelper::print_msg(const char * str, ...)
239329
#else
240330
va_list ap;
241331
va_start(ap, str);
332+
242333
#if KBE_PLATFORM == PLATFORM_WIN32
243-
_vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
334+
uint32 size = _vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
244335
#else
245-
vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
336+
uint32 size = vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
246337
#endif
247338
va_end(ap);
248339
LOG4CXX_INFO(g_logger, _g_buf);
249340
#endif
250341

251-
onMessage(LOG_PRINT, _g_buf);
342+
onMessage(LOG_DEBUG, _g_buf, size);
252343
}
253344

254345
//-------------------------------------------------------------------------------------
@@ -286,15 +377,15 @@ void DebugHelper::error_msg(const char * err, ...)
286377
va_list ap;
287378
va_start(ap, err);
288379
#if KBE_PLATFORM == PLATFORM_WIN32
289-
_vsnprintf(_g_buf, DBG_PT_SIZE, err, ap);
380+
uint32 size = _vsnprintf(_g_buf, DBG_PT_SIZE, err, ap);
290381
#else
291-
vsnprintf(_g_buf, DBG_PT_SIZE, err, ap);
382+
uint32 size = vsnprintf(_g_buf, DBG_PT_SIZE, err, ap);
292383
#endif
293384
va_end(ap);
294385
LOG4CXX_ERROR(g_logger, _g_buf);
295386
#endif
296387

297-
onMessage(LOG_ERROR, _g_buf);
388+
onMessage(LOG_DEBUG, _g_buf, size);
298389
}
299390

300391
//-------------------------------------------------------------------------------------
@@ -332,15 +423,15 @@ void DebugHelper::info_msg(const char * info, ...)
332423
va_list ap;
333424
va_start(ap, info);
334425
#if KBE_PLATFORM == PLATFORM_WIN32
335-
_vsnprintf(_g_buf, DBG_PT_SIZE, info, ap);
426+
uint32 size = _vsnprintf(_g_buf, DBG_PT_SIZE, info, ap);
336427
#else
337-
vsnprintf(_g_buf, DBG_PT_SIZE, info, ap);
428+
uint32 size = vsnprintf(_g_buf, DBG_PT_SIZE, info, ap);
338429
#endif
339430
va_end(ap);
340431
LOG4CXX_INFO(g_logger, _g_buf);
341432
#endif
342433

343-
onMessage(LOG_INFO, _g_buf);
434+
onMessage(LOG_DEBUG, _g_buf, size);
344435
}
345436

346437
//-------------------------------------------------------------------------------------
@@ -378,15 +469,15 @@ void DebugHelper::debug_msg(const char * str, ...)
378469
va_list ap;
379470
va_start(ap, str);
380471
#if KBE_PLATFORM == PLATFORM_WIN32
381-
_vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
472+
uint32 size = _vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
382473
#else
383-
vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
474+
uint32 size = vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
384475
#endif
385476
va_end(ap);
386477
LOG4CXX_DEBUG(g_logger, _g_buf);
387478
#endif
388479

389-
onMessage(LOG_DEBUG, _g_buf);
480+
onMessage(LOG_DEBUG, _g_buf, size);
390481
}
391482

392483
//-------------------------------------------------------------------------------------
@@ -424,16 +515,16 @@ void DebugHelper::warning_msg(const char * str, ...)
424515
va_list ap;
425516
va_start(ap, str);
426517
#if KBE_PLATFORM == PLATFORM_WIN32
427-
_vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
518+
uint32 size = _vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
428519
#else
429-
vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
520+
uint32 size = vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
430521
#endif
431522
va_end(ap);
432523
// printf("CRITICAL:%s(%d)\n\t%s\n", _currFile.c_str(), _currLine, _g_buf);
433524
LOG4CXX_WARN(g_logger, _g_buf);
434525
#endif
435526

436-
onMessage(LOG_WARNING, _g_buf);
527+
onMessage(LOG_WARNING, _g_buf, size);
437528
}
438529

439530
void DebugHelper::critical_msg(const char * str, ...)
@@ -470,9 +561,9 @@ void DebugHelper::critical_msg(const char * str, ...)
470561
va_list ap;
471562
va_start(ap, str);
472563
#if KBE_PLATFORM == PLATFORM_WIN32
473-
_vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
564+
uint32 size = _vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
474565
#else
475-
vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
566+
uint32 size = vsnprintf(_g_buf, DBG_PT_SIZE, str, ap);
476567
#endif
477568
va_end(ap);
478569
char buf[DBG_PT_SIZE];
@@ -483,7 +574,7 @@ void DebugHelper::critical_msg(const char * str, ...)
483574

484575
setFile("", "", 0);
485576

486-
onMessage(LOG_CRITICAL, _g_buf);
577+
onMessage(LOG_CRITICAL, _g_buf, size);
487578
}
488579
//-------------------------------------------------------------------------------------
489580

‎kbe/src/lib/helper/debug_helper.hpp‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
2525
#include <assert.h>
2626
#include <time.h>
2727
#include <stdarg.h>
28+
#include "cstdkbe/tasks.hpp"
2829
#include "cstdkbe/singleton.hpp"
2930
#include "thread/threadmutex.hpp"
3031
#include "network/common.hpp"
@@ -33,6 +34,8 @@ namespace KBEngine{
3334

3435
namespace Mercury{
3536
class Channel;
37+
class Bundle;
38+
class EventDispatcher;
3639
}
3740

3841
/**
@@ -41,7 +44,8 @@ namespace Mercury{
4144
void vutf8printf(FILE *out, const char *str, va_list* ap);
4245
void utf8printf(FILE *out, const char *str, ...);
4346

44-
class DebugHelper : public Singleton<DebugHelper>
47+
class DebugHelper : public Task,
48+
public Singleton<DebugHelper>
4549
{
4650
public:
4751
enum LOG_TYPE
@@ -67,18 +71,26 @@ class DebugHelper : public Singleton<DebugHelper>
6771
_currLine = line;
6872
_currFuncName = funcname;
6973
}
74+
75+
/**
76+
同步日志到messagelog
77+
*/
78+
void sync();
79+
bool process();
7080

7181
void outTime();
7282
static void outTimestamp(FILE* file);
7383

84+
void pDispatcher(Mercury:: EventDispatcher* dispatcher);
85+
7486
void print_msg(const char * str, ...);
7587
void debug_msg(const char * str, ...);
7688
void error_msg(const char * err, ...);
7789
void info_msg(const char * info, ...);
7890
void warning_msg(const char * str, ...);
7991
void critical_msg(const char * str, ...);
8092

81-
void onMessage(LOG_TYPE logType, const char * str);
93+
void onMessage(int8 logType, const char * str, uint32 length);
8294

8395
void registerWatch(Mercury::MessageID msgID, Mercury::Channel* pChannel);
8496
void unregisterWatch(Mercury::MessageID msgID, Mercury::Channel* pChannel);
@@ -88,6 +100,9 @@ class DebugHelper : public Singleton<DebugHelper>
88100
uint32 _currLine;
89101
WATCH_CHANNELS watcherChannels_;
90102
KBEngine::thread::ThreadMutex logMutex;
103+
Mercury::Bundle* pBundle_;
104+
bool syncStarting_;
105+
Mercury:: EventDispatcher* pDispatcher_;
91106
};
92107

93108
/*---------------------------------------------------------------------------------

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ int kbeMainT(int argc, char * argv[], COMPONENT_TYPE componentType,
7979
Resmgr::pirnt();
8080

8181
Mercury::EventDispatcher dispatcher;
82+
DebugHelper::getSingleton().pDispatcher(&dispatcher);
8283
Mercury::NetworkInterface networkInterface(&dispatcher,
8384
extlisteningPort_min, extlisteningPort_max, extlisteningInterface,
8485
(intlisteningPort != -1) ? htons(intlisteningPort) : -1, intlisteningInterface);

‎kbe/src/lib/server/serverapp.cpp‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,13 @@ void ServerApp::onAppActiveTick(Mercury::Channel* pChannel, COMPONENT_TYPE compo
261261
Components::ComponentInfos* cinfos =
262262
Componentbridge::getComponents().findComponent(componentType, KBEngine::getUserUID(), componentID);
263263

264-
KBE_ASSERT(cinfos != NULL);
264+
if(cinfos == NULL)
265+
{
266+
ERROR_MSG("ServerApp::onAppActiveTick[%x]: %s:%"PRAppID" not found.\n",
267+
pChannel, COMPONENT_NAME[componentType], componentID);
268+
return;
269+
}
270+
265271
pTargetChannel = cinfos->pChannel;
266272
pTargetChannel->updateLastReceivedTime();
267273
}

‎kbe/src/server/tools/bots/main.cpp‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,16 @@ along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
6060
#define DEFINE_IN_INTERFACE
6161
#include "cellappmgr/cellappmgr_interface.hpp"
6262

63+
#undef DEFINE_IN_INTERFACE
64+
#include "tools/message_log/messagelog_interface.hpp"
65+
#define DEFINE_IN_INTERFACE
66+
#include "tools/message_log/messagelog_interface.hpp"
67+
68+
#undef DEFINE_IN_INTERFACE
69+
#include "resourcemgr/resourcemgr_interface.hpp"
70+
#define DEFINE_IN_INTERFACE
71+
#include "resourcemgr/resourcemgr_interface.hpp"
72+
6373
using namespace KBEngine;
6474

6575
int KBENGINE_MAIN(int argc, char* argv[])

0 commit comments

Comments
 (0)