// // c_logger.hpp // logging // // Created by Sam Jaffe on 4/1/19. // #pragma once #include #include #include #include #include #include "level.h" #define VA_LOGN(level, size) \ do { \ va_list vargs; \ va_start(vargs, fmt); \ vlognf(level, size, fmt, vargs); \ va_end(vargs); \ } while (0) #define VA_LOG(level) VA_LOGN(level, c_logger::LOGF_MAX_SIZE) #define MAKE_LOGGER_FMT(lvl) \ inline void lvl##f(char const * fmt, ...) { VA_LOG(level::lvl); } \ inline void lvl##nf(size_t n, char const * fmt, ...) { \ VA_LOGN(level::lvl, n); \ } \ inline void lvl(char const * msg) { log(level::lvl, msg); } \ inline void lvl(std::string const & msg) { log(level::lvl, msg); } namespace logging { enum class level : int; class logger_impl; /** * A simple logger interface that is used for performing C-Style logging a la * printf()/nprintf(). * TODO: Add the option to provide location_info with the log_here macro * Unlink the logging::logger class, this class only provides access to its * logging methods through functions like warn(), error(), fatal(), etc. * Each of these functions corresponds to a logging::level enum, and * constructs its message by calling vsnprintf() under the hood. * Overrides for no formatting, std::string inputs, and explicitly sizing the * storage buffer are made available with the following function names e.g.: * error("This is a string") -> logs "This is a string" * error(std::string("This is a string")) -> logs "This is a string" * errorf("This is a %s", "string") -> logs "This is a string" * errornf(14, "This is a %s", "string") -> logs "This is a str" */ class c_logger { private: friend class manager; std::string logger_name_; std::shared_ptr impl_; public: static const size_t LOGF_MAX_SIZE = 2048; public: MAKE_LOGGER_FMT(trace) MAKE_LOGGER_FMT(debug) MAKE_LOGGER_FMT(info) MAKE_LOGGER_FMT(warn) MAKE_LOGGER_FMT(error) MAKE_LOGGER_FMT(critical) MAKE_LOGGER_FMT(fatal) void flush(); ~c_logger(); protected: c_logger(std::string const & name, std::shared_ptr impl); private: void vlognf(level, size_t, char const *, va_list); void log(level, std::string const &); }; } #undef VA_LOG #undef LA_LOGN