xref: /openbmc/linux/tools/perf/util/usage.c (revision 86470930)
186470930SIngo Molnar /*
286470930SIngo Molnar  * GIT - The information manager from hell
386470930SIngo Molnar  *
486470930SIngo Molnar  * Copyright (C) Linus Torvalds, 2005
586470930SIngo Molnar  */
686470930SIngo Molnar #include "util.h"
786470930SIngo Molnar 
886470930SIngo Molnar static void report(const char *prefix, const char *err, va_list params)
986470930SIngo Molnar {
1086470930SIngo Molnar 	char msg[1024];
1186470930SIngo Molnar 	vsnprintf(msg, sizeof(msg), err, params);
1286470930SIngo Molnar 	fprintf(stderr, "%s%s\n", prefix, msg);
1386470930SIngo Molnar }
1486470930SIngo Molnar 
1586470930SIngo Molnar static NORETURN void usage_builtin(const char *err)
1686470930SIngo Molnar {
1786470930SIngo Molnar 	fprintf(stderr, "\n usage: %s\n", err);
1886470930SIngo Molnar 	exit(129);
1986470930SIngo Molnar }
2086470930SIngo Molnar 
2186470930SIngo Molnar static NORETURN void die_builtin(const char *err, va_list params)
2286470930SIngo Molnar {
2386470930SIngo Molnar 	report("fatal: ", err, params);
2486470930SIngo Molnar 	exit(128);
2586470930SIngo Molnar }
2686470930SIngo Molnar 
2786470930SIngo Molnar static void error_builtin(const char *err, va_list params)
2886470930SIngo Molnar {
2986470930SIngo Molnar 	report("error: ", err, params);
3086470930SIngo Molnar }
3186470930SIngo Molnar 
3286470930SIngo Molnar static void warn_builtin(const char *warn, va_list params)
3386470930SIngo Molnar {
3486470930SIngo Molnar 	report("warning: ", warn, params);
3586470930SIngo Molnar }
3686470930SIngo Molnar 
3786470930SIngo Molnar /* If we are in a dlopen()ed .so write to a global variable would segfault
3886470930SIngo Molnar  * (ugh), so keep things static. */
3986470930SIngo Molnar static void (*usage_routine)(const char *err) NORETURN = usage_builtin;
4086470930SIngo Molnar static void (*die_routine)(const char *err, va_list params) NORETURN = die_builtin;
4186470930SIngo Molnar static void (*error_routine)(const char *err, va_list params) = error_builtin;
4286470930SIngo Molnar static void (*warn_routine)(const char *err, va_list params) = warn_builtin;
4386470930SIngo Molnar 
4486470930SIngo Molnar void set_die_routine(void (*routine)(const char *err, va_list params) NORETURN)
4586470930SIngo Molnar {
4686470930SIngo Molnar 	die_routine = routine;
4786470930SIngo Molnar }
4886470930SIngo Molnar 
4986470930SIngo Molnar void usage(const char *err)
5086470930SIngo Molnar {
5186470930SIngo Molnar 	usage_routine(err);
5286470930SIngo Molnar }
5386470930SIngo Molnar 
5486470930SIngo Molnar void die(const char *err, ...)
5586470930SIngo Molnar {
5686470930SIngo Molnar 	va_list params;
5786470930SIngo Molnar 
5886470930SIngo Molnar 	va_start(params, err);
5986470930SIngo Molnar 	die_routine(err, params);
6086470930SIngo Molnar 	va_end(params);
6186470930SIngo Molnar }
6286470930SIngo Molnar 
6386470930SIngo Molnar int error(const char *err, ...)
6486470930SIngo Molnar {
6586470930SIngo Molnar 	va_list params;
6686470930SIngo Molnar 
6786470930SIngo Molnar 	va_start(params, err);
6886470930SIngo Molnar 	error_routine(err, params);
6986470930SIngo Molnar 	va_end(params);
7086470930SIngo Molnar 	return -1;
7186470930SIngo Molnar }
7286470930SIngo Molnar 
7386470930SIngo Molnar void warning(const char *warn, ...)
7486470930SIngo Molnar {
7586470930SIngo Molnar 	va_list params;
7686470930SIngo Molnar 
7786470930SIngo Molnar 	va_start(params, warn);
7886470930SIngo Molnar 	warn_routine(warn, params);
7986470930SIngo Molnar 	va_end(params);
8086470930SIngo Molnar }
81