Files | |
file | log_lib.c |
This is the implementation of the Logging Library. | |
file | log_lib.h |
This is the header for the Logging Library. | |
Defines | |
#define | LOG_LIB_BUFF_SIZE 150 |
The maximum length of the log message string. | |
#define | LOGSYSLOG 0x08 |
Used to log to syslogd. | |
#define | LOGSTDERR 0x04 |
Used to log to stderr. | |
#define | LOGSTDOUT 0x02 |
Used to log to stdout. | |
#define | LOGFILE 0x01 |
Used to log to a file. | |
Functions | |
void | logmessage (char *message, int unsigned location, char *process_name, char *log_file_name, char *filename, int line_num, char *function) |
Writes log messages to stderr, stdout, syslogd, or a file. |
Over the years, I have come to realize that logging is an extremely important part of an application. Inevitably, a situation arises when not every piece of the process is configured properly to work with its environment. Log files are invaluable in situations of this nature. When a catastrophe occurs, the log gives you the information needed to avoid future problems. With major processes in Linux, it is helpful to have this information presented chronologically in the same file. In Linux, this functionality is provided by the syslog daemon (syslogd).
In the early stages of development it is also very common to log activities. Most developers use the occasional print statement that allows them to view the current progress or state of the application. Capturing all of the data that defines the state of the program and then printing it, can often be tedious.
There are also situations where a developer might want to print the log message to the screen and write to a file. To perform this task by hand would take twice as many lines of code.
To help solve both of these problems I created the logging library. This module makes logging messages much easier while providing more helpful information. The library uses a #define macro to make the function calls shorter.
The #define macro in the program that is using the module:
#define LOGMESSAGE(string, location) logmessage(string, location,\ ALSAD_PROC_NAME, ALSAD_LOG_FILE, __FILE__,\ __LINE__, __FUNCTION__);
It is also necessary to #define ALSAD_PROC_NAME and ALSAD_LOG_FILE.
These two variables are the name of the calling process and the name and path of the log file the process should write to, respectively.
A call such as:
LOGMESSAGE("Hello World!", LOGSTDERR | LOGSYSLOG);
logs and formats 'Hello World!' along with the other function parameters to stderr and syslogd.
|
The maximum length of the log message string.
|
|
Used to log to a file.
|
|
Used to log to stderr.
|
|
Used to log to stdout.
|
|
Used to log to syslogd.
|
|
Writes log messages to stderr, stdout, syslogd, or a file. This function will write to any combination of the listed locations. The easiest way to use this is to #define a macro that fills in most of the parameters automatically. This is shown in the details of this library.
|