| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192 |
- //
- // c_logger.hpp
- // logging
- //
- // Created by Sam Jaffe on 4/1/19.
- //
- #pragma once
- #include <cstdarg>
- #include <cstdlib>
- #include <cstring>
- #include <memory>
- #include <string>
- #include "logger_fwd.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)
- namespace logging {
- class logger_impl;
-
- class c_logger {
- public:
- static const size_t LOGF_MAX_SIZE = 2048;
- public:
- inline void tracef(char const* fmt, ...) { VA_LOG(LTRACE); }
- inline void tracenf(size_t n, char const* fmt, ...) { VA_LOGN(LTRACE, n); }
- inline void trace(char const* msg) { log(LTRACE, msg); }
- inline void trace(std::string const& msg) { log(LTRACE, msg); }
-
- inline void debugf(char const* fmt, ...) { VA_LOG(LDEBUG); }
- inline void debugnf(size_t n, char const* fmt, ...) { VA_LOGN(LDEBUG, n); }
- inline void debug(char const* msg) { log(LDEBUG, msg); }
- inline void debug(std::string const& msg) { log(LDEBUG, msg); }
-
- inline void infof(char const* fmt, ...) { VA_LOG(LINFO); }
- inline void infonf(size_t n, char const* fmt, ...) { VA_LOGN(LINFO, n); }
- inline void info(char const* msg) { log(LINFO, msg); }
- inline void info(std::string const& msg) { log(LINFO, msg); }
-
- inline void warnf(char const* fmt, ...) { VA_LOG(LWARNING); }
- inline void warnnf(size_t n, char const* fmt, ...) { VA_LOGN(LWARNING, n); }
- inline void warn(char const* msg) { log(LWARNING, msg); }
- inline void warn(std::string const& msg) { log(LWARNING, msg); }
-
- inline void errorf(char const* fmt, ...) { VA_LOG(LERROR); }
- inline void errornf(size_t n, char const* fmt, ...) { VA_LOGN(LERROR, n); }
- inline void error(char const* msg) { log(LERROR, msg); }
- inline void error(std::string const& msg) { log(LERROR, msg); }
-
- inline void criticalf(char const* fmt, ...) { VA_LOG(LCRITICAL); }
- inline void criticalnf(size_t n, char const* fmt, ...) { VA_LOGN(LCRITICAL, n); }
- inline void critical(char const* msg) { log(LCRITICAL, msg); }
- inline void critical(std::string const& msg) { log(LCRITICAL, msg); }
-
- inline void fatalf(char const* fmt, ...) { VA_LOG(LFATAL); }
- inline void fatalnf(size_t n, char const* fmt, ...) { VA_LOGN(LFATAL, n); }
- inline void fatal(char const* msg) { log(LFATAL, msg); }
- inline void fatal(std::string const& msg) { log(LFATAL, msg); }
-
- template <typename... Args>
- inline void log(log_level ll, std::string const & interp, Args && ...args);
-
- void flush();
-
- ~c_logger();
-
- protected:
- friend class manager;
- c_logger(std::string const & name, std::shared_ptr<logger_impl> impl);
- private:
- void vlognf(log_level, size_t, char const*, va_list);
- void log(log_level, std::string const&);
-
- private:
- log_level min_level_;
- std::string logger_name_;
- std::shared_ptr<logger_impl> impl_;
- };
- }
- #undef VA_LOG
- #undef LA_LOGN
|