Commit 90ad28be authored by Alberto Miranda's avatar Alberto Miranda ♨️
Browse files

Add logger for libraries using the ERR macro

For now, only available in *_debug libraries
parent 513c12f5
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ libnorns_common_la_SOURCES = \
	communication.h \
	defaults.h \
	errors.c \
	log.c \
	requests.c \
	requests.h \
	resources.c \
+2 −0
Original line number Diff line number Diff line
@@ -43,6 +43,7 @@
#include "requests.h"
#include "communication.h"
#include "xmalloc.h"
#include "log.h"

static int connect_to_daemon(const char* socket_path);
static int send_message(int conn, const msgbuffer_t* buffer);
@@ -341,6 +342,7 @@ connect_to_daemon(const char* socket_path) {
	server.sun_path[sizeof(server.sun_path)-1] = '\0';

	if (connect(sfd, (struct sockaddr *) &server, sizeof(struct sockaddr_un)) < 0) {
	    ERR("!connect");
	    return -1;
    }

+1 −0
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@
#ifndef __DAEMON_COMMUNICATION_H__
#define __DAEMON_COMMUNICATION_H__

#include "nornsctl.h"
#include "requests.h"

#pragma GCC visibility push(hidden)

lib/log.c

0 → 100644
+121 −0
Original line number Diff line number Diff line
/*************************************************************************
 * Copyright (C) 2017-2018 Barcelona Supercomputing Center               *
 *                         Centro Nacional de Supercomputacion           *
 * All rights reserved.                                                  *
 *                                                                       *
 * This file is part of the NORNS Data Scheduler, a service that allows  *
 * other programs to start, track and manage asynchronous transfers of   *
 * data resources transfers requests between different storage backends. *
 *                                                                       *
 * See AUTHORS file in the top level directory for information           *
 * regarding developers and contributors.                                *
 *                                                                       *
 * The NORNS Data Scheduler is free software: you can redistribute it    *
 * and/or modify it under the terms of the GNU Lesser General Public     *
 * License as published by the Free Software Foundation, either          *
 * version 3 of the License, or (at your option) any later version.      *
 *                                                                       *
 * The NORNS Data Scheduler is distributed in the hope that it will be   *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General             *
 * Public License along with the NORNS Data Scheduler.  If not, see      *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

#include <stdbool.h>
#include <stdarg.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "log.h"

static const char* log_prefix;

#define MAX_LOG_MSG 8192
#define MAX_ERROR_MSG 128

void
log_init(const char* prefix) {

    static bool once = false;

    if(once) {
        return;
    }

    log_prefix = prefix;

    once = true;
}

static void
log_error_helper(const char* file, int line, const char* func, 
                 const char* suffix, const char* fmt, va_list ap) {

    int saved_errno = errno;
    char buffer[MAX_LOG_MSG];
    char errstr[MAX_ERROR_MSG] = "";
    unsigned cc = 0;
    const char* sep = "";
    int ret;

    if(file != NULL) {

        char* f;

        while((f = strchr(file, '/')) != NULL) {
            file = f+1;
        }

        ret = snprintf(&buffer[cc], MAX_LOG_MSG - cc,
                "<%s>: [%s:%d %s] ", log_prefix, file, line, func);

        if(ret < 0) {
            fprintf(stderr, "vsnprintf failed");
            goto end;
        }

        cc += (unsigned) ret;
    }

    if(fmt != NULL) {

        // if fmt begins with '!', append also the system's error message
        if(*fmt == '!') {
            ++fmt;
            sep = ": ";
            strerror_r(errno, errstr, MAX_ERROR_MSG);
        }

        ret = vsnprintf(&buffer[cc], MAX_LOG_MSG - cc, fmt, ap);

        if(ret < 0) {
            fprintf(stderr, "vsnprintf failed");
            goto end;
        }

        cc += (unsigned) ret;
    }

    snprintf(&buffer[cc], MAX_LOG_MSG - cc, "%s%s%s", sep, errstr, suffix);

    fprintf(stderr, "%s", buffer);

end:
	errno = saved_errno;
}

void
log_error(const char* file, int line, const char* func, 
          const char* fmt, ...) {

    va_list ap;
    va_start(ap, fmt);
    log_error_helper(file, line, func, "\n", fmt, ap);
    va_end(ap);
}

lib/log.h

0 → 100644
+42 −0
Original line number Diff line number Diff line
/*************************************************************************
 * Copyright (C) 2017-2018 Barcelona Supercomputing Center               *
 *                         Centro Nacional de Supercomputacion           *
 * All rights reserved.                                                  *
 *                                                                       *
 * This file is part of the NORNS Data Scheduler, a service that allows  *
 * other programs to start, track and manage asynchronous transfers of   *
 * data resources transfers requests between different storage backends. *
 *                                                                       *
 * See AUTHORS file in the top level directory for information           *
 * regarding developers and contributors.                                *
 *                                                                       *
 * The NORNS Data Scheduler is free software: you can redistribute it    *
 * and/or modify it under the terms of the GNU Lesser General Public     *
 * License as published by the Free Software Foundation, either          *
 * version 3 of the License, or (at your option) any later version.      *
 *                                                                       *
 * The NORNS Data Scheduler is distributed in the hope that it will be   *
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty   *
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  *
 * Lesser General Public License for more details.                       *
 *                                                                       *
 * You should have received a copy of the GNU Lesser General             *
 * Public License along with the NORNS Data Scheduler.  If not, see      *
 * <http://www.gnu.org/licenses/>.                                       *
 *************************************************************************/

#ifndef __NORNS_LOG_H__
#define __NORNS_LOG_H__ 1

#ifdef __NORNS_DEBUG__
#define ERR(...)\
    log_error(__FILE__, __LINE__, __func__, __VA_ARGS__)
#else
#define ERR(...)
#endif /* __NORNS_DEBUG__ */

void log_init(const char* prefix);
void log_error(const char* file, int line, const char* func, 
               const char* fmt, ...);

#endif /* __NORNS_LOG_H__ */
Loading