1 /****************************************************************************
2  * bfs                                                                      *
3  * Copyright (C) 2019 Tavian Barnes <tavianator@tavianator.com>             *
4  *                                                                          *
5  * Permission to use, copy, modify, and/or distribute this software for any *
6  * purpose with or without fee is hereby granted.                           *
7  *                                                                          *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES *
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF         *
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR  *
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES   *
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN    *
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF  *
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.           *
15  ****************************************************************************/
16 
17 #include "diag.h"
18 #include "ctx.h"
19 #include "color.h"
20 #include "util.h"
21 #include <errno.h>
22 #include <stdarg.h>
23 
bfs_perror(const struct bfs_ctx * ctx,const char * str)24 void bfs_perror(const struct bfs_ctx *ctx, const char *str) {
25 	bfs_error(ctx, "%s: %m.\n", str);
26 }
27 
bfs_error(const struct bfs_ctx * ctx,const char * format,...)28 void bfs_error(const struct bfs_ctx *ctx, const char *format, ...)  {
29 	va_list args;
30 	va_start(args, format);
31 	bfs_verror(ctx, format, args);
32 	va_end(args);
33 }
34 
bfs_warning(const struct bfs_ctx * ctx,const char * format,...)35 bool bfs_warning(const struct bfs_ctx *ctx, const char *format, ...)  {
36 	va_list args;
37 	va_start(args, format);
38 	bool ret = bfs_vwarning(ctx, format, args);
39 	va_end(args);
40 	return ret;
41 }
42 
bfs_debug(const struct bfs_ctx * ctx,enum debug_flags flag,const char * format,...)43 bool bfs_debug(const struct bfs_ctx *ctx, enum debug_flags flag, const char *format, ...)  {
44 	va_list args;
45 	va_start(args, format);
46 	bool ret = bfs_vdebug(ctx, flag, format, args);
47 	va_end(args);
48 	return ret;
49 }
50 
bfs_verror(const struct bfs_ctx * ctx,const char * format,va_list args)51 void bfs_verror(const struct bfs_ctx *ctx, const char *format, va_list args) {
52 	int error = errno;
53 
54 	bfs_error_prefix(ctx);
55 
56 	errno = error;
57 	cvfprintf(ctx->cerr, format, args);
58 }
59 
bfs_vwarning(const struct bfs_ctx * ctx,const char * format,va_list args)60 bool bfs_vwarning(const struct bfs_ctx *ctx, const char *format, va_list args) {
61 	int error = errno;
62 
63 	if (bfs_warning_prefix(ctx)) {
64 		errno = error;
65 		cvfprintf(ctx->cerr, format, args);
66 		return true;
67 	} else {
68 		return false;
69 	}
70 }
71 
bfs_vdebug(const struct bfs_ctx * ctx,enum debug_flags flag,const char * format,va_list args)72 bool bfs_vdebug(const struct bfs_ctx *ctx, enum debug_flags flag, const char *format, va_list args) {
73 	int error = errno;
74 
75 	if (bfs_debug_prefix(ctx, flag)) {
76 		errno = error;
77 		cvfprintf(ctx->cerr, format, args);
78 		return true;
79 	} else {
80 		return false;
81 	}
82 }
83 
bfs_error_prefix(const struct bfs_ctx * ctx)84 void bfs_error_prefix(const struct bfs_ctx *ctx) {
85 	cfprintf(ctx->cerr, "${bld}%s:${rs} ${er}error:${rs} ", xbasename(ctx->argv[0]));
86 }
87 
bfs_warning_prefix(const struct bfs_ctx * ctx)88 bool bfs_warning_prefix(const struct bfs_ctx *ctx) {
89 	if (ctx->warn) {
90 		cfprintf(ctx->cerr, "${bld}%s:${rs} ${wr}warning:${rs} ", xbasename(ctx->argv[0]));
91 		return true;
92 	} else {
93 		return false;
94 	}
95 }
96 
bfs_debug_prefix(const struct bfs_ctx * ctx,enum debug_flags flag)97 bool bfs_debug_prefix(const struct bfs_ctx *ctx, enum debug_flags flag) {
98 	if (ctx->debug & flag) {
99 		cfprintf(ctx->cerr, "${bld}%s:${rs} ${cyn}-D %s${rs}: ", xbasename(ctx->argv[0]), debug_flag_name(flag));
100 		return true;
101 	} else {
102 		return false;
103 	}
104 }
105