From e986713657bc5f9880e91ed49dd9b0d0227f048d Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Mon, 17 Jun 2024 17:39:06 +0200 Subject: Use callback function to log to stderr --- src/cube.c | 22 +++++++++++++++++----- src/cube.h | 4 ++++ src/cube_generic.h | 18 +++++++++--------- src/cube_public.h | 6 ++++++ src/cube_transform_with_switch.h | 6 +++--- src/io_cube.h | 12 ++++++------ src/io_move_trans.h | 2 +- src/solve_generic.h | 26 +++++++++++++------------- src/solve_h48.h | 26 +++++++++++++------------- 9 files changed, 72 insertions(+), 50 deletions(-) (limited to 'src') diff --git a/src/cube.c b/src/cube.c index 97f7844..890e3fb 100644 --- a/src/cube.c +++ b/src/cube.c @@ -1,20 +1,32 @@ #include +#include #include #include +void (*nissy_log)(const char *, va_list); + +void +_log(const char *str, ...) /* TODO: rename */ +{ + va_list args; + + if (nissy_log != NULL) { + va_start(args, str); + nissy_log(str, args); + va_end(args); + } +} + #ifdef DEBUG -#include #define _static #define _static_inline -#define DBG_LOG(...) fprintf(stderr, __VA_ARGS__) -#define DBG_WARN(condition, ...) if (!(condition)) DBG_LOG(__VA_ARGS__); +#define DBG_WARN(condition, ...) if (!(condition)) _log(__VA_ARGS__); #define DBG_ASSERT(condition, retval, ...) \ - if (!(condition)) { DBG_LOG(__VA_ARGS__); return retval; } + if (!(condition)) { _log(__VA_ARGS__); return retval; } #else #define _static static #define _static_inline static inline -#define DBG_LOG(...) #define DBG_WARN(condition, ...) #define DBG_ASSERT(condition, retval, ...) #endif diff --git a/src/cube.h b/src/cube.h index fb96823..4c2c492 100644 --- a/src/cube.h +++ b/src/cube.h @@ -1,3 +1,5 @@ +/* include: inttypes, stdarg, stdbool, string */ + /* All the functions below return 0 in case of success and a positive number in case of error, unless otherwise specified. See (TODO: @@ -81,3 +83,5 @@ int64_t nissy_solve( const void *data, char *solutions ); + +void nissy_setlogger(void (*logger_function)(const char *, va_list)); diff --git a/src/cube_generic.h b/src/cube_generic.h index 7ff9992..d3080f7 100644 --- a/src/cube_generic.h +++ b/src/cube_generic.h @@ -65,16 +65,16 @@ isconsistent(cube_t cube) return true; inconsistent_ep: - DBG_LOG("Inconsistent EP\n"); + _log("Inconsistent EP\n"); return false; inconsistent_cp: - DBG_LOG("Inconsistent CP\n"); + _log("Inconsistent CP\n"); return false; inconsistent_eo: - DBG_LOG("Inconsistent EO\n"); + _log("Inconsistent EO\n"); return false; inconsistent_co: - DBG_LOG("Inconsistent CO\n"); + _log("Inconsistent CO\n"); return false; } @@ -114,13 +114,13 @@ issolvable(cube_t cube) return true; issolvable_parity: - DBG_LOG("EP and CP parities are different\n"); + _log("EP and CP parities are different\n"); return false; issolvable_eo: - DBG_LOG("Odd number of flipped edges\n"); + _log("Odd number of flipped edges\n"); return false; issolvable_co: - DBG_LOG("Sum of corner orientation is not multiple of 3\n"); + _log("Sum of corner orientation is not multiple of 3\n"); return false; } @@ -161,7 +161,7 @@ applymoves_finish: return cube; applymoves_error: - DBG_LOG("applymoves error\n"); + _log("applymoves error\n"); return zero; } @@ -238,7 +238,7 @@ move(cube_t c, uint8_t m) case _move_B3: return _move(B3, c); default: - DBG_LOG("move error, unknown move\n"); + _log("move error, unknown move\n"); return zero; } } diff --git a/src/cube_public.h b/src/cube_public.h index 65d5d54..e2255c5 100644 --- a/src/cube_public.h +++ b/src/cube_public.h @@ -166,3 +166,9 @@ nissy_solve( /* TODO: move solve_generic here? */ return -1; } + +void +nissy_setlogger(void (*log)(const char *, va_list)) +{ + nissy_log = log; +} diff --git a/src/cube_transform_with_switch.h b/src/cube_transform_with_switch.h index de62bf5..bc066fb 100644 --- a/src/cube_transform_with_switch.h +++ b/src/cube_transform_with_switch.h @@ -118,7 +118,7 @@ transform_edges(cube_t c, uint8_t t) case _trans_BLm: return _trans_edges_mirrored(BLm, c); default: - DBG_LOG("transform error, unknown transformation\n"); + _log("transform error, unknown transformation\n"); return zero; } } @@ -224,7 +224,7 @@ transform_corners(cube_t c, uint8_t t) case _trans_BLm: return _trans_corners_mirrored(BLm, c); default: - DBG_LOG("transform error, unknown transformation\n"); + _log("transform error, unknown transformation\n"); return zero; } } @@ -330,7 +330,7 @@ transform(cube_t c, uint8_t t) case _trans_BLm: return _trans_mirrored(BLm, c); default: - DBG_LOG("transform error, unknown transformation\n"); + _log("transform error, unknown transformation\n"); return zero; } } diff --git a/src/io_cube.h b/src/io_cube.h index 3e7880e..72113ff 100644 --- a/src/io_cube.h +++ b/src/io_cube.h @@ -49,7 +49,7 @@ readcube(const char *format, const char *buf) if (!strcmp(format, ioformat[i].name)) return ioformat[i].read(buf); - DBG_LOG("Cannot read cube in the given format\n"); + _log("Cannot read cube in the given format\n"); return zero; } @@ -76,7 +76,7 @@ writecube(const char *format, cube_t cube, char *buf) errormsg = "ERROR: format"; writecube_error: - DBG_LOG("writecube error, see stdout for details\n"); + _log("writecube error, see stdout for details\n"); len = strlen(errormsg); memcpy(buf, errormsg, len); buf[len] = '\n'; @@ -93,7 +93,7 @@ readco(const char *str) if (*str == '2') return _ctwist_ccw; - DBG_LOG("Error reading CO\n"); + _log("Error reading CO\n"); return _error; } @@ -107,7 +107,7 @@ readcp(const char *str) !strncmp(str, cornerstralt[c], 3)) return c; - DBG_LOG("Error reading CP\n"); + _log("Error reading CP\n"); return _error; } @@ -119,7 +119,7 @@ readeo(const char *str) if (*str == '1') return _eflip; - DBG_LOG("Error reading EO\n"); + _log("Error reading EO\n"); return _error; } @@ -132,7 +132,7 @@ readep(const char *str) if (!strncmp(str, edgestr[e], 2)) return e; - DBG_LOG("Error reading EP\n"); + _log("Error reading EP\n"); return _error; } diff --git a/src/io_move_trans.h b/src/io_move_trans.h index e36b8e5..14130c7 100644 --- a/src/io_move_trans.h +++ b/src/io_move_trans.h @@ -49,7 +49,7 @@ readtrans(const char *buf) if (!strncmp(buf, transstr[t], 11)) return t; - DBG_LOG("readtrans error\n"); + _log("readtrans error\n"); return _error; } diff --git a/src/solve_generic.h b/src/solve_generic.h index 4e7833f..a8d6b39 100644 --- a/src/solve_generic.h +++ b/src/solve_generic.h @@ -49,11 +49,11 @@ solve( solutions ); } else { - DBG_LOG("solve: unknown solver '%s'\n", solver); + _log("solve: unknown solver '%s'\n", solver); return -1; } - DBG_LOG("solve: error\n"); + _log("solve: error\n"); return -1; } @@ -63,7 +63,7 @@ solve_generic_appendsolution(dfsarg_generic_t *arg) int strl; strl = writemoves(arg->moves, arg->depth, *arg->nextsol); - DBG_LOG("Solution found: %s\n", *arg->nextsol); + _log("Solution found: %s\n", *arg->nextsol); *arg->nextsol += strl; **arg->nextsol = '\n'; (*arg->nextsol)++; @@ -126,12 +126,12 @@ solve_generic( int64_t ret, tmp, first; if (!issolvable(cube)) { - DBG_LOG("solve: cube is not solvable\n"); + _log("solve: cube is not solvable\n"); return -1; } if (issolved(cube)) { - DBG_LOG("solve: cube is already solved\n"); + _log("solve: cube is already solved\n"); sols[0] = '\n'; sols[1] = 0; return 1; @@ -141,32 +141,32 @@ solve_generic( "solve: NISS not implemented yet, 'nisstype' ignored\n"); if (minmoves < 0) { - DBG_LOG("solve: 'minmoves' is negative, setting to 0\n"); + _log("solve: 'minmoves' is negative, setting to 0\n"); minmoves = 0; } if (maxmoves < 0) { - DBG_LOG("solve: invalid 'maxmoves', setting to 20\n"); + _log("solve: invalid 'maxmoves', setting to 20\n"); maxmoves = 20; } if (maxsols < 0) { - DBG_LOG("solve: 'maxsols' is negative\n"); + _log("solve: 'maxsols' is negative\n"); return -1; } if (maxsols == 0) { - DBG_LOG("solve: 'maxsols' is 0\n"); + _log("solve: 'maxsols' is 0\n"); return 0; } if (sols == NULL) { - DBG_LOG("solve: return parameter 'sols' is NULL\n"); + _log("solve: return parameter 'sols' is NULL\n"); return -1; } if (estimate == NULL) { - DBG_LOG("solve: 'estimate' is NULL\n"); + _log("solve: 'estimate' is NULL\n"); return -1; } @@ -187,7 +187,7 @@ solve_generic( if (tmp != 0) first = arg.depth; - DBG_LOG("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", + _log("Found %" PRId64 " solution%s at depth %" PRIu8 "\n", tmp, tmp == 1 ? "" : "s", arg.depth); if (ret >= maxsols) @@ -248,7 +248,7 @@ gendata(const char *solver, const char *options, void *data) maxdepth = atoi(&options[i+1]); ret = gendata_h48(data, h, maxdepth); } else { - DBG_LOG("gendata: implemented only for H48 solver\n"); + _log("gendata: implemented only for H48 solver\n"); ret = -1; } diff --git a/src/solve_h48.h b/src/solve_h48.h index 8be67b6..fc3b9c9 100644 --- a/src/solve_h48.h +++ b/src/solve_h48.h @@ -146,13 +146,13 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep) .rep = rep }; for (i = 0, n = 0, cc = 0; i < 10; i++) { - DBG_LOG("cocsep: generating depth %" PRIu8 "\n", i); + _log("cocsep: generating depth %" PRIu8 "\n", i); memset(visited, 0, COCSEP_VISITEDSIZE); arg.depth = 0; arg.maxdepth = i; cc = gendata_cocsep_dfs(&arg); info[i+2] = cc; - DBG_LOG("found %" PRIu32 "\n", cc); + _log("found %" PRIu32 "\n", cc); } info[0] = (uint32_t)n; @@ -161,12 +161,12 @@ gendata_cocsep(void *buf, uint64_t *selfsim, cube_t *rep) "cocsep: computed %" PRIu16 " symmetry classes, " "expected %zu\n", n, COCSEP_CLASSES); - DBG_LOG("cocsep data computed\n"); - DBG_LOG("Symmetry classes: %" PRIu32 "\n", info[0]); - DBG_LOG("Maximum pruning value: %" PRIu32 "\n", info[1]); - DBG_LOG("Pruning value distribution:\n"); + _log("cocsep data computed\n"); + _log("Symmetry classes: %" PRIu32 "\n", info[0]); + _log("Maximum pruning value: %" PRIu32 "\n", info[1]); + _log("Pruning value distribution:\n"); for (j = 0; j < 10; j++) - DBG_LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); + _log("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+2]); gendata_cocsep_return_size: return COCSEP_FULLSIZE; @@ -264,21 +264,21 @@ gendata_h48(void *buf, uint8_t h, uint8_t maxdepth) tot < esep_max && arg.depth <= maxdepth; arg.depth++ ) { - DBG_LOG("esep: generating depth %" PRIu8 "\n", arg.depth); + _log("esep: generating depth %" PRIu8 "\n", arg.depth); cc = gendata_esep_bfs(&arg); tot += cc; info[arg.depth+1] = cc; - DBG_LOG("found %" PRIu64 "\n", cc); + _log("found %" PRIu64 "\n", cc); } info[0] = arg.depth-1; infosize = 4 * (size_t)(info[0] + 2); - DBG_LOG("h48 pruning table computed\n"); - DBG_LOG("Maximum pruning value: %" PRIu32 "\n", info[0]); - DBG_LOG("Pruning value distribution:\n"); + _log("h48 pruning table computed\n"); + _log("Maximum pruning value: %" PRIu32 "\n", info[0]); + _log("Pruning value distribution:\n"); for (j = 0; j <= info[0]; j++) - DBG_LOG("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); + _log("%" PRIu8 ":\t%" PRIu32 "\n", j, info[j+1]); gendata_h48_return_size: return cocsepsize + ESEP_TABLESIZE(h, k) + infosize; -- cgit v1.3