264 lines
5.3 KiB
C++
264 lines
5.3 KiB
C++
#define _DEBUG_CPP_
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <stdarg.h>
|
|
#include <time.h>
|
|
#include <string.h>
|
|
|
|
#include <string>
|
|
#include <iostream>
|
|
|
|
#ifdef _WIN32
|
|
#include <windows.h>
|
|
#include <tchar.h>
|
|
#endif
|
|
|
|
//#include "object.h"
|
|
#include "Socket.h"
|
|
#include "debug.h"
|
|
|
|
EDPrintDest g_nDest;//表示打印的目的地
|
|
CTCPSocket * g_pNetChannel = NULL;
|
|
FILE * g_pFileChannel = NULL;
|
|
|
|
//using namespace std;
|
|
/*******************************************************
|
|
int DebugPrint(const char * fmt, ...)
|
|
功能描述:
|
|
提供类似于printf的调试打印语句
|
|
********************************************************/
|
|
int DebugPrint(const char * fmt, ...)
|
|
{
|
|
char sDebug[MAX_DEBUG_PRINT];
|
|
sDebug[0] = '\0';
|
|
/*
|
|
int nLen=strlen(sDebug);
|
|
sDebug[nLen]='@';
|
|
sDebug[nLen+1]='\0';
|
|
*/
|
|
if( g_nDest!=e_ToNet ){
|
|
time_t now;
|
|
time(&now);
|
|
strftime(sDebug, sizeof(sDebug), "[%H:%M:%S] ",localtime(&now));
|
|
}
|
|
|
|
va_list vaParams;
|
|
va_start(vaParams,fmt);
|
|
|
|
vsprintf(sDebug+strlen(sDebug),fmt,vaParams);
|
|
|
|
if( g_nDest==e_ToNet ){
|
|
int nLen=strlen(sDebug);
|
|
sDebug[nLen]='@';
|
|
sDebug[nLen+1]='\0';
|
|
}
|
|
|
|
switch (g_nDest){
|
|
case e_ToFile:
|
|
{
|
|
if( g_pFileChannel==NULL )
|
|
return 0;
|
|
|
|
fprintf(g_pFileChannel,"%s\n",sDebug);
|
|
fflush(g_pFileChannel);
|
|
break;
|
|
}
|
|
case e_ToNet:
|
|
{
|
|
if( g_pNetChannel==NULL || !g_pNetChannel->IsOpen() )
|
|
return 0;
|
|
|
|
g_pNetChannel->Send( sDebug, strlen(sDebug));
|
|
break;
|
|
}
|
|
case e_ToScreen:
|
|
{
|
|
printf( "%s\n", sDebug );
|
|
break;
|
|
}
|
|
default:
|
|
{
|
|
#ifdef _WIN32
|
|
OutputDebugString( sDebug );
|
|
#else
|
|
printf( "%s\n", sDebug );
|
|
#endif
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
/*******************************************************
|
|
void DebugModuleInitialize(int nDebug,EDPrintDest nDest,char *sHost)
|
|
参数说明:
|
|
int nDebug
|
|
产品的名称
|
|
EDPrintDest nDest
|
|
使用什么方式打印
|
|
char *sHost
|
|
使用网络打印时用的主机地址
|
|
|
|
功能描述:
|
|
初始化调试模块
|
|
********************************************************/
|
|
void DebugModuleInitialize(int nDebug,EDPrintDest nDest,char *sHost)
|
|
{
|
|
if( nDebug==0 ) return;
|
|
g_nDebugFlag = nDebug;
|
|
g_nDest = nDest;
|
|
|
|
switch (g_nDest){
|
|
case e_ToFile:
|
|
{
|
|
g_pFileChannel = fopen( sHost, "a+" );
|
|
break;
|
|
}
|
|
case e_ToNet:
|
|
{
|
|
g_pNetChannel = new CTCPSocket( DEBUG_HOST, DEBUG_TCPPORT );
|
|
if( g_pNetChannel && !g_pNetChannel->IsOpen() ){
|
|
delete g_pNetChannel;
|
|
g_pNetChannel = NULL;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
/*******************************************************
|
|
void DebugModuleClose()
|
|
功能描述:
|
|
停止使用调试模块
|
|
********************************************************/
|
|
void DebugModuleClose()
|
|
{
|
|
switch (g_nDest){
|
|
case e_ToFile:
|
|
{
|
|
if( g_pFileChannel )
|
|
fclose( g_pFileChannel );
|
|
g_pFileChannel = NULL;
|
|
|
|
break;
|
|
}
|
|
case e_ToNet:
|
|
{
|
|
if( g_pNetChannel )
|
|
delete g_pNetChannel;
|
|
g_pNetChannel = NULL;
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DllDebugModuleInitialize(int nDebug,EDPrintDest nDest,void* pChannel)
|
|
{
|
|
if( nDebug==0 ) return;
|
|
g_nDebugFlag = nDebug;
|
|
g_nDest = nDest;
|
|
switch (g_nDest){
|
|
case e_ToFile:
|
|
{
|
|
g_pFileChannel = (FILE*)pChannel;
|
|
break;
|
|
}
|
|
case e_ToNet:
|
|
{
|
|
g_pNetChannel = (CTCPSocket*)pChannel;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void DllDebugModuleClose()
|
|
{
|
|
g_nDest = e_NoDest;
|
|
g_pFileChannel = NULL;
|
|
g_pNetChannel = NULL;
|
|
}
|
|
|
|
void GetDebugParams(int &nDebug,EDPrintDest &nDest,void **pChan)
|
|
{
|
|
nDebug = g_nDebugFlag;
|
|
nDest = g_nDest;
|
|
switch (g_nDest){
|
|
case e_ToFile:
|
|
*pChan = (void*)g_pFileChannel;
|
|
break;
|
|
|
|
case e_ToNet:
|
|
*pChan = (void*)g_pNetChannel;
|
|
break;
|
|
|
|
case e_ToScreen:
|
|
case e_ToDebug:
|
|
*pChan = NULL;
|
|
break;
|
|
}
|
|
}
|
|
|
|
long GetMemoryUsage()
|
|
{
|
|
long mem = 0L;
|
|
FILE* fp = fopen( "/proc/self/statm", "r" );
|
|
if( fp != NULL ){
|
|
long rss;
|
|
if ( fscanf( fp, "%*s%ld", &rss ) == 1 )
|
|
mem = rss << 2;
|
|
|
|
fclose( fp );
|
|
}
|
|
|
|
return mem;
|
|
}
|
|
|
|
#include <signal.h>
|
|
#include <thread.h>
|
|
#include <malloc.h>
|
|
extern char g_strServiceDir[256];
|
|
int u_nCurrMemUsedMax(0), u_nCurrMemUsedMin(0);
|
|
char u_szMemLog[256] = "";
|
|
CSema u_smMemLog("smMemLog",1);
|
|
|
|
void CheckMemoryUsage(int iParam)
|
|
{
|
|
CMutexLock lockProcess( &u_smMemLog );
|
|
|
|
int mem = (int)GetMemoryUsage();
|
|
if(mem>u_nCurrMemUsedMax){
|
|
if(u_nCurrMemUsedMax>u_nCurrMemUsedMin){ //记录波峰波谷
|
|
if(strlen(u_szMemLog)==0)
|
|
sprintf( u_szMemLog, "%s/mem.log", g_strServiceDir );
|
|
|
|
char sTime[32] = "";
|
|
time_t now;
|
|
time(&now);
|
|
strftime(sTime, 32, "%Y-%m-%d %H:%M:%S",localtime(&now));
|
|
|
|
FILE* fp = fopen( u_szMemLog, "a+" );
|
|
if(fp){
|
|
fprintf(fp, "[%s] %8d %8d\n", sTime, u_nCurrMemUsedMin, u_nCurrMemUsedMax);
|
|
fflush(fp);
|
|
fclose(fp);
|
|
}
|
|
}
|
|
|
|
u_nCurrMemUsedMax = u_nCurrMemUsedMin = mem;
|
|
|
|
DebugPrint("!!!!! Memory used: %d, msg: 0x%X", mem, iParam);
|
|
|
|
if(mem>131072 && !malloc_trim(0)){
|
|
DebugPrint("!!!!! Memory exceeded: %d", mem);
|
|
raise(SIGUSR1);
|
|
}
|
|
}
|
|
else if(mem<u_nCurrMemUsedMin){
|
|
u_nCurrMemUsedMin = mem;
|
|
DebugPrint("!!!!! Memory used: %d, msg: 0x%X", mem, iParam);
|
|
}
|
|
else if(iParam==0){
|
|
DebugPrint("!!!!! Memory used: %d, (%d - %d)", mem, u_nCurrMemUsedMin, u_nCurrMemUsedMax);
|
|
}
|
|
} |