How to throw an exception in the C language of Linux or write the exception into the log file

Source: Internet
Author: User
Tags openlog syslog
Write System logs in C language in Linux

__________________________________________________________________________________________________

Author: jobo

Time: 2011/11/24

Emaile: jibo.tiger@gmail.com

Statement: I am not opposed to reprinting my articles by other people and groups, but please indicate the source, 3q. If you have any questions about this article

Leave a message in the blog or send an emaile to contact me (you cannot reply in time. The time is limited. Please forgive me)

_________________________________________________________________________________________________

When I am working on some large projects, I will take into account when writing the program. If some exceptions occur, do I directly throw the exception or write the exception into the log file, to the system administrator. The following describes how to write exceptions to logs or directly throw exceptions in Linux.

1. Throw an exception

Throwing an exception in C language is the most commonly used and simplest. Because let's talk about how to use it first.

1. Use the exit () function to throw an exception

1> header file: # include <stdlib. h>

2> when an exception occurs in the program, you can use exit () to exit the program directly, that is, to throw an exception.

3> the exit () function parameter is exit_failure, which indicates that the program failed to run.

4> instance: When a file fails to be opened in Linux, the exit () function is used to throw an exception.

# Include <stdio. h>

# Include <stdlib. h>
# Include <sys/types. h>

# Include <sys/STAT. h>

# Include <fcntl. h>

 

Int main ()

{

Int FD;

FD = open ("tiger. C", o_rdwr );

If (FD <0 ){

Perror ("openfailed \ n ");

Exit (exit_failure );

}

}

After the executable program is executed, as shown in:

2. Write exceptions to log files

Sometimes, we need to write the debugging information into the log file for the system administrator to view it, rather than directly exiting the program.

1. Use the syslog (), openlog (), closelog () functions to write debugging information to the log file.

1> the three functions openlog, syslog, and closelog are a set of system log writing interfaces. In addition, the vsyslog and Syslog functions are the same, but the parameter format is different.

2> generally, the Syslog daemon reads record messages in three formats. This daemon reads a configuration file at startup. Generally, the file name is/etc/syslog. conf, which determines where different types of messages should be sent. For example, an emergency message can be sent to the system administrator (if logged on) and displayed on the console. A warning message can be recorded in a file. This mechanism provides syslog functions. The Calling format is as follows:
# Include <syslog. h>
Void openlog (char * Ident, int option, int facility );
Void syslog (INT priority, char * format ,......)
Void closelog ();

3> openlog is optional. If openlog is not called, openlog is automatically called when syslog is called for the first time. You can also choose to call closelog, which only disables the descriptor used for communicating with the Syslog daemon. By calling openlog, we can specify an ident. Later, this ident will be added to each recorded message. IDENT is generally the program name. 4> openlog and closelog Functions
The function is prototype as follows:
Void openlog (const char * Ident, int option, int facility );
This function is used to open a connection to the system logging program. After it is enabled, you can use the syslog or vsyslog function to add information to the system logs. The closelog function is used to close the connection.

The first parameter ident will be a tag. The string represented by ident will be added to the front of each line of log to identify the log. Generally, it is written as the name of the current program for marking.

The second option parameter is the result of the following values and operations: log_cons, log_ndelay, log_nowait, log_odelay, log_perror, and log_pid. For the meanings of each value, see man openlog manual:

L log_cons: write directly to system console if there is an error while sendingto system logger.

L log_ndelay: Open the connection immediately (normally, the connection is openedwhen the first message is logged ).

L log_nowait: Don't wait for child processes that may have beencreated while logging the message. (The gnu c library does not create a childprocess, so this option has no effect on Linux .)

L log_odelay: the converse of log_ndelay; opening of the connection is delayeduntil syslog () is called. (This is the default, and neednot be specified .)

L log_perror :( not in susv3.) print to stderr as well.

L log_pid: Include PID with eachmessage.

The third parameter specifies the program type for logging.

5> syslog functions and Parameters
The SYSLOG function is used to send log messages to syslogd of the system program for recording. The prototype of this function is void syslog (INT priority, const char * format ,...);
The first parameter is the urgency level of the message, the second parameter is the message format, and the second parameter is the parameter corresponding to the format. It is used like the printf function.

If our program needs to use the system log function, we only need to use the openlog function when the program starts to connect to the syslogd program. Then we can use the syslog function to write logs at any time.

2. program instance:


After the program is executed, the terminal does not respond, but it must be:

Tail/var/log/syslog allows you to view system log information


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.