Anklang-0.3.0.dev595+g65331842 anklang-0.3.0.dev595+g65331842
ASE — Anklang Sound Engine (C++)

« « « Anklang Documentation
Loading...
Searching...
No Matches
logging.hh
Go to the documentation of this file.
1 // This Source Code Form is licensed MPL-2.0: http://mozilla.org/MPL/2.0
2#pragma once
3
4#include <ase/cxxaux.hh>
5#include <ase/formatter.hh>
6#include <source_location>
7
8namespace Ase {
9
10// == stdio ==
11template<class... A> void printout (const char *format, const A &...args) ASE_PRINTF (1, 0);
12template<class... A> void printerr (const char *format, const A &...args) ASE_PRINTF (1, 0);
13
16
17// == Sincere Messages ==
19struct LogFormat {
20 std::source_location location;
21 const char *const cstr = nullptr;
23 location (l),
24 cstr (s)
25 {}
26};
27
28template<class ...A> void fatal_error (const LogFormat &format, const A &...args) ASE_NORETURN;
29template<class ...A> void warning (const char *format, const A &...args);
30template<class ...A> void info (const char *format, const A &...args) ASE_ALWAYS_INLINE;
31template<class ...A> void diag (const char *format, const A &...args) ASE_ALWAYS_INLINE;
32
33// == Debugging ==
34void logging_handle_terminate ();
35template<class ...A> void debug (const char *cond, const char *format, const A &...args) ASE_ALWAYS_INLINE;
36inline bool debug_enabled () ASE_ALWAYS_INLINE ASE_PURE;
37bool debug_key_enabled (const char *conditional) noexcept ASE_PURE;
38const char* getenv_ase_debug ();
39extern bool ase_debugging_enabled;
40
41// == Implementations ==
42enum Logging : int { FATAL, ASSERTION, WARN, HINT, INFO, DIAG, TRACE, DEBUGALL, };
43[[noreturn]]
44void logging_abort (Logging level, const std::string &message, const char *file, uint32_t line, const char *func) noexcept;
45void logging (Logging level, const std::string &message, const char *file, uint32_t line, const char *func) noexcept;
46void logging_debug (const char *cond, const std::string &message) noexcept;
47void stdio_flush (uint8 code, const String &txt) noexcept;
48extern bool logging_fatal_warnings;
49
50bool logging_configure (bool to_file, Logging level = Logging (-1));
51
53inline bool ASE_ALWAYS_INLINE ASE_PURE
58
60template<class ...Args> inline void ASE_ALWAYS_INLINE
61debug (const char *cond, const char *format, const Args &...args)
62{
63#ifndef NDEBUG
64 if (debug_enabled())
65 {
67 logging_debug (cond, string_format (format, args...));
68 }
69#endif
70}
71
73template<class ...Args> inline void ASE_ALWAYS_INLINE
74info (const char *format, const Args &...args)
75{
76 logging (INFO, string_format (format, args...), nullptr, -1, nullptr);
77}
78
80template<class ...Args> inline void ASE_ALWAYS_INLINE
81diag (const char *format, const Args &...args)
82{
83#ifndef NDEBUG
84 logging_debug (nullptr, string_format (format, args...));
85#endif
86}
87
89template<class ...Args> void
90fatal_error (const LogFormat &format, const Args &...args)
91{
92 logging_abort (FATAL, string_format (format.cstr, args...),
93 format.location.file_name(), format.location.line(), format.location.function_name());
94}
95
97template<class ...Args> void
98warning (const char *format, const Args &...args)
99{
100 logging (WARN, string_format (format, args...), nullptr, -1, nullptr);
101}
102
104template<class... Args> void
105printout (const char *format, const Args &...args)
106{
107 stdio_flush ('o', string_format (format, args...));
108}
109
111template<class... Args> void
112printerr (const char *format, const Args &...args)
113{
114 stdio_flush ('e', string_format (format, args...));
115}
116
117} // Ase
T current(T... args)
#define ASE_UNLIKELY(expr)
Compiler hint to optimize for expr evaluating to false.
Definition cxxaux.hh:46
The Anklang C++ API namespace.
Definition api.hh:9
std::string string_format(const char *format, const Args &...args) __attribute__((__format__(__printf__
Format a string similar to sprintf(3) with support for std::string and std::ostringstream convertible...
void stdio_flush(uint8 code, const String &txt) noexcept
Handle stdout and stderr printing with flushing.
Definition logging.cc:54
uint8_t uint8
An 8-bit unsigned integer.
Definition cxxaux.hh:22
void logging_debug(const char *cond, const std::string &message) noexcept
Print a debug/diag message, called from Ase::debug().
Definition logging.cc:536
uint64_t timestamp_now()
Current time in µseconds.
Definition logging.cc:63
bool ase_debugging_enabled
Flag to optimize checks for debugging.
Definition logging.cc:278
bool debug_enabled() ASE_PURE
Check if any kind of debugging is enabled by $ASE_DEBUG.
Definition logging.hh:54
bool logging_fatal_warnings
Global flag to cause the program to abort on warnings.
Definition logging.cc:281
std::string String
Convenience alias for std::string.
Definition cxxaux.hh:35
bool debug_key_enabled(const char *conditional) noexcept
Check if conditional is enabled by $ASE_DEBUG.
Definition logging.cc:297
typedef uint64_t
Wrap a string together with its source code location.
Definition logging.hh:19