view logging.c @ 197:79db2c87d1b0

New build. Do not install configuration file.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Mon, 31 Mar 2014 17:03:37 +0200
parents 0fed60877099
children 111610d871af
line wrap: on
line source

#include <ansi_c.h>
#include <inifile.h>
#include <lowlvlio.h>

#include "config.h"
#include "logging.h"

/* this refers to the event time defined as a global variable in the main program */
extern double utc;


int __logger_init(struct logger *l)
{
	int rv, panel;
	char path[MAX_PATHNAME_LEN], *filename;
	
	l->fd = -1;
	
	panel = LoadPanel(0, "FXAnalyse.uir", LOGGING);
	if (panel < 0)
		return -1;
	l->panel = panel;
	
	/* configuration file path */
	GetIniFilePath(path); 

	/* load configuration file */ 
	IniText configuration = Ini_New(TRUE);
	Ini_ReadFromFile(configuration, path);

	/* logging file name */
	rv = Ini_GetStringCopy(configuration, "logging", "filename", &filename);
	if (rv > 0)
		l->fd = open(filename, O_CREAT|O_WRONLY|O_APPEND, 00744);
		
	free(filename);
	Ini_Dispose(configuration);
	
	return 0;
}


void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...)
{
	static const char *levels[] = {
		"DEBUG",
		"INFO",
		"WARNING",
		"ERROR",
	};

	char msg[1024];
	int len = 0;
	
	/* timestamp */
	len += sprintf(msg, "%014.3f ", utc);
	time_t now = time(NULL);
	struct tm *t = localtime(&now);
	len += strftime(msg + len, sizeof(msg) - len, "%d-%m-%Y %H:%M:%S ", t);
	
	/* level */
	len += snprintf(msg + len, sizeof(msg) - len, "%s: ", levels[level]);
	
	/* message */
	va_list args;
	va_start(args, frmt);
	len += vsnprintf(msg + len, sizeof(msg) - len, frmt, args);
	va_end(args);
	
	/* newline */
	len = MIN(len, sizeof(msg) - 3);
	msg[len++] = '\r';
	msg[len++] = '\n';
	
	/* string terminator */
	msg[len] = '\0';
	
	/* display message */
	SetCtrlVal(l->panel, LOGGING_LOGGING, msg);
	
	/* write to log file */
	if (l->fd >= 0)
		write(l->fd, msg, len);
}