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

📄 hlog.c

📁 《Web编程专家指南》
💻 C
字号:
/*
 * hlog.c - routines to handle logging transactions and errors
 *
 * John Calcote 5/10/95
 * Internet: jcalcote@novell.com
 */

#include <stdarg.h>

#include "hcommon.h"


/* Module static variables - all threads, but only accessed from here */
static FILE *error_log;
static FILE *access_log;


/*************************************************************************
 * OpenLogs - open all log files into global i/o streams.
 *
 * Parameters:
 *    None.
 *
 * Returns:
 *    LOG_OK (0) if okay,
 *    LOG_NOERRLOG if error log file could not be opened.
 *    LOG_NOACCLOG if access log file could not be opened.
 *    In the event of a logfile open error, no logs are left open.
 */

int OpenLogs(void) {
   if (pCfg->pszErrorLog) {
   	if((error_log = fopen(pCfg->pszErrorLog, "a")) == NULL)
   		return LOG_NOERRLOG;
   }
   if (pCfg->pszAccessLog) {
   	if((access_log = fopen(pCfg->pszAccessLog, "a")) == NULL) {
         if (pCfg->pszErrorLog)
      		fclose(error_log);
   		return LOG_NOACCLOG;
   	}
   }
	return LOG_OK;
}


/*************************************************************************
 * CloseLogs - close all log files.
 *
 * Parameters:
 *    None.
 *
 * Returns:
 *    Nothing.
 */

void CloseLogs(void) {
   if (pCfg->pszAccessLog) {
   	fflush(access_log);
   	fclose(access_log);
   }
   if (pCfg->pszErrorLog) {
   	fflush(error_log);
   	fclose(error_log);
   }
	return;
}


/*************************************************************************
 * LogTransaction - write transaction to access log
 *
 * Parameters:
 *    request  - IN http client context block.
 *
 * Returns:
 *    Nothing.
 */

void LogTransaction(REQUEST *request) {
	time_t timer;
	char   *str, *tstr;

   if (pCfg->pszAccessLog) {
   	if ((str = malloc(MAX_STRING * 2)) != NULL) {
   		time(&timer);
   		tstr = asctime(localtime(&timer));
   		tstr[ strlen(tstr)-1 ] = '\0';

   		sprintf(str, "[%s] \"%s\" ",
   			request->szAddr ? request->szAddr : "<NO ADDR>",
   			tstr,
   			request->szRequest);

   		if (request->nStatus != -1)
   			sprintf(str + strlen(str), "%d ", request->nStatus);
   		else
   			strcat(str, "- ");

   		if (iosent(request->strm))
   			sprintf(str + strlen(str), "%d", iosent(request->strm));
   		else
   			strcat(str, "-");

   		fprintf(access_log, "%s\n", str);
   		fflush(access_log);
   		free(str);
      }
	}
	return;
}


/*************************************************************************
 * LogError - write error message to error log
 *
 *    format: "[Sun Sep 16 01:03:52 1973] <errmsgtext>\n"
 *
 * Parameters:
 *    None.
 *
 * Returns:
 *    Nothing.
 */

void LogError(char *format, ... ) {
	char   *tstr;
	time_t  timer;
	va_list arglist;

   if (pCfg->pszErrorLog) {
   	time(&timer);
   	tstr = asctime(localtime(&timer));
   	tstr[ strlen(tstr) - 1 ] = '\0';

   	EnterCritSec();					/* so no other threads can write till exit */
   	printf("[%s] ", tstr);
   	va_start(arglist, format);
   	vprintf(format, arglist);
   	va_end(arglist);
   	printf("\n");
   	ExitCritSec();

   	if  (error_log != NULL) {
   		fprintf(error_log, "[%s] ", tstr);
   		va_start(arglist, format);
   		vfprintf(error_log, format, arglist);
   		va_end(arglist);
   		fprintf(error_log, "\n");
   	}
   }
	return;
}


/*************************************************************************
 * LogReason - call LogError with formatted error message
 *
 *    format: "HTTP: Access to <file> failed
 *                   for <remotehost>, reason: <reason>"
 * Parameters:
 *    request  - IN http client context block
 *    reason   - IN reason for failure (text)
 *    file     - IN requested file (text)
 *
 * Returns:
 *    Nothing.
 */

void LogReason(REQUEST *request, char *reason, char *file) {
	LogError("HTTP: Access to %s failed for %s, reason: %s",
		file, request->szAddr ? request->szAddr : "<NO ADDR>", reason);
	return;
}


⌨️ 快捷键说明

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