From 7524acda0a8778572262429f27b359d7ee91fc90 Mon Sep 17 00:00:00 2001 From: George Hazan Date: Tue, 7 Jan 2020 22:56:32 +0300 Subject: libmdbx: update to 0.5.0 --- libs/libmdbx/src/test/log.cc | 177 +++++++++++++++++++++++++++---------------- 1 file changed, 111 insertions(+), 66 deletions(-) (limited to 'libs/libmdbx/src/test/log.cc') diff --git a/libs/libmdbx/src/test/log.cc b/libs/libmdbx/src/test/log.cc index 79544e11bb..2b22e28785 100644 --- a/libs/libmdbx/src/test/log.cc +++ b/libs/libmdbx/src/test/log.cc @@ -20,7 +20,7 @@ void failure(const char *fmt, ...) { va_list ap; va_start(ap, fmt); fflushall(); - logging::output(logging::failure, fmt, ap); + logging::output_nocheckloglevel_ap(logging::failure, fmt, ap); va_end(ap); fflushall(); exit(EXIT_FAILURE); @@ -37,29 +37,18 @@ void __noreturn failure_perror(const char *what, int errnum) { //----------------------------------------------------------------------------- -static void mdbx_logger(int type, const char *function, int line, +static void mdbx_logger(int priority, const char *function, int line, const char *msg, va_list args) { - logging::loglevel level = logging::info; - if (type & MDBX_DBG_EXTRA) - level = logging::extra; - if (type & MDBX_DBG_TRACE) - level = logging::trace; - if (type & MDBX_DBG_PRINT) - level = logging::verbose; - if (!function) function = "unknown"; - if (type & MDBX_DBG_ASSERT) { - log_error("mdbx: assertion failure: %s, %d", function, line); - level = logging::failure; - } - if (logging::output( - level, - strncmp(function, "mdbx_", 5) == 0 ? "%s: " : "mdbx: %s: ", function)) - logging::feed_ap(msg, args); - if (type & MDBX_DBG_ASSERT) - abort(); + if (priority == MDBX_LOG_FATAL) + log_error("mdbx: fatal failure: %s, %d", function, line); + + logging::output_nocheckloglevel( + logging::loglevel(priority), + strncmp(function, "mdbx_", 5) == 0 ? "%s: " : "mdbx %s: ", function); + logging::feed_ap(msg, args); } namespace logging { @@ -69,19 +58,16 @@ static std::string suffix; static loglevel level; static FILE *last; -void setlevel(loglevel _level) { - level = (_level > error) ? failure : _level; - int mdbx_dbg_opts = MDBX_DBG_ASSERT | MDBX_DBG_JITTER | MDBX_DBG_DUMP; - if (level <= trace) - mdbx_dbg_opts |= MDBX_DBG_TRACE; - if (level <= verbose) - mdbx_dbg_opts |= MDBX_DBG_PRINT; - int rc = mdbx_setup_debug(mdbx_dbg_opts, mdbx_logger); +void setlevel(loglevel priority) { + level = priority; + int rc = mdbx_setup_debug(priority, + MDBX_DBG_ASSERT | MDBX_DBG_AUDIT | MDBX_DBG_JITTER, + mdbx_logger); log_trace("set mdbx debug-opts: 0x%02x", rc); } -void setup(loglevel _level, const std::string &_prefix) { - setlevel(_level); +void setup(loglevel priority, const std::string &_prefix) { + setlevel(priority); prefix = _prefix; } @@ -95,10 +81,10 @@ const char *level2str(const loglevel alevel) { return "extra"; case trace: return "trace"; + case debug: + return "debug"; case verbose: return "verbose"; - case info: - return "info"; case notice: return "notice"; case warning: @@ -111,26 +97,28 @@ const char *level2str(const loglevel alevel) { } bool output(const loglevel priority, const char *format, ...) { - if (priority < level) + if (lower(priority, level)) return false; va_list ap; va_start(ap, format); - output(priority, format, ap); + output_nocheckloglevel_ap(priority, format, ap); va_end(ap); return true; } -bool output(const logging::loglevel priority, const char *format, va_list ap) { +void output_nocheckloglevel_ap(const logging::loglevel priority, + const char *format, va_list ap) { if (last) { putc('\n', last); fflush(last); + if (last == stderr) { + putc('\n', stdout); + fflush(stdout); + } last = nullptr; } - if (priority < level) - return false; - chrono::time now = chrono::now_realtime(); struct tm tm; #ifdef _MSC_VER @@ -144,14 +132,14 @@ bool output(const logging::loglevel priority, const char *format, va_list ap) { last = stdout; fprintf(last, - "[ %02d%02d%02d-%02d:%02d:%02d.%06d_%05u %-10s %.4s ] %s" /* TODO */, + "[ %02d%02d%02d-%02d:%02d:%02d.%06d_%05lu %-10s %.4s ] %s" /* TODO */, tm.tm_year - 100, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, - tm.tm_sec, chrono::fractional2us(now.fractional), osal_getpid(), + tm.tm_sec, chrono::fractional2us(now.fractional), (long)osal_getpid(), prefix.c_str(), level2str(priority), suffix.c_str()); va_list ones; memset(&ones, 0, sizeof(ones)) /* zap MSVC and other stupid compilers */; - if (priority >= error) + if (same_or_higher(priority, error)) va_copy(ones, ap); vfprintf(last, format, ap); @@ -178,29 +166,36 @@ bool output(const logging::loglevel priority, const char *format, va_list ap) { break; } - if (priority >= error) { + if (same_or_higher(priority, error)) { if (last != stderr) { - fprintf(stderr, "[ %05u %-10s %.4s ] %s", osal_getpid(), prefix.c_str(), - level2str(priority), suffix.c_str()); + fprintf(stderr, "[ %05lu %-10s %.4s ] %s", (long)osal_getpid(), + prefix.c_str(), level2str(priority), suffix.c_str()); vfprintf(stderr, format, ones); - if (end != '\n') - putc('\n', stderr); - fflush(stderr); + if (end == '\n') + fflush(stderr); + else + last = stderr; } va_end(ones); } - - return true; } bool feed_ap(const char *format, va_list ap) { if (!last) return false; + if (last == stderr) { + va_list ones; + va_copy(ones, ap); + vfprintf(stdout, format, ones); + va_end(ones); + } vfprintf(last, format, ap); size_t len = strlen(format); if (len && format[len - 1] == '\n') { fflush(last); + if (last == stderr) + fflush(stdout); last = nullptr; } return true; @@ -242,73 +237,123 @@ void local_suffix::pop() { local_suffix::~local_suffix() { suffix.erase(trim_pos); } +void progress_canary(bool active) { + static chrono::time progress_timestamp; + chrono::time now = chrono::now_motonic(); + + if (now.fixedpoint - progress_timestamp.fixedpoint < + chrono::from_ms(42).fixedpoint) + return; + + if (osal_progress_push(active)) { + progress_timestamp = now; + return; + } + + if (progress_timestamp.fixedpoint == 0) { + putc('>', stderr); + progress_timestamp = now; + } else if (global::config::console_mode) { + if (active) { + static int last_point = -1; + int point = (now.fixedpoint >> 29) & 3; + if (point != last_point) { + progress_timestamp = now; + fprintf(stderr, "%c\b", "-\\|/"[last_point = point]); + } + } else if (now.fixedpoint - progress_timestamp.fixedpoint > + chrono::from_seconds(2).fixedpoint) { + progress_timestamp = now; + fprintf(stderr, "%c\b", "@*"[now.utc & 1]); + } + } else { + static int count; + if (active && now.fixedpoint - progress_timestamp.fixedpoint > + chrono::from_seconds(1).fixedpoint) { + putc('.', stderr); + progress_timestamp = now; + ++count; + } else if (now.fixedpoint - progress_timestamp.fixedpoint > + chrono::from_seconds(5).fixedpoint) { + putc("@*"[now.utc & 1], stderr); + progress_timestamp = now; + ++count; + } + if (count == 60) { + count = 0; + putc('\n', stderr); + } + } + fflush(stderr); +} + } // namespace logging void log_extra(const char *msg, ...) { - if (logging::extra >= logging::level) { + if (logging::same_or_higher(logging::extra, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::extra, msg, ap); + logging::output_nocheckloglevel_ap(logging::extra, msg, ap); va_end(ap); } else logging::last = nullptr; } void log_trace(const char *msg, ...) { - if (logging::trace >= logging::level) { + if (logging::same_or_higher(logging::trace, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::trace, msg, ap); + logging::output_nocheckloglevel_ap(logging::trace, msg, ap); va_end(ap); } else logging::last = nullptr; } -void log_verbose(const char *msg, ...) { - if (logging::verbose >= logging::level) { +void log_debug(const char *msg, ...) { + if (logging::same_or_higher(logging::debug, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::verbose, msg, ap); + logging::output_nocheckloglevel_ap(logging::debug, msg, ap); va_end(ap); } else logging::last = nullptr; } -void log_info(const char *msg, ...) { - if (logging::info >= logging::level) { +void log_verbose(const char *msg, ...) { + if (logging::same_or_higher(logging::verbose, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::info, msg, ap); + logging::output_nocheckloglevel_ap(logging::verbose, msg, ap); va_end(ap); } else logging::last = nullptr; } void log_notice(const char *msg, ...) { - if (logging::notice >= logging::level) { + if (logging::same_or_higher(logging::notice, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::notice, msg, ap); + logging::output_nocheckloglevel_ap(logging::notice, msg, ap); va_end(ap); } else logging::last = nullptr; } void log_warning(const char *msg, ...) { - if (logging::warning >= logging::level) { + if (logging::same_or_higher(logging::warning, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::warning, msg, ap); + logging::output_nocheckloglevel_ap(logging::warning, msg, ap); va_end(ap); } else logging::last = nullptr; } void log_error(const char *msg, ...) { - if (logging::error >= logging::level) { + if (logging::same_or_higher(logging::error, logging::level)) { va_list ap; va_start(ap, msg); - logging::output(logging::error, msg, ap); + logging::output_nocheckloglevel_ap(logging::error, msg, ap); va_end(ap); } else logging::last = nullptr; @@ -319,7 +364,7 @@ void log_trouble(const char *where, const char *what, int errnum) { } bool log_enabled(const logging::loglevel priority) { - return (priority >= logging::level); + return logging::same_or_higher(priority, logging::level); } void log_flush(void) { fflushall(); } -- cgit v1.2.3