Some code of debug mode

Source: Internet
Author: User

# Include "common. h"

# Include "CIniFile. h"
# Include "CRecordCfg. h"
# Include "CFileFactory. h"
# Include "CCtrlThread. h"
# Include "CThread. h"
# Include "CDeleteThread. h"
# Include "CSendFileinfoThread. h"
# Include "CConfigExt. h"
# Include "CUtil. h"
# Include "CStringExt. h"

// ================================================ ==============
// É (æ ¬ å °) ¨ ± å é
// ================================================ ==============
// Mahathíço ID
Static int g_nPID = 0;

// É å ° æ å ¿
Static char g_cExitFalg = '\ 0 ';

// Please refer Ȧ
Static int g_nGenerateCfg =-1;

// Export ¥ ç ç° «
Static int g_nLogModId = 0;

// *******************************
Static int doparams (INT argc, char * argv []);
Static int usage ();
Static void displayappinfo ();

// Mahathíço
Static void initlog ();
Static void setlogconfig (cconfigext * pconf );
Static void * checkstatusthread (void * Arg );
Static int mainfun (INT argc, char ** argv );

// Zookeeper ¡
// Static void starttransmitservice ();

// ~~%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
# Define debug_bt

Static void debug_printf_frameinfos ();
Static void dump_debug (INT signo );

// ================================================ ==============
// ¨ ± Å è %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% ¡
// ================================================ ==============
Cctrlthread * g_pctrlthread = NULL;
Crecordcfg * g_precordcfg = NULL;
Cconfigext * g_pconfigext = NULL;

// ================================================ ==============
// ĸ» ço ¨ ° ç ç°
// ================================================ ==============
Int main (INT argc, char ** argv)
{
G_cexitfalg = '\ 0 ';

Int ret = doparams (argc, argv );
If (Ret <= 0)
{
If (Ret <0)
{
Usage ();
}
Return 0;
}

If (g_npid = fork () = 0)/* executed by child process */
{
Mainfun (argc, argv );
}
Else/* executed by parent process */
{
Pthread_attr_t ATTR;
Pthread_t thrd;
Struct sched_param schedparam;
Pthread_attr_init (& ATTR );
Pthread_attr_setdetachstate (& ATTR, pthread_create_detached );
Pthread_attr_setschedpolicy (& ATTR, sched_fifo );
Sched_getparam (0, & schedparam );
Schedparam. sched_priority = sched_get_priority_max (sched_fifo );
Pthread_attr_setschedparam (& ATTR, & schedparam );

Int S = pthread_create (& thrd, & ATTR, checkstatusthread, null );
If (s! = 0)
{
Printf ("pthread_create () failed. % s \ n", strerror (errno ));
}
}

// Test_cmb ();
// Wait until the user entered 'q' to exit the main process (and the child process ).
While (g_cexitfalg! = 'Q ')
{
Scanf ("% C", & g_cexitfalg );
Sleep (1 );
}
Kill (g_npid, sigkill );

Return 0;
}

