Spicy
logging.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 
8 #include <hilti/rt/debug-logger.h>
9 #include <hilti/rt/global-state.h>
10 #include <hilti/rt/types/string.h>
11 #include <hilti/rt/util.h>
12 
13 namespace hilti::rt {
14 
21 void fatalError(const std::string& msg) __attribute__((noreturn));
22 
24 void warning(const std::string& msg);
25 
31 #define HILTI_RT_DEBUG(stream, msg) \
32  { \
33  if ( ::hilti::rt::detail::globalState()->debug_logger && \
34  ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream) ) \
35  ::hilti::rt::debug::detail::print(stream, msg); \
36  }
37 
39 #define __location__(x) ::hilti::rt::debug::setLocation(x);
40 
41 namespace debug {
42 
43 namespace detail {
45 inline void print(const std::string& stream, const char* msg) {
46  if ( ::hilti::rt::detail::globalState()->debug_logger )
47  ::hilti::rt::detail::globalState()->debug_logger->print(stream, msg);
48 }
49 
51 inline void print(const std::string& stream, const std::string_view& s) {
52  if ( ::hilti::rt::detail::globalState()->debug_logger )
53  ::hilti::rt::detail::globalState()->debug_logger->print(stream, hilti::rt::escapeBytes(s, false));
54 }
55 
56 template<typename T, typename std::enable_if_t<not std::is_convertible<T, std::string_view>::value>* = nullptr>
58 inline void print(const std::string& stream, const T& t) {
59  if ( ::hilti::rt::detail::globalState()->debug_logger )
60  ::hilti::rt::detail::globalState()->debug_logger->print(stream, hilti::rt::to_string_for_print(t));
61 }
62 } // namespace detail
63 
65 inline bool isEnabled(const std::string& stream) {
66  return ::hilti::rt::detail::globalState()->debug_logger &&
67  ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream);
68 }
69 
71 inline void indent(const std::string& stream) {
72  if ( ::hilti::rt::detail::globalState()->debug_logger )
73  ::hilti::rt::detail::globalState()->debug_logger->indent(stream);
74 }
75 
77 inline void dedent(const std::string& stream) {
78  if ( ::hilti::rt::detail::globalState()->debug_logger )
79  ::hilti::rt::detail::globalState()->debug_logger->dedent(stream);
80 }
81 
85 inline const char* location() {
86  const auto context = ::hilti::rt::context::detail::current();
87  return context ? context->source_location : nullptr;
88 }
89 
94 inline void setLocation(const char* l = nullptr) {
95  if ( auto context = ::hilti::rt::context::detail::current() )
96  context->source_location = l;
97 }
98 
104 template<typename T>
105 inline void print(const std::string& stream, T&& msg) {
106  if ( ::hilti::rt::detail::globalState()->debug_logger &&
107  ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream) )
108  ::hilti::rt::debug::detail::print(stream, std::forward<T>(msg));
109 }
110 
111 } // namespace debug
112 } // namespace hilti::rt
void fatalError(const std::string &msg) __attribute__((noreturn))
Definition: logging.cc:21
Definition: any.h:7
void print(const T &t, bool newline=true)
Definition: hilti.h:22
Definition: location.h:94
void warning(const std::string &msg)
Definition: logging.cc:28