Blame source/uds/logger.h

Packit Service 310c69
/*
Packit Service 310c69
 * Copyright (c) 2020 Red Hat, Inc.
Packit Service 310c69
 *
Packit Service 310c69
 * This program is free software; you can redistribute it and/or
Packit Service 310c69
 * modify it under the terms of the GNU General Public License
Packit Service 310c69
 * as published by the Free Software Foundation; either version 2
Packit Service 310c69
 * of the License, or (at your option) any later version.
Packit Service 310c69
 * 
Packit Service 310c69
 * This program is distributed in the hope that it will be useful,
Packit Service 310c69
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service 310c69
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service 310c69
 * GNU General Public License for more details.
Packit Service 310c69
 * 
Packit Service 310c69
 * You should have received a copy of the GNU General Public License
Packit Service 310c69
 * along with this program; if not, write to the Free Software
Packit Service 310c69
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
Packit Service 310c69
 * 02110-1301, USA. 
Packit Service 310c69
 *
Packit Service 310c69
 * $Id: //eng/uds-releases/jasper/src/uds/logger.h#5 $
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
#ifndef LOGGER_H
Packit Service 310c69
#define LOGGER_H 1
Packit Service 310c69
Packit Service 310c69
#ifdef __KERNEL__
Packit Service 310c69
#include <linux/ratelimit.h>
Packit Service 310c69
#include <linux/version.h>
Packit Service 310c69
#else
Packit Service 310c69
#include <stdarg.h>
Packit Service 310c69
#include "minisyslog.h"
Packit Service 310c69
#endif
Packit Service 310c69
Packit Service 310c69
#ifdef __KERNEL__
Packit Service 310c69
#define LOG_EMERG       0       /* system is unusable */
Packit Service 310c69
#define LOG_ALERT       1       /* action must be taken immediately */
Packit Service 310c69
#define LOG_CRIT        2       /* critical conditions */
Packit Service 310c69
#define LOG_ERR         3       /* error conditions */
Packit Service 310c69
#define LOG_WARNING     4       /* warning conditions */
Packit Service 310c69
#define LOG_NOTICE      5       /* normal but significant condition */
Packit Service 310c69
#define LOG_INFO        6       /* informational */
Packit Service 310c69
#define LOG_DEBUG       7       /* debug-level messages */
Packit Service 310c69
#endif
Packit Service 310c69
Packit Service 310c69
#ifdef __KERNEL__
Packit Service 310c69
// Make it easy to log real pointer values using %px when in development.
Packit Service 310c69
#ifdef LOG_INTERNAL
Packit Service 310c69
#define PRIptr "px"
Packit Service 310c69
#else
Packit Service 310c69
#define PRIptr "pK"
Packit Service 310c69
#endif
Packit Service 310c69
#else // not __KERNEL__
Packit Service 310c69
// For compatibility with hooks we need when compiling in kernel mode.
Packit Service 310c69
#define PRIptr "p"
Packit Service 310c69
#endif
Packit Service 310c69
Packit Service 310c69
/*
Packit Service 310c69
 * Apply a rate limiter to a log method call.
Packit Service 310c69
 *
Packit Service 310c69
 * @param logFunc  A method that does logging, which is not invoked if we are
Packit Service 310c69
 *                 running in the kernel and the ratelimiter detects that we
Packit Service 310c69
 *                 are calling it frequently.
Packit Service 310c69
 */
Packit Service 310c69
#ifdef __KERNEL__
Packit Service 310c69
#define logRatelimit(logFunc, ...)                                 \
Packit Service 310c69
  do {                                                             \
Packit Service 310c69
    static DEFINE_RATELIMIT_STATE(_rs, DEFAULT_RATELIMIT_INTERVAL, \
Packit Service 310c69
                                  DEFAULT_RATELIMIT_BURST);        \
Packit Service 310c69
    if (__ratelimit(&_rs)) {                                       \
Packit Service 310c69
      logFunc(__VA_ARGS__);                                        \
Packit Service 310c69
    }                                                              \
Packit Service 310c69
  } while (0)
Packit Service 310c69
#else
Packit Service 310c69
#define logRatelimit(logFunc, ...) logFunc(__VA_ARGS__)
Packit Service 310c69
#endif
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * @file
Packit Service 310c69
 *
Packit Service 310c69
 * All of the log<Level>() functions will preserve the callers value of errno.
Packit Service 310c69
 **/
Packit Service 310c69
Packit Service 310c69
#ifndef __KERNEL__
Packit Service 310c69
/*
Packit Service 310c69
 * In user mode, the functions in this file are not thread safe in the sense
Packit Service 310c69
 * that nothing prevents multiple threads from closing loggers out from under
Packit Service 310c69
 * other threads.  In reality this isn't a problem since there are no calls to
Packit Service 310c69
 * closeLogger() in production code.
Packit Service 310c69
 */
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Start the logger.
Packit Service 310c69
 **/
Packit Service 310c69
void openLogger(void);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Stop the logger.
Packit Service 310c69
 **/
Packit Service 310c69
void closeLogger(void);
Packit Service 310c69
#endif
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Get the current logging level.
Packit Service 310c69
 *
Packit Service 310c69
 * @return  the current logging priority level.
Packit Service 310c69
 **/
Packit Service 310c69
int getLogLevel(void);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Set the current logging level.
Packit Service 310c69
 *
Packit Service 310c69
 * @param newLogLevel  the new value for the logging priority level.
Packit Service 310c69
 **/
Packit Service 310c69
void setLogLevel(int newLogLevel);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Return the integer logging priority represented by a name.
Packit Service 310c69
 *
Packit Service 310c69
 * @param string  the name of the logging priority (case insensitive).
Packit Service 310c69
 *
Packit Service 310c69
 * @return the integer priority named by string, or LOG_INFO if not recognized.
Packit Service 310c69
 **/
Packit Service 310c69
int stringToPriority(const char *string);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Return the printable name of a logging priority.
Packit Service 310c69
 *
Packit Service 310c69
 * @return the priority name
Packit Service 310c69
 **/
Packit Service 310c69
const char *priorityToString(int priority);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a debug message.
Packit Service 310c69
 *
Packit Service 310c69
 * @param format The format of the message (a printf style format)
Packit Service 310c69
 **/
Packit Service 310c69
void logDebug(const char *format, ...) __attribute__((format(printf, 1, 2)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log an informational message.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  format The format of the message (a printf style format)
Packit Service 310c69
 **/
Packit Service 310c69
void logInfo(const char *format, ...) __attribute__((format(printf, 1, 2)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a normal (but notable) condition.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  format The format of the message (a printf style format)
Packit Service 310c69
 **/
Packit Service 310c69
void logNotice(const char *format, ...) __attribute__((format(printf, 1, 2)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a warning.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  format The format of the message (a printf style format)
Packit Service 310c69
 **/
Packit Service 310c69
void logWarning(const char *format, ...) __attribute__((format(printf, 1, 2)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log an error.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  format The format of the message (a printf style format)
Packit Service 310c69
  **/
Packit Service 310c69
void logError(const char *format, ...) __attribute__((format(printf, 1, 2)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a message embedded within another message.
Packit Service 310c69
 *
Packit Service 310c69
 * @param priority      the priority at which to log the message
Packit Service 310c69
 * @param prefix        optional string prefix to message, may be NULL
Packit Service 310c69
 * @param fmt1          format of message first part, may be NULL
Packit Service 310c69
 * @param args1         arguments for message first part
Packit Service 310c69
 * @param fmt2          format of message second part
Packit Service 310c69
 **/
Packit Service 310c69
void logEmbeddedMessage(int         priority,
Packit Service 310c69
                        const char *prefix,
Packit Service 310c69
                        const char *fmt1,
Packit Service 310c69
                        va_list     args1,
Packit Service 310c69
                        const char *fmt2,
Packit Service 310c69
                        ...)
Packit Service 310c69
  __attribute__((format(printf, 3, 0), format(printf, 5, 6)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a message pack consisting of multiple variable sections.
Packit Service 310c69
 *
Packit Service 310c69
 * @param priority      the priority at which to log the message
Packit Service 310c69
 * @param prefix        optional string prefix to message, may be NULL
Packit Service 310c69
 * @param fmt1          format of message first part, may be NULL
Packit Service 310c69
 * @param args1         arguments for message first part
Packit Service 310c69
 * @param fmt2          format of message second part, may be NULL
Packit Service 310c69
 * @param args2         arguments for message second part
Packit Service 310c69
 **/
Packit Service 310c69
void logMessagePack(int         priority,
Packit Service 310c69
                    const char *prefix,
Packit Service 310c69
                    const char *fmt1,
Packit Service 310c69
                    va_list     args1,
Packit Service 310c69
                    const char *fmt2,
Packit Service 310c69
                    va_list     args2)
Packit Service 310c69
  __attribute__((format(printf, 3, 0)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a stack backtrace.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  priority The priority at which to log the backtrace
Packit Service 310c69
 **/
Packit Service 310c69
void logBacktrace(int priority);
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a message with an error from an error code.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  priority The priority of the logging entry
Packit Service 310c69
 * @param  errnum   Int value of errno or a UDS_* value.
Packit Service 310c69
 * @param  format   The format of the message (a printf style format)
Packit Service 310c69
 *
Packit Service 310c69
 * @return errnum
Packit Service 310c69
 **/
Packit Service 310c69
int logWithStringError(int priority, int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 3, 4)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a message with an error from an error code.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  priority The priority of the logging entry
Packit Service 310c69
 * @param  errnum   Int value of errno or a UDS_* value.
Packit Service 310c69
 * @param  format   The format of the message (a printf style format)
Packit Service 310c69
 * @param  args     The list of arguments with format.
Packit Service 310c69
 *
Packit Service 310c69
 * @return errnum
Packit Service 310c69
 **/
Packit Service 310c69
int vLogWithStringError(int         priority,
Packit Service 310c69
                        int         errnum,
Packit Service 310c69
                        const char *format,
Packit Service 310c69
                        va_list     args)
Packit Service 310c69
  __attribute__((format(printf, 3, 0)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log an error prefixed with the string associated with the errnum.
Packit Service 310c69
 *
Packit Service 310c69
 * @param errnum Int value of errno or a UDS_* value.
Packit Service 310c69
 * @param format The format of the message (a printf style format)
Packit Service 310c69
 *
Packit Service 310c69
 * @return errnum
Packit Service 310c69
 **/
Packit Service 310c69
int logErrorWithStringError(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int logDebugWithStringError(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int logInfoWithStringError(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int logNoticeWithStringError(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int logWarningWithStringError(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**********************************************************************/
Packit Service 310c69
int logFatalWithStringError(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * IF the result is an error, log a FATAL level message and return the result
Packit Service 310c69
 * after marking it unrecoverable.  The UDS_SUCCESS and UDS_QUEUED results are
Packit Service 310c69
 * not considered errors and are returned unmodified.
Packit Service 310c69
 *
Packit Service 310c69
 * @param errnum  int value of errno or a UDS_* value.
Packit Service 310c69
 * @param format  The format of the message (a printf style format)
Packit Service 310c69
 *
Packit Service 310c69
 * @return makeUnrecoverable(errnum) or UDS_SUCCESS or UDS_QUEUED
Packit Service 310c69
 **/
Packit Service 310c69
int logUnrecoverable(int errnum, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a fatal error.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  format The format of the message (a printf style format)
Packit Service 310c69
 **/
Packit Service 310c69
void logFatal(const char *format, ...) __attribute__((format(printf, 1, 2)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a message -- for internal use only.
Packit Service 310c69
 *
Packit Service 310c69
 * @param  priority The syslog priority value for the message.
Packit Service 310c69
 * @param  format   The format of the message (a printf style format)
Packit Service 310c69
 * @param  args     The variadic argument list of format parameters.
Packit Service 310c69
 **/
Packit Service 310c69
void vLogMessage(int priority, const char *format, va_list args)
Packit Service 310c69
  __attribute__((format(printf, 2, 0)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Log a message
Packit Service 310c69
 *
Packit Service 310c69
 * @param  priority The syslog priority value for the message.
Packit Service 310c69
 * @param  format   The format of the message (a printf style format)
Packit Service 310c69
 **/
Packit Service 310c69
void logMessage(int priority, const char *format, ...)
Packit Service 310c69
  __attribute__((format(printf, 2, 3)));
Packit Service 310c69
Packit Service 310c69
/**
Packit Service 310c69
 * Sleep or delay a short time (likely a few milliseconds) in an attempt allow
Packit Service 310c69
 * the log buffers to be written out in case they might be overrun. This is
Packit Service 310c69
 * unnecessary in user-space (and is a no-op there), but is needed when
Packit Service 310c69
 * quickly issuing a lot of log output in the Linux kernel, as when dumping a
Packit Service 310c69
 * large number of data structures.
Packit Service 310c69
 **/
Packit Service 310c69
void pauseForLogger(void);
Packit Service 310c69
Packit Service 310c69
#endif /* LOGGER_H */