⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 exception.cpp

📁 ABis无线接口全套资料
💻 CPP
字号:
/* ======================================================================== *\
 |
 |
 |  JOYIT Communication Technology
 |  Copyright (C)  2002-2003,  All Right Reserved.
 |
 |  System: Programmable Signaling Gateway
 |  Sub-system:
 |  Filename: exception.cpp
 |  Environment:    LINUX -- Red Hat 7.2 & GNU C/C++ Compiler 2.96
 |                vxWorks -- Tornado 2.0 & vxWorks 5.4
 |  Function description: Define method of handle error message class.
 |
 |
\* ======================================================================== */

#ifndef _EXCEPTION_HPP
#include "exception.hpp"
#endif

extern "C"
{
#ifndef _STRING_H
#include <string.h>
#endif

#ifndef _STDIO_H
#include <stdio.h>
#endif

#ifndef _TIME_H
#include <time.h>
#endif
};

Exception  * xcpPtr = NULL;

Exception::Exception(int ms, const char  * const logfilename) : memblock(NULL), memsize(ms*1024), nonceindex(0),
		queryindex(0), logit(DONT_LOG), log( )
{
	memblock = new char [memsize];
	if (NULL == memblock)
	{
		fprintf(stderr, "!!! Out of memory at Exception object. Require free memory size = %d KB\n", memsize/1024);
		fprintf(stderr, "!!! The system can performance continue, but it may be unstable.\n");
		// return;
	}

	for (int i=0; i<MAX_ERROR_MESSAGE_HOLDER; ++i)
	{
		memset(errmsg[i], 0, MAX_ERROR_MESSAGE_LENGTH);
	}

	/*
	 * Initialize the LogFile objects.
	 */

	if (logfilename != NULL)
	{
		log.SetFileName(logfilename);
		// log.LengthLimit( );
		log.OpenFile( );    // Open the log file.
		logit = LOG_IT;
		error(E_SUCC, SF_EXCEPTION, "System starting.");
	}
}

Exception::~Exception( )
{
	if (memblock != NULL)
	{
		delete memblock;
		memblock = NULL;
	}
	error(E_SUCC, SF_EXCEPTION, "System terminated.");
	log.CloseFile( );
}

void Exception::idle( )
{
	static time_t previous = 0;
	time_t current;
	current = time(NULL);

	if ((current - previous) > 60)
	{
		log.Flush( );
		previous = current;
	}
}

void Exception::SetLogFileName(const char * const logfilename)
{
	log.SetFileName(logfilename);
	log.OpenFile( );
}

void Exception::error(ERRCOD_T werr, UINT16 mid, const char * em)
{
	if (NULL == em)
	{
		return;
	}

	errorCode = werr;
	objId = mid;

	int len = strlen(em);

	if (len > MAX_ERROR_MESSAGE_LENGTH)
		len = MAX_ERROR_MESSAGE_LENGTH-1;

	if (len > 0)
	{
		memcpy(errmsg[nonceindex], em, len);
		errmsg[nonceindex][len] = '\0';
		if (logit == LOG_IT)
		{
			struct tm * ltm;
			time_t nonce;
			char buf[256];

			nonce = time(NULL);
			ltm = localtime(&nonce);
			sprintf(buf, "%02d%02d%02d %02d:%02d:%02d   ", ltm->tm_year+100, ltm->tm_mon+1,
					ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
			sprintf(buf+18, "ErrorCode=0x%04x ObjId=0x%04x  %s", werr, mid, errmsg[nonceindex]);
			log.Log(buf);
		}
		nonceindex++;
		nonceindex %= MAX_ERROR_MESSAGE_HOLDER;
		hasMessage = 1;
	}
	else
		hasMessage = 0;
}

void Exception::error(const char  * em)
{
	if (NULL == em)
	{
		return;
	}

	int len = strlen(em);

	if (len > MAX_ERROR_MESSAGE_LENGTH)
		len = MAX_ERROR_MESSAGE_LENGTH-1;

	if (len > 0)
	{
		memcpy(errmsg[nonceindex], em, len);
		errmsg[nonceindex][len] = '\0';
		if (logit == LOG_IT)
		{
			struct tm * ltm;
			time_t nonce;
			char buf[256];

			nonce = time(NULL);
			ltm = localtime(&nonce);
			sprintf(buf, "%02d%02d%02d %02d:%02d:%02d   ", ltm->tm_year+100, ltm->tm_mon+1,
					ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
			sprintf(buf+18, "%s", errmsg[nonceindex]);
			log.Log(buf);
		}
		nonceindex++;
		nonceindex %= MAX_ERROR_MESSAGE_HOLDER;
		hasMessage = 1;
	}
	else
		hasMessage = 0;
}

void Exception::error(const char  * cn, const char  * fn, ERRCOD_T werr, const char  * em) // Add 2005-01-14, by Wujianjin.
{
	if ((NULL == cn)  ||
		(NULL == fn))
	{
		return;
	}

	int len = 0;

	if (NULL != em)
	{
		len = strlen(em);
	}

	if (len > MAX_ERROR_MESSAGE_LENGTH)
	{
		len = MAX_ERROR_MESSAGE_LENGTH-1;
	}

	if (len > 0)
	{
		memcpy(errmsg[nonceindex], em, len);
		errmsg[nonceindex][len] = '\0';

		if (LOG_IT == logit)
		{
			struct tm * ltm;
			time_t nonce;
			char buf[256];

			nonce = time(NULL);
			ltm = localtime(&nonce);
			sprintf(buf, "%02d%02d%02d %02d:%02d:%02d   ", ltm->tm_year+100, ltm->tm_mon+1,
					ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
			sprintf(buf+18, "0x%X %s::%s  %s", werr, cn, fn, errmsg[nonceindex]);
			log.Log(buf);
		}
		nonceindex++;
		nonceindex %= MAX_ERROR_MESSAGE_HOLDER;
		hasMessage = 1;
	}
	else
		hasMessage = 0;
}
const char  * Exception::GetFirst( )
{
	INT8    tmpidx;

	tmpidx = nonceindex+1;
	tmpidx %= MAX_ERROR_MESSAGE_HOLDER;

	while ((tmpidx != nonceindex)  &&  (errmsg[tmpidx][0] == '\0'))
	{
		tmpidx++;
		tmpidx %= MAX_ERROR_MESSAGE_HOLDER;
	}

	queryindex = tmpidx;

	return errmsg[queryindex];
}

const char  * Exception::GetNext( )
{
	INT8    tmpidx;

	tmpidx = queryindex+1;
	tmpidx %= MAX_ERROR_MESSAGE_HOLDER;

	while ((tmpidx != queryindex)  &&  (errmsg[tmpidx][0] == '\0'))
	{
		tmpidx++;
		tmpidx %= MAX_ERROR_MESSAGE_HOLDER;
	}

	queryindex = tmpidx;

	return errmsg[queryindex];
}

const char  * Exception::GetLast( )
{
	INT8    tmpidx;

	tmpidx = (nonceindex == 0) ? (MAX_ERROR_MESSAGE_HOLDER-1) : (nonceindex-1);

	while ((tmpidx != (nonceindex-1))  &&  (errmsg[tmpidx][0] == '\0'))
	{
		tmpidx = (tmpidx == 0) ? (MAX_ERROR_MESSAGE_HOLDER-1) : (tmpidx-1);
	}

	queryindex = tmpidx;

	return errmsg[queryindex];
}

const char  * Exception::LastError(ERRCOD_T& werr, UINT16& mid)
{
	werr = errorCode;
	mid = objId;
	return (hasMessage) ? GetLast( ) : NULL;
}

// ------------------------------------------------------------------------
//
//  Revision list.
//  ==============
//
//  1.0,        2003-04-18,     Wu jianjin
//      Initial version.
//  1.1,        2003-05-19,     Wu jianjin
//      Ported to vxWorks.
//
// ------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -