|
LDM Logging
|
#include "../misc/ErrObj.h"#include <errno.h>#include <pthread.h>#include <stdarg.h>#include <stdbool.h>#include <stdlib.h>#include <syslog.h>#include "log_private.h"Go to the source code of this file.
Macros | |
| #define | LOG_LOCALTIME 0x100u |
| Use local time. Default is UTC. More... | |
| #define | LOG_NOTIME 0x200u |
| Don't add a timestamp. | |
| #define | LOG_IDENT 0x400u |
| Add the facility identifier. | |
| #define | log_fini() |
| #define | log_free() |
| #define | log_is_enabled_warning log_is_level_enabled(LOG_LEVEL_WARNING) |
| #define | log_is_enabled_notice log_is_level_enabled(LOG_LEVEL_NOTICE) |
| #define | log_is_enabled_info log_is_level_enabled(LOG_LEVEL_INFO) |
| #define | log_is_enabled_debug log_is_level_enabled(LOG_LEVEL_DEBUG) |
| #define | log_debug(...) |
| #define | log_info(...) |
| #define | log_notice(...) |
| #define | log_warning(...) |
| #define | log_error(...) |
| #define | log_fatal(...) |
| #define | log_syserr(...) log_errno(errno, __VA_ARGS__) |
| #define | log_add_syserr(...) log_add_errno(errno, __VA_ARGS__) |
| #define | log_error_q(...) LOG_LOG(LOG_LEVEL_ERROR, __VA_ARGS__) |
| #define | log_warning_q(...) LOG_LOG(LOG_LEVEL_WARNING, __VA_ARGS__) |
| #define | log_notice_q(...) LOG_LOG(LOG_LEVEL_NOTICE, __VA_ARGS__) |
| #define | log_info_q(...) LOG_LOG(LOG_LEVEL_INFO, __VA_ARGS__) |
| #define | log_debug_q(...) |
| #define | log_log_q(level, ...) LOG_LOG(level, __VA_ARGS__) |
| #define | log_flush_fatal() log_flush(LOG_LEVEL_FATAL) |
| #define | log_flush_error() log_flush(LOG_LEVEL_ERROR) |
| #define | log_flush_warning() log_flush(LOG_LEVEL_WARNING) |
| #define | log_flush_notice() log_flush(LOG_LEVEL_NOTICE) |
| #define | log_flush_info() log_flush(LOG_LEVEL_INFO) |
| #define | log_flush_debug() log_flush(LOG_LEVEL_DEBUG) |
| #define | log_realloc(buf, nbytes, msg) |
| #define | log_abort(...) |
| #define | log_assert(expr) |
Enumerations | |
| enum | log_level_t { LOG_LEVEL_DEBUG = 0 , LOG_LEVEL_INFO , LOG_LEVEL_NOTICE , LOG_LEVEL_WARNING , LOG_LEVEL_ERROR , LOG_LEVEL_FATAL , LOG_LEVEL_COUNT } |
| Logging levels. More... | |
Functions | |
| const char * | log_get_default_daemon_destination (void) |
| const char * | log_get_default_destination (void) |
| bool | log_stderr_is_open (void) |
| bool | log_amDaemon (void) |
| int | log_init (const char *const id) |
| void | log_avoid_stderr (void) |
| void | log_refresh (void) |
| int | log_set_level (const log_level_t level) |
| log_level_t | log_get_level (void) |
| void | log_roll_level (void) |
| int | log_set_id (const char *const id) |
| int | log_set_upstream_id (const char *const hostId, const bool isFeeder) |
| const char * | log_get_id (void) |
| int | log_set_options (const unsigned options) |
| unsigned | log_get_options (void) |
| int | log_set_facility (const int facility) |
| int | log_get_facility (void) |
| int | log_set_destination (const char *const dest) |
| const char * | log_get_destination (void) |
| void | log_clear (void) |
| int | log_dispose (const log_level_t level, ErrObj *errObj) |
| int | log_flush (const log_level_t level) |
Copyright 2019 University Corporation for Atmospheric Research. All rights reserved. See the the file COPYRIGHT in the top-level source-directory for licensing conditions.
This file defines the API for LDM logging.
| #define log_abort | ( | ... | ) |
Logs an error message and then aborts the current process.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_add_syserr | ( | ... | ) | log_add_errno(errno, __VA_ARGS__) |
Adds a message based on the system error code (i.e., errno) to the current thread's queue of error messages:
| [in] | ... | Optional arguments of the message. |
| #define log_assert | ( | expr | ) |
Tests an assertion. Logs an error-message and then aborts the process if the assertion is false.
This is not a thread cancellation point.
| [in] | expr | The assertion to be tested. |
| #define log_debug | ( | ... | ) |
Logs a single message at the DEBUG level, bypassing the message-queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_debug_q | ( | ... | ) |
Adds a message to the current thread's queue of messages, logs the queue at the DEBUG level, and then clears the queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_error | ( | ... | ) |
Logs a single message at the ERROR level, bypassing the message-queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_error_q | ( | ... | ) | LOG_LOG(LOG_LEVEL_ERROR, __VA_ARGS__) |
The following macros add a message to the current thread's queue of messages, log the queue, and then clear the queue:
The argument-list of the variadic macros must comprise at least a NULL argument in order to avoid a syntax error. Adds a message to the current thread's queue of messages, logs the queue at the ERROR level, and then clears the queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_fatal | ( | ... | ) |
Logs a single message at the FATAL level, bypassing the message-queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_fini | ( | ) |
Finalizes the logging module. Should be called eventually after log_init(), after which no more logging should occur.
| #define log_flush_debug | ( | ) | log_flush(LOG_LEVEL_DEBUG) |
Logs the message-queue of the current thread at the DEBUG level and then clears the queue.
| #define log_flush_error | ( | ) | log_flush(LOG_LEVEL_ERROR) |
Logs the message-queue of the current thread at the ERROR level and then clears the queue.
| #define log_flush_fatal | ( | ) | log_flush(LOG_LEVEL_FATAL) |
Logs the message-queue of the current thread at the FATAL level and then clears the queue.
| #define log_flush_info | ( | ) | log_flush(LOG_LEVEL_INFO) |
Logs the message-queue of the current thread at the INFO level and then clears the queue.
| #define log_flush_notice | ( | ) | log_flush(LOG_LEVEL_NOTICE) |
Logs the message-queue of the current thread at the NOTICE level and then clears the queue.
| #define log_flush_warning | ( | ) | log_flush(LOG_LEVEL_WARNING) |
Logs the message-queue of the current thread at the WARNING level and then clears the queue.
| #define log_free | ( | ) |
DEPRECATED. No longer necessary.
Frees the log-message resources of the current thread. Should only be called when no more logging by the current thread will occur.
This is not a thread-cancellation point.
| #define log_info | ( | ... | ) |
Logs a single message at the INFO level, bypassing the message-queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_info_q | ( | ... | ) | LOG_LOG(LOG_LEVEL_INFO, __VA_ARGS__) |
Adds a message to the current thread's queue of messages, logs the queue at the INFO level, and then clears the queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_is_enabled_debug log_is_level_enabled(LOG_LEVEL_DEBUG) |
Indicates if a log message of DEBUG level will be written. Useful if a format argument of a message is expensive to evaluate.
| #define log_is_enabled_info log_is_level_enabled(LOG_LEVEL_INFO) |
Indicates if a log message of INFO level will be written. Useful if a format argument of a message is expensive to evaluate.
| #define log_is_enabled_notice log_is_level_enabled(LOG_LEVEL_NOTICE) |
Indicates if a log message of NOTICE level will be written. Useful if a format argument of a message is expensive to evaluate.
| #define log_is_enabled_warning log_is_level_enabled(LOG_LEVEL_WARNING) |
Indicates if a log message of WARNING level will be written. Useful if a format argument of a message is expensive to evaluate.
| #define LOG_LOCALTIME 0x100u |
Use local time. Default is UTC.
Macros for the ulog backend reproduced here with the "LOG_" prefix:
| #define log_log_q | ( | level, | |
| ... | |||
| ) | LOG_LOG(level, __VA_ARGS__) |
Adds a message to the current thread's queue of messages, logs the queue at the given level, and then clears the queue.
| [in] | level | log_level_t logging level. |
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_notice | ( | ... | ) |
Logs a single message at the NOTICE level, bypassing the message-queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_notice_q | ( | ... | ) | LOG_LOG(LOG_LEVEL_NOTICE, __VA_ARGS__) |
Adds a message to the current thread's queue of messages, logs the queue at the NOTICE level, and then clears the queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_realloc | ( | buf, | |
| nbytes, | |||
| msg | |||
| ) |
Re-allocates memory. Adds an message the current thread's queue of messages if an error occurs.
| [in] | buf | Previously-allocated buffer |
| [in] | nbytes | Number of bytes to allocate. |
| [in] | msg | Message to print on error. Should complete the sentence "Couldn't allocate <n> bytes for ...". |
| NULL | Out of memory. Log message added. |
| #define log_syserr | ( | ... | ) | log_errno(errno, __VA_ARGS__) |
Logs a single message at the ERROR level based on errno, bypassing the message queue.
| [in] | ... | Optional arguments of the message. |
| #define log_warning | ( | ... | ) |
Logs a single message at the WARNING level, bypassing the message-queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| #define log_warning_q | ( | ... | ) | LOG_LOG(LOG_LEVEL_WARNING, __VA_ARGS__) |
Adds a message to the current thread's queue of messages, logs the queue at the WARNING level, and then clears the queue.
| [in] | ... | Optional arguments of the message – starting with the format of the message. |
| enum log_level_t |
| bool log_amDaemon | ( | void | ) |
Indicates if the current process is a daemon (i.e., has no controlling terminal).
| <tt>true</tt> | Current process is a daemon |
| <tt>false</tt> | Current process is not a daemon |
| void log_avoid_stderr | ( | void | ) |
Tells this module to avoid using the standard error stream (because the process has become a daemon, for example).
| void log_clear | ( | void | ) |
Clears the message-queue of the current thread.
| int log_flush | ( | const log_level_t | level | ) |
Logs the currently-accumulated log-messages of the current thread and resets the message-queue for the current thread.
This is not a thread-cancellation point.
| [in] | level | The level at which to log the messages. One of LOG_LEVEL_ERROR, LOG_LEVEL_WARNING, LOG_LEVEL_NOTICE, LOG_LEVEL_INFO, or LOG_LEVEL_DEBUG; otherwise, the behavior is undefined. |
| 0 | Success |
| -1 | Failure |
| const char* log_get_default_daemon_destination | ( | void | ) |
Returns the default destination for log messages if the process is a daemon (i.e., doesn't have a controlling terminal).
| "" | The system logging daemon |
| const char* log_get_default_destination | ( | void | ) |
Returns the default destination for log messages, which depends on whether or not log_avoid_stderr() has been called. If it hasn't been called, then the default destination will be the standard error stream; otherwise, the default destination will be that given by log_get_default_daemon_destination().
| "" | The system logging daemon |
| - | The standard error stream |
| const char* log_get_destination | ( | void | ) |
Returns the logging destination. Should be called between log_init() and log_fini().
| int log_get_facility | ( | void | ) |
Returns the facility that will be used (e.g., LOG_LOCAL0) when logging to the system logging daemon. Should be called between log_init() and log_fini().
| const char* log_get_id | ( | void | ) |
Returns the logging identifier. Should be called between log_init() and log_fini().
| log_level_t log_get_level | ( | void | ) |
Returns the current logging level. Should be called between log_init() and log_fini().
| unsigned log_get_options | ( | void | ) |
Returns the implementation-defined logging options. Should be called between log_init() and log_fini().
| int log_init | ( | const char *const | id | ) |
Initializes the logging module. Should be called before most other functions.
LOG_LDM. LOG_LEVEL_NOTICE. | [in] | id | The pathname of the program (e.g., argv[0]). Caller may free. |
| 0 | Success. |
| -1 | Error. |
| void log_refresh | ( | void | ) |
Refreshes the logging module. If logging is to the system logging daemon, then it will continue to be. If logging is to a file, then the file will be closed and re-opened when the next message is logged; thus enabling log file rotation. If logging is to the standard error stream, then it will continue to be if log_avoid_stderr() hasn't been called; otherwise, logging will be to the provider default.
| void log_roll_level | ( | void | ) |
Lowers the logging threshold by one. Wraps at the bottom. Should be called between log_init() and log_fini().
| int log_set_destination | ( | const char *const | dest | ) |
Sets the logging destination. Should be called between log_init() and log_fini().
| [in] | dest | The logging destination. Caller may free. One of
|
| 0 | Success. |
| -1 | Failure. |
| int log_set_facility | ( | const int | facility | ) |
Sets the facility that might be used (e.g., LOG_LOCAL0) when logging to the system logging daemon. Should be called between log_init() and log_fini(). May do nothing.
| [in] | facility | The facility that will be used when logging to the system logging daemon. |
| 0 | Success. |
| -1 | Error. |
| int log_set_id | ( | const char *const | id | ) |
Sets the logging identifier. Should be called between log_init() and log_fini().
| [in] | id | The new identifier. Caller may free. |
| 0 | Success. |
| -1 | Failure. |
| int log_set_level | ( | const log_level_t | level | ) |
Enables logging down to a given level. Should be called between log_init() and log_fini().
| [in] | level | The lowest level through which logging should occur. The levels are ordered: LOG_LEVEL_DEBUG < LOG_LEVEL_INFO < LOG_LEVEL_NOTICE < LOG_LEVEL_WARNING < LOG_LEVEL_ERROR. |
| 0 | Success. |
| -1 | Failure. |
| int log_set_options | ( | const unsigned | options | ) |
Sets the implementation-defined logging options. Should be called between log_init() and log_fini().
| [in] | options | The logging options. Bitwise or of LOG_PID Log the pid with each message (default) LOG_CONS Log on the console if errors in sending LOG_ODELAY Delay open until first syslog() LOG_NDELAY Don't delay open (default) LOG_NOWAIT Don't wait for console forks: DEPRECATED LOG_PERROR Log to stderr as well |
| 0 | Success |
| -1 | Failure |
| int log_set_upstream_id | ( | const char *const | hostId, |
| const bool | isFeeder | ||
| ) |
Modifies the logging identifier. Should be called between log_init() and log_fini().
| [in] | hostId | The identifier of the remote host. Caller may free. |
| [in] | isFeeder | Whether or not the process is sending data-products or just notifications. |
| 0 | Success. |
| bool log_stderr_is_open | ( | void | ) |
Indicates if the standard error file descriptor is open. This function may be called at any time.
| true | Standard error file descriptor is open |
| false | Standard error file descriptor is closed |