From 938bfc24d843a11de83d9b4676868b14b81cad1f Mon Sep 17 00:00:00 2001 From: Aleksey Dobrunov Date: Sat, 19 Oct 2024 17:42:34 +0500 Subject: [PATCH] tests: update logger --- .gdbinit | 1 + icu_unicodestring_prettyprinter.py | 53 ++++++++++++++++++++++++++++++ tests/unit/TestLogger.h | 5 +++ 3 files changed, 59 insertions(+) create mode 100644 .gdbinit create mode 100644 icu_unicodestring_prettyprinter.py diff --git a/.gdbinit b/.gdbinit new file mode 100644 index 0000000..8b19fd5 --- /dev/null +++ b/.gdbinit @@ -0,0 +1 @@ +source icu_unicodestring_prettyprinter.py \ No newline at end of file diff --git a/icu_unicodestring_prettyprinter.py b/icu_unicodestring_prettyprinter.py new file mode 100644 index 0000000..ab08f2d --- /dev/null +++ b/icu_unicodestring_prettyprinter.py @@ -0,0 +1,53 @@ +# To autoload this file into GDB, put the following line in ~/.gdbinit: +# +# python execfile("/path/to/icu_unicodestring_prettyprinter.py") +# +# You can also run that line of code in the GDB console without adding it to ~/.gdbinit. + +from __future__ import print_function +from array import array +import re +import gdb + +class UnicodeStringPrinter: + """GDB pretty printer for ICU4C UnicodeString""" + + def __init__(self, val): + self.val = val + + def to_string(self): + + fLengthAndFlags = self.val["fUnion"]["fFields"]["fLengthAndFlags"] + + if fLengthAndFlags >= 0: + # Short length + length = fLengthAndFlags >> 5 + else: + # Long length + length = self.val["fUnion"]["fFields"]["fLength"] + + if (fLengthAndFlags & 2) != 0: + # Stack storage + if (fLengthAndFlags & 1) != 0: + return u"UnicodeString (BOGUS)" + stack = True + buffer = self.val["fUnion"]["fStackFields"]["fBuffer"] + else: + # Heap storage + stack = False + buffer = self.val["fUnion"]["fFields"]["fArray"] + + content = array('B', [buffer[i] for i in range(length)]).tostring() + return u"UnicodeString (%d on %s): \"%s\"" % ( + length, + u"stack" if stack else u"heap", + content) + +unicode_string_re = re.compile("^icu_?\d*::UnicodeString$") + +def lookup_type(val): + if unicode_string_re.match(str(val.type.unqualified().strip_typedefs())): + return UnicodeStringPrinter(val) + return None + +gdb.pretty_printers.append(lookup_type) diff --git a/tests/unit/TestLogger.h b/tests/unit/TestLogger.h index 309e870..e00491c 100644 --- a/tests/unit/TestLogger.h +++ b/tests/unit/TestLogger.h @@ -23,6 +23,11 @@ class TestLogger : public Logger //std::cerr << message << '\n'; } + void flush() override + { + + } + bool message_exists() const { return !log_messages.empty(); } bool message_print(bool waiting_exist = false) const {