Exemple #1
0
void VLog(LogLevel level, const char *fmt, va_list ap)
{
    LoggingContext *lctx = GetCurrentThreadContext();

    char *msg = StringVFormat(fmt, ap);
    const char *hooked_msg = NULL;

    if (lctx->pctx && lctx->pctx->log_hook)
    {
        hooked_msg = lctx->pctx->log_hook(lctx->pctx, msg);
    }
    else
    {
        hooked_msg = msg;
    }

    if (level <= lctx->report_level)
    {
        LogToStdout(hooked_msg, level, lctx->color);
    }

    if (level <= lctx->log_level)
    {
        LogToSystemLog(hooked_msg, level);
    }
    free(msg);
}
Exemple #2
0
void VLog(LogLevel level, const char *fmt, va_list ap)
{
    LoggingContext *lctx = GetCurrentThreadContext();

    bool log_to_console = ( level <= lctx->report_level );
    bool log_to_syslog  = ( level <= lctx->log_level &&
                            level < LOG_LEVEL_VERBOSE );
    bool force_hook     = ( lctx->pctx &&
                            lctx->pctx->log_hook &&
                            lctx->pctx->force_hook_level >= level );

    if (!log_to_console && !log_to_syslog && !force_hook)
    {
        return;                            /* early return - save resources */
    }

    char *msg = StringVFormat(fmt, ap);
    char *hooked_msg = NULL;

    /* Remove ending EOLN. */
    for (char *sp = msg; *sp != '\0'; sp++)
    {
        if (*sp == '\n' && *(sp+1) == '\0')
        {
            *sp = '\0';
            break;
        }
    }

    if (lctx->pctx && lctx->pctx->log_hook)
    {
        hooked_msg = lctx->pctx->log_hook(lctx->pctx, level, msg);
    }
    else
    {
        hooked_msg = msg;
    }

    if (log_to_console)
    {
        LogToConsole(hooked_msg, level, lctx->color);
    }
    if (log_to_syslog)
    {
        LogToSystemLog(hooked_msg, level);
    }

    if (hooked_msg != msg)
    {
        free(hooked_msg);
    }
    free(msg);
}
Exemple #3
0
void VLog(LogLevel level, const char *fmt, va_list ap)
{
    LoggingContext *lctx = GetCurrentThreadContext();

    char *msg = StringVFormat(fmt, ap);
    char *hooked_msg = NULL;

    for (char *sp = msg; *sp != '\0'; sp++)
    {
        if (*sp == '\n' && *(sp+1) == '\0')
        {
            *sp = '\0';
            break;
        }
    }

    if (lctx->pctx && lctx->pctx->log_hook)
    {
        hooked_msg = lctx->pctx->log_hook(lctx->pctx, level, msg);
    }
    else
    {
        hooked_msg = xstrdup(msg);
    }

    if (level <= lctx->report_level)
    {
        if (MACHINE_OUTPUT)
        {
            LogToConsole(hooked_msg, level, lctx->color);
        }
        else
        {
            LogToConsole(msg, level, lctx->color);
        }
    }

    if (level <= lctx->log_level && level < LOG_LEVEL_VERBOSE)
    {
        LogToSystemLog(hooked_msg, level);
    }
    free(msg);
    free(hooked_msg);
}
Exemple #4
0
/**
 * @brief Logs binary data in #buf, with unprintable bytes translated to '.'.
 *        Message is prefixed with #prefix.
 * @param #buflen must be no more than CF_MAX_BUFSIZE
 */
void LogRaw(LogLevel level, const char *prefix, const void *buf, size_t buflen)
{
    const unsigned char *src = buf;
    unsigned char dst[buflen+1];
    assert(sizeof(dst) <= CF_MAX_BUFSIZE);

    LoggingContext *lctx = GetCurrentThreadContext();
    if (level <= lctx->report_level || level <= lctx->log_level)
    {
        size_t i;
        for (i = 0; i < buflen; i++)
        {
            dst[i] = isprint(src[i]) ? src[i] : '.';
        }
        dst[i] = '\0';

        /* And Log the translated buffer, which is now a valid string. */
        Log(level, "%s%s", prefix, dst);
    }
}
Exemple #5
0
void LoggingPrivSetLevels(LogLevel log_level, LogLevel report_level)
{
    LoggingContext *lctx = GetCurrentThreadContext();
    lctx->log_level = log_level;
    lctx->report_level = report_level;
}
Exemple #6
0
LoggingPrivContext *LoggingPrivGetContext(void)
{
    LoggingContext *lctx = GetCurrentThreadContext();
    return lctx->pctx;
}
Exemple #7
0
void LoggingPrivSetContext(LoggingPrivContext *pctx)
{
    LoggingContext *lctx = GetCurrentThreadContext();
    lctx->pctx = pctx;
}
Exemple #8
0
void LoggingSetColor(bool enabled)
{
    LoggingContext *lctx = GetCurrentThreadContext();
    lctx->color = enabled;
}
Exemple #9
0
LogLevel LogGetGlobalLevel(void)
{
    const LoggingContext *lctx = GetCurrentThreadContext();
    return lctx->global_level;
}
Exemple #10
0
void LogSetGlobalLevel(LogLevel level)
{
    LoggingContext *lctx = GetCurrentThreadContext();
    lctx->global_level = level;
    LoggingPrivSetLevels(level, level);
}