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 
16 // void internalError(const std::string& msg) __attribute__((noreturn)); // Declared in util.h
17 
24 void fatalError(const std::string& msg) __attribute__((noreturn));
25 
27 void warning(const std::string& msg);
28 
34 #define HILTI_RT_DEBUG(stream, msg) \
35  { \
36  if ( ::hilti::rt::detail::globalState()->debug_logger && \
37  ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream) ) \
38  ::hilti::rt::debug::detail::print(stream, msg); \
39  }
40 
42 #define __location__(x) ::hilti::rt::debug::setLocation(x);
43 
44 namespace debug {
45 
46 namespace detail {
48 inline void print(const std::string& stream, const char* msg) {
49  if ( ::hilti::rt::detail::globalState()->debug_logger )
50  ::hilti::rt::detail::globalState()->debug_logger->print(stream, msg);
51 }
52 
54 inline void print(const std::string& stream, const std::string_view& s) {
55  if ( ::hilti::rt::detail::globalState()->debug_logger )
56  ::hilti::rt::detail::globalState()->debug_logger->print(stream, hilti::rt::escapeBytes(s, false));
57 }
58 
59 template<typename T, typename std::enable_if_t<not std::is_convertible<T, std::string_view>::value>* = nullptr>
61 inline void print(const std::string& stream, const T& t) {
62  if ( ::hilti::rt::detail::globalState()->debug_logger )
63  ::hilti::rt::detail::globalState()->debug_logger->print(stream, hilti::rt::to_string_for_print(t));
64 }
65 } // namespace detail
66 
68 inline bool isEnabled(const std::string& stream) {
69  return ::hilti::rt::detail::globalState()->debug_logger &&
70  ::hilti::rt::detail::globalState()->debug_logger->isEnabled(stream);
71 }
72 
74 inline void indent(const std::string& stream) {
75  if ( ::hilti::rt::detail::globalState()->debug_logger )
76  ::hilti::rt::detail::globalState()->debug_logger->indent(stream);
77 }
78 
80 inline void dedent(const std::string& stream) {
81  if ( ::hilti::rt::detail::globalState()->debug_logger )
82  ::hilti::rt::detail::globalState()->debug_logger->dedent(stream);
83 }
84 
88 inline const char* location() {
89  const auto context = ::hilti::rt::context::detail::current();
90  return context ? context->source_location : nullptr;
91 }
92 
97 inline void setLocation(const char* l = nullptr) {
98  if ( auto context = ::hilti::rt::context::detail::current() )
99  context->source_location = l;
100 }
101 
102 } // namespace debug
103 } // namespace hilti::rt
void fatalError(const std::string &msg) __attribute__((noreturn))
Definition: logging.cc:22
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:29