Int mainfun (INT argc, char ** argv)
{
DisplayAppInfo ();

// Maid
# Ifdef DEBUG_BT
Debug_Printf_FrameInfos ();
# Endif

// Please refer to the following link for more information:
InitLog ();
LOG (LOG_TYPE_NOTICE, "[Thread: 0x % x] mainFun Start.", pthread_self ());

// È ;» ************************
G_pConfigExt = new CConfigExt (CUtil: GetAppPath () + CONF_FILE_EXT );
If (g_pConfigExt! = NULL)
{
If (g_pConfigExt-> Parse ())
{
G_pConfigExt-> Dump ();

// É æ ° è ç ¥ å ç «
SetLogConfig (g_pConfigExt );

CUtil: SetLocalPort (g_pConfigExt-> m_nLocalPort );
}
Else // ***************************************
{
// Delete g_pConfigExt;
// G_pConfigExt = NULL;
//
// LOG (LOG_TYPE_FATAL, "éC ½ ä ä» mahathé .");
// Exit (EXIT_FAILURE );
}
}

// Adjust bandwidth § equal bandwidth ° equal capacity ° C equal bandwidth
CMysqlDatabase: InitLibrary ();

// *********************************************
G_pCtrlThread = new CCtrlThread ();
CThread * pThread = new CThread (g_pCtrlThread );
G_pCtrlThread-> m_pThread = pThread;

// É æ ä» ä ä¿ ¡ç ç° ç ço
CSendFileinfoThread * pSendFileInfoThread = new CSendFileinfoThread ();
CThread * pThreadSendFileInfo = new CThread (pSendFileInfoThread );
PSendFileInfoThread-> m_pThread = pThreadSendFileInfo;
G_pCtrlThread-> m_pSendinfoThread = pSendFileInfoThread;

// È ;» é éç½ æ ä» renewal »¶
CObjectFactory * pFac = new CFileFactory (CUtil: GetAppPath () + CONF_FILE );
If (pFac = NULL)
{
LOG (LOG_TYPE_ERROR, "pFac = NULL .");
Exit (EXIT_FAILURE );
}

// ¨ ±Éç½ å ¯ è ± ¡
G_pRecordCfg = new CRecordCfg (pFac );
If (! G_pRecordCfg-> ParseObjects ())
{
LOG (LOG_TYPE_FATAL, "éC ½ ä ä» è § £;;{±'' ¥ ï ç ?° ° é ° .");

Delete g_pRecordCfg;
Delete pFac;
Delete g_pCtrlThread;

Exit (EXIT_FAILURE );
}

// Please refer to the following picture for more information:
CMysqlInfo * pMysqlInfo = g_pRecordCfg-> m_pGlobal-> m_pMysqlInfo;
If (pMysqlInfo! = NULL)
{
CMysqlDatabase * pDb = CMysqlDatabase: GetInstance ();
PDb-> Init (pMysqlInfo );
}

¡
If (! G_pRecordCfg-> NewChannelModules ())
{
LOG (LOG_TYPE_FATAL, "GetChannelModules () failed .");

Delete g_pRecordCfg;
Delete pFac;
Delete g_pCtrlThread;

Exit (EXIT_FAILURE );
}

// £;{¥É }°
If (g_pRecordCfg-> m_pRecordModules.size () <= 0)
{
LOG (LOG_TYPE_ERROR, "No Valid RecordModule ");

// Ço å ° ç°è ·, ~~~~~ç° ç ço) ç ¡
// Delete g_pRecordCfg;
// Delete pFac;
// Exit (EXIT_FAILURE );
}

// Æ ¥ ç ~~~~%± ~~%° then
G_pRecordCfg-> DumpObjects (g_nGenerateCfg );

// É ¤ æ ä» ç ç° ç ço
Cdelethread * pDeleteThread = new CDeleteThread (g_pRecordCfg );
CThread * pThreadDelete = new CThread (pDeleteThread );
Pdelethread-> m_pThread = pThreadDelete;
G_pCtrlThread-> m_pDeleteThread = pdelethread;

// Éçá æ 'ç °ç° ç ço
CConfigThread * pConfigThread = new CConfigThread;
CThread * pThreadConfig = new CThread (pConfigThread );
PConfigThread-> m_pThread = pThreadConfig;
G_pCtrlThread-> m_pConfigThread = pConfigThread;

CSnmpThread * pSnmpThread = new CSnmpThread;
CThread * pThreadSnmp = new CThread (pSnmpThread );
PSnmpThread-> m_pThread = pThreadSnmp;
G_pCtrlThread-> m_pSnmpThread = pSnmpThread;

// Zookeeper ¨ ç ° ç ¨
G_pCtrlThread-> setRecordCfg (g_pRecordCfg );
PThread-> start ();

SNMP_NOTIFY ("Record Started! ");

// ĸ» ç° ç ço: DO NOTHING
While (1)
{
Sleep (1000 );
}
CMysqlDatabase: DeInitLibrary ();
Return 0;
}

