1192661Ssam/*-
2192661Ssam * Copyright (c) 2002-2007 Sam Leffler, Errno Consulting
3192661Ssam * All rights reserved.
4192661Ssam *
5192661Ssam * Redistribution and use in source and binary forms, with or without
6192661Ssam * modification, are permitted provided that the following conditions
7192661Ssam * are met:
8192661Ssam * 1. Redistributions of source code must retain the above copyright
9192661Ssam *    notice, this list of conditions and the following disclaimer,
10192661Ssam *    without modification.
11192661Ssam * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12192661Ssam *    similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any
13192661Ssam *    redistribution must be conditioned upon including a substantially
14192661Ssam *    similar Disclaimer requirement for further binary redistribution.
15192661Ssam *
16192661Ssam * NO WARRANTY
17192661Ssam * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18192661Ssam * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19192661Ssam * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY
20192661Ssam * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21192661Ssam * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY,
22192661Ssam * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23192661Ssam * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24192661Ssam * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25192661Ssam * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26192661Ssam * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
27192661Ssam * THE POSSIBILITY OF SUCH DAMAGES.
28192661Ssam *
29192661Ssam * $FreeBSD$
30192661Ssam */
31192661Ssam
32192661Ssam#ifndef _STATFOO_H_
33192661Ssam#define	_STATFOO_H_
34192661Ssam/*
35192661Ssam * Base class for managing+displaying periodically collected statistics.
36192661Ssam */
37192661Ssam
38192661Ssam/*
39192661Ssam * Statistic definition/description.  The are defined
40192661Ssam * for stats that correspond 1-1 w/ a collected stat
41192661Ssam * and for stats that are calculated indirectly.
42192661Ssam */
43192661Ssamstruct fmt {
44192661Ssam	int	width;			/* printed field width */
45192661Ssam	const char* name;		/* stat field name referenced by user */
46192661Ssam	const char* label;		/* printed header label */
47192661Ssam	const char* desc;		/* verbose description */
48192661Ssam};
49192661Ssam
50192661Ssam#define	STATFOO_DECL_METHODS(_p) \
51192661Ssam	/* set the format of the statistics to display */	\
52192661Ssam	void (*setfmt)(_p, const char *);			\
53192661Ssam	/* collect+store ``current statistics'' */		\
54192661Ssam	void (*collect_cur)(_p);				\
55192661Ssam	/* collect+store ``total statistics'' */		\
56192661Ssam	void (*collect_tot)(_p);				\
57192661Ssam	/* update ``total statistics'' if necessary from current */ \
58192661Ssam	void (*update_tot)(_p);					\
59192661Ssam	/* format a statistic from the current stats */		\
60192661Ssam	int (*get_curstat)(_p, int, char [], size_t);		\
61192661Ssam	/* format a statistic from the total stats */		\
62192661Ssam	int (*get_totstat)(_p, int, char [], size_t);		\
63192661Ssam	/* print field headers terminated by a \n */		\
64192661Ssam	void (*print_header)(_p, FILE *);			\
65192661Ssam	/* print current statistics terminated by a \n */	\
66192661Ssam	void (*print_current)(_p, FILE *);			\
67192661Ssam	/* print total statistics terminated by a \n */		\
68192661Ssam	void (*print_total)(_p, FILE *);			\
69192661Ssam	/* print total statistics in a verbose (1 stat/line) format */ \
70192661Ssam	void (*print_verbose)(_p, FILE *);			\
71192661Ssam	/* print available statistics */			\
72192661Ssam	void (*print_fields)(_p, FILE *)
73192661Ssam
74192661Ssam/*
75192661Ssam * Statistics base class.  This class is not usable; only
76192661Ssam * classes derived from it are useful.
77192661Ssam */
78192661Ssamstruct statfoo {
79192661Ssam	const char *name;		/* statistics name, e.g. wlanstats */
80192661Ssam	const struct fmt *stats;	/* statistics in class */
81192661Ssam	int nstats;			/* number of stats */
82192661Ssam	unsigned char fmts[4096];	/* private: compiled stats to display */
83192661Ssam
84192661Ssam	STATFOO_DECL_METHODS(struct statfoo *);
85192661Ssam};
86192661Ssam
87192661Ssamvoid	statfoo_init(struct statfoo *, const char *name,
88192661Ssam		const struct fmt *stats, int nstats);
89192661Ssam
90192661Ssam#define	STATFOO_DEFINE_BOUNCE(_t) \
91192661Ssamstatic void _t##_setfmt(struct _t *wf, const char *fmt0)	\
92192661Ssam	{ wf->base.setfmt(&wf->base, fmt0); }			\
93192661Ssamstatic void _t##_collect_cur(struct _t *wf)			\
94192661Ssam	{ wf->base.collect_cur(&wf->base); }			\
95192661Ssamstatic void _t##_collect_tot(struct _t *wf)			\
96192661Ssam	{ wf->base.collect_tot(&wf->base); }			\
97192661Ssamstatic void _t##_update_tot(struct _t *wf)			\
98192661Ssam	{ wf->base.update_tot(&wf->base); }			\
99192661Ssamstatic int _t##_get_curstat(struct _t *wf, int s, char b[], size_t bs) \
100192661Ssam	{ return wf->base.get_curstat(&wf->base, s, b, bs); }	\
101192661Ssamstatic int _t##_get_totstat(struct _t *wf, int s, char b[], size_t bs) \
102192661Ssam	{ return wf->base.get_totstat(&wf->base, s, b, bs); }	\
103192661Ssamstatic void _t##_print_header(struct _t *wf, FILE *fd)		\
104192661Ssam	{ wf->base.print_header(&wf->base, fd); }		\
105192661Ssamstatic void _t##_print_current(struct _t *wf, FILE *fd)		\
106192661Ssam	{ wf->base.print_current(&wf->base, fd); }		\
107192661Ssamstatic void _t##_print_total(struct _t *wf, FILE *fd)		\
108192661Ssam	{ wf->base.print_total(&wf->base, fd); }		\
109192661Ssamstatic void _t##_print_verbose(struct _t *wf, FILE *fd)		\
110192661Ssam	{ wf->base.print_verbose(&wf->base, fd); }		\
111192661Ssamstatic void _t##_print_fields(struct _t *wf, FILE *fd)		\
112192661Ssam	{ wf->base.print_fields(&wf->base, fd); }
113192661Ssam
114192661Ssam#define	STATFOO_BOUNCE(_p, _t) do {				\
115192661Ssam	_p->base.setfmt = _t##_setfmt;				\
116192661Ssam	_p->base.collect_cur = _t##_collect_cur;		\
117192661Ssam	_p->base.collect_tot = _t##_collect_tot;		\
118192661Ssam	_p->base.update_tot = _t##_update_tot;			\
119192661Ssam	_p->base.get_curstat = _t##_get_curstat;		\
120192661Ssam	_p->base.get_totstat = _t##_get_totstat;		\
121192661Ssam	_p->base.print_header = _t##_print_header;		\
122192661Ssam	_p->base.print_current = _t##_print_current;		\
123192661Ssam	_p->base.print_total = _t##_print_total;		\
124192661Ssam	_p->base.print_verbose = _t##_print_verbose;		\
125192661Ssam	_p->base.print_fields = _t##_print_fields;		\
126192661Ssam} while (0)
127192661Ssam#endif /* _STATFOO_H_ */
128