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

📄 error.c

📁 < 虚拟机设计与实现> 的source code, linux版本
💻 C
字号:
/*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+                                                                   +
+ error.c - centralize syntax and symantic error handling           +
+                                                                   +
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include "error.h"

#include <time.h>
#include <string.h>

int maxErrors;  /*maximum # errors before shutdown, default = 10 */
int nErrors;	/*keep track of number of errors*/

FILE * errPtr;
char errFileName[128];

void incErrors()
{
	nErrors++;
	return;

} /*end incErrrors*/

void setUpErrorFile()
{
	time_t now;
	struct tm local_time;
	char digits[16];
	int retval;

	now = time(NULL);
	local_time = *(localtime(&now));

	strcpy(errFileName, "VM_ERROR_");
	
	sprintf(digits, "%u", (local_time.tm_mon + 1));
	strcat(errFileName, digits);
	strcat(errFileName, "_");

	sprintf(digits, "%u", local_time.tm_mday);
	strcat(errFileName, digits);
	strcat(errFileName, "_");

	sprintf(digits, "%lu", (local_time.tm_year + 1900));
	strcat(errFileName, digits);
	strcat(errFileName, "_");

	sprintf(digits, "%u", local_time.tm_hour);
	strcat(errFileName, digits);
	strcat(errFileName, "_");

	sprintf(digits, "%u", local_time.tm_min);
	strcat(errFileName, digits);
	strcat(errFileName, "_");

	sprintf(digits,"%u", local_time.tm_sec);
	strcat(errFileName, digits);

	strcat(errFileName, ".XML");

	errPtr = fopen(errFileName, "wb");
	if (errPtr == NULL)
	{
		ERROR1("setUpErrorFile(): error opening %s \n", errFileName);
		return;
	}

	retval = fprintf(errPtr, "<ERRORS>\r\n");
	if (retval < 0)
	{
		ERROR1("setUpErrorFile(): error writing to %s\n", errFileName);
		return;
	}
	return;

}

void closeErrorFile()
{
	int retval;

	if (errPtr != NULL)
	{			
		retval = fprintf(errPtr, "</ERRORS>");
		if (retval < 0)
		{
			ERROR1("closeErrorFile(): error writing </ERROR> to %s\n", errFileName);
		}
		if (fclose(errPtr))
		{
			ERROR1("closeErrorFile(): error closing %s\n", errFileName);
		}
		errPtr = NULL;
	}
}

void xmlBegin()
{
	char str1[] = "<Message>\r\n\t";
	char str2[] = "<time>%s</time>\r\n\t<content>";
	char datestr[64];
	time_t now;
	int retval;
	int i;

	retval = fprintf(errPtr, str1);
	if (retval < 0)
	{
		ERROR1("xmlBegin(): error writing to %s\n", errFileName);
		return;
	}
	
	/*get rid of '\n' at end of datestr created by ctime()*/
	now = time(NULL);
	strcpy(datestr, ctime(&now));
	retval = strlen(datestr);
	for (i = 0; i < retval; i++)
	{ 
		if (datestr[i] == '\n')
		{ 
			datestr[i]='\0'; 
		} 
	}

	retval = fprintf(errPtr, str2, datestr);
	if (retval < 0)
	{
		ERROR1("xmlBegin(): error writing to %s\n", errFileName);
	}
	return;

} /*end xmlBegin*/

void xmlEnd()
{
	int retval;

	retval = fprintf(errPtr, "</content>\r\n</Message>\r\n");
	if (retval < 0)
	{
		ERROR1("xmlEnd(): error writing to %s\n", errFileName);
	}
	return;

}

void testErrorFile()
{
    setUpErrorFile();
ERROR0("error0");
ERROR1("error2 : %s", "error2 content.");
	closeErrorFile();
}

⌨️ 快捷键说明

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