From d332827aee7d70cdb642631d7a289751e1d8a36a Mon Sep 17 00:00:00 2001 From: Benjamin Bannier Date: Fri, 17 Nov 2023 22:10:56 +0100 Subject: [PATCH] Use non-owning strings in the logging framework. Closes #1591. --- hilti/runtime/include/debug-logger.h | 14 +++++++------- hilti/runtime/include/logging.h | 20 ++++++++++---------- hilti/runtime/include/util.h | 2 +- hilti/runtime/src/debug-logger.cc | 6 +++--- hilti/runtime/src/logging.cc | 7 ++++--- 5 files changed, 25 insertions(+), 24 deletions(-) diff --git a/hilti/runtime/include/debug-logger.h b/hilti/runtime/include/debug-logger.h index d0a9dae43..87d019af3 100644 --- a/hilti/runtime/include/debug-logger.h +++ b/hilti/runtime/include/debug-logger.h @@ -6,7 +6,7 @@ #include #include #include -#include +#include #include #include @@ -18,17 +18,17 @@ class DebugLogger { public: DebugLogger(hilti::rt::filesystem::path output); - void print(const std::string& stream, const std::string& msg); - void enable(const std::string& streams); + void print(std::string_view stream, std::string_view msg); + void enable(std::string_view streams); - bool isEnabled(const std::string& stream) { return _streams.find(stream) != _streams.end(); } + bool isEnabled(std::string_view stream) { return _streams.find(stream) != _streams.end(); } - void indent(const std::string& stream) { + void indent(std::string_view stream) { if ( isEnabled(stream) ) _streams[stream] += 1; } - void dedent(const std::string& stream) { + void dedent(std::string_view stream) { if ( isEnabled(stream) ) { auto& indent = _streams[stream]; @@ -41,7 +41,7 @@ class DebugLogger { hilti::rt::filesystem::path _path; std::ostream* _output = nullptr; std::unique_ptr _output_file; - std::map> _streams; + std::map> _streams; }; } // namespace hilti::rt::detail diff --git a/hilti/runtime/include/logging.h b/hilti/runtime/include/logging.h index c46d3a3c5..7cd144a6a 100644 --- a/hilti/runtime/include/logging.h +++ b/hilti/runtime/include/logging.h @@ -2,7 +2,7 @@ #pragma once -#include +#include #include #include @@ -18,10 +18,10 @@ namespace hilti::rt { * anything that can happen during "normal" operation (which is almost * everything). */ -void fatalError(const std::string& msg) __attribute__((noreturn)); +void fatalError(std::string_view msg) __attribute__((noreturn)); /** Reports a warning. */ -void warning(const std::string& msg); +void warning(std::string_view msg); /** * Prints a string, or a runtime value, to a specific debug stream. This is a @@ -42,39 +42,39 @@ namespace debug { namespace detail { /** Prints a debug message to a specific debug stream. */ -inline void print(const std::string& stream, const char* msg) { +inline void print(std::string_view stream, const char* msg) { if ( ::hilti::rt::detail::globalState()->debug_logger ) ::hilti::rt::detail::globalState()->debug_logger->print(stream, msg); } /** Print a string to a specific debug stream with proper escaping. */ -inline void print(const std::string& stream, const std::string_view& s) { +inline void print(std::string_view stream, std::string_view s) { if ( ::hilti::rt::detail::globalState()->debug_logger ) ::hilti::rt::detail::globalState()->debug_logger->print(stream, hilti::rt::escapeBytes(s, false)); } template::value>* = nullptr> /** Prints the string representastion of a HILTI runtime value to a specific debug stream. */ -inline void print(const std::string& stream, const T& t) { +inline void print(std::string_view stream, const T& t) { if ( ::hilti::rt::detail::globalState()->debug_logger ) ::hilti::rt::detail::globalState()->debug_logger->print(stream, hilti::rt::to_string_for_print(t)); } } // namespace detail /** Returns true if debug logging is enabled for a given stream. */ -inline bool isEnabled(const std::string& stream) { +inline bool isEnabled(std::string_view stream) { return ::hilti::rt::detail::globalState()->debug_logger && ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream); } /** Increases the indentation level for a debug stream. */ -inline void indent(const std::string& stream) { +inline void indent(std::string_view stream) { if ( ::hilti::rt::detail::globalState()->debug_logger ) ::hilti::rt::detail::globalState()->debug_logger->indent(stream); } /** Decreases the indentation level for a debug stream. */ -inline void dedent(const std::string& stream) { +inline void dedent(const std::string_view stream) { if ( ::hilti::rt::detail::globalState()->debug_logger ) ::hilti::rt::detail::globalState()->debug_logger->dedent(stream); } @@ -103,7 +103,7 @@ inline void setLocation(const char* l = nullptr) { detail::tls_location = l; } * arguments if nothing is going to get logged. */ template -inline void print(const std::string& stream, T&& msg) { +inline void print(std::string_view stream, T&& msg) { if ( ::hilti::rt::detail::globalState()->debug_logger && ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream) ) ::hilti::rt::debug::detail::print(stream, std::forward(msg)); diff --git a/hilti/runtime/include/util.h b/hilti/runtime/include/util.h index 798b33ced..f86a9072c 100644 --- a/hilti/runtime/include/util.h +++ b/hilti/runtime/include/util.h @@ -76,7 +76,7 @@ namespace hilti::rt { /** Reports an internal error and aborts execution. */ -void internalError(const std::string& msg) __attribute__((noreturn)); +void internalError(std::string_view msg) __attribute__((noreturn)); } // namespace hilti::rt diff --git a/hilti/runtime/src/debug-logger.cc b/hilti/runtime/src/debug-logger.cc index 3b675ec49..c1e5d7f24 100644 --- a/hilti/runtime/src/debug-logger.cc +++ b/hilti/runtime/src/debug-logger.cc @@ -15,12 +15,12 @@ using namespace hilti::rt; detail::DebugLogger::DebugLogger(hilti::rt::filesystem::path output) : _path(std::move(output)) {} -void detail::DebugLogger::enable(const std::string& streams) { +void detail::DebugLogger::enable(std::string_view streams) { for ( auto s : split(streams, ":") ) - _streams[std::string(trim(s))] = 0; + _streams[trim(s)] = 0; } -void detail::DebugLogger::print(const std::string& stream, const std::string& msg) { +void detail::DebugLogger::print(std::string_view stream, std::string_view msg) { if ( _path.empty() ) return; diff --git a/hilti/runtime/src/logging.cc b/hilti/runtime/src/logging.cc index 8ff982d94..caaec419d 100644 --- a/hilti/runtime/src/logging.cc +++ b/hilti/runtime/src/logging.cc @@ -9,21 +9,22 @@ using namespace hilti::rt::detail; #include #include +#include using namespace hilti::rt; HILTI_THREAD_LOCAL const char* debug::detail::tls_location = nullptr; -void hilti::rt::internalError(const std::string& msg) { +void hilti::rt::internalError(std::string_view msg) { std::cerr << fmt("[libhilti] Internal error: %s", msg) << std::endl; abort_with_backtrace(); } -void hilti::rt::fatalError(const std::string& msg) { +void hilti::rt::fatalError(std::string_view msg) { std::cerr << fmt("[libhilti] Fatal error: %s", msg) << std::endl; // We do a hard abort here, with no cleanup, because ASAN may have trouble // terminating otherwise if the fiber stacks are still hanging out. _exit(1); } -void hilti::rt::warning(const std::string& msg) { std::cerr << fmt("[libhilti] Warning: %s", msg) << std::endl; } +void hilti::rt::warning(std::string_view msg) { std::cerr << fmt("[libhilti] Warning: %s", msg) << std::endl; }