|
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.c#3 $
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "logger.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#include "common.h"
|
|
Packit Service |
310c69 |
#include "errors.h"
|
|
Packit Service |
310c69 |
#include "stringUtils.h"
|
|
Packit Service |
310c69 |
#include "threads.h"
|
|
Packit Service |
310c69 |
#include "uds.h"
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
typedef struct {
|
|
Packit Service |
310c69 |
const char *name;
|
|
Packit Service |
310c69 |
const int priority;
|
|
Packit Service |
310c69 |
} PriorityName;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
static const PriorityName PRIORITIES[] = {
|
|
Packit Service |
310c69 |
{ "ALERT", LOG_ALERT },
|
|
Packit Service |
310c69 |
{ "CRITICAL", LOG_CRIT },
|
|
Packit Service |
310c69 |
{ "CRIT", LOG_CRIT },
|
|
Packit Service |
310c69 |
{ "DEBUG", LOG_DEBUG },
|
|
Packit Service |
310c69 |
{ "EMERGENCY", LOG_EMERG },
|
|
Packit Service |
310c69 |
{ "EMERG", LOG_EMERG },
|
|
Packit Service |
310c69 |
{ "ERROR", LOG_ERR },
|
|
Packit Service |
310c69 |
{ "ERR", LOG_ERR },
|
|
Packit Service |
310c69 |
{ "INFO", LOG_INFO },
|
|
Packit Service |
310c69 |
{ "NOTICE", LOG_NOTICE },
|
|
Packit Service |
310c69 |
{ "PANIC", LOG_EMERG },
|
|
Packit Service |
310c69 |
{ "WARN", LOG_WARNING },
|
|
Packit Service |
310c69 |
{ "WARNING", LOG_WARNING },
|
|
Packit Service |
310c69 |
{ NULL, -1 },
|
|
Packit Service |
310c69 |
};
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
static const char *const PRIORITY_STRINGS[] = {
|
|
Packit Service |
310c69 |
"EMERGENCY",
|
|
Packit Service |
310c69 |
"ALERT",
|
|
Packit Service |
310c69 |
"CRITICAL",
|
|
Packit Service |
310c69 |
"ERROR",
|
|
Packit Service |
310c69 |
"WARN",
|
|
Packit Service |
310c69 |
"NOTICE",
|
|
Packit Service |
310c69 |
"INFO",
|
|
Packit Service |
310c69 |
"DEBUG",
|
|
Packit Service |
310c69 |
};
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
static int logLevel = LOG_INFO;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int getLogLevel(void)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
return logLevel;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void setLogLevel(int newLogLevel)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
logLevel = newLogLevel;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int stringToPriority(const char *string)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
int i;
|
|
Packit Service |
310c69 |
for (i = 0; PRIORITIES[i].name != NULL; i++) {
|
|
Packit Service |
310c69 |
if (strcasecmp(string, PRIORITIES[i].name) == 0) {
|
|
Packit Service |
310c69 |
return PRIORITIES[i].priority;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
return LOG_INFO;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
const char *priorityToString(int priority)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
if ((priority < 0) || (priority >= (int) COUNT_OF(PRIORITY_STRINGS))) {
|
|
Packit Service |
310c69 |
return "unknown";
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
return PRIORITY_STRINGS[priority];
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
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 |
{
|
|
Packit Service |
310c69 |
va_list ap;
|
|
Packit Service |
310c69 |
va_start(ap, fmt2);
|
|
Packit Service |
310c69 |
logMessagePack(priority, prefix, fmt1, args1, fmt2, ap);
|
|
Packit Service |
310c69 |
va_end(ap);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
#pragma GCC diagnostic push
|
|
Packit Service |
310c69 |
/*
|
|
Packit Service |
310c69 |
* GCC (version 8.1.1 20180502 (Red Hat 8.1.1-1)) on Fedora 28 seems
|
|
Packit Service |
310c69 |
* to think that this function should get a printf format
|
|
Packit Service |
310c69 |
* attribute. But we have no second format string, and no additional
|
|
Packit Service |
310c69 |
* arguments at the call site, and GCC also gets unhappy trying to
|
|
Packit Service |
310c69 |
* analyze the format and values when there are none. So we'll just
|
|
Packit Service |
310c69 |
* shut it up.
|
|
Packit Service |
310c69 |
*/
|
|
Packit Service |
310c69 |
#pragma GCC diagnostic ignored "-Wsuggest-attribute=format"
|
|
Packit Service |
310c69 |
/**
|
|
Packit Service |
310c69 |
* Log a message.
|
|
Packit Service |
310c69 |
*
|
|
Packit Service |
310c69 |
* This helper function exists solely to create a valid va_list with
|
|
Packit Service |
310c69 |
* no useful info. It does the real work of vLogMessage, which wants a
|
|
Packit Service |
310c69 |
* second va_list object to pass down.
|
|
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 |
static void vLogMessageHelper(int priority,
|
|
Packit Service |
310c69 |
const char *format,
|
|
Packit Service |
310c69 |
va_list args,
|
|
Packit Service |
310c69 |
...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list dummy;
|
|
Packit Service |
310c69 |
va_start(dummy, args);
|
|
Packit Service |
310c69 |
logMessagePack(priority, NULL, format, args, NULL, dummy);
|
|
Packit Service |
310c69 |
va_end(dummy);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
#pragma GCC diagnostic pop
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void vLogMessage(int priority, const char *format, va_list args)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
vLogMessageHelper(priority, format, args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logMessage(int priority, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(priority, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logDebug(const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(LOG_DEBUG, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logInfo(const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(LOG_INFO, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logNotice(const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(LOG_NOTICE, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logWarning(const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(LOG_WARNING, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logError(const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(LOG_ERR, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
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 |
{
|
|
Packit Service |
310c69 |
char errbuf[ERRBUF_SIZE];
|
|
Packit Service |
310c69 |
logEmbeddedMessage(priority, NULL, format, args, ": %s (%d)",
|
|
Packit Service |
310c69 |
stringError(errnum, errbuf, sizeof(errbuf)),
|
|
Packit Service |
310c69 |
errnum);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logWithStringError(int priority, int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(priority, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logErrorWithStringError(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_ERR, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logWarningWithStringError(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_WARNING, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logDebugWithStringError(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_DEBUG, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logInfoWithStringError(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_INFO, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logNoticeWithStringError(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_NOTICE, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logFatalWithStringError(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_CRIT, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
int logUnrecoverable(int errnum, const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
if (isSuccessful(errnum)) {
|
|
Packit Service |
310c69 |
return errnum;
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogWithStringError(LOG_CRIT, errnum, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
return makeUnrecoverable(errnum);
|
|
Packit Service |
310c69 |
}
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
/*****************************************************************************/
|
|
Packit Service |
310c69 |
void logFatal(const char *format, ...)
|
|
Packit Service |
310c69 |
{
|
|
Packit Service |
310c69 |
va_list args;
|
|
Packit Service |
310c69 |
|
|
Packit Service |
310c69 |
va_start(args, format);
|
|
Packit Service |
310c69 |
vLogMessage(LOG_CRIT, format, args);
|
|
Packit Service |
310c69 |
va_end(args);
|
|
Packit Service |
310c69 |
}
|