Line data Source code
1 : #ifdef __PLUMED_HAS_ASMJIT 2 : // This file is part of AsmJit project <https://asmjit.com> 3 : // 4 : // See asmjit.h or LICENSE.md for license and copyright information 5 : // SPDX-License-Identifier: Zlib 6 : 7 : #ifndef ASMJIT_CORE_LOGGING_H_INCLUDED 8 : #define ASMJIT_CORE_LOGGING_H_INCLUDED 9 : 10 : #include "inst.h" 11 : #include "moved_string.h" 12 : #include "formatter.h" 13 : 14 : #ifndef ASMJIT_NO_LOGGING 15 : 16 : ASMJIT_BEGIN_NAMESPACE 17 : 18 : //! \addtogroup asmjit_logging 19 : //! \{ 20 : 21 : //! Logging interface. 22 : //! 23 : //! This class can be inherited and reimplemented to fit into your own logging needs. When reimplementing a logger 24 : //! use \ref Logger::_log() method to log customize the output. 25 : //! 26 : //! There are two `Logger` implementations offered by AsmJit: 27 : //! - \ref FileLogger - logs into a `FILE*`. 28 : //! - \ref StringLogger - concatenates all logs into a \ref String. 29 : class ASMJIT_VIRTAPI Logger { 30 : public: 31 : ASMJIT_BASE_CLASS(Logger) 32 : ASMJIT_NONCOPYABLE(Logger) 33 : 34 : //! Format options. 35 : FormatOptions _options; 36 : 37 : //! \name Construction & Destruction 38 : //! \{ 39 : 40 : //! Creates a `Logger` instance. 41 : ASMJIT_API Logger() noexcept; 42 : //! Destroys the `Logger` instance. 43 : ASMJIT_API virtual ~Logger() noexcept; 44 : 45 : //! \} 46 : 47 : //! \name Format Options 48 : //! \{ 49 : 50 : //! Returns \ref FormatOptions of this logger. 51 0 : inline FormatOptions& options() noexcept { return _options; } 52 : //! \overload 53 : inline const FormatOptions& options() const noexcept { return _options; } 54 : //! Sets formatting options of this Logger to `options`. 55 : inline void setOptions(const FormatOptions& options) noexcept { _options = options; } 56 : //! Resets formatting options of this Logger to defaults. 57 : inline void resetOptions() noexcept { _options.reset(); } 58 : 59 : //! Returns formatting flags. 60 : inline FormatFlags flags() const noexcept { return _options.flags(); } 61 : //! Tests whether the logger has the given `flag` enabled. 62 : inline bool hasFlag(FormatFlags flag) const noexcept { return _options.hasFlag(flag); } 63 : //! Sets formatting flags to `flags`. 64 : inline void setFlags(FormatFlags flags) noexcept { _options.setFlags(flags); } 65 : //! Enables the given formatting `flags`. 66 : inline void addFlags(FormatFlags flags) noexcept { _options.addFlags(flags); } 67 : //! Disables the given formatting `flags`. 68 : inline void clearFlags(FormatFlags flags) noexcept { _options.clearFlags(flags); } 69 : 70 : //! Returns indentation of a given indentation `group`. 71 : inline uint32_t indentation(FormatIndentationGroup type) const noexcept { return _options.indentation(type); } 72 : //! Sets indentation of the given indentation `group` to `n` spaces. 73 : inline void setIndentation(FormatIndentationGroup type, uint32_t n) noexcept { _options.setIndentation(type, n); } 74 : //! Resets indentation of the given indentation `group` to 0 spaces. 75 : inline void resetIndentation(FormatIndentationGroup type) noexcept { _options.resetIndentation(type); } 76 : 77 : //! Returns padding of a given padding `group`. 78 : inline size_t padding(FormatPaddingGroup type) const noexcept { return _options.padding(type); } 79 : //! Sets padding of a given padding `group` to `n`. 80 : inline void setPadding(FormatPaddingGroup type, uint32_t n) noexcept { _options.setPadding(type, n); } 81 : //! Resets padding of a given padding `group` to 0, which means that a default will be used. 82 : inline void resetPadding(FormatPaddingGroup type) noexcept { _options.resetPadding(type); } 83 : 84 : //! \} 85 : 86 : //! \name Logging Interface 87 : //! \{ 88 : 89 : //! Logs `str` - must be reimplemented. 90 : //! 91 : //! The function can accept either a null terminated string if `size` is `SIZE_MAX` or a non-null terminated 92 : //! string of the given `size`. The function cannot assume that the data is null terminated and must handle 93 : //! non-null terminated inputs. 94 : virtual Error _log(const char* data, size_t size) noexcept = 0; 95 : 96 : //! Logs string `str`, which is either null terminated or having size `size`. 97 0 : inline Error log(const char* data, size_t size = SIZE_MAX) noexcept { return _log(data, size); } 98 : //! Logs content of a string `str`. 99 0 : inline Error log(const String& str) noexcept { return _log(str.data(), str.size()); } 100 : 101 : //! Formats the message by using `snprintf()` and then passes the formatted string to \ref _log(). 102 : ASMJIT_API Error logf(const char* fmt, ...) noexcept; 103 : 104 : //! Formats the message by using `vsnprintf()` and then passes the formatted string to \ref _log(). 105 : ASMJIT_API Error logv(const char* fmt, va_list ap) noexcept; 106 : 107 : //! \} 108 : }; 109 : 110 : //! Logger that can log to a `FILE*`. 111 : class ASMJIT_VIRTAPI FileLogger : public Logger { 112 : public: 113 : ASMJIT_NONCOPYABLE(FileLogger) 114 : 115 : FILE* _file; 116 : 117 : //! \name Construction & Destruction 118 : //! \{ 119 : 120 : //! Creates a new `FileLogger` that logs to `FILE*`. 121 : ASMJIT_API FileLogger(FILE* file = nullptr) noexcept; 122 : //! Destroys the `FileLogger`. 123 : ASMJIT_API virtual ~FileLogger() noexcept; 124 : 125 : //! \} 126 : 127 : //! \name Accessors 128 : //! \{ 129 : 130 : //! Returns the logging output stream or null if the logger has no output stream. 131 : inline FILE* file() const noexcept { return _file; } 132 : 133 : //! Sets the logging output stream to `stream` or null. 134 : //! 135 : //! \note If the `file` is null the logging will be disabled. When a logger is attached to `CodeHolder` or any 136 : //! emitter the logging API will always be called regardless of the output file. This means that if you really 137 : //! want to disable logging at emitter level you must not attach a logger to it. 138 : inline void setFile(FILE* file) noexcept { _file = file; } 139 : 140 : //! \} 141 : 142 : ASMJIT_API Error _log(const char* data, size_t size = SIZE_MAX) noexcept override; 143 : }; 144 : 145 : //! Logger that stores everything in an internal string buffer. 146 : class ASMJIT_VIRTAPI StringLogger : public Logger { 147 : public: 148 : ASMJIT_NONCOPYABLE(StringLogger) 149 : 150 : //! Logger data as string. 151 : String _content; 152 : 153 : //! \name Construction & Destruction 154 : //! \{ 155 : 156 : //! Create new `StringLogger`. 157 : ASMJIT_API StringLogger() noexcept; 158 : //! Destroys the `StringLogger`. 159 : ASMJIT_API virtual ~StringLogger() noexcept; 160 : 161 : //! \} 162 : 163 : //! \name Logger Data Accessors 164 : //! \{ 165 : 166 : //! Returns the content of the logger as \ref String. 167 : //! 168 : //! It can be moved, if desired. 169 : inline String& content() noexcept { return _content; } 170 : //! \overload 171 : inline const String& content() const noexcept { return _content; } 172 : 173 : //! Returns aggregated logger data as `char*` pointer. 174 : //! 175 : //! The pointer is owned by `StringLogger`, it can't be modified or freed. 176 : inline const char* data() const noexcept { return _content.data(); } 177 : //! Returns size of the data returned by `data()`. 178 : inline size_t dataSize() const noexcept { return _content.size(); } 179 : 180 : //! \} 181 : 182 : //! \name Logger Data Manipulation 183 : //! \{ 184 : 185 : //! Clears the accumulated logger data. 186 : inline void clear() noexcept { _content.clear(); } 187 : 188 : //! \} 189 : 190 : ASMJIT_API Error _log(const char* data, size_t size = SIZE_MAX) noexcept override; 191 : }; 192 : 193 : //! \} 194 : 195 : ASMJIT_END_NAMESPACE 196 : 197 : #endif 198 : 199 : #endif // ASMJIT_CORE_LOGGER_H_INCLUDED 200 : 201 : #endif // __PLUMED_HAS_ASMJIT