Linux系统编程42 进程控制 - 系统日志

Linux 系统日志:cd /var/log/
/var/log/ 下面的各种日志文件 中有一个 主日志文件 :message

在 ubuntu16.04中 主日志文件是 syslog
-rw-r----- 1 syslog adm 258172 Feb 22 18:37 syslog

syslogd服务:所有需要写系统日志的进程,都把自己所写的系统日志提交给 syslogd服务

那么怎么提交自己的系统日志给 syslogd服务呢? 通过如下接口:

openlog()
syslog()
closelog()

这三个函数配合使用 会将需要日志打印到 /var/log/message 等主日志文件中

NAME
       closelog, openlog, syslog, vsyslog - send messages to the system logger

SYNOPSIS
       #include <syslog.h>

       void openlog(const char *ident, int option, int facility);//参数 :名字 ,日志携带标志 ,记录消息的程序类型
       void syslog(int priority, const char *format, ...);//参数: 日志级别 ,提交内容
       void closelog(void);

openlog()为程序打开到系统日志记录器的连接。ident所指向的字符串被置于每条消息的前面,通常被设置为程序名。如果ident为空,则使用程序名。option参数指定了控制openlog()操作和后续对syslog()调用的标志

syslog()产生一条日志消息,通过syslogd(8)分发。priority参数是通过将工具和级别值(后面会解释)相加而形成的。剩下的参数是一种格式,如
printf(3)和格式要求的任何参数,除了两个字符序列%m将被错误消息字符串strerror(errno)替换。如果需要,可以添加末尾的换行符。

option

   The option argument to openlog() is an OR of any of these:

   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 POSIX.1-2001 or POSIX.1-2008.)  Print to stderr as well.

   LOG_PID        Include PID with each message.

facility

The facility argument is used to specify what type of program is logging the message. This lets the configuration file specify that messages from different facilities will be handled differently.

   LOG_AUTH       security/authorization messages

   LOG_AUTHPRIV   security/authorization messages (private)

   LOG_CRON       clock daemon (cron and at)

   LOG_DAEMON     system daemons without separate facility value

   LOG_FTP        ftp daemon

   LOG_KERN       kernel messages (these can't be generated from user processes)

   LOG_LOCAL0 through LOG_LOCAL7
                  reserved for local use

   LOG_LPR        line printer subsystem

   LOG_MAIL       mail subsystem

   LOG_NEWS       USENET news subsystem

   LOG_SYSLOG     messages generated internally by syslogd(8)

   LOG_USER (default)
                  generic user-level messages

   LOG_UUCP       UUCP subsystem

level
This determines the importance of the message. The levels are, in order of decreasing importance:

   LOG_EMERG      system is unusable

   LOG_ALERT      action must be taken immediately

   LOG_CRIT       critical conditions

   LOG_ERR        error conditions

   LOG_WARNING    warning conditions

   LOG_NOTICE     normal, but significant, condition

   LOG_INFO       informational message

   LOG_DEBUG      debug-level message

   The function setlogmask(3) can be used to restrict logging to specified levels only.

实验:打印系统日志、修改守护进程实验

#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <syslog.h>

#define FILENAME "/tmp/out"

static int craetdeamon(void)
{
	pid_t pid;
	int fd;	
	
	pid = fork();
	if(pid < 0)
	{
		perror("fork()");
		return -1;
	}
	
	if(pid > 0)
	{
		printf("%d\n",getpid());
		exit(0);
	}
		

	fd = open("/dev/null",O_RDWR);
	if(fd < 0)
	{	
		perror("open()");
		return -1;
	}


	dup2(fd,0);
	dup2(fd,1);
	dup2(fd,2);	
	if(fd > 2)
	{
		close(fd);
	}

	setsid();
	
	chdir("/");

	return 0;
}

int main(int argc,char* argv[])
{

	FILE* fp;
	int i;

	openlog("craetdeamon",LOG_PID,LOG_DAEMON);//与系统日志建立联系
	
	if(craetdeamon())
	{
		syslog(LOG_ERR,"craetdeamon failed!");//上报
		exit(1);
	}else{
		syslog(LOG_INFO,"craetdeamon successded!");
	}
	
	fp = fopen(FILENAME,"w");
	if(fp == NULL)
	{
		syslog(LOG_ERR,"fopen %s failed!",FILENAME);
		exit(1);
	}

	syslog(LOG_INFO,"fopen %s successede!",FILENAME);

	for(i = 0; ;i++)
	{
		fprintf(fp,"%d\n",i);
		fflush(fp);
		syslog(LOG_DEBUG,"%d is printed!",i);
		sleep(1);
	}
	
	
	exit(0);
}

以root 权限查看 /var/log/syslog 即可看到 系统日志文件输出

猜你喜欢

转载自blog.csdn.net/LinuxArmbiggod/article/details/113976403