Anklang-0.3.0.dev835+g24d8ae08 anklang-0.3.0.dev835+g24d8ae08
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 (const std::string &log_file_ident, Logging level = Logging (-1));
51
53void logging_prune_old_logs (double age_seconds);
54
56inline bool ASE_ALWAYS_INLINE ASE_PURE
61
63template<class ...Args> inline void ASE_ALWAYS_INLINE
64debug (const char *cond, const char *format, const Args &...args)
65{
66#ifndef NDEBUG
67 if (debug_enabled())
68 {
70 logging_debug (cond, string_format (format, args...));
71 }
72#endif
73}
74
76template<class ...Args> inline void ASE_ALWAYS_INLINE
77info (const char *format, const Args &...args)
78{
79 logging (INFO, string_format (format, args...), nullptr, -1, nullptr);
80}
81
83template<class ...Args> inline void ASE_ALWAYS_INLINE
84diag (const char *format, const Args &...args)
85{
86#ifndef NDEBUG
87 logging_debug (nullptr, string_format (format, args...));
88#endif
89}
90
92template<class ...Args> void
93fatal_error (const LogFormat &format, const Args &...args)
94{
95 logging_abort (FATAL, string_format (format.cstr, args...),
96 format.location.file_name(), format.location.line(), format.location.function_name());
97}
98
100template<class ...Args> void
101warning (const char *format, const Args &...args)
102{
103 logging (WARN, string_format (format, args...), nullptr, -1, nullptr);
104}
105
107template<class... Args> void
108printout (const char *format, const Args &...args)
109{
110 stdio_flush ('o', string_format (format, args...));
111}
112
114template<class... Args> void
115printerr (const char *format, const Args &...args)
116{
117 stdio_flush ('e', string_format (format, args...));
118}
119
120} // 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_prune_old_logs(double age_seconds)
Delete log files older than age seconds.
Definition logging.cc:233
void logging_debug(const char *cond, const std::string &message) noexcept
Print a debug/diag message, called from Ase::debug().
Definition logging.cc:559
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:301
bool debug_enabled() ASE_PURE
Check if any kind of debugging is enabled by $ASE_DEBUG.
Definition logging.hh:57
bool logging_fatal_warnings
Global flag to cause the program to abort on warnings.
Definition logging.cc:304
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:320
typedef uint64_t
Wrap a string together with its source code location.
Definition logging.hh:19