summaryrefslogtreecommitdiffstats
path: root/color.c
diff options
context:
space:
mode:
authorTavian Barnes <tavianator@tavianator.com>2020-06-02 15:39:19 -0400
committerTavian Barnes <tavianator@tavianator.com>2020-06-02 15:40:30 -0400
commita1c8bda52a9c07d32127762ee0c865660a0268ab (patch)
treec79a8a60573fa712c898ece6021929ab539e625e /color.c
parentc074645660f58faee1f653e4e8bd1ebf832c21f1 (diff)
downloadbfs-a1c8bda52a9c07d32127762ee0c865660a0268ab.tar.xz
diag: Unify debug printing
Diffstat (limited to 'color.c')
-rw-r--r--color.c71
1 files changed, 71 insertions, 0 deletions
diff --git a/color.c b/color.c
index c06840b..cfb3f47 100644
--- a/color.c
+++ b/color.c
@@ -17,6 +17,7 @@
#include "color.h"
#include "bftw.h"
#include "dstring.h"
+#include "expr.h"
#include "fsade.h"
#include "stat.h"
#include "trie.h"
@@ -863,6 +864,65 @@ done:
return ret;
}
+/** Dump a parsed expression tree, for debugging. */
+static int print_expr(CFILE *cfile, const struct expr *expr, bool verbose) {
+ if (fputs("(", cfile->file) == EOF) {
+ return -1;
+ }
+
+ if (expr->lhs || expr->rhs) {
+ if (cfprintf(cfile, "${red}%s${rs}", expr->argv[0]) < 0) {
+ return -1;
+ }
+ } else {
+ if (cfprintf(cfile, "${blu}%s${rs}", expr->argv[0]) < 0) {
+ return -1;
+ }
+ }
+
+ for (size_t i = 1; i < expr->argc; ++i) {
+ if (cfprintf(cfile, " ${bld}%s${rs}", expr->argv[i]) < 0) {
+ return -1;
+ }
+ }
+
+ if (verbose) {
+ double rate = 0.0, time = 0.0;
+ if (expr->evaluations) {
+ rate = 100.0*expr->successes/expr->evaluations;
+ time = (1.0e9*expr->elapsed.tv_sec + expr->elapsed.tv_nsec)/expr->evaluations;
+ }
+ if (cfprintf(cfile, " [${ylw}%zu${rs}/${ylw}%zu${rs}=${ylw}%g%%${rs}; ${ylw}%gns${rs}]",
+ expr->successes, expr->evaluations, rate, time)) {
+ return -1;
+ }
+ }
+
+ if (expr->lhs) {
+ if (fputs(" ", cfile->file) == EOF) {
+ return -1;
+ }
+ if (print_expr(cfile, expr->lhs, verbose) != 0) {
+ return -1;
+ }
+ }
+
+ if (expr->rhs) {
+ if (fputs(" ", cfile->file) == EOF) {
+ return -1;
+ }
+ if (print_expr(cfile, expr->rhs, verbose) != 0) {
+ return -1;
+ }
+ }
+
+ if (fputs(")", cfile->file) == EOF) {
+ return -1;
+ }
+
+ return 0;
+}
+
int cfprintf(CFILE *cfile, const char *format, ...) {
va_list args;
va_start(args, format);
@@ -946,6 +1006,17 @@ int cvfprintf(CFILE *cfile, const char *format, va_list args) {
}
break;
+ case 'e':
+ if (print_expr(cfile, va_arg(args, const struct expr *), false) != 0) {
+ return -1;
+ }
+ break;
+ case 'E':
+ if (print_expr(cfile, va_arg(args, const struct expr *), true) != 0) {
+ return -1;
+ }
+ break;
+
default:
goto invalid;
}