LINUX下C语言编程怎么打印日志

Python014

LINUX下C语言编程怎么打印日志,第1张

我们的程序一般都会产生输出信息。但是服务器程序一般却不希望输出信息到屏幕上,因为没有人盯着你的程序执行。所以我们要把一些信息写成日志文件,正常情况下运行程序的人不用关心日志里的内容,只有在出现问题的时候才会查看日志文件里的内容以确定问题所在。

但如果我们的程序要自己生成一个文件来保存日志却不是好主意,因为这一方面增加了维护程序运行的人的负担,另一方面自己维护起系统来也多有不便。

在Linux系统中有一个系统日志,通常放在/var/log目录下,比如文件名是syslog的,系统中的一些程序产生的日志信息都会存放到这个文件里。日志文件有固定的格式,比如第1列是消息产生的时间,第2列是机器名(因为日志记录程序支持远程连接),第3列是标记信息(一般就是程序名称)等。而且对应的有一些工具来对这个日志进行维护,比如通过轮回机制保证日志文件大小不会把磁盘空间占尽。所以我们把自己程序的信息也写到这个系统日志里是比较好的想法。

在GNU C语言库提供的内容中,有接口可以用来做这件事。用下面的命令查看:

nm -D /lib/libc.so.6 | grep log

可以看到一些调用:

000b9410 T closelog

0008b870 T getlogin

0008b960 T getlogin_r

000d0180 T __getlogin_r_chk

000bd190 T klogctl

00027450 T __open_catalog

000b9380 T openlog

0008bae0 T setlogin

000b8b80 T setlogmask

000b9350 T syslog

000b9320 T __syslog_chk

000b92f0 T vsyslog

000b8da0 T __vsyslog_chk

这里面的三个函数openlog, syslog, closelog是一套系统日志写入接口。另外那个vsyslog和syslog功能一样,只是参数格式不同。

程序的用法示例代码如下:

#include <syslog.h>

int main(int argc, char **argv)

{

    openlog("MyMsgMARK", LOG_CONS | LOG_PID, 0)

    syslog(LOG_DEBUG,

           "This is a syslog test message generated by program '%s'\n",

           argv[0])

    closelog()

    return 0

}

编译生成可执行程序后,运行一次程序将向/var/log/syslog文件添加一行信息如下:

   Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog test message generated by program './a.out'

Feb 12 08:48:38 localhost MyMsgMARK[7085]: This is a syslog test message generated by program './a.out'

  LOG_CONS

              Write directly to system console if there is an error while sending to system logger.

       LOG_NDELAY

              Open the connection immediately (normally, the connection is opened when the first message is logged).

       LOG_NOWAIT

              Don’t  wait  for  child processes that may have been created while logging the message.  (The GNU C library does not create a

              child process, so this option has no effect on Linux.)

       LOG_ODELAY

              The converse of LOG_NDELAY opening of the connection is delayed until syslog() is called.  (This is the  default,  and  need

              not be specified.)

       LOG_PERROR

              (Not in SUSv3.) Print to stderr as well.

       LOG_PID

              Include PID with each message.

第三个参数指明记录日志的程序的类型。

syslog函数及参数

syslog函数用于把日志消息发给系统程序syslogd去记录,此函数原型是:

void syslog(int priority, const char *format, ...)

第一个参数是消息的紧急级别,第二个参数是消息的格式,之后是格式对应的参数。就是printf函数一样使用。

如果我们的程序要使用系统日志功能,只需要在程序启动时使用openlog函数来连接syslogd程序,后面随时用syslog函数写日志就行了。

另外,作为syslog的替代程序的新一代工具是syslog-ng,syslog-ng具有很强的网络功能,可以方便地把多台机器上的日志保存到一台中心日志服务器上。

#include <stdlib.h>

#include <string.h>

#include <stdio.h>

#include <dirent.h>

#include <time.h>

#define LOGFILE "./dir_log_0"

int g_Count

//#define MAXLEN  1024

void WriteDebugLog(char *str)

int main(int argc, char **argv)

{

    char str[1024]={0}

    strcpy(str,"file no find")

    int i=0,j=0

    for (i=0i<10i++)

    {

        for (j=0j<50j++)

        {

            WriteDebugLog(str)    

        }

            

    }

    return 0

}

void WriteDebugLog(char *str)

