From b25a939e2d8b045c083caf3264c4e5aac1cf88fb Mon Sep 17 00:00:00 2001 From: Sebastiano Tronto Date: Tue, 15 Apr 2025 15:51:40 +0200 Subject: 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. --- src/nissy.c | 21 ++++++++++----------- src/nissy.h | 9 +++++++-- src/utils/dbg_log.h | 9 +++++---- 3 files changed, 22 insertions(+), 17 deletions(-) (limited to 'src') diff --git a/src/nissy.c b/src/nissy.c index 92fc01b..a7f8432 100644 --- 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 index 816e03a..b13f417 100644 --- 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 index 2467983..e5b3e24 100644 --- a/src/utils/dbg_log.h +++ b/src/utils/dbg_log.h @@ -1,10 +1,11 @@ #include -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__); -- cgit v1.3