Blame tools/fpgad/api/logging.c

Packit 6639f8
// Copyright(c) 2018-2020, Intel Corporation
Packit 6639f8
//
Packit 6639f8
// Redistribution  and  use  in source  and  binary  forms,  with  or  without
Packit 6639f8
// modification, are permitted provided that the following conditions are met:
Packit 6639f8
//
Packit 6639f8
// * Redistributions of  source code  must retain the  above copyright notice,
Packit 6639f8
//   this list of conditions and the following disclaimer.
Packit 6639f8
// * Redistributions in binary form must reproduce the above copyright notice,
Packit 6639f8
//   this list of conditions and the following disclaimer in the documentation
Packit 6639f8
//   and/or other materials provided with the distribution.
Packit 6639f8
// * Neither the name  of Intel Corporation  nor the names of its contributors
Packit 6639f8
//   may be used to  endorse or promote  products derived  from this  software
Packit 6639f8
//   without specific prior written permission.
Packit 6639f8
//
Packit 6639f8
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit 6639f8
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,  BUT NOT LIMITED TO,  THE
Packit 6639f8
// IMPLIED WARRANTIES OF  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit 6639f8
// ARE DISCLAIMED.  IN NO EVENT  SHALL THE COPYRIGHT OWNER  OR CONTRIBUTORS BE
Packit 6639f8
// LIABLE  FOR  ANY  DIRECT,  INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
Packit 6639f8
// CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT  OF
Packit 6639f8
// SUBSTITUTE GOODS OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
Packit 6639f8
// INTERRUPTION)  HOWEVER CAUSED  AND ON ANY THEORY  OF LIABILITY,  WHETHER IN
Packit 6639f8
// CONTRACT,  STRICT LIABILITY,  OR TORT  (INCLUDING NEGLIGENCE  OR OTHERWISE)
Packit 6639f8
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
Packit 6639f8
// POSSIBILITY OF SUCH DAMAGE.
Packit 6639f8
Packit 6639f8
#ifdef HAVE_CONFIG_H
Packit 6639f8
#include <config.h>
Packit 6639f8
#endif // HAVE_CONFIG_H
Packit 6639f8
Packit 6639f8
#include <time.h>
Packit 6639f8
#include "logging.h"
Packit 6639f8
Packit 6639f8
#ifdef LOG
Packit 6639f8
#undef LOG
Packit 6639f8
#endif
Packit 6639f8
#define LOG(format, ...) \
Packit 6639f8
log_printf("logging: " format, ##__VA_ARGS__)
Packit 6639f8
Packit 6639f8
STATIC pthread_mutex_t log_lock = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
Packit 6639f8
STATIC FILE *log_file;
Packit 6639f8
Packit 6639f8
#define BUF_TIME_LEN    256
Packit 6639f8
Packit 6639f8
int log_open(const char *filename)
Packit 6639f8
{
Packit 6639f8
	int res;
Packit 6639f8
	int err;
Packit 6639f8
Packit 6639f8
	fpgad_mutex_lock(err, &log_lock);
Packit 6639f8
Packit 6639f8
	log_file = fopen(filename, "a");
Packit 6639f8
	if (log_file) {
Packit 6639f8
		time_t raw;
Packit 6639f8
		struct tm tm;
Packit 6639f8
		char timebuf[BUF_TIME_LEN];
Packit 6639f8
		size_t len;
Packit 6639f8
Packit 6639f8
		time(&raw;;
Packit 6639f8
		localtime_r(&raw, &tm;;
Packit 6639f8
		asctime_r(&tm, timebuf);
Packit 6639f8
Packit 6639f8
		len = strnlen(timebuf, sizeof(timebuf));
Packit 6639f8
		if (len < BUF_TIME_LEN) {
Packit 6639f8
			timebuf[len - 1] = '\0'; /* erase \n */
Packit 6639f8
		} else {
Packit 6639f8
			printf(" Invalid time stamp buffer size \n");
Packit 6639f8
			fpgad_mutex_unlock(err, &log_lock);
Packit 6639f8
			return -1;
Packit 6639f8
		}
Packit 6639f8
Packit 6639f8
		res = fprintf(log_file, "----- %s -----\n", timebuf);
Packit 6639f8
		fflush(log_file);
Packit 6639f8
	} else {
Packit 6639f8
		res = -1;
Packit 6639f8
	}
Packit 6639f8
Packit 6639f8
	fpgad_mutex_unlock(err, &log_lock);
Packit 6639f8
Packit 6639f8
	return res;
Packit 6639f8
}
Packit 6639f8
Packit 6639f8
int log_printf(const char *fmt, ...)
Packit 6639f8
{
Packit 6639f8
	va_list l;
Packit 6639f8
	int res = -1;
Packit 6639f8
	int err;
Packit 6639f8
Packit 6639f8
	va_start(l, fmt);
Packit 6639f8
Packit 6639f8
	fpgad_mutex_lock(err, &log_lock);
Packit 6639f8
Packit 6639f8
	if (log_file) {
Packit 6639f8
		res = vfprintf(log_file, fmt, l);
Packit 6639f8
		fflush(log_file);
Packit 6639f8
	}
Packit 6639f8
Packit 6639f8
	fpgad_mutex_unlock(err, &log_lock);
Packit 6639f8
Packit 6639f8
	va_end(l);
Packit 6639f8
Packit 6639f8
	return res;
Packit 6639f8
}
Packit 6639f8
Packit 6639f8
void log_set(FILE *fptr)
Packit 6639f8
{
Packit 6639f8
	int err;
Packit 6639f8
Packit 6639f8
	fpgad_mutex_lock(err, &log_lock);
Packit 6639f8
Packit 6639f8
	log_close();
Packit 6639f8
	log_file = fptr;
Packit 6639f8
Packit 6639f8
	fpgad_mutex_unlock(err, &log_lock);
Packit 6639f8
}
Packit 6639f8
Packit 6639f8
void log_close(void)
Packit 6639f8
{
Packit 6639f8
	int err;
Packit 6639f8
Packit 6639f8
	fpgad_mutex_lock(err, &log_lock);
Packit 6639f8
Packit 6639f8
	if (log_file) {
Packit 6639f8
		if (log_file != stdout &&
Packit 6639f8
		    log_file != stderr) {
Packit 6639f8
			fclose(log_file);
Packit 6639f8
		}
Packit 6639f8
		log_file = NULL;
Packit 6639f8
	}
Packit 6639f8
Packit 6639f8
	fpgad_mutex_unlock(err, &log_lock);
Packit 6639f8
}