| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- //
- // logger.hpp
- // logger
- //
- // Created by Sam Jaffe on 9/3/16.
- //
- #pragma once
- //
- // Logger.hpp
- // DanmakuRPG
- //
- // Created by Sam Jaffe on 7/18/15.
- // Copyright (c) 2015 Sam Jaffe. All rights reserved.
- //
- #pragma once
- #include <cstdarg>
- #include <cstdlib>
- #include <cstring>
- #include <memory>
- #include <string>
- #include "logger_fwd.hpp"
- #include "format.hpp"
- #define log_message( logger, level, ... ) logger.log( level, { __FILE__, __LINE__, STRING( FUNCTION ) }, __VA_ARGS__ )
- namespace logging {
- class logger_impl;
- typedef logger_impl& (*_binding)(void);
- bool bind_logger_impl(_binding impl);
-
- const char* level_header(log_level);
-
- class logger {
- public:
- static logger& instance();
- static logger& instance(std::string const & key);
- public:
- template <typename... Args>
- inline void log(log_level ll,
- std::string const & interp, Args && ...args) {
- log( ll, location_info{}, interp, std::forward<Args>(args)... );
- }
-
- template <typename... Args>
- inline void log(log_level ll, location_info info,
- std::string const & interp, Args && ...args) {
- if ( should_log( ll ) ) {
- log( ll, info, format_msg( interp, std::forward<Args>(args)... ) );
- }
- }
-
- void flush();
-
- ~logger();
- private:
- bool should_log( log_level ) const;
- void log( log_level ll, location_info info, std::string const& );
-
- explicit logger(std::string const & name = "");
- logger(logger const&);
- logger& operator=(logger const&);
-
- log_level min_level_;
- std::string const logger_name_;
- logger_impl& impl_;
- };
-
- class logger_impl {
- public:
- bool should_log( log_level ll ) const;
- virtual void write( logpacket const & pkt ) = 0;
- virtual void flush() = 0;
- virtual ~logger_impl() = default;
- private:
- log_level min_log_level;
- };
- }
|