| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2.1 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Lesser General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Lesser General Public |
| 15 | * License along with this library; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 | */ |
| 18 |
| 19 | #ifndef VDEBUG_H |
| 20 | #define VDEBUG_H |
| 21 |
| 22 | #include "config.h" |
| 23 |
| 24 | #ifdef LOTTIE_LOGGING_SUPPORT |
| 25 |
| 26 | #include <cstdint> |
| 27 | #include <iosfwd> |
| 28 | #include <memory> |
| 29 | #include <string> |
| 30 | #include <type_traits> |
| 31 |
| 32 | enum class LogLevel : uint8_t { INFO, WARN, CRIT, OFF }; |
| 33 |
| 34 | class VDebug { |
| 35 | public: |
| 36 | VDebug(); |
| 37 | VDebug& debug() { return *this; } |
| 38 | VDebug(LogLevel level, char const* file, char const* function, |
| 39 | uint32_t line); |
| 40 | ~VDebug(); |
| 41 |
| 42 | VDebug(VDebug&&) = default; |
| 43 | VDebug& operator=(VDebug&&) = default; |
| 44 |
| 45 | void stringify(std::ostream& os); |
| 46 |
| 47 | VDebug& operator<<(char arg); |
| 48 | VDebug& operator<<(int32_t arg); |
| 49 | VDebug& operator<<(uint32_t arg); |
| 50 | // VDebug& operator<<(int64_t arg); |
| 51 | // VDebug& operator<<(uint64_t arg); |
| 52 |
| 53 | VDebug& operator<<(long arg); |
| 54 | VDebug& operator<<(unsigned long arg); |
| 55 | VDebug& operator<<(double arg); |
| 56 | VDebug& operator<<(std::string const& arg); |
| 57 |
| 58 | template <size_t N> |
| 59 | VDebug& operator<<(const char (&arg)[N]) |
| 60 | { |
| 61 | encode(string_literal_t(arg)); |
| 62 | return *this; |
| 63 | } |
| 64 |
| 65 | template <typename Arg> |
| 66 | typename std::enable_if<std::is_same<Arg, char const*>::value, |
| 67 | VDebug&>::type |
| 68 | operator<<(Arg const& arg) |
| 69 | { |
| 70 | encode(arg); |
| 71 | return *this; |
| 72 | } |
| 73 |
| 74 | template <typename Arg> |
| 75 | typename std::enable_if<std::is_same<Arg, char*>::value, VDebug&>::type |
| 76 | operator<<(Arg const& arg) |
| 77 | { |
| 78 | encode(arg); |
| 79 | return *this; |
| 80 | } |
| 81 |
| 82 | struct string_literal_t { |
| 83 | explicit string_literal_t(char const* s) : m_s(s) {} |
| 84 | char const* m_s; |
| 85 | }; |
| 86 |
| 87 | private: |
| 88 | char* buffer(); |
| 89 |
| 90 | template <typename Arg> |
| 91 | void encode(Arg arg); |
| 92 |
| 93 | template <typename Arg> |
| 94 | void encode(Arg arg, uint8_t type_id); |
| 95 |
| 96 | void encode(char* arg); |
| 97 | void encode(char const* arg); |
| 98 | void encode(string_literal_t arg); |
| 99 | void encode_c_string(char const* arg, size_t length); |
| 100 | void resize_buffer_if_needed(size_t additional_bytes); |
| 101 | void stringify(std::ostream& os, char* start, char const* const end); |
| 102 |
| 103 | private: |
| 104 | size_t m_bytes_used{0}; |
| 105 | size_t m_buffer_size{0}; |
| 106 | std::unique_ptr<char[]> m_heap_buffer; |
| 107 | bool m_logAll; |
| 108 | char m_stack_buffer[256 - sizeof(bool) - 2 * sizeof(size_t) - |
| 109 | sizeof(decltype(m_heap_buffer)) - 8 /* Reserved */]; |
| 110 | }; |
| 111 |
| 112 | struct VDebugServer { |
| 113 | /* |
| 114 | * Ideally this should have been operator+= |
| 115 | * Could not get that to compile, so here we are... |
| 116 | */ |
| 117 | bool operator==(VDebug&); |
| 118 | }; |
| 119 |
| 120 | void set_log_level(LogLevel level); |
| 121 |
| 122 | bool is_logged(LogLevel level); |
| 123 |
| 124 | /* |
| 125 | * Non guaranteed logging. Uses a ring buffer to hold log lines. |
| 126 | * When the ring gets full, the previous log line in the slot will be dropped. |
| 127 | * Does not block producer even if the ring buffer is full. |
| 128 | * ring_buffer_size_mb - LogLines are pushed into a mpsc ring buffer whose size |
| 129 | * is determined by this parameter. Since each LogLine is 256 bytes, |
| 130 | * ring_buffer_size = ring_buffer_size_mb * 1024 * 1024 / 256 |
| 131 | */ |
| 132 | struct NonGuaranteedLogger { |
| 133 | NonGuaranteedLogger(uint32_t ring_buffer_size_mb_) |
| 134 | : ring_buffer_size_mb(ring_buffer_size_mb_) |
| 135 | { |
| 136 | } |
| 137 | uint32_t ring_buffer_size_mb; |
| 138 | }; |
| 139 |
| 140 | /* |
| 141 | * Provides a guarantee log lines will not be dropped. |
| 142 | */ |
| 143 | struct GuaranteedLogger { |
| 144 | }; |
| 145 |
| 146 | /* |
| 147 | * Ensure initialize() is called prior to any log statements. |
| 148 | * log_directory - where to create the logs. For example - "/tmp/" |
| 149 | * log_file_name - root of the file name. For example - "nanolog" |
| 150 | * This will create log files of the form - |
| 151 | * /tmp/nanolog.1.txt |
| 152 | * /tmp/nanolog.2.txt |
| 153 | * etc. |
| 154 | * log_file_roll_size_mb - mega bytes after which we roll to next log file. |
| 155 | */ |
| 156 | void initialize(GuaranteedLogger gl, std::string const& log_directory, |
| 157 | std::string const& log_file_name, |
| 158 | uint32_t log_file_roll_size_mb); |
| 159 | void initialize(NonGuaranteedLogger ngl, std::string const& log_directory, |
| 160 | std::string const& log_file_name, |
| 161 | uint32_t log_file_roll_size_mb); |
| 162 |
| 163 | #define VDEBUG_LOG(LEVEL) \ |
| 164 | VDebugServer() == VDebug(LEVEL, __FILE__, __func__, __LINE__).debug() |
| 165 | #define vDebug is_logged(LogLevel::INFO) && VDEBUG_LOG(LogLevel::INFO) |
| 166 | #define vWarning is_logged(LogLevel::WARN) && VDEBUG_LOG(LogLevel::WARN) |
| 167 | #define vCritical is_logged(LogLevel::CRIT) && VDEBUG_LOG(LogLevel::CRIT) |
| 168 |
| 169 | #else |
| 170 |
| 171 | struct VDebug |
| 172 | { |
| 173 | template<typename Args> |
| 174 | VDebug& operator<<(const Args &){return *this;} |
| 175 | }; |
| 176 |
| 177 | #define vDebug VDebug() |
| 178 | #define vWarning VDebug() |
| 179 | #define vCritical VDebug() |
| 180 |
| 181 | #endif //LOTTIE_LOGGING_SUPPORT |
| 182 |
| 183 | #endif // VDEBUG_H |
| 184 |