@@ -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+
5358namespace KBEngine {
5459
5560KBE_SINGLETON_INIT (DebugHelper);
@@ -96,13 +101,17 @@ _currFile(),
96101_currFuncName (),
97102_currLine (0 ),
98103watcherChannels_ (),
99- logMutex ()
104+ logMutex (),
105+ pBundle_ (NULL ),
106+ syncStarting_ (false ),
107+ pDispatcher_ (NULL )
100108{
101109}
102110
103111// -------------------------------------------------------------------------------------
104112DebugHelper::~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
439530void 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
0 commit comments