Void displayAppInfo ()
{
Printf ("************************************* * ******************** \ n ");
Printf ");
Printf ("********************************* \ n ");
Printf ("********************************* \ n ");
Printf ("******* \ n ");
Printf ("******* \ n ");
Printf ("***************** \ n ");
Printf ("***************** \ n ");
Printf ("******* \ n ");
Printf ("******* \ n ");
Printf ("***************** \ n ");
Printf ("**************** \ n ");
Printf ("CopyRight  2011 seconds later 2012, All rights reserved. \ n ");
Printf ("ShangHai Figure-IT, Inc. \ n ");
Printf ("************************************* * ******************** \ n ");
Printf ("ço å ° çæ ¬: \ t % s \ n", strVession. c_str ());
Printf ("ç è {}¥ }:\ t % s \ n", _ DATE __, _ TIME __);
Printf ("gcc ç ¬: \ t % s \ n", _ VERSION __);
Printf ("è ~~~~~=°: \ t % s \ n", strPlatform. c_str ());
Printf ("ç ço ¨ å: \ n ");
For (unsigned int I = 0; I <(sizeof (strEnableModules)/sizeof (strEnableModules [0]); I + = 2)
{
Printf ("% s \ t % s \ n", strEnableModules [I]. c_str (), strEnableModules [I + 1]. c_str ());
}

Printf ("ç ço ¨ å: \ n ");
For (unsigned int I = 0; I <(sizeof (strDisableModules)/sizeof (strDisableModules [0]); I + = 2)
{
Printf ("% s \ t % s \ n", strDisableModules [I]. c_str (), strDisableModules [I + 1]. c_str ());
}
Printf ("\ n ");
}

Int usage ()
{
Printf ("usage: \ e [1mRecord \ e [0 m [\ e [4moption \ e [0 m] \ n ");
Printf ("\ e [34 m [\ e [4m-version \ e [0m \ e [34 m]: \ e [0m \ tprint the version and exit. \ n ");
Printf ("\ e [34 m [\ e [4m-help \ e [0m \ e [34 m]: \ e [0m \ tprint this usage and exit. \ n ");
Printf ("\ e [34 m [\ e [4m-gen \ e [0m \ e [34 m]: \ e [0m \ tgenerate the parsed configure to a file named 'config. sys. gen '. \ n ");

Return 0;
}

Void pr_exit (int status)
{
If (WIFEXITED (status ))
{
Printf ("normal termination, exit status = % d \ n", WEXITSTATUS (status ));
}
Else if (WIFSIGNALED (status ))
{
Printf ("abnormal termination, killed by signal number = % d % s \ n", WTERMSIG (status ),
# Ifdef WCOREDUMP
WCOREDUMP (status )? "(Core file generated) \ n": "(No core file generated) \ n ");
# Else
"WCOREDUMP not defined. \ n ");
# Endif
}
Else if (WIFSTOPPED (status ))
{
Printf ("child stopped, signal number = % d \ n", WSTOPSIG (status ));
}
}

Void * CheckStatusThread (void * Arg)
{
Printf ("[Thread] CheckStatusThread Start. \ n ");

Int nStatus = 0;
While (g_cExitFalg! = 'Q ')
{
Int n = waitpid (g_nPID, & nStatus, 0 );

// Continue;
If (n <= 0)
{
If (errno = ECHILD)
{
}
}
Else
{
Pr_exit (nStatus );

Printf ("************************************* * ****************** \ n ");
Printf ("notifition: Programme exited! Function will restart now! \ N ");
Printf ("************************************* * ****************** \ n ");

If (g_npid = fork () = 0)
{
Mainfun (0, null );
}
}
}

Return NULL;
}

Int doparams (INT argc, char * argv [])
{
Int ret = 1;
If (argc> 1)
{
Int I = 1;
For (; I <argc; I ++)
{
If (strcasecmp (argv [I], "-version") = 0) | (strcasecmp (argv [I], "-V") = 0 ))
{
Displayappinfo ();
Ret = 0;
}
Else if (strcasecmp (argv [I], "-help") = 0) | (strcasecmp (argv [I], "-h") = 0 ))
{
Usage ();
Ret = 0;
}
Else if (strcasecmp (argv [I], "-gen") | (strcasecmp (argv [I], "-G") = 0 )))
{
G_ngeneratecfg = 1;
}
Else
{
Ret =-1;
}
}
}

Return ret;
}

Void initlog ()
{
G_nlogmodid = cfitlog: instance ()-> registermod ("record ");
Cfitlog: instance ()-> setmodpriority (g_nlogmodid, log_type_notice );
Cfitlog: instance ()-> enablestdout ();
Cfitlog: instance ()-> enablerollingfileoutput (cutil: getapppath () + "record. log", 50*1024*1024, 3 );
// Cfitlog: instance ()-> enablememoutput ();
}

Void setlogconfig (cconfigext * pconf)
{
If (pconf = NULL)
{
Return;
}

String strLevel = pConf-> m_strLogLevel;
If (strLevel = "LOG_DEBUG ")
{
CFitLog: Instance ()-> setModPriority (g_nLogModId, LOG_TYPE_DEBUG );
}
Else if (strLevel = "LOG_INFO ")
{
CFitLog: Instance ()-> setModPriority (g_nLogModId, LOG_TYPE_INFO );
}
Else if (strLevel = "LOG_NOTICE ")
{
CFitLog: Instance ()-> setModPriority (g_nLogModId, LOG_TYPE_NOTICE );
}
Else if (strLevel = "LOG_WARN ")
{
CFitLog: Instance ()-> setModPriority (g_nLogModId, LOG_TYPE_WARN );
}
Else if (strLevel = "LOG_ERROR ")
{
CFitLog: Instance ()-> setModPriority (g_nLogModId, LOG_TYPE_ERROR );
}
Else if (strLevel = "LOG_FATAL ")
{
CFitLog: Instance ()-> setModPriority (g_nLogModId, LOG_TYPE_FATAL );
}
Else
{
Printf ("Invalid log level: '% s' \ n", strLevel. c_str ());
}

If (pConf-> m_nEnableLogSocket! = 0)
{
CFitLog: Instance ()-> enableNetworkOutput ("Record", LOG_NET_IP, LOG_NET_PORT, pConf-> m_strLogSocketAddr );
}
}

Void dump_debug (int signo)
{
Void * array [30] = {0 };
Size_t size;
Char ** strings = NULL;
Size_t I;

String strFile = CUtil: GetAppPath () + "signal. log ";
FILE * pf = fopen (strFile. c_str (), "w"); // your region should have your region ç ç°, é² ç ço å ° ä¸ é å ¯ sig'signal. log ä ä §

Size = backtrace (array, 30 );
Strings = backtrace_symbols (array, size );
If (strings! = NULL)
{
Fprintf (stderr, "[% s] get SIGSEGV [% d] signel. \ n", CDateTime: GetCurrentTime (). c_str (), signo );
Fprintf (stderr, "[% s] Obtained % zd stack frames. \ n", CDateTime: GetCurrentTime (). c_str (), size );
If (pf! = NULL)
{
Fprintf (pf, "[% s] get SIGSEGV [% d] signel. \ n", CDateTime: GetCurrentTime (). c_str (), signo );
Fprintf (pf, "[% s] Obtained % zd stack frames. \ n", CDateTime: GetCurrentTime (). c_str (), size );
Fflush (pf );
}

For (I = 0; I <size; I ++)
{
Fprintf (stderr, "[% s] % s \ n", CDateTime: GetCurrentTime (). c_str (), strings [I]);
Fflush (stderr );

If (pf! = NULL)
{
Fprintf (pf, "[% s] % s \ n", CDateTime: GetCurrentTime (). c_str (), strings [I]);
Fflush (pf );
}
}
}

If (pf! = NULL)
{
Fclose (pf );
Pf = NULL;
}
Free (strings );

Exit (0 );
}

Void Debug_Printf_FrameInfos ()
{
Signal (SIGSEGV, dump_debug );
Signal (SIGABRT, dump_debug );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.