{

    char buf[2048]={0}

    char logFileName[50]={0}

    //long MAXLEN = 50*1024*1024//50MB

    int iMax = 1024//1K

    time_t timep

    FILE *fp = NULL

    struct tm *p

    time(&timep)

    p = localtime(&timep)

    memset(buf,0,sizeof(buf))

    sprintf(buf,"[%d-%d-%d %d:%d:%d][DEBUG]",(1900+p->tm_year),(1+p->tm_mon), p->tm_mday,p->tm_hour, p->tm_min, p->tm_sec)//星期p->tm_wday

    strcat(buf,str)

    strcat(buf,"\r\n")

    strcpy(logFileName,LOGFILE)

    int len = strlen(logFileName)

    logFileName[len-1] = '0'+g_Count

    fp = fopen(logFileName,"r")

    if(fp==NULL)

    {

        fp = fopen(logFileName,"w+")

    }

    else

    {

        fseek(fp,0,2)//SEEK_END值为2

        if( ftell(fp) >= iMax)

        {

            fclose(fp)

                

            if (g_Count >= 9) 

            {

                logFileName[len-1] = '0'

                g_Count=0

            }

            else

            {

                g_Count++

                logFileName[len-1] = '0'+g_Count

            //  printf("\n%c",'0'+g_Count)

            }

            fp = fopen(logFileName,"w+")

        }

        else

        { 

            fclose(fp)

            fp = fopen(logFileName,"a")

        }

    }

    fwrite(buf,1,strlen(buf),fp)

    fclose(fp)

}

可以分三步来做:

做两个简单的守护进程,并能正常运行

监控进程是否在运行

启动进程

综合起来就可以了,代码如下:

被监控进程thisisatest.c(来自http://www.cnblogs.com/ringwang/p/3528093.html):

#include<unistd.h>

#include<signal.h>

#include<stdio.h>

#include<stdlib.h>

#include<sys/param.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<time.h>

void init_daemon()

{

int pid

int i

pid=fork()

if(pid<0)  

  exit(1) //创建错误,退出

else if(pid>0) //父进程退出

  exit(0)

setsid()//使子进程成为组长

pid=fork()

if(pid>0)

  exit(0)//再次退出,使进程不是组长,这样进程就不会打开控制终端

else if(pid<0)  

  exit(1)

//关闭进程打开的文件句柄

for(i=0i<NOFILEi++)

  close(i)

chdir("/root/test") //改变目录

umask(0)//重设文件创建的掩码

return

}

void main()

{

  FILE *fp

  time_t t

  init_daemon()

  while(1)

  {

      sleep(60)//等待一分钟再写入

      fp=fopen("testfork2.log","a")

      if(fp>=0)

      {

          time(&t)

          fprintf(fp,"current time is:%s\n",asctime(localtime(&t))) //转换为本地时间输出

          fclose(fp)

      }

  }

  return

}

监控进程monitor.c:

#include<unistd.h>

#include<signal.h>

#include<stdio.h>

#include<stdlib.h>

#include<sys/param.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<time.h>

#include<sys/wait.h>

#include<fcntl.h>

#include<limits.h>

#define BUFSZ 150

void init_daemon()

{

int pid

int i

pid=fork()

if(pid<0)

  exit(1) //创建错误,退出

else if(pid>0) //父进程退出

  exit(0)

setsid()//使子进程成为组长

pid=fork()

if(pid>0)

  exit(0)//再次退出,使进程不是组长,这样进程就不会打开控制终端

else if(pid<0)

  exit(1)

//关闭进程打开的文件句柄

for(i=0i<NOFILEi++)

  close(i)

chdir("/root/test") //改变目录

umask(0)//重设文件创建的掩码

return

}

void err_quit(char *msg)

{

perror(msg)

exit(EXIT_FAILURE)

}

// 判断程序是否在运行

int does_service_work()

{

FILE* fp

int count

char buf[BUFSZ]

char command[150]

sprintf(command, "ps -ef | grep thisisatest | grep -v grep | wc -l" )

if((fp = popen(command,"r")) == NULL)

err_quit("popen")

if( (fgets(buf,BUFSZ,fp))!= NULL )

{

count = atoi(buf)

}

pclose(fp)

  return count

// exit(EXIT_SUCCESS)

}

void main()

{

  FILE *fp

  time_t t

  int count

  init_daemon()

  while(1)

  {

      sleep(10)//等待一分钟再写入

      fp=fopen("testfork3.log","a")

      if(fp>=0)

      {

          count = does_service_work()

          time(&t)

          if(count>0)

              fprintf(fp,"current time is:%s and the process exists, the count is %d\n",asctime(localtime(&t)), count) //转换为本地时间输出

          else

          {

              fprintf(fp,"current time is:%s and the process does not exist, restart it!\n",asctime(localtime(&t))) //转换为本地时间输出

              system("/home/user/daemon/thisisatest")//启动服务

          }

          fclose(fp)

      }

  }

  return

}

具体CMD命令:

cc thisisatest.c -o thisisatest

./thisisatest

cc monitor.c -o monitor

./monitor

tail -f testfork3.log   -- 查看日志