Lely core libraries 2.3.4
|
This file is part of the utilities library; it contains the diagnostic function definitions. More...
#include "util.h"
#include <lely/libc/stdio.h>
#include <lely/util/diag.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
Go to the source code of this file.
Functions | |
size_t | floc_lex (struct floc *at, const char *begin, const char *end) |
Increments a file location by reading characters from a memory buffer. More... | |
int | snprintf_floc (char *s, size_t n, const struct floc *at) |
Prints a file location to a string buffer. More... | |
void | diag_get_handler (diag_handler_t **phandler, void **phandle) |
Retrieves the handler function for diag(). More... | |
void | diag_set_handler (diag_handler_t *handler, void *handle) |
Sets the handler function for diag(). More... | |
void | diag_at_get_handler (diag_at_handler_t **phandler, void **phandle) |
Retrieves the handler function for diag_at(). More... | |
void | diag_at_set_handler (diag_at_handler_t *handler, void *handle) |
Sets the handler function for diag_at(). More... | |
void | diag (enum diag_severity severity, int errc, const char *format,...) |
Emits a diagnostic message. More... | |
void | vdiag (enum diag_severity severity, int errc, const char *format, va_list ap) |
Emits a diagnostic message. More... | |
void | diag_at (enum diag_severity severity, int errc, const struct floc *at, const char *format,...) |
Emits a diagnostic message occurring at a location in a text file. More... | |
void | vdiag_at (enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
Emits a diagnostic message occurring at a location in a text file. More... | |
void | diag_if (enum diag_severity severity, int errc, const struct floc *at, const char *format,...) |
Emits a diagnostic message occurring at a location in a text file. More... | |
void | vdiag_if (enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
Emits a diagnostic message occurring at a location in a text file. More... | |
void | default_diag_handler (void *handle, enum diag_severity severity, int errc, const char *format, va_list ap) |
The default diag() handler. More... | |
void | default_diag_at_handler (void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
The default diag_at() handler. More... | |
void | cmd_diag_handler (void *handle, enum diag_severity severity, int errc, const char *format, va_list ap) |
The diag() handler used for command-line programs. More... | |
void | daemon_diag_handler (void *handle, enum diag_severity severity, int errc, const char *format, va_list ap) |
The diag() handler for daemons. More... | |
void | daemon_diag_at_handler (void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
The diag_at() handler for daemons. More... | |
void | log_diag_handler (void *handle, enum diag_severity severity, int errc, const char *format, va_list ap) |
The diag() handler for log files. More... | |
void | log_diag_at_handler (void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
The diag_at() handler used for log-files. More... | |
void | syslog_diag_handler (void *handle, enum diag_severity severity, int errc, const char *format, va_list ap) |
The diag() handler used for the system logging facilities. More... | |
void | syslog_diag_at_handler (void *handle, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
The diag_at() handler used for the system logging facilities. More... | |
int | vsnprintf_diag (char *s, size_t n, enum diag_severity severity, int errc, const char *format, va_list ap) |
Prints a diagnostic message to a string buffer. More... | |
int | vasprintf_diag (char **ps, enum diag_severity severity, int errc, const char *format, va_list ap) |
Equivalent to vsnprintf_diag(), except that it allocates a string large enough to hold the output, including the terminating null byte. More... | |
int | vsnprintf_diag_at (char *s, size_t n, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
Prints a diagnostic message occurring at a location in a text file to a string buffer. More... | |
int | vasprintf_diag_at (char **ps, enum diag_severity severity, int errc, const struct floc *at, const char *format, va_list ap) |
Equivalent to vsnprintf_diag_at(), except that it allocates a string large enough to hold the output, including the terminating null byte. More... | |
const char * | cmdname (const char *path) |
Extracts the command name from a path. More... | |
This file is part of the utilities library; it contains the diagnostic function definitions.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
Definition in file diag.c.
size_t floc_lex | ( | struct floc * | at, |
const char * | begin, | ||
const char * | end | ||
) |
Increments a file location by reading characters from a memory buffer.
This function assumes a tab stop of 8 spaces.
at | a pointer to a file location (can be NULL). |
begin | a pointer to the start of the buffer. |
end | a pointer to one past the last character in the buffer (can be NULL if the buffer is null-terminated). |
int snprintf_floc | ( | char * | s, |
size_t | n, | ||
const struct floc * | at | ||
) |
Prints a file location to a string buffer.
The filename, line and column are separated by a colon. If the line or column is zero, it is omitted from the output. This allows the user to print a partial location if, for example, the column is unknown or meaningless.
s | the address of the output buffer. If s is not NULL, at most n - 1 characters are written, plus a terminating null byte. |
n | the size (in bytes) of the buffer at s. If n is zero, nothing is written. |
at | a pointer the location in a text file. |
errno
. void diag_get_handler | ( | diag_handler_t ** | phandler, |
void ** | phandle | ||
) |
Retrieves the handler function for diag().
phandler | the address at which to store a pointer to the handler function (can be NULL). |
phandle | the address at which to store the pointer to the extra argument for the handler function (can be NULL). |
void diag_set_handler | ( | diag_handler_t * | handler, |
void * | handle | ||
) |
Sets the handler function for diag().
handler | a pointer to the new handler function. |
handle | an optional pointer to an extra argument for the handler function (can be NULL). |
void diag_at_get_handler | ( | diag_at_handler_t ** | phandler, |
void ** | phandle | ||
) |
Retrieves the handler function for diag_at().
phandler | the address at which to store a pointer to the handler function (can be NULL). |
phandle | the address at which to store the pointer to the extra argument for the handler function (can be NULL). |
void diag_at_set_handler | ( | diag_at_handler_t * | handler, |
void * | handle | ||
) |
Sets the handler function for diag_at().
handler | a pointer to the new handler function. |
handle | an optional pointer to an extra argument for the handler function (can be NULL). |
|
inline |
Emits a diagnostic message.
This function SHOULD print the severity of the diagnostic followed by the user-specified message. If the specified severity is DIAG_FATAL, the program SHOULD be terminated after printing the message.
The default error handler can be changed with diag_set_handler(), in which case the new handler is responsible for terminating the program in case of an error with severity DIAG_FATAL.
severity | the severity of the message. |
errc | the native error code generated by a system call or library function, given by get_errc(). If errc is non-zero, a text string describing the error SHOULD be printed after the user-specified message. |
format | a printf-style format string. |
... | an optional list of arguments to be printed according to format. |
void vdiag | ( | enum diag_severity | severity, |
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
|
inline |
Emits a diagnostic message occurring at a location in a text file.
This function SHOULD print the location in a text file, where the diagnostic presumably occurred, followed by the severity and a user-specified message. If the specified severity is DIAG_FATAL, the program SHOULD be terminated after printing the message.
The default error handler can be changed with diag_at_set_handler(), in which case the new handler is responsible for terminating the program in case of an error with severity DIAG_FATAL.
severity | the severity of the message. |
errc | the native error code generated by a system call or library function, given by get_errc(). If errc is non-zero, a text string describing the error will be printed after the user-specified message. |
at | a pointer the location in a text file (can be NULL). If at is not NULL, the location SHOULD be printed before the severity. |
format | a printf-style format string. |
... | an optional list of arguments to be printed according to format. |
void vdiag_at | ( | enum diag_severity | severity, |
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
|
inline |
Emits a diagnostic message occurring at a location in a text file.
This function is equivalent to diag_at(), except that it only invokes the error handler if at is not NULL. It therefore MUST NOT be used with severity DIAG_FATAL unless at is guaranteed to be non-NULL.
void vdiag_if | ( | enum diag_severity | severity, |
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
Emits a diagnostic message occurring at a location in a text file.
This function is equivalent to vdiag_at(), except that it only invokes the error handler if at is not NULL. It therefore MUST NOT be used with severity DIAG_FATAL unless at is guaranteed to be non-NULL.
void default_diag_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
The default diag() handler.
This function prints the string generated by vsnprintf_diag() to stderr
. If severity equals DIAG_FATAL, this function calls exit(EXIT_FAILURE)
instead of returning.
void default_diag_at_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
The default diag_at() handler.
This function is similar to default_diag_handler(), except that it prints the string generated by vsnprintf_diag_at().
void cmd_diag_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag() handler used for command-line programs.
This function prints the string at handle, which SHOULD point to the name of the program (see cmdname()), and then calls default_diag_handler().
void daemon_diag_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag() handler for daemons.
On Windows this function is equivalent to dialog_diag_handler(), on other platforms to syslog_diag_handler().
void daemon_diag_at_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag_at() handler for daemons.
This function is similar to daemon_diag_handler(), except that it prints the string generated by vsnprintf_diag_at().
void log_diag_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag() handler for log files.
This function is similar to default_diag_handler(), except that it prepends each message with a RFC-2822-compliant timestamp.
void log_diag_at_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag_at() handler used for log-files.
This function is similar to default_diag_at_handler(), except that it prepends each message with a RFC-2822-compliant timestamp.
void syslog_diag_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag() handler used for the system logging facilities.
On POSIX platforms this function invokes syslog(); on other platforms this function is equivalent to log_diag_handler().
void syslog_diag_at_handler | ( | void * | handle, |
enum diag_severity | severity, | ||
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
The diag_at() handler used for the system logging facilities.
On POSIX platforms this function invokes syslog(); on other platforms this function is equivalent to log_diag_at_handler().
int vsnprintf_diag | ( | char * | s, |
size_t | n, | ||
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
Prints a diagnostic message to a string buffer.
This function prints the severity of the message (except in the case of DIAG_INFO), followed by a colon and the user-specified message. If errc is non-zero, the message is followed by another colon and the result of errc2str().
s | the address of the output buffer. If s is not NULL, at most n - 1 characters are written, plus a terminating null byte. |
n | the size (in bytes) of the buffer at s. If n is zero, nothing is written. |
severity | the severity of the message. |
errc | the native error code generated by a system call or library function, given by get_errc(). If errc is non-zero, a text string describing the error will be printed after the user-specified message. |
format | a printf-style format string. |
ap | the list of arguments to be printed according to format. |
errno
.int vasprintf_diag | ( | char ** | ps, |
enum diag_severity | severity, | ||
int | errc, | ||
const char * | format, | ||
va_list | ap | ||
) |
Equivalent to vsnprintf_diag(), except that it allocates a string large enough to hold the output, including the terminating null byte.
ps | the address of a value which, on success, contains a pointer to the allocated string. This pointer SHOULD be passed to free() to release the allocated storage. |
severity | the severity of the message. |
errc | the native error code generated by a system call or library function, given by get_errc(). If errc is non-zero, a text string describing the error will be printed after the user-specified message. |
format | a printf-style format string. |
ap | the list of arguments to be printed according to format. |
errno
.int vsnprintf_diag_at | ( | char * | s, |
size_t | n, | ||
enum diag_severity | severity, | ||
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
Prints a diagnostic message occurring at a location in a text file to a string buffer.
This function prints the location in a text file, where the diagnostic presumably occurred, followed a colon and the severity of the message (except in the case of DIAG_INFO), followed by a colon and the user-specified message. If errc is non-zero, the message is followed by another colon and the result of errc2str().
s | the address of the output buffer. If s is not NULL, at most n - 1 characters are written, plus a terminating null byte. |
n | the size (in bytes) of the buffer at s. If n is zero, nothing is written. |
severity | the severity of the message. |
errc | the native error code generated by a system call or library function, given by get_errc(). If errc is non-zero, a text string describing the error will be printed after the user-specified message. |
at | a pointer the location in a text file (can be NULL). If at is not NULL, the location will be printed before the severity according to snprintf_floc(). |
format | a printf-style format string. |
ap | the list of arguments to be printed according to format. |
errno
.int vasprintf_diag_at | ( | char ** | ps, |
enum diag_severity | severity, | ||
int | errc, | ||
const struct floc * | at, | ||
const char * | format, | ||
va_list | ap | ||
) |
Equivalent to vsnprintf_diag_at(), except that it allocates a string large enough to hold the output, including the terminating null byte.
ps | the address of a value which, on success, contains a pointer to the allocated string. This pointer SHOULD be passed to free() to release the allocated storage. |
severity | the severity of the message. |
errc | the native error code generated by a system call or library function, given by get_errc(). If errc is non-zero, a text string describing the error will be printed after the user-specified message. |
at | a pointer the location in a text file (can be NULL). If at is not NULL, the location will be printed before the severity according to snprintf_floc(). |
format | a printf-style format string. |
ap | the list of arguments to be printed according to format. |
errno
.