diff options
| author | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-15 15:51:40 +0200 |
|---|---|---|
| committer | Sebastiano Tronto <sebastiano@tronto.net> | 2025-04-15 15:51:40 +0200 |
| commit | b25a939e2d8b045c083caf3264c4e5aac1cf88fb (patch) | |
| tree | 37b862c82389ceddcf4624b5e26536390d6b8265 /src | |
| parent | b02c083a3545b6e6b36ab62680128edec619a804 (diff) | |
| download | nissy-core-b25a939e2d8b045c083caf3264c4e5aac1cf88fb.tar.gz nissy-core-b25a939e2d8b045c083caf3264c4e5aac1cf88fb.zip | |
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 (limited to '')
| -rw-r--r-- | src/nissy.c | 21 | ||||
| -rw-r--r-- | src/nissy.h | 9 | ||||
| -rw-r--r-- | src/utils/dbg_log.h | 9 |
3 files changed, 22 insertions, 17 deletions
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( | |||
| 366 | long long | 366 | long long |
| 367 | nissy_datainfo( | 367 | nissy_datainfo( |
| 368 | uint64_t data_size, | 368 | uint64_t data_size, |
| 369 | const char data[data_size], | 369 | const char data[data_size] |
| 370 | void (*write)(const char *) | ||
| 371 | ) | 370 | ) |
| 372 | { | 371 | { |
| 373 | uint8_t i; | 372 | uint8_t i; |
| @@ -383,8 +382,7 @@ nissy_datainfo( | |||
| 383 | if (ret != 0) | 382 | if (ret != 0) |
| 384 | return ret; | 383 | return ret; |
| 385 | 384 | ||
| 386 | write_wrapper(write, | 385 | LOG("\n---------\n\n" |
| 387 | "\n---------\n\n" | ||
| 388 | "Table information for '%s'\n\n" | 386 | "Table information for '%s'\n\n" |
| 389 | "Size: %" PRIu64 " bytes\n" | 387 | "Size: %" PRIu64 " bytes\n" |
| 390 | "Entries: %" PRIu64 " (%" PRIu8 " bits per entry)\n", | 388 | "Entries: %" PRIu64 " (%" PRIu8 " bits per entry)\n", |
| @@ -392,15 +390,14 @@ nissy_datainfo( | |||
| 392 | 390 | ||
| 393 | switch (info.type) { | 391 | switch (info.type) { |
| 394 | case TABLETYPE_PRUNING: | 392 | case TABLETYPE_PRUNING: |
| 395 | write_wrapper(write, "\nTable distribution:\n" | 393 | LOG("\nTable distribution:\nValue\tPositions\n"); |
| 396 | "Value\tPositions\n"); | ||
| 397 | for (i = 0; i <= info.maxvalue; i++) { | 394 | for (i = 0; i <= info.maxvalue; i++) { |
| 398 | write_wrapper(write, "%" PRIu8 "\t%" PRIu64 "\n", | 395 | LOG("%" PRIu8 "\t%" PRIu64 "\n", |
| 399 | i + info.base, info.distribution[i]); | 396 | i + info.base, info.distribution[i]); |
| 400 | } | 397 | } |
| 401 | break; | 398 | break; |
| 402 | case TABLETYPE_SPECIAL: | 399 | case TABLETYPE_SPECIAL: |
| 403 | write_wrapper(write, "This is an ad-hoc table\n"); | 400 | LOG("This is an ad-hoc table\n"); |
| 404 | break; | 401 | break; |
| 405 | default: | 402 | default: |
| 406 | LOG("datainfo: unknown table type\n"); | 403 | LOG("datainfo: unknown table type\n"); |
| @@ -409,9 +406,9 @@ nissy_datainfo( | |||
| 409 | 406 | ||
| 410 | if (info.next != 0) | 407 | if (info.next != 0) |
| 411 | return nissy_datainfo( | 408 | return nissy_datainfo( |
| 412 | data_size - info.next, (char *)data + info.next, write); | 409 | data_size - info.next, (char *)data + info.next); |
| 413 | 410 | ||
| 414 | write_wrapper(write, "\n---------\n"); | 411 | LOG("\n---------\n"); |
| 415 | 412 | ||
| 416 | return NISSY_OK; | 413 | return NISSY_OK; |
| 417 | } | 414 | } |
| @@ -624,9 +621,11 @@ nissy_countmoves( | |||
| 624 | 621 | ||
| 625 | long long | 622 | long long |
| 626 | nissy_setlogger( | 623 | nissy_setlogger( |
| 627 | void (*log)(const char *) | 624 | void (*log)(const char *, void *), |
| 625 | void *user_data | ||
| 628 | ) | 626 | ) |
| 629 | { | 627 | { |
| 630 | nissy_log = log; | 628 | nissy_log = log; |
| 629 | nissy_log_data = user_data; | ||
| 631 | return NISSY_OK; | 630 | return NISSY_OK; |
| 632 | } | 631 | } |
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 | |||
| 421 | disables logging. | 421 | disables logging. |
| 422 | 422 | ||
| 423 | Parameters: | 423 | Parameters: |
| 424 | write - A callback writer with the same signature as printf(3). | 424 | logger_function - A pointer to a function that takes two parameters: |
| 425 | * A C string, the string to be printed. | ||
| 426 | * Any other data via a void pointer. | ||
| 427 | user_data - Any data that will be provided by the logger when | ||
| 428 | calling logger_function. | ||
| 425 | 429 | ||
| 426 | Return values: | 430 | Return values: |
| 427 | NISSY_OK - Logger set succesfully. No warning or error is going to be given | 431 | NISSY_OK - Logger set succesfully. No warning or error is going to be given |
| @@ -429,5 +433,6 @@ Return values: | |||
| 429 | */ | 433 | */ |
| 430 | long long | 434 | long long |
| 431 | nissy_setlogger( | 435 | nissy_setlogger( |
| 432 | void (*logger_function)(const char *) | 436 | void (*logger_function)(const char *, void *), |
| 437 | void *user_data | ||
| 433 | ); | 438 | ); |
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 @@ | |||
| 1 | #include <stdio.h> | 1 | #include <stdio.h> |
| 2 | 2 | ||
| 3 | void (*nissy_log)(const char *); | 3 | void (*nissy_log)(const char *, void *); |
| 4 | void write_wrapper(void (*)(const char *), const char *, ...); | 4 | void *nissy_log_data; |
| 5 | void write_wrapper(void (*)(const char *, void *), const char *, ...); | ||
| 5 | 6 | ||
| 6 | void | 7 | void |
| 7 | write_wrapper(void (*write)(const char *), const char *str, ...) | 8 | write_wrapper(void (*write)(const char *, void *), const char *str, ...) |
| 8 | { | 9 | { |
| 9 | static const size_t len = 1000; | 10 | static const size_t len = 1000; |
| 10 | char message[len]; | 11 | char message[len]; |
| @@ -14,7 +15,7 @@ write_wrapper(void (*write)(const char *), const char *str, ...) | |||
| 14 | vsprintf(message, str, args); | 15 | vsprintf(message, str, args); |
| 15 | va_end(args); | 16 | va_end(args); |
| 16 | 17 | ||
| 17 | write(message); | 18 | write(message, nissy_log_data); |
| 18 | } | 19 | } |
| 19 | 20 | ||
| 20 | #define LOG(...) if (nissy_log != NULL) write_wrapper(nissy_log, __VA_ARGS__); | 21 | #define LOG(...) if (nissy_log != NULL) write_wrapper(nissy_log, __VA_ARGS__); |
