Skip to content

Commit

Permalink
Use non-owning strings in the logging framework.
Browse files Browse the repository at this point in the history
Closes #1591.
  • Loading branch information
bbannier committed Nov 29, 2023
1 parent eff3e56 commit 5dd1075
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
14 changes: 7 additions & 7 deletions hilti/runtime/include/debug-logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <map>
#include <memory>
#include <optional>
#include <string>
#include <string_view>

#include <hilti/rt/filesystem.h>
#include <hilti/rt/util.h>
Expand All @@ -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];

Expand All @@ -41,7 +41,7 @@ class DebugLogger {
hilti::rt::filesystem::path _path;
std::ostream* _output = nullptr;
std::unique_ptr<std::ofstream> _output_file;
std::map<std::string, integer::safe<uint64_t>> _streams;
std::map<std::string_view, integer::safe<uint64_t>> _streams;
};

} // namespace hilti::rt::detail
20 changes: 10 additions & 10 deletions hilti/runtime/include/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#pragma once

#include <string>
#include <string_view>
#include <utility>

#include <hilti/rt/debug-logger.h>
Expand All @@ -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
Expand All @@ -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<typename T, typename std::enable_if_t<not std::is_convertible<T, std::string_view>::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);
}
Expand Down Expand Up @@ -103,7 +103,7 @@ inline void setLocation(const char* l = nullptr) { detail::tls_location = l; }
* arguments if nothing is going to get logged.
*/
template<typename T>
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<T>(msg));
Expand Down
2 changes: 1 addition & 1 deletion hilti/runtime/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 3 additions & 3 deletions hilti/runtime/src/debug-logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 4 additions & 3 deletions hilti/runtime/src/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,22 @@ using namespace hilti::rt::detail;

#include <cstdlib>
#include <iostream>
#include <string_view>

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; }

0 comments on commit 5dd1075

Please sign in to comment.