pianod2
multisource multiuser scriptable networked music player
logging.h
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <config.h>
12 
13 #include <cstring>
14 #include <cstdarg>
15 #include <iostream>
16 #include <utility>
17 
18 enum class LogType {
19  ERROR = 0, // Nonmaskable
20  LOG_000 = 0x01, // Corresponding to response message groups
21  LOG_100 = 0x02,
22  LOG_200 = 0x04,
23  LOG_300 = 0x08,
24  LOG_400 = 0x10,
25  LOG_500 = 0x20,
26  // 0x40 not used yet
27  GENERAL = 0x80,
28  // 0x100 EVENT no longer used
29  WARNING = 0x200,
30  QUEUE = 0x400,
31  // 0x800 not used yet
32  USERACTION = 0x1000,
33  PROTOCOL = 0x2000,
34  ALLOCATIONS = 0x4000,
35  CACHES = 0x8000,
36  TUNING = 0x10000,
37  BIASING = 0x20000,
38  METADATA = 0x40000,
39  AUDIO = 0x80000,
40  PANDORA = 0x100000,
41  FILESYSTEM = 0x2000000
42 };
43 using Log = LogType;
44 
45 // Logging
46 extern LogType flogging_level;
47 extern bool flog_include_timestamp;
48 extern void set_logging (LogType logtype);
49 extern LogType get_logging();
50 extern void flog_timestamp ();
51 
53 inline constexpr LogType operator |(const LogType a, const LogType b) {
54  return static_cast <LogType> (static_cast <unsigned> (a) | static_cast <unsigned> (b));
55 }
56 
58 inline constexpr LogType operator &(const LogType a, const LogType b) {
59  return static_cast <LogType> (static_cast <unsigned> (a) & static_cast <unsigned> (b));
60 }
61 
63 inline constexpr LogType operator ^(const LogType a, const LogType b) {
64  return static_cast <LogType> (static_cast <unsigned> (a) ^ static_cast <unsigned> (b));
65 }
66 
68 inline constexpr LogType operator -(const LogType a, const LogType b) {
69  return static_cast <LogType> (static_cast <unsigned> (a) & ~static_cast <unsigned> (b));
70 }
71 
72 
73 static inline const char *trim_filename (const char *path) {
74  const char *name = strrchr (path, '/');
75  return name ? name + 1 : path;
76 }
77 
78 // LOG_WHERE macro inserts file, line and function for C++ flog calls
79 // when in debug mode. In release mode, file, line, and function are omitted.
80 // LOG_FUNCTION Is similar, but function name is retained in release mode.
81 // Unlike Football, where the FB_WHERE macro is required, these are optional.
82 #ifdef NDEBUG
83 #define LOG_WHERE(level) (level)
84 #define LOG_FUNCTION(level) (level), __func__, ": "
85 #else
86 #define LOG_WHERE(level) (level), trim_filename(__FILE__), ":", __LINE__, " ", __func__, " (", #level, "): "
87 #define LOG_FUNCTION(level) (level), trim_filename(__FILE__), ":", __LINE__, " ", __func__, " (", #level, "): "
88 #endif
89 
91 inline void flog_details (void) {
92  std::clog << std::endl;
93 };
94 
95 inline bool logging_enabled (LogType level) {
96  return (level == Log::ERROR || static_cast <unsigned> (flogging_level & level));
97 }
98 
100 template<typename OutputItem, typename... MoreItems>
101 void flog_details (OutputItem message, MoreItems&&... more) {
102  std::clog << message;
103  flog_details (std::forward <MoreItems> (more)...);
104 }
105 
108 template<typename... MoreItems>
109 void flog (LogType level, MoreItems&&... more) {
110  if (logging_enabled (level)) {
112  flog_timestamp();
113  flog_details (std::forward<MoreItems> (more)...);
114  std::clog.flush();
115  }
116 }
117 
void flog(LogType level, MoreItems &&... more)
Log messages when their logging category is enabled.
Definition: logging.h:109
constexpr LogType operator-(const LogType a, const LogType b)
Allow LogType values to have bits removed..
Definition: logging.h:68
void flog_timestamp()
Logging helper subroutine to format and emit the time.
Definition: logging.cpp:28
constexpr LogType operator|(const LogType a, const LogType b)
Allow LogType values to be OR'ed together.
Definition: logging.h:53
static const char * trim_filename(const char *path)
Definition: logging.h:73
bool logging_enabled(LogType level)
Definition: logging.h:95
void flog_details(void)
Implement empty argument list for flogging.
Definition: logging.h:91
constexpr LogType operator^(const LogType a, const LogType b)
Allow LogType values to be toggled.
Definition: logging.h:63
LogType flogging_level
Definition: logging.cpp:13
bool flog_include_timestamp
Definition: logging.cpp:14
void set_logging(LogType logtype)
Set the logging selections.
Definition: logging.cpp:18
constexpr LogType operator&(const LogType a, const LogType b)
Allow LogType values to be AND'ed apart.
Definition: logging.h:58
LogType get_logging()
Definition: logging.cpp:22
LogType
Definition: logging.h:18
@ TUNING
Tables of station rating averages for automatic station selection.
@ BIASING
Tables of pick probabilities after applying ratings & recent biases.