view logging.c @ 183:791ca26fee6a

Rewrite data writing to file Fmt() uses by default truncation to conver double arguments to their text representation. Rounding must be used. Rewrite using standard C functions to get rid of this problem (and probably make it more efficient). Extend to handle arbitrary number of channels and to report errors on opening the data files.
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Fri, 21 Feb 2014 18:42:30 +0100
parents 97112b45b838
children 0fed60877099
line wrap: on
line source

#include <ansi_c.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 panel = LoadPanel(0, "FXAnalyse.uir", LOGGING);
	if (panel < 0)
		return -1;
	l->panel = panel;
	return 0;
}


void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...)
{
	static const char levels[][16] = {
		"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);
	
	// add newline
	len = MIN(len, sizeof(msg) - 2);
	msg[len] = '\n';
	msg[len + 1] = '\0';
	
	// display message
	SetCtrlVal(l->panel, LOGGING_LOGGING, msg);
}