h48

A prototype for an optimal Rubik's cube solver, work in progress.
git clone https://git.tronto.net/h48
Download | Log | Files | Refs | README | LICENSE

commit b25a939e2d8b045c083caf3264c4e5aac1cf88fb
parent b02c083a3545b6e6b36ab62680128edec619a804
Author: Sebastiano Tronto <sebastiano@tronto.net>
Date:   Tue, 15 Apr 2025 15:51:40 +0200

Better logging function

Now it is possible to provide some data together with the logging function.
This is useful for example in C++, where I can now provide an arbitrary
callable object as data, and a simple wrapper function that call the
callable object as logging function.

Diffstat:
Mcpp/examples/solve_h48h3k2.cpp | 2+-
Mcpp/nissy.cpp | 7+++++--
Mcpp/nissy.h | 2+-
Mpython/nissy_module.c | 4++--
Mshell/shell.c | 4++--
Msrc/nissy.c | 21++++++++++-----------
Msrc/nissy.h | 9+++++++--
Msrc/utils/dbg_log.h | 9+++++----
Mtest/test.h | 4++--
Mtools/000_gendata/gendata.c | 4++--
Mtools/001_derive_h48/derive_h48.c | 2+-
Mtools/100_checkdata/checkdata.c | 2+-
Mtools/300_solve_small/solve_small.c | 2+-
Mtools/301_solve_file/solve_file.c | 2+-
Mtools/302_solve_multisol/solve_multisol.c | 2+-
Mtools/400_solvetest/solve_test.c | 2+-
Mtools/nissy_extra.h | 2+-
Mtools/tool.h | 15++++-----------
18 files changed, 48 insertions(+), 47 deletions(-)

diff --git a/cpp/examples/solve_h48h3k2.cpp b/cpp/examples/solve_h48h3k2.cpp @@ -8,7 +8,7 @@ int main() { // Get verbose output - nissy::set_logger([](const char* s) { std::cout << s; }); + nissy::set_logger([](const char* s, void *u) { std::cout << s; }, NULL); // Get the scramble from the user std::cout << "Enter scramble: "; diff --git a/cpp/nissy.cpp b/cpp/nissy.cpp @@ -24,7 +24,7 @@ extern "C" { unsigned, unsigned, int, int, unsigned long long, const char *, unsigned, char *, long long *); long long nissy_countmoves(const char *); - long long nissy_setlogger(void (*)(const char *)); + long long nissy_setlogger(void (*)(const char *, void *), void *); } namespace nissy { @@ -227,5 +227,8 @@ namespace nissy { return error{err}; } - void set_logger(void (*log)(const char *)) { nissy_setlogger(log); } + void set_logger(void (*log)(const char *, void *), void *data) + { + nissy_setlogger(log, data); + } } diff --git a/cpp/nissy.h b/cpp/nissy.h @@ -100,7 +100,7 @@ namespace nissy { }; error count_moves(const std::string&); - void set_logger(void (*)(const char*)); + void set_logger(void (*)(const char *, void *), void *); } #endif diff --git a/python/nissy_module.c b/python/nissy_module.c @@ -421,7 +421,7 @@ static struct PyModuleDef nissy_python_module = { }; static void -log_stdout(const char *str) +log_stdout(const char *str, void *unused) { fprintf(stderr, "%s", str); } @@ -429,7 +429,7 @@ log_stdout(const char *str) PyMODINIT_FUNC PyInit_nissy_python_module(void) { PyObject *module; - nissy_setlogger(log_stdout); + nissy_setlogger(log_stdout, NULL); module = PyModule_Create(&nissy_python_module); PyModule_AddStringConstant(module, "solved_cube", NISSY_SOLVED_CUBE); diff --git a/shell/shell.c b/shell/shell.c @@ -780,7 +780,7 @@ set_threads(int argc, char **argv, args_t *args) } void -log_stderr(const char *str) +log_stderr(const char *str, void *unused) { fprintf(stderr, "%s", str); } @@ -792,7 +792,7 @@ main(int argc, char **argv) args_t args; srand(time(NULL)); - nissy_setlogger(log_stderr); + nissy_setlogger(log_stderr, NULL); parse_error = parse_args(argc-1, argv+1, &args); if (parse_error) diff --git a/src/nissy.c b/src/nissy.c @@ -366,8 +366,7 @@ nissy_getcube( long long nissy_datainfo( uint64_t data_size, - const char data[data_size], - void (*write)(const char *) + const char data[data_size] ) { uint8_t i; @@ -383,8 +382,7 @@ nissy_datainfo( if (ret != 0) return ret; - write_wrapper(write, - "\n---------\n\n" + LOG("\n---------\n\n" "Table information for '%s'\n\n" "Size: %" PRIu64 " bytes\n" "Entries: %" PRIu64 " (%" PRIu8 " bits per entry)\n", @@ -392,15 +390,14 @@ nissy_datainfo( switch (info.type) { case TABLETYPE_PRUNING: - write_wrapper(write, "\nTable distribution:\n" - "Value\tPositions\n"); + LOG("\nTable distribution:\nValue\tPositions\n"); for (i = 0; i <= info.maxvalue; i++) { - write_wrapper(write, "%" PRIu8 "\t%" PRIu64 "\n", + LOG("%" PRIu8 "\t%" PRIu64 "\n", i + info.base, info.distribution[i]); } break; case TABLETYPE_SPECIAL: - write_wrapper(write, "This is an ad-hoc table\n"); + LOG("This is an ad-hoc table\n"); break; default: LOG("datainfo: unknown table type\n"); @@ -409,9 +406,9 @@ nissy_datainfo( if (info.next != 0) return nissy_datainfo( - data_size - info.next, (char *)data + info.next, write); + data_size - info.next, (char *)data + info.next); - write_wrapper(write, "\n---------\n"); + LOG("\n---------\n"); return NISSY_OK; } @@ -624,9 +621,11 @@ nissy_countmoves( long long nissy_setlogger( - void (*log)(const char *) + void (*log)(const char *, void *), + void *user_data ) { nissy_log = log; + nissy_log_data = user_data; return NISSY_OK; } diff --git a/src/nissy.h b/src/nissy.h @@ -421,7 +421,11 @@ Set a global logger function used by this library. Setting the logger to NULL disables logging. Parameters: - write - A callback writer with the same signature as printf(3). + logger_function - A pointer to a function that takes two parameters: + * A C string, the string to be printed. + * Any other data via a void pointer. + user_data - Any data that will be provided by the logger when + calling logger_function. Return values: NISSY_OK - Logger set succesfully. No warning or error is going to be given @@ -429,5 +433,6 @@ Return values: */ long long nissy_setlogger( - void (*logger_function)(const char *) + void (*logger_function)(const char *, void *), + void *user_data ); diff --git a/src/utils/dbg_log.h b/src/utils/dbg_log.h @@ -1,10 +1,11 @@ #include <stdio.h> -void (*nissy_log)(const char *); -void write_wrapper(void (*)(const char *), const char *, ...); +void (*nissy_log)(const char *, void *); +void *nissy_log_data; +void write_wrapper(void (*)(const char *, void *), const char *, ...); void -write_wrapper(void (*write)(const char *), const char *str, ...) +write_wrapper(void (*write)(const char *, void *), const char *str, ...) { static const size_t len = 1000; char message[len]; @@ -14,7 +15,7 @@ write_wrapper(void (*write)(const char *), const char *str, ...) vsprintf(message, str, args); va_end(args); - write(message); + write(message, nissy_log_data); } #define LOG(...) if (nissy_log != NULL) write_wrapper(nissy_log, __VA_ARGS__); diff --git a/test/test.h b/test/test.h @@ -31,13 +31,13 @@ int64_t writecube(const char *, cube_t, size_t n, char [n]); /* Test function to be implemented by all tests */ void run(void); -void log_stderr(const char *str) +void log_stderr(const char *str, void *unused) { fprintf(stderr, "%s", str); } int main(void) { - nissy_setlogger(log_stderr); + nissy_setlogger(log_stderr, NULL); run(); return 0; } diff --git a/tools/000_gendata/gendata.c b/tools/000_gendata/gendata.c @@ -17,7 +17,7 @@ run(void) { case -2: goto gendata_run_finish; default: - nissy_datainfo(size, buf, write_stdout); + nissy_datainfo(size, buf); consistent = nissy_checkdata(size, buf) == 0; expected = check_distribution(solver, size, buf); if (consistent && expected) { @@ -51,7 +51,7 @@ int main(int argc, char **argv) { parse_h48_solver(solver, &h, &k); expected = expected_h48[h][k]; - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); timerun(run); diff --git a/tools/001_derive_h48/derive_h48.c b/tools/001_derive_h48/derive_h48.c @@ -32,7 +32,7 @@ int main(int argc, char **argv) { filename_large = argv[3]; filename_small = argv[4]; - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); timerun(run); diff --git a/tools/100_checkdata/checkdata.c b/tools/100_checkdata/checkdata.c @@ -41,7 +41,7 @@ int main(int argc, char **argv) { solver = argv[1]; filename = argv[2]; - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); timerun(run); diff --git a/tools/300_solve_small/solve_small.c b/tools/300_solve_small/solve_small.c @@ -56,7 +56,7 @@ int main(int argc, char **argv) { solver = argv[1]; srand(time(NULL)); - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); sprintf(filename, "tables/%s", solver); diff --git a/tools/301_solve_file/solve_file.c b/tools/301_solve_file/solve_file.c @@ -46,7 +46,7 @@ int main(int argc, char **argv) { scrfilename = argv[2]; srand(time(NULL)); - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); sprintf(filename, "tables/%s", solver); if (getdata(solver, &buf, filename) != 0) diff --git a/tools/302_solve_multisol/solve_multisol.c b/tools/302_solve_multisol/solve_multisol.c @@ -50,7 +50,7 @@ int main(int argc, char **argv) { solver = argv[1]; nsol = atoi(argv[2]); srand(time(NULL)); - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); sprintf(filename, "tables/%s", solver); diff --git a/tools/400_solvetest/solve_test.c b/tools/400_solvetest/solve_test.c @@ -104,7 +104,7 @@ int main(int argc, char **argv) { solver = argv[1]; srand(time(NULL)); - nissy_setlogger(log_stderr); + nissy_setlogger(log_stdout, NULL); sprintf(filename, "tables/%s", solver); if (getdata(solver, &buf, filename) != 0) diff --git a/tools/nissy_extra.h b/tools/nissy_extra.h @@ -11,5 +11,5 @@ for testing purposes only. size_t gendata_h48_derive(uint8_t, const void *, void *); int parse_h48_solver(const char *, uint8_t [static 1], uint8_t [static 1]); -long long int nissy_datainfo(uint64_t, const char *, void (*)(const char *)); +long long int nissy_datainfo(uint64_t, const char *); long long int nissy_derivedata(const char *, const void *, void *); diff --git a/tools/tool.h b/tools/tool.h @@ -9,8 +9,7 @@ #include "../src/nissy.h" #include "nissy_extra.h" -static void log_stderr(const char *); -static void log_stdout(const char *); +static void log_stdout(const char *, void *); static double timerun(void (*)(void)); static void writetable(const char *, int64_t, const char *); static long long int generatetable(const char *, char **, @@ -23,13 +22,7 @@ static void derivedata_run( const char *, const char *, const char *, const char *); static void -log_stderr(const char *str) -{ - fprintf(stderr, "%s", str); -} - -static void -write_stdout(const char *str) +log_stdout(const char *str, void *unused) { fprintf(stdout, "%s", str); } @@ -211,7 +204,7 @@ gendata_run( case -2: goto gendata_run_finish; default: - nissy_datainfo(size, buf, write_stdout); + nissy_datainfo(size, buf); printf("\n"); printf("Succesfully generated %lld bytes. " "See above for details on the tables.\n", size); @@ -244,7 +237,7 @@ derivedata_run( case -2: goto derivedata_run_finish; default: - nissy_datainfo(size, buf, write_stdout); + nissy_datainfo(size, buf); printf("\n"); printf("Succesfully generated %lld bytes. " "See above for details on the tables.\n", size);