xref: /openbmc/linux/tools/perf/util/usage.c (revision 5f9273d6)
186470930SIngo Molnar /*
25f9273d6SNamhyung Kim  * usage.c
35f9273d6SNamhyung Kim  *
45f9273d6SNamhyung Kim  * Various reporting routines.
55f9273d6SNamhyung Kim  * Originally copied from GIT source.
686470930SIngo Molnar  *
786470930SIngo Molnar  * Copyright (C) Linus Torvalds, 2005
886470930SIngo Molnar  */
986470930SIngo Molnar #include "util.h"
1086470930SIngo Molnar 
1186470930SIngo Molnar static void report(const char *prefix, const char *err, va_list params)
1286470930SIngo Molnar {
1386470930SIngo Molnar 	char msg[1024];
1486470930SIngo Molnar 	vsnprintf(msg, sizeof(msg), err, params);
1586470930SIngo Molnar 	fprintf(stderr, " %s%s\n", prefix, msg);
1686470930SIngo Molnar }
1786470930SIngo Molnar 
1886470930SIngo Molnar static NORETURN void usage_builtin(const char *err)
1986470930SIngo Molnar {
20716c69feSIngo Molnar 	fprintf(stderr, "\n Usage: %s\n", err);
2186470930SIngo Molnar 	exit(129);
2286470930SIngo Molnar }
2386470930SIngo Molnar 
2486470930SIngo Molnar static NORETURN void die_builtin(const char *err, va_list params)
2586470930SIngo Molnar {
26716c69feSIngo Molnar 	report(" Fatal: ", err, params);
2786470930SIngo Molnar 	exit(128);
2886470930SIngo Molnar }
2986470930SIngo Molnar 
3086470930SIngo Molnar static void error_builtin(const char *err, va_list params)
3186470930SIngo Molnar {
32716c69feSIngo Molnar 	report(" Error: ", err, params);
3386470930SIngo Molnar }
3486470930SIngo Molnar 
3586470930SIngo Molnar static void warn_builtin(const char *warn, va_list params)
3686470930SIngo Molnar {
37716c69feSIngo Molnar 	report(" Warning: ", warn, params);
3886470930SIngo Molnar }
3986470930SIngo Molnar 
4086470930SIngo Molnar /* If we are in a dlopen()ed .so write to a global variable would segfault
4186470930SIngo Molnar  * (ugh), so keep things static. */
4286470930SIngo Molnar static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
4386470930SIngo Molnar static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
4486470930SIngo Molnar static void (*error_routine)(const char *err, va_list params) = error_builtin;
4586470930SIngo Molnar static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
4686470930SIngo Molnar 
4786470930SIngo Molnar void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
4886470930SIngo Molnar {
4986470930SIngo Molnar 	die_routine = routine;
5086470930SIngo Molnar }
5186470930SIngo Molnar 
5286470930SIngo Molnar void usage(const char *err)
5386470930SIngo Molnar {
5486470930SIngo Molnar 	usage_routine(err);
5586470930SIngo Molnar }
5686470930SIngo Molnar 
5786470930SIngo Molnar void die(const char *err, ...)
5886470930SIngo Molnar {
5986470930SIngo Molnar 	va_list params;
6086470930SIngo Molnar 
6186470930SIngo Molnar 	va_start(params, err);
6286470930SIngo Molnar 	die_routine(err, params);
6386470930SIngo Molnar 	va_end(params);
6486470930SIngo Molnar }
6586470930SIngo Molnar 
6686470930SIngo Molnar int error(const char *err, ...)
6786470930SIngo Molnar {
6886470930SIngo Molnar 	va_list params;
6986470930SIngo Molnar 
7086470930SIngo Molnar 	va_start(params, err);
7186470930SIngo Molnar 	error_routine(err, params);
7286470930SIngo Molnar 	va_end(params);
7386470930SIngo Molnar 	return -1;
7486470930SIngo Molnar }
7586470930SIngo Molnar 
7686470930SIngo Molnar void warning(const char *warn, ...)
7786470930SIngo Molnar {
7886470930SIngo Molnar 	va_list params;
7986470930SIngo Molnar 
8086470930SIngo Molnar 	va_start(params, warn);
8186470930SIngo Molnar 	warn_routine(warn, params);
8286470930SIngo Molnar 	va_end(params);
8386470930SIngo Molnar }
84