diag.c 1.64 KB
Newer Older
1
/*
2 3 4 5
 * Copyright (C) 2013 CERN (www.cern.ch)
 * Author: Alessandro Rubini
 *
 * Released according to the GNU LGPL, version 2.1 or any later version.
6 7 8 9
 */
#include <stdarg.h>
#include <ppsi/ppsi.h>

10 11 12 13
#ifndef ARRAY_SIZE
#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
#endif

14 15 16 17 18 19 20
static char *thing_name[] = {
	[pp_dt_fsm]	= "diag-fsm",
	[pp_dt_time]	= "diag-time",
	[pp_dt_frames]	= "diag-frames",
	[pp_dt_servo]	= "diag-servo",
	[pp_dt_bmc]	= "diag-bmc",
	[pp_dt_ext]	= "diag-extension",
21
	[pp_dt_config]	= "diag-config",
22 23 24 25 26 27 28
};


void __pp_diag(struct pp_instance *ppi, enum pp_diag_things th,
		      int level, char *fmt, ...)
{
	va_list args;
29
	char *name;
30 31 32 33

	if (!__PP_DIAG_ALLOW(ppi, th, level))
		return;

34 35
	name = ppi ? ppi->port_name : "ppsi";

36
	/* Use the normal output channel for diagnostics */
37 38 39
	if (PP_DIAG_EXTRA_PRINT_TIME) {
		int hours, minutes, seconds;

40 41
		if (ppi && TOPS(ppi) && TOPS(ppi)->get_utc_time )
			TOPS(ppi)->get_utc_time(ppi, &hours, &minutes, &seconds);
42 43 44 45 46 47
		else
			hours=minutes=seconds=0;
		pp_printf("%02d:%02d:%02d %s-%i-%s: ", hours, minutes,seconds,thing_name[th], level, name);
	} else {
		pp_printf("%s-%i-%s: ", thing_name[th], level, name);
	}
48 49 50 51 52 53 54 55
	va_start(args, fmt);
	pp_vprintf(fmt, args);
	va_end(args);
}

unsigned long pp_diag_parse(char *diaglevel)
{
	unsigned long res = 0;
56
	int i = 28; /* number of bits to shift the nibble: 28..31 is first */
57
	int nthings = ARRAY_SIZE(thing_name);
58

59
	while (*diaglevel && i >= (32 - 4 * nthings)) {
60 61 62 63 64 65 66 67 68 69 70
		if (*diaglevel < '0' || *diaglevel > '3')
			break;
		res |= ((*diaglevel - '0') << i);
		i -= 4;
		diaglevel++;
	}
	if (*diaglevel)
		pp_printf("%s: error parsing \"%s\"\n", __func__, diaglevel);

	return res;
}