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