Linux System Programming 42 Process Control-System Log

Linux system log: cd /var/log/
/var/log/ There is a main log file among the various log files below: message

In ubuntu16.04, the main log file is syslog
-rw-r----- 1 syslog adm 258172 Feb 22 18:37 syslog

syslogd service: all processes that need to write system logs submit their own system logs to the syslogd service

So how do you submit your system logs to the syslogd service? Through the following interface:

openlog()
syslog()
closelog()

The combination of these three functions will print the required logs to the main log file such as /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() opens a connection to the system logger for the program. The string pointed to by ident is placed in front of each message and is usually set as the program name. If ident is empty, the program name is used. The option parameter specifies the flags that control the openlog() operation and subsequent calls to syslog()

syslog() generates a log message, which is distributed through syslogd(8). The priority parameter is formed by adding the tool and the level value (explained later). The remaining parameters are a format, such as
printf(3) and any parameters required by the format, except for the two character sequence %m, which will be replaced by the error message string strerror(errno). If needed, you can add a newline at the end.

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.

Experiment: Print the system log, modify the daemon process experiment

#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);
}

View /var/log/syslog with root privileges to see the system log file output

Guess you like

Origin blog.csdn.net/LinuxArmbiggod/article/details/113976403