Syslog (), closelog () and openlog () -- log operation function (1)

Source: Internet
Author: User
Tags openlog

Article Source: http://blog.csdn.net/xx77009833/archive/2010/07/30/5776383.aspx

For some purposes, logging is necessary.

In a typical Linux installation,/var/log/messages contains all system messages, And/var/log/mail contains other log messages from the mail system, /var/log/debug may contain debugging messages. Depending on your Linux version, you can check your system (Message) configuration in the/etc/syslog. conf or/etc/syslog-ng/syslog-ng.conf file.

Although the format and storage location of system messages may be different, the method for generating messages is standard. Specification provides an interface for all programs to generate log messages.
Syslog ()Function implementation:

[CPP]View plaincopy
  1. # Include <syslog. h>
  2. Void syslog (INT priority, const char * message, arguments ...);

The syslog () function sends a log message to the Log Device (log tool facility. Each message has a priority parameter, which is obtained by a severity level and a program Identifier value (facility value.

The "Hazard coefficient" is used to control how a log message is responded (at which hazard level ); the program identifier code records the program that sends the message. (The severity level controls how the log message is acted)

The program ID code (custom syslog. h) contains log_user ---- used to indicate that the message is from the user's application (default.
Log_local0, log_local1..log_local7 -- these values can be assigned by the local administrator.

The following is a descending order of hazard levels:

Priority Level description
Log_emerg emergency
Log_alert high-priority faults, such as database corruption
Log_crit critical (critical) error, such as hardware operation failure
Log_err Error
Log_warning general warning
Special notes for log_notice
Log_info notification message (information messages)
Log_debug debugging message

According to the system configuration, log_emerg messages may be broadcast to all users. log_alert messages may send an email to the Administrator, and log_debug messages may be ignored, other messages are written to a message file. When you want to create a log message, you can simply write a program using the log tool by calling the syslog () function.

The log message created by the syslog () function is composed of the message header and message body. The message title is created by the device identifier (Facility indicator), date, and time. The message content is generated by the message parameter in the syslog () function. This is similar to the use of the printf () format string. Other parameters after syslog () are given according to the printf () style conversion instruction in the message string (further arguments to syslog () are used according to printf () style conversion specifiers in the message string ). In addition, the indicator % m can be used to insert an error message string, which corresponds to the current error variable errno. This is useful for logging error messages.

Test code:

[CPP]View plaincopy
  1. 01 # include <syslog. h>
  2. 02 # include <stdio. h>
  3. 03 # include <stdlib. h>
  4. 04
  5. 05 int main ()
  6. 06 {
  7. 07 file * F;
  8. 08
  9. 09 F = fopen ("not_here", "R ");
  10. 10
  11. 11 if (! F)
  12. 12 syslog (log_err | log_user, "oops-% M/N ");
  13. 13 exit (0 );
  14. 14}

Execution output:
No output is displayed, but the error message is written to the log file. You can view/var/log/messages:
Feb 24 00:49:45 localhost syslog.exe: Oops-no such file or directory

The % m conversion controller is replaced by an error description. In the preceding program, the error is caused by the failure to find the required file. This is more useful than simply reporting error numbers.

Other functions used to change the log function are also defined in the syslog. h header file:

[CPP]View plaincopy
  1. # Include <syslog. h>
  2. Void closelog (void );
  3. Void openlog (const char * Ident, int logopt, int facility );
  4. Int setlogmask (INT maskpri );

You can change the display mode of your log messages by calling the openlog () function.This allows you to set a string -- Ident, which will be added at the beginning of your log message. You can use these to indicate which program is creating this message.. The facility parameter records a default program identifier code, which will be used later in syslog.
The default facility value is log_user. logopt. You can configure the behavior of calling the syslog () function in the future. It is the or operation of zero or more of the following parameters:

Logopt parameter description
Log_pid and process identifier (a unique digital identifier assigned to each process by the system)
Log_cons send messages to the console if the messages cannot be recorded in the log file
Log_ndelay enables the log function when the syslog function is called for the first time.
Log_ndelay enables the log function immediately, not until the first log

The openlog () function will allocate and open a file descriptor for writing to the log function. You can also disable it by calling the closelog () function.Note that you do not need to call openlog () before calling syslog (), because syslog itself will enable the log function if necessary.

By settingLog mask(Using the setlogmask () function) You can control the priority of log messages. After that, all syslog () calls with no priority set in the log mask will be discarded. For example, you can use this to disable the log_debug message without affecting the program content.

Using log_mask (priority), you can create a mask for the log message. The mask created by this mask consists of only one priority; or log_upto (priority) the created mask consists of all the previous priorities (including the specific priority itself ).

[CPP]View plaincopy
  1. 01 # include <syslog. h>
  2. 02 # include <stdio. h>
  3. 03 # include <unistd. h>
  4. 04 # include <stdlib. h>
  5. 05
  6. 06 int main ()
  7. 07 {
  8. 08 int logmask;
  9. 09
  10. 10 openlog ("logmask", log_pid | log_cons, log_user );
  11. 11 syslog (log_info, "informative message, pid = % d", getpid ());
  12. 12 syslog (log_debug, "Debug message, shocould appear ");
  13. 13 logmask = setlogmask (log_upto (log_notice ));
  14. 14 syslog (log_debug, "Debug message, shocould not appear ");
  15. 15
  16. 16 exit (0 );
  17. 17}

Program description:
No output is generated after the logmask program is run, but on a typical Linux system, at the end of/var/log/messages, you can see

[CPP]View plaincopy
  1. Feb 26 17:48:59 localhost logmask [14184]: informative message, pid = 14184

This file may not exist in/var/log/debug (centos5) or sometimes/var/log/messages contains the following lines:

[CPP]View plaincopy
  1. Feb 26 22:23:42 localhost logmask [11730]: Debug message, shocould appear

10). The openlog () function is called. The openlog () function is used to open a log connection. The ident parameter is a string pointer, and the string it points to is placed before each message. Generally, the application is set to the program name to indicate which program is used to create the message. So here is the logmask program name.

The second parameter, log_pid | log_cons, is used to indicate that process PID information must be included when syslog () is called to record logs. If you cannot record logs, send the message to the console.

The third parameter is the facility. Facility parameter, which is used to specify the program type in log recording. For example, the default type is log_user (common user-level message), log_ftp (message recorded by the FTP daemon <FTP daemon program type>), and log_kern (kernel message <kernel program type>) for more information, see the man manual. The default type of log_user is used in the preceding example.

Note: In the above program, the/var/log/debug file is not originally available because the log function is not activated. to activate the debug record function, go to/etc/syslog. add the following content to conf:

[CPP]View plaincopy
  1. Reference
  2. # Debug messages
  3. *. Debug/var/log/debug

After editing and saving the file, restart the computer.

In the program, the message "Debug message" and "shocould not appear" will not appear. Because the setlogmask () function is called, all messages whose priority is lower than log_notice are ignored, while log_debug has the lowest priority, it will not be recorded in the/var/log/debug file. (Note: This method does not work in earlier kernels ).

This is because log_upto (priority) is a macro, which indicates that messages with a lower priority than priority will not be placed in the mask, that is, when the syslog () function is further called, messages in the log file cannot be recorded.

The above program uses the getpid () function, which is defined together with the function of a very close function getppid:

[CPP]View plaincopy
    1. # Include <sys/types. h>
    2. # Include <unistd. h>
    3. Pid_t getpid (void );
    4. Pid_t getppid (void );
    5. Http://www.groad.net/bbs/read.php? Tid-612-fpage-5.html
    6. Log_alert = 1
    7. Log_crit = 3
    8. Log_err = 3

Syslog (), closelog () and openlog () -- log operation function (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.