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

📄 log.cpp

📁 联通接收发送新程序
💻 CPP
字号:
/**************************************************************************/
/*	Copyright(C) 2001 by Nanjing XINWANG TEC CO.,LTD.					  */
/**************************************************************************/
/*	Name:		log.cpp					Version: 1.0.0					  */
/*	Created by:	Zhangbo					Date: 2001-12-04				  */
/*	Content:	implementation of class LogFile							  */
/*	Comment:	standard output and log file output						  */
/*				global variable clLog used by files which includes log.hpp*/ 
/*	Modified:															  */
/**************************************************************************/

#include <stdlib.h>
#include <time.h>
#include "log.hpp"

static const int LOG_SIZE = 1024;	//max length of log file line

//LogFile g_clLog;

void LogFile::SetProcName(const char* sProcName)
{
	assert((NULL != sProcName) && (0 != strlen(sProcName)));

	if (NULL != m_sProcName)
	{
		delete m_sProcName;
	}

	m_sProcName = new char[strlen(sProcName) + 1];
	if (NULL == m_sProcName)
	{
		Panic(0, "Memory exhausted !");
	}
	strcpy(m_sProcName, sProcName);
}

void LogFile::SetLogLevel(OUTPUT_LEVEL eLevel)
{
	if ((eLevel < LEVEL_DEBUG) || (eLevel >= LEVEL_COUNT))
	{
		Error(0, "Unkown log level %d !", (int)eLevel);
	}
	else
	{
		m_eLogLevel = eLevel;
	}
}

void LogFile::SetTermLevel(OUTPUT_LEVEL eLevel)
{
	if ((eLevel < LEVEL_DEBUG) || (eLevel >= LEVEL_COUNT))
	{
		Error(0, "Unkown log level %d !", (int)eLevel);
	}
	else
	{
		m_eTermLevel = eLevel;
	}
}

int LogFile::Open(const char* sFileName)
{
	assert (NULL != sFileName);
	if (0 == strlen(sFileName))
	{
		Error(0, "Log file name is blank !");
		return (-1);
	}

	if (m_bOpen)
	{
		if ((NULL != m_sFileName) && (0!= strcmp(sFileName, m_sFileName)))
		{
			fclose(m_pFile);
			m_pFile = NULL;
			m_bOpen = false;
		}
	}

	if (NULL != m_sFileName)
	{
		delete []m_sFileName;
	}
	m_sFileName = new char[strlen(sFileName) + 1];
	if (NULL == m_sFileName)
	{
		Panic(0, "Memory exhausted !");
	}
	strcpy(m_sFileName, sFileName);

	if (!m_bOpen)
	{
		m_pFile = fopen(m_sFileName, "a");
		if (NULL == m_pFile)
		{
			Error(errno, "Unable to open log file %s", m_sFileName);
			return (-1);
		}
		setbuf(m_pFile, NULL);
		m_bOpen = true;
	}

	return(0);
}

int LogFile::Close(void)
{
	if (m_bOpen) {
		if(fclose(m_pFile)) {
			Error(0, "Unable to close log file %s !", m_sFileName); 
			return(-1);
		}
		m_pFile = NULL;
		m_bOpen = false;
	} else {
		DeBug(0, "Log file %s is already closed !", m_sFileName);
	}

	return(0);
}

void LogFile::DeBug(int iErrno, const char* sFormat, ...)
{
	va_list args;
	va_start(args, sFormat);
	Output(LEVEL_DEBUG, iErrno, sFormat, args);
	va_end(args);
}

void LogFile::Info(int iErrno, const char* sFormat, ...)
{
	va_list args;
	va_start(args, sFormat);
	Output(LEVEL_INFO, iErrno, sFormat, args);
	va_end(args);
}

void LogFile::Warn(int iErrno, const char* sFormat, ...)
{
	va_list args;
	va_start(args, sFormat);
	Output(LEVEL_WARN, iErrno, sFormat, args);
	va_end(args);
}

void LogFile::Error(int iErrno, const char* sFormat, ...)
{
	va_list args;
	va_start(args, sFormat);
	Output(LEVEL_ERROR, iErrno, sFormat, args);
	va_end(args);
}

void LogFile::Panic(int iErrno, const char* sFormat, ...)
{
	va_list args;
	va_start(args, sFormat);
	Output(LEVEL_PANIC, iErrno, sFormat, args);
	va_end(args);
	exit(1);
}

void LogFile::Output(OUTPUT_LEVEL eOutputLevel, int iErrno,
				 const char* sFormat, va_list args)
{
	char sPrefix[LOG_SIZE] = {0};	//store common prefix of output
	char* p = sPrefix;

	//date and time
	time_t t = time(NULL);
	tm* stTm = localtime(&t);
	sprintf(p, "%04d-%02d-%02d %02d:%02d:%02d ",
		stTm->tm_year + 1900, stTm->tm_mon + 1, stTm->tm_mday,
		stTm->tm_hour, stTm->tm_min, stTm->tm_sec);

	//process name
	p = strchr(p, '\0');
	sprintf(p, "[%s] ", m_sProcName);

	//output level
	char* psTab[] = {"DEBUG: ", "INFO:  ", "WARN:  ", "ERROR: ", "PANIC: "};
	int iTabSize = sizeof(psTab) / sizeof(psTab[0]);
	p = strchr(p, '\0');
	int iLevel = (int)eOutputLevel;
	if ((iLevel < 0) || (iLevel >= iTabSize))
	{
		sprintf(p, "UNKOWN:");
	}
	else
	{
		sprintf(p, psTab[iLevel]);
	}

	char sBuffer[LOG_SIZE] = {0};	//real format
	if ((strlen(sPrefix) + strlen(sFormat)) > LOG_SIZE/ 2)
	{
		sprintf(sBuffer, "%s <OUTPUT message too long>\n", sPrefix);
	}
	else
	{
		if (iErrno == 0)
		{
			sprintf(sBuffer, "%s%s\n", sPrefix, sFormat);
		}
		else
		{
			sprintf(sBuffer, "%s%s\n%sSystem error %d: %s\n", sPrefix,
				sFormat, sPrefix, iErrno, strerror(iErrno));
		}
	}

	//standard output
	if (eOutputLevel >= m_eTermLevel) {
#ifdef _TEST
	vsprintf((char*)m_sOutput, sBuffer, args);
#else
		vprintf(sBuffer, args);
#endif
	}

	//log file output
	if ((eOutputLevel >= m_eLogLevel) && m_bOpen) {
		vfprintf(m_pFile, sBuffer, args);
	}
}

#ifdef _TEST
char* LogFile::GetOutput(void)
{
	return((char*)m_sOutput);
}
#endif

⌨️ 快捷键说明

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