Blame shared/nm-utils/nm-vpn-plugin-macros.h

Packit Service 87a54e
/* SPDX-License-Identifier: LGPL-2.1-or-later */
Packit 5756e2
/*
Packit 5756e2
 * Copyright (C) 2016 Red Hat, Inc.
Packit 5756e2
 */
Packit 5756e2
Packit 5756e2
#ifndef __NM_VPN_PLUGIN_MACROS_H__
Packit 5756e2
#define __NM_VPN_PLUGIN_MACROS_H__
Packit 5756e2
Packit 5756e2
#include <syslog.h>
Packit 5756e2
Packit 5756e2
static inline int
Packit Service a1bd4f
nm_utils_syslog_coerce_from_nm(int syslog_level)
Packit 5756e2
{
Packit Service a1bd4f
    /* NetworkManager uses internally NMLogLevel levels. When spawning
Packit Service a1bd4f
     * the VPN plugin, it maps those levels to syslog levels as follows:
Packit Service a1bd4f
     *
Packit Service a1bd4f
     *  LOGL_INFO = LOG_NOTICE,
Packit Service a1bd4f
     *  LOGL_DEBUG = LOG_INFO,
Packit Service a1bd4f
     *  LOGL_TRACE = LOG_DEBUG,
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * However, when actually printing to syslog, we don't want to print messages
Packit Service a1bd4f
     * with LOGL_INFO level as LOG_NOTICE, because they are *not* to be highlighted.
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * In other words: NetworkManager has 3 levels that should not require highlighting:
Packit Service a1bd4f
     * LOGL_INFO, LOGL_DEBUG, LOGL_TRACE. syslog on the other hand has only LOG_INFO and LOG_DEBUG.
Packit Service a1bd4f
     *
Packit Service a1bd4f
     * So, coerce those values before printing to syslog. When you receive the syslog_level
Packit Service a1bd4f
     * from NetworkManager, instead of calling
Packit Service a1bd4f
     *   syslog(syslog_level, ...)
Packit Service a1bd4f
     * you should call
Packit Service a1bd4f
     *   syslog(nm_utils_syslog_coerce_from_nm(syslog_level), ...)
Packit Service a1bd4f
     */
Packit Service a1bd4f
    switch (syslog_level) {
Packit Service a1bd4f
    case LOG_INFO:
Packit Service a1bd4f
        return LOG_DEBUG;
Packit Service a1bd4f
    case LOG_NOTICE:
Packit Service a1bd4f
        return LOG_INFO;
Packit Service a1bd4f
    default:
Packit Service a1bd4f
        return syslog_level;
Packit Service a1bd4f
    }
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
static inline const char *
Packit Service a1bd4f
nm_utils_syslog_to_str(int syslog_level)
Packit 5756e2
{
Packit Service a1bd4f
    /* Maps the levels the same way as NetworkManager's nm-logging.c does */
Packit Service a1bd4f
    if (syslog_level >= LOG_DEBUG)
Packit Service a1bd4f
        return "<trace>";
Packit Service a1bd4f
    if (syslog_level >= LOG_INFO)
Packit Service a1bd4f
        return "<debug>";
Packit Service a1bd4f
    if (syslog_level >= LOG_NOTICE)
Packit Service a1bd4f
        return "<info>";
Packit Service a1bd4f
    if (syslog_level >= LOG_WARNING)
Packit Service a1bd4f
        return "<warn>";
Packit Service a1bd4f
    return "<error>";
Packit 5756e2
}
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
/* possibly missing defines from newer libnm API. */
Packit 5756e2
Packit 5756e2
#ifndef NM_VPN_PLUGIN_CONFIG_PROXY_PAC
Packit Service a1bd4f
    #define NM_VPN_PLUGIN_CONFIG_PROXY_PAC "pac"
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#ifndef NM_VPN_PLUGIN_IP4_CONFIG_PRESERVE_ROUTES
Packit Service a1bd4f
    #define NM_VPN_PLUGIN_IP4_CONFIG_PRESERVE_ROUTES "preserve-routes"
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
#ifndef NM_VPN_PLUGIN_IP6_CONFIG_PRESERVE_ROUTES
Packit Service a1bd4f
    #define NM_VPN_PLUGIN_IP6_CONFIG_PRESERVE_ROUTES "preserve-routes"
Packit 5756e2
#endif
Packit 5756e2
Packit 5756e2
/*****************************************************************************/
Packit 5756e2
Packit 5756e2
#endif /* __NM_VPN_PLUGIN_MACROS_H__ */