Common logs implemented in C language-abort \ log \ info \ error

Source: Internet
Author: User

In these cases, a small project or an executable file composed of several independent files is required to have some simple output, including logs and abnormal exit. However, we do not want to install some open-source log packages similar to glog and log3cpp because we do not need some calls similar to thread security, but want to be simpler, the simpler the better ....

Yes, that's it.

The header file log. h is as follows:


[Cpp]
# Ifndef _ LOG_H _
# Define _ LOG_H _
 
# Ifdef _ cplusplus
Extern "C "{
# Endif
 
Extern void set_syslog (int syslog );
Extern void abort (const char * msg ,...);
Extern void log (const char * msg ,...);
Extern void error (const char * msg ,...);
Extern void info (const char * msg ,...);
 
# Ifdef _ cplusplus
}
# Endif
 
# Endif

# Ifndef _ LOG_H _
# Define _ LOG_H _

# Ifdef _ cplusplus
Extern "C "{
# Endif

Extern void set_syslog (int syslog );
Extern void abort (const char * msg ,...);
Extern void log (const char * msg ,...);
Extern void error (const char * msg ,...);
Extern void info (const char * msg ,...);

# Ifdef _ cplusplus
}
# Endif

# Endif
The implementation file log. c is as follows:


[Cpp]
# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <stdarg. h>
 
# If HAVE_SYSLOG_H
# Include <syslog. h>
# Endif
 
Static int _ syslog = 0;
 
Void set_syslog (int syslog)
{
_ Syslog = syslog;
}
 
Void abort (const char * msg ,...)
{
Va_list ap;
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_ERR, msg, ap );
} Else
# Endif
Vprintf (msg, ap );
Va_end (ap );
Exit (1 );
}
 
Void log (const char * msg ,...)
{
Va_list ap;
If (_ debug ){
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_DEBUG, msg, ap );
} Else
# Endif
Vprintf (msg, ap );
Va_end (ap );
}
}
 
 
Void error (const char * msg ,...)
{
Va_list ap;
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_ERR, msg, ap );
} Else
# Endif
Vfprintf (stderr, msg, ap );
Va_end (ap );
}
 
 
Void info (const char * msg ,...)
{
Va_list ap;
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_INFO, msg, ap );
} Else
# Endif
Vfprintf (stderr, msg, ap );
Va_end (ap );
}

# Include <stdio. h>
# Include <stdlib. h>
# Include <string. h>
# Include <stdarg. h>

# If HAVE_SYSLOG_H
# Include <syslog. h>
# Endif

Static int _ syslog = 0;

Void set_syslog (int syslog)
{
_ Syslog = syslog;
}

Void abort (const char * msg ,...)
{
Va_list ap;
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_ERR, msg, ap );
} Else
# Endif
Vprintf (msg, ap );
Va_end (ap );
Exit (1 );
}

Void log (const char * msg ,...)
{
Va_list ap;
If (_ debug ){
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_DEBUG, msg, ap );
} Else
# Endif
Vprintf (msg, ap );
Va_end (ap );
}
}


Void error (const char * msg ,...)
{
Va_list ap;
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_ERR, msg, ap );
} Else
# Endif
Vfprintf (stderr, msg, ap );
Va_end (ap );
}


Void info (const char * msg ,...)
{
Va_list ap;
Va_start (ap, msg );
# If HAVE_VSYSLOG
If (_ syslog ){
Vsyslog (LOG_INFO, msg, ap );
} Else
# Endif
Vfprintf (stderr, msg, ap );
Va_end (ap );
}

 

Related Article

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.