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

📄 logalert.cc

📁 此源代码只用于学习,不得用于其他商业活动 .
💻 CC
字号:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <time.h>

#include"logalert.h"

C_LogAlert::C_LogAlert()
{
	Stream = 0;
	m_pOperInfo = 0;
}

C_LogAlert::~C_LogAlert()
{
	Commit();
}

bool C_LogAlert::Initialize(const char *pchPath,const char *pchChannelName)
{
	if(Stream != NULL)
	{
		Commit();
	}
	char chTempPath[PATHLENGTH];

	if (strchr(pchChannelName, '/') != NULL)
	{
		return false;
	}

	if ((pchPath[0] == '\0') || (access(pchPath, F_OK|X_OK|R_OK) == -1))
	{
		return false;
	}

	strcpy(chTempPath, pchPath);

	if('/' == chTempPath[strlen(pchPath) - 1])
	{
		chTempPath[strlen(pchPath) -1 ] = '\0';
	}

	memset(pchCurrentPath,0,PATHLENGTH);
	strncpy(pchCurrentPath,chTempPath,PATHLENGTH);
	memset(pchCurrentChannelName,0,PATHLENGTH);
	strncpy(pchCurrentChannelName,pchChannelName,PATHLENGTH);

	struct hostent *he = 0;
	struct sockaddr_in their_addr;
	
	//获取主机名
	if(gethostname(szHostName, sizeof(szHostName)) != 0)
	{
		perror("gethostname()");
		return false;
	}

	if((he = gethostbyname(szHostName)) == 0)
	{
		perror("gethostbyname()");
		return false;
	}
	their_addr.sin_addr = *((struct in_addr *)he->h_addr);

	//填入主机ip
	strcpy(szHostIp, inet_ntoa(their_addr.sin_addr));


	return true;
}

bool C_LogAlert::Write(const char *pchLogAlert)
{
	if (Stream == NULL)
	{
		for(;;)
		{
			time_t Now;
  			struct tm  *Time;

		  	time(&Now);
  			Time = localtime(&Now);

			sprintf(pchCurrentFileName,"%s/%s.%04d%02d%02d%02d%02d%02d",
				pchCurrentPath,
				pchCurrentChannelName,
				Time->tm_year+1900,
				Time->tm_mon+1,
				Time->tm_mday,
				Time->tm_hour,
				Time->tm_min,
				Time->tm_sec);
			sprintf(pchTempFileName,"%s/~%s.%04d%02d%02d%02d%02d%02d",
				pchCurrentPath,
				pchCurrentChannelName,
				Time->tm_year+1900,
				Time->tm_mon+1,
				Time->tm_mday,
				Time->tm_hour,
				Time->tm_min,
				Time->tm_sec);
			if ((access(pchCurrentFileName,F_OK) == 0)||(access(pchTempFileName,F_OK) == 0))
			{
				sleep(1);
				continue;
			}
			if ((Stream = fopen(pchTempFileName,"wb+")) == NULL)
			{
				return false;
			}
			else
			{
				break;
			}
		}
	}
	if (fprintf(Stream,"%s\n",pchLogAlert) > 0)
	{
		fflush(Stream);
		return true;
	}
	else
	{
		return false;
	}
}

void C_LogAlert::Commit(void)
{
	if (Stream != NULL)
	{
		fclose(Stream);
		Stream = NULL;
		chmod(pchTempFileName,S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP);
		rename(pchTempFileName,pchCurrentFileName);
	}
}

bool C_LogAlert::InsertAlert(const char *Model_Code, const char *Alert_Level, const char *Alert_Code,
				const char *Channel_Code, const char *Oper_Object)
{
	time_t Now;
  	struct tm *Time;

	if (Stream == NULL)
	{
		for(;;)
		{
			time(&Now);
  			Time = localtime(&Now);

			sprintf(pchCurrentFileName,"%s/%s.%04d%02d%02d%02d%02d%02d",
				pchCurrentPath,
				pchCurrentChannelName,
				Time->tm_year+1900,
				Time->tm_mon+1,
				Time->tm_mday,
				Time->tm_hour,
				Time->tm_min,
				Time->tm_sec);
			sprintf(pchTempFileName,"%s/~%s.%04d%02d%02d%02d%02d%02d",
				pchCurrentPath,
				pchCurrentChannelName,
				Time->tm_year+1900,
				Time->tm_mon+1,
				Time->tm_mday,
				Time->tm_hour,
				Time->tm_min,
				Time->tm_sec);
			if ((access(pchCurrentFileName,F_OK) == 0)||(access(pchTempFileName,F_OK) == 0))
			{
				sleep(1);
				continue;
			}
			if ((Stream = fopen(pchTempFileName,"wb+")) == NULL)
			{
				return false;
			}
			else
			{
				break;
			}
		}
	}

	time(&Now);
        Time = localtime(&Now);
	if (fprintf(Stream,"%s,%s,%s,%s,%s,%s,%04d%02d%02d%02d%02d%02d,%s,0,0\n",
			Model_Code,
			Alert_Level,
			szHostName,
			szHostIp,
			Alert_Code,
			Channel_Code,
			Time->tm_year+1900,
			Time->tm_mon+1,
			Time->tm_mday,
			Time->tm_hour,
			Time->tm_min,
			Time->tm_sec,
			Oper_Object
			) > 0)
	{
		fflush(Stream);
		return true;
	}
	else
	{
		return false;
	}
}

//初始化记录启停信息的通道
//pchPath:存放操作日志文件的路径
//pchModelName:模块名
//pchChannelCode:通道号
bool C_LogAlert::InitForInfo(const char *pszPath, const char *pszModelName,	const char *pszChannelCode)
{
	char szTemp[STRING_LEN];
	memset(szTemp, 0, sizeof(szTemp));
	strcpy(szTemp, pszModelName);
	strcat(szTemp, ".");
	strcat(szTemp, pszChannelCode);
	if(!Initialize(pszPath, szTemp))
	{
		return false;
	}
	m_pOperInfo = new S_OperInfo;

	//填入用户名
	strcpy(m_pOperInfo->szUser, getenv("LOGNAME"));
	//填入模块名,也就是操作对象
	strcpy(m_pOperInfo->szModelName, pszModelName);
	struct hostent *he = 0;
	struct sockaddr_in their_addr;
	char szhostname[STRING_LEN];
	memset(szhostname, 0, sizeof(szhostname));

	if(gethostname(szhostname, sizeof(szhostname)) != 0)
	{
		perror("gethostname()");
		return false;
	}

	//填入主机名
	strcpy(m_pOperInfo->szHostName, szhostname);

	if((he = gethostbyname(szhostname)) == 0)
	{
		perror("gethostbyname()");
		return false;
	}
	their_addr.sin_addr = *((struct in_addr *)he->h_addr);

	//填入主机ip
	strcpy(m_pOperInfo->szIp, inet_ntoa(their_addr.sin_addr));

	//填入通道号
	strcpy(m_pOperInfo->szChannelCode, pszChannelCode);

	return true;
}

//记录进程启动信息
bool C_LogAlert::Start(const char *pszOperCode)
{
	strcpy(m_pOperInfo->szOperCode, pszOperCode);
	strcpy(m_pOperInfo->szOperation, "1");
	strcpy(m_pOperInfo->szOperResult, "1-");
	strcat(m_pOperInfo->szOperResult, m_pOperInfo->szChannelCode);
	if(!WriteOperLog())
	{
		return false;
	}
	return true;
}

//记录进程退出信息
bool C_LogAlert::Stop(const char *pszOperCode)
{
	strcpy(m_pOperInfo->szOperCode, pszOperCode);
	strcpy(m_pOperInfo->szOperation, "1");
	strcpy(m_pOperInfo->szOperResult, "0-");
	strcat(m_pOperInfo->szOperResult, m_pOperInfo->szChannelCode);
	if(!WriteOperLog())
	{
		return false;
	}
	delete m_pOperInfo;
	return true;
}

//记录进程操作信息
bool C_LogAlert::WriteOperLog(void)
{
	char szBuffer[STRING_LEN];
	memset(szBuffer, 0, sizeof(szBuffer));

	char szDate[STRING_LEN];
	char szTime[STRING_LEN];

	memset(szDate, 0, sizeof(szDate));
	memset(szTime, 0, sizeof(szTime));

	time_t timer;
	struct tm *today = 0;

	timer = time(0);
	today = localtime(&timer);
	sprintf(szDate,"%04d%02d%02d",today->tm_year + 1900,today->tm_mon+1,today->tm_mday);

	struct tm *timenow = 0;

	timer = time(0);
	timenow = localtime(&timer);
	sprintf(szTime,"%02d%02d%02d",timenow->tm_hour,timenow->tm_min,timenow->tm_sec);

	sprintf(m_pOperInfo->szStartDateTime, "%s%s", szDate, szTime);
	sprintf(m_pOperInfo->szEndDateTime, "%s%s", szDate, szTime);
	//用户名称,ip地址,主机名称,操作对象,操作编码,操作,操作结果,操作开始日期时间,操作结束日期时间
	sprintf(
			szBuffer,
			"%s,%s,%s,%s,%s,%s,%s,%s,%s",
			m_pOperInfo->szUser, m_pOperInfo->szIp, m_pOperInfo->szHostName,
			m_pOperInfo->szModelName, m_pOperInfo->szOperCode, m_pOperInfo->szOperation,
			m_pOperInfo->szOperResult, m_pOperInfo->szStartDateTime, m_pOperInfo->szEndDateTime
			);

	if(!Write(szBuffer))
	{
		return false;
	}
	return true;
}

bool C_LogAlert::InsertInfoPoint(const char *InfoCode, const char *Result1, const char *Result2)
{
	time_t Now;
  	struct tm *Time;

	if (Stream == NULL)
	{
		for(;;)
		{
			time(&Now);
  			Time = localtime(&Now);

			sprintf(pchCurrentFileName,"%s/%s.%04d%02d%02d%02d%02d%02d",
				pchCurrentPath,
				pchCurrentChannelName,
				Time->tm_year+1900,
				Time->tm_mon+1,
				Time->tm_mday,
				Time->tm_hour,
				Time->tm_min,
				Time->tm_sec);
			sprintf(pchTempFileName,"%s/~%s.%04d%02d%02d%02d%02d%02d",
				pchCurrentPath,
				pchCurrentChannelName,
				Time->tm_year+1900,
				Time->tm_mon+1,
				Time->tm_mday,
				Time->tm_hour,
				Time->tm_min,
				Time->tm_sec);
			if ((access(pchCurrentFileName,F_OK) == 0)||(access(pchTempFileName,F_OK) == 0))
			{
				sleep(1);
				continue;
			}
			if ((Stream = fopen(pchTempFileName,"wb+")) == NULL)
			{
				return false;
			}
			else
			{
				break;
			}
		}
	}

	time(&Now);
        Time = localtime(&Now);
	if (fprintf(Stream,"%s,%s,%s\n",
			InfoCode,
			Result1,
			Result2
			) > 0)
	{
		fflush(Stream);
		return true;
	}
	else
	{
		return false;
	}
}

⌨️ 快捷键说明

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