From: Jérémie Galarneau Date: Wed, 1 May 2024 15:21:04 +0000 (-0400) Subject: Clean-up: modernize pretty_xml.cpp X-Git-Url: https://git.lttng.org/?p=lttng-tools.git;a=commitdiff_plain;h=HEAD;ds=sidebyside Clean-up: modernize pretty_xml.cpp Since pretty_xml was converted to C++, clang-tidy had a number of grievances such as the use of NULL (instead of nullptr). Since the file is very small, it is modernized to address those issues: - Wrap libxml2 resources in RAII wrappers - Use C++ IO APIs in lieu of fprintf Signed-off-by: Jérémie Galarneau Change-Id: Ie90e3e05130f7916f411e0de36e8aac72a0f790c --- diff --git a/tests/utils/xml-utils/common.hpp b/tests/utils/xml-utils/common.hpp index 5acb1b1be..01a9b5207 100644 --- a/tests/utils/xml-utils/common.hpp +++ b/tests/utils/xml-utils/common.hpp @@ -13,8 +13,37 @@ #include #include -using xml_parser_ctx_uptr = std::unique_ptr< +namespace lttng { +namespace libxml { + +using parser_ctx_uptr = std::unique_ptr< xmlParserCtxt, lttng::memory::create_deleter_class::deleter>; +using doc_uptr = + std::unique_ptr::deleter>; + +/* + * Manage the global parser context of libxml2. + * There should only be one instance of this class per process. + */ +class global_parser_context { +public: + global_parser_context() + { + xmlInitParser(); + } + + ~global_parser_context() + { + xmlCleanupParser(); + } + /* Deactivate copy and assignment. */ + global_parser_context(const global_parser_context&) = delete; + global_parser_context(global_parser_context&&) = delete; + global_parser_context& operator=(const global_parser_context&) = delete; + global_parser_context& operator=(global_parser_context&&) = delete; +}; +} /* namespace libxml */ +} /* namespace lttng */ #endif /* TESTS_UTILS_XML_UTILS_COMMON_HPP */ diff --git a/tests/utils/xml-utils/extract_xml.cpp b/tests/utils/xml-utils/extract_xml.cpp index ad319d3c4..280f2ed02 100644 --- a/tests/utils/xml-utils/extract_xml.cpp +++ b/tests/utils/xml-utils/extract_xml.cpp @@ -38,6 +38,8 @@ #include #include +namespace ll = lttng::libxml; + #if defined(LIBXML_XPATH_ENABLED) static int opt_verbose; @@ -178,7 +180,7 @@ static int extract_xpath(const char *xml_path, const xmlChar *xpath) LTTNG_ASSERT(xml_path); LTTNG_ASSERT(xpath); - xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() }; + ll::parser_ctx_uptr parserCtx{ xmlNewParserCtxt() }; if (!parserCtx) { fprintf(stderr, "ERR: could not allocate an XML parser context\n"); diff --git a/tests/utils/xml-utils/pretty_xml.cpp b/tests/utils/xml-utils/pretty_xml.cpp index 7eb4710ca..8a6e967a7 100644 --- a/tests/utils/xml-utils/pretty_xml.cpp +++ b/tests/utils/xml-utils/pretty_xml.cpp @@ -12,34 +12,28 @@ #include "common.hpp" +#include + +#include #include #include +namespace ll = lttng::libxml; + int main() { - xmlDocPtr doc = NULL; - - /* Init libxml. */ - xmlInitParser(); - - { - xml_parser_ctx_uptr parserCtx{ xmlNewParserCtxt() }; - - /* Parse the XML document from stdin. */ - doc = xmlCtxtReadFd( - parserCtx.get(), STDIN_FILENO, nullptr, nullptr, XML_PARSE_NOBLANKS); - if (!doc) { - fprintf(stderr, "ERR parsing: xml input invalid"); - return -1; - } - - xmlDocFormatDump(stdout, doc, 1); - - xmlFreeDoc(doc); + const ll::global_parser_context global_parser_context; + const ll::parser_ctx_uptr parserCtx{ xmlNewParserCtxt() }; + + /* Parse the XML document from stdin. */ + const ll::doc_uptr doc{ xmlCtxtReadFd( + parserCtx.get(), STDIN_FILENO, nullptr, nullptr, XML_PARSE_NOBLANKS) }; + if (!doc) { + std::cerr << "Error: invalid XML input on stdin\n"; + return -1; } - /* Shutdown libxml. */ - xmlCleanupParser(); + xmlDocFormatDump(stdout, doc.get(), 1); return 0; }