From 32df144e8f87caf4fd93ce9dc6c4d5d8400c1433 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Fri, 26 May 2023 20:14:13 +0300 Subject: [PATCH 12/12] Add randlog facilities See osdn #47667 Signed-off-by: Marko Lindqvist --- utility/rand.c | 38 +++++++++++++++++++++++++++++++++++--- utility/rand.h | 24 +++++++++++++++++++++++- 2 files changed, 58 insertions(+), 4 deletions(-) diff --git a/utility/rand.c b/utility/rand.c index 3070c4da9d..ea8964555e 100644 --- a/utility/rand.c +++ b/utility/rand.c @@ -44,6 +44,39 @@ #define log_rand log_debug + +#define STD_RAND_VALUE_LOG(_c, _s, _nr, _f, _l) \ + log_rand("%s(%lu) = %lu at %s:%d", _c, \ + (unsigned long) _s, (unsigned long) _nr, \ + _f, _l); + + +#ifdef LOG_RAND_VALUES +static bool randlog_enabled = TRUE; + +/*********************************************************************//** + Control if generated random numbers get written to log at LOG_NORMAL + logging level. + + @param enable Whether to enable or disable the feature +*************************************************************************/ +void enable_randlog(bool enable) +{ + randlog_enabled = enable; +} + +#define RAND_VALUE_LOG(_c, _s, _nr, _f, _l) \ + if (randlog_enabled) { \ + log_normal("%lu -> %lu %s:%d", \ + (unsigned long) _s, (unsigned long) _nr, \ + fc_basename(_f), _l); \ + } else { \ + STD_RAND_VALUE_LOG(_c, _s, _nr, _f, _l); \ + } +#else +#define RAND_VALUE_LOG STD_RAND_VALUE_LOG +#endif + /* A global random state: * Initialized by fc_srand(), updated by fc_rand(), * Can be duplicated/saved/restored via fc_rand_state() @@ -116,9 +149,8 @@ RANDOM_TYPE fc_rand_debug(RANDOM_TYPE size, const char *called_as, new_rand = 0; } - log_rand("%s(%lu) = %lu at %s:%d", - called_as, (unsigned long) size, - (unsigned long) new_rand, file, line); + RAND_VALUE_LOG(called_as, (unsigned long) size, + (unsigned long) new_rand, file, line); return new_rand; } diff --git a/utility/rand.h b/utility/rand.h index 74f5f14055..31dde53002 100644 --- a/utility/rand.h +++ b/utility/rand.h @@ -25,10 +25,32 @@ extern "C" { typedef uint32_t RANDOM_TYPE; +/* Usually random number generator results are logged at LOG_DEBUG level + * Uncommenting this, and using RANDLOG_ON and RANDLOG_OFF macros in + * the code will make them logged at LOG_NORMAL level, between such + * RANDLOG_ON / RANDLOG_OFF calls. + * Single RANDLOG_OFF will cancel any number of RANDLOG_ONs - + * there's no reference count involved. */ +/* #define LOG_RAND_VALUES */ + +#ifdef LOG_RAND_VALUES + +void enable_randlog(bool enable); + +#define RANDLOG_ON enable_randlog(TRUE); +#define RANDLOG_OFF enable_randlog(FALSE); + +#else /* LOG_RAND_VALUES */ + +#define RANDLOG_ON +#define RANDLOG_OFF + +#endif /* LOG_RAND_VALUES */ + typedef struct { RANDOM_TYPE v[56]; int j, k, x; - bool is_init; /* initially FALSE for static storage */ + bool is_init; /* Initially FALSE for static storage */ } RANDOM_STATE; #define fc_rand(_size) \ -- 2.39.2