Blame src/memkind_log.c

Packit 345191
/*
Packit 345191
 * Copyright (C) 2016 - 2019 Intel Corporation.
Packit 345191
 * All rights reserved.
Packit 345191
 *
Packit 345191
 * Redistribution and use in source and binary forms, with or without
Packit 345191
 * modification, are permitted provided that the following conditions are met:
Packit 345191
 * 1. Redistributions of source code must retain the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer.
Packit 345191
 * 2. Redistributions in binary form must reproduce the above copyright notice(s),
Packit 345191
 *    this list of conditions and the following disclaimer in the documentation
Packit 345191
 *    and/or other materials provided with the distribution.
Packit 345191
 *
Packit 345191
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY EXPRESS
Packit 345191
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
Packit 345191
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
Packit 345191
 * EVENT SHALL THE COPYRIGHT HOLDER(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit 345191
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
Packit 345191
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
Packit 345191
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
Packit 345191
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
Packit 345191
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
Packit 345191
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit 345191
 */
Packit 345191
Packit 345191
#include <memkind/internal/memkind_log.h>
Packit 345191
Packit 345191
#include <assert.h>
Packit 345191
#include <stdio.h>
Packit 345191
#include <pthread.h>
Packit 345191
#include <stdarg.h>
Packit 345191
#include <stdlib.h>
Packit 345191
#include <string.h>
Packit 345191
#include <stdbool.h>
Packit 345191
Packit 345191
typedef enum {
Packit 345191
    MESSAGE_TYPE_INFO,
Packit 345191
    MESSAGE_TYPE_ERROR,
Packit 345191
    MESSAGE_TYPE_FATAL,
Packit 345191
    MESSAGE_TYPE_MAX_VALUE,
Packit 345191
} message_type_t;
Packit 345191
Packit 345191
static char *message_prefixes[MESSAGE_TYPE_MAX_VALUE] = {
Packit 345191
    [MESSAGE_TYPE_INFO]     = "MEMKIND_INFO",
Packit 345191
    [MESSAGE_TYPE_ERROR]    = "MEMKIND_ERROR",
Packit 345191
    [MESSAGE_TYPE_FATAL]    = "MEMKIND_FATAL",
Packit 345191
};
Packit 345191
Packit 345191
static bool log_enabled;
Packit 345191
Packit 345191
static pthread_once_t init_once = PTHREAD_ONCE_INIT;
Packit 345191
static void log_init_once(void)
Packit 345191
{
Packit 345191
    char *memkind_debug_env = secure_getenv("MEMKIND_DEBUG");
Packit 345191
Packit 345191
    if (memkind_debug_env) {
Packit 345191
        if(strcmp(memkind_debug_env, "1") == 0) {
Packit 345191
            log_enabled = true;
Packit 345191
        } else {
Packit 345191
            fprintf(stderr,
Packit 345191
                    "MEMKIND_WARNING: debug option \"%s\" unknown; Try man memkind for available options.\n",
Packit 345191
                    memkind_debug_env);
Packit 345191
        }
Packit 345191
    }
Packit 345191
}
Packit 345191
Packit 345191
static pthread_mutex_t log_lock = PTHREAD_MUTEX_INITIALIZER;
Packit 345191
static void log_generic(message_type_t type, const char *format, va_list args)
Packit 345191
{
Packit 345191
    pthread_once(&init_once, log_init_once);
Packit 345191
    if(log_enabled || (type == MESSAGE_TYPE_FATAL)) {
Packit 345191
        if (pthread_mutex_lock(&log_lock) != 0)
Packit 345191
            assert(0 && "failed to acquire mutex");
Packit 345191
        fprintf(stderr, "%s: ", message_prefixes[type]);
Packit 345191
        vfprintf(stderr, format, args);
Packit 345191
        fprintf(stderr, "\n");
Packit 345191
        if (pthread_mutex_unlock(&log_lock) != 0)
Packit 345191
            assert(0 && "failed to release mutex");
Packit 345191
    }
Packit 345191
}
Packit 345191
Packit 345191
void log_info(const char *format, ...)
Packit 345191
{
Packit 345191
    va_list args;
Packit 345191
    va_start(args, format);
Packit 345191
    log_generic(MESSAGE_TYPE_INFO, format, args);
Packit 345191
    va_end(args);
Packit 345191
}
Packit 345191
Packit 345191
void log_err(const char *format, ...)
Packit 345191
{
Packit 345191
    va_list args;
Packit 345191
    va_start(args, format);
Packit 345191
    log_generic(MESSAGE_TYPE_ERROR, format, args);
Packit 345191
    va_end(args);
Packit 345191
}
Packit 345191
Packit 345191
void log_fatal(const char *format, ...)
Packit 345191
{
Packit 345191
    va_list args;
Packit 345191
    va_start(args, format);
Packit 345191
    log_generic(MESSAGE_TYPE_FATAL, format, args);
Packit 345191
    va_end(args);
Packit 345191
}
Packit 345191