日志实现的文档说明
本日志的实现有cuizong007应用纯C完成,用于监控程序的运行情况。为了方便日志的说明cuizong007列举了一个加减乘除的小程序(除法只能整除),用于说明日志的实现方式。日志程序的实现可能会存在一些不合理的地方,请多多包涵和指正,欢迎技术交流。
Author:cuizong007
Date:2012-02-09
E-mail:cuizong007@sina.com
以下是源程序:
#include <time.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
/* open log 1,close log 0 */
#define LOG_RW 1
#define CONTENT_LEN 1024
char log_content[CONTENT_LEN];
int log_txt(int content_size)
{
int openfd = 0, write_num = 0;
time_t timer;
char time_tmp[30],time_tmp_name[13];
struct tm * timenow;
memset(time_tmp, 0, sizeof(time_tmp));
memset(time_tmp_name, 0, sizeof(time_tmp_name));
timer = time(NULL);
timenow=localtime(&timer);
sprintf( time_tmp, "%d-%02d-%02d %02d:%02d:%02d ", timenow->tm_year + 1900, timenow->tm_mon + 1,
timenow->tm_mday, timenow->tm_hour, timenow->tm_min, timenow->tm_sec);
sprintf( time_tmp_name, "%d%02d%02d.log", timenow->tm_year + 1900, timenow->tm_mon + 1,timenow->tm_mday);
openfd = open(time_tmp_name, O_CREAT | O_RDWR | O_APPEND, 0644);
if(openfd < 0)
{
printf("Open fplog file failed!\n");
return 0;
}
write_num = write(openfd, time_tmp, 22);
write_num = write(openfd, log_content, content_size);
if(write_num < content_size)
{
close(openfd);
printf("Write log failed!\n");
return 0;
}
close(openfd);
return 0;
}
int log_level(int level,int content_size)
{
if(1 == level)
{
log_txt(content_size);
}
return 0;
}
int main(int argc, char * argv[])
{
int num1 = 0, num2 = 0;
char ch = '0';
//do{
sprintf(log_content,"%s","---start register log---\n");
log_level(LOG_RW, strlen(log_content));
printf("Please input:");
scanf("%d%c%d",&num1, &ch, &num2);
sprintf(log_content,"input two num:num1 = %d, num2 = %d\n",num1, num2);
log_level(LOG_RW, strlen(log_content));
switch(ch)
{
case '+':
printf("%d + %d = %d\n",num1, num2, num1 + num2);
sprintf(log_content,"%d + %d = %d\n",num1, num2, num1 + num2);
log_level(LOG_RW,strlen(log_content));
break;
case '-':
printf("%d - %d = %d\n",num1, num2, num1 - num2);
sprintf(log_content,"%d - %d = %d\n",num1, num2, num1 - num2);
log_level(LOG_RW,strlen(log_content));
break;
case '*':
printf("%d * %d = %d\n",num1, num2, num1 * num2);
sprintf(log_content,"%d * %d = %d\n",num1, num2, num1 * num2);
log_level(LOG_RW,strlen(log_content));
break;
case '/':
printf("%d / %d = %d\n",num1, num2, num1 / num2);
sprintf(log_content,"%d / %d = %d\n",num1, num2, num1 / num2);
log_level(LOG_RW,strlen(log_content));
break;
default:
sprintf(log_content,"Sorry.The program is not found [%c] characters.\n",ch);
log_level(LOG_RW,strlen(log_content));
printf("Input error,please check your input number or characters!\n");
break;
}
sprintf(log_content,"%s","---finish---\n");
log_level(LOG_RW,strlen(log_content));
return 0;
}
函数说明:
1. sprinf(char * log_content, “format”,”” );
l char * log_content 表示存放要格式化的内容;
l format 表示根据具体的需要格式;
l ‘’’’ 表示的是根据format的格式,填写相应的内容。
2. log_level(int LOG_RW, int content_size);
l int LOG_RW 表示控制日志是否进行记入(0表示不计入,1表示记入);
l int content_size 表示log_content内容的长度。
3. log_txt(char * content_size);
l char * content_size 表示的是log_content内容的长度。
函数调用关系:
开始 -> main() -> sprint() -> log_level() -> log_txt() -> open() -> write() -> close() -> 结束