xref: /openbmc/linux/tools/perf/util/usage.c (revision 2690c730)
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"
100d37aa34SArnaldo Carvalho de Melo #include "debug.h"
1186470930SIngo Molnar 
1286470930SIngo Molnar static void report(const char *prefix, const char *err, va_list params)
1386470930SIngo Molnar {
1486470930SIngo Molnar 	char msg[1024];
1586470930SIngo Molnar 	vsnprintf(msg, sizeof(msg), err, params);
1686470930SIngo Molnar 	fprintf(stderr, " %s%s\n", prefix, msg);
1786470930SIngo Molnar }
1886470930SIngo Molnar 
1986470930SIngo Molnar static NORETURN void usage_builtin(const char *err)
2086470930SIngo Molnar {
21716c69feSIngo Molnar 	fprintf(stderr, "\n Usage: %s\n", err);
2286470930SIngo Molnar 	exit(129);
2386470930SIngo Molnar }
2486470930SIngo Molnar 
2586470930SIngo Molnar static NORETURN void die_builtin(const char *err, va_list params)
2686470930SIngo Molnar {
27716c69feSIngo Molnar 	report(" Fatal: ", err, params);
2886470930SIngo Molnar 	exit(128);
2986470930SIngo Molnar }
3086470930SIngo Molnar 
3186470930SIngo Molnar static void error_builtin(const char *err, va_list params)
3286470930SIngo Molnar {
33716c69feSIngo Molnar 	report(" Error: ", err, params);
3486470930SIngo Molnar }
3586470930SIngo Molnar 
3686470930SIngo Molnar static void warn_builtin(const char *warn, va_list params)
3786470930SIngo Molnar {
38716c69feSIngo Molnar 	report(" Warning: ", warn, params);
3986470930SIngo Molnar }
4086470930SIngo Molnar 
4186470930SIngo Molnar /* If we are in a dlopen()ed .so write to a global variable would segfault
4286470930SIngo Molnar  * (ugh), so keep things static. */
4386470930SIngo Molnar static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
4486470930SIngo Molnar static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
4586470930SIngo Molnar static void (*error_routine)(const char *err, va_list params) = error_builtin;
4686470930SIngo Molnar static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
4786470930SIngo Molnar 
4886470930SIngo Molnar void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
4986470930SIngo Molnar {
5086470930SIngo Molnar 	die_routine = routine;
5186470930SIngo Molnar }
5286470930SIngo Molnar 
532690c730SNamhyung Kim void set_warning_routine(void (*routine)(const char *err, va_list params))
542690c730SNamhyung Kim {
552690c730SNamhyung Kim 	warn_routine = routine;
562690c730SNamhyung Kim }
572690c730SNamhyung Kim 
5886470930SIngo Molnar void usage(const char *err)
5986470930SIngo Molnar {
6086470930SIngo Molnar 	usage_routine(err);
6186470930SIngo Molnar }
6286470930SIngo Molnar 
6386470930SIngo Molnar void die(const char *err, ...)
6486470930SIngo Molnar {
6586470930SIngo Molnar 	va_list params;
6686470930SIngo Molnar 
6786470930SIngo Molnar 	va_start(params, err);
6886470930SIngo Molnar 	die_routine(err, params);
6986470930SIngo Molnar 	va_end(params);
7086470930SIngo Molnar }
7186470930SIngo Molnar 
7286470930SIngo Molnar int error(const char *err, ...)
7386470930SIngo Molnar {
7486470930SIngo Molnar 	va_list params;
7586470930SIngo Molnar 
7686470930SIngo Molnar 	va_start(params, err);
7786470930SIngo Molnar 	error_routine(err, params);
7886470930SIngo Molnar 	va_end(params);
7986470930SIngo Molnar 	return -1;
8086470930SIngo Molnar }
8186470930SIngo Molnar 
8286470930SIngo Molnar void warning(const char *warn, ...)
8386470930SIngo Molnar {
8486470930SIngo Molnar 	va_list params;
8586470930SIngo Molnar 
8686470930SIngo Molnar 	va_start(params, warn);
8786470930SIngo Molnar 	warn_routine(warn, params);
8886470930SIngo Molnar 	va_end(params);
8986470930SIngo Molnar }
90