From a2ee029d0772e894911c84fb8a0cab5f253e145b Mon Sep 17 00:00:00 2001 From: Neale Ranns Date: Tue, 28 Nov 2017 22:29:13 -0800 Subject: VOM: logging, populate and stats fixes logging: allow a client to register a callback handler to recieve log messages that way the client can maintain a correctly sequenced log populate: fix the creation of interface and the setting of the handle stats: the reset promise idea is not defined behaviour. Use an eanble/disable command pair Change-Id: I347720bb65df2874c7619e722d593bc863ee2bf1 Signed-off-by: Neale Ranns --- src/vpp-api/vom/logger.hpp | 107 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 97 insertions(+), 10 deletions(-) (limited to 'src/vpp-api/vom/logger.hpp') diff --git a/src/vpp-api/vom/logger.hpp b/src/vpp-api/vom/logger.hpp index 941623e6679..6d2e3dd82f6 100644 --- a/src/vpp-api/vom/logger.hpp +++ b/src/vpp-api/vom/logger.hpp @@ -18,6 +18,7 @@ #include #include +#include #include "vom/enum_base.hpp" @@ -35,6 +36,11 @@ private: * Private constructor taking the value and the string name */ log_level_t(int v, const std::string& s); + + /* + * not allowed to construct + */ + log_level_t() = delete; }; /** @@ -45,14 +51,36 @@ class log_t { public: /** - * Construct a logger + * */ - log_t(); + class handler + { + public: + /** + * Default Constructor + */ + handler() = default; + + /** + * Default Destructor + */ + virtual ~handler() = default; + + /** + * Handle a log message + */ + virtual void handle_message(const std::string& file, + const int line, + const std::string& function, + const log_level_t& level, + const std::string& message) = 0; + }; /** - * Return the stream + * Construct a logger */ - std::ostream& stream(const char* file, int line, const log_level_t& level); + log_t(handler* h); + log_t(); /** * The configured level @@ -67,23 +95,82 @@ public: /** * set a file to receive the logging data */ - void set(const std::string& ofile); + void set(handler* h); + + /** + * An entry in the log + */ + class entry + { + public: + entry(const char* file, + const char* function, + int line, + const log_level_t& level); + ~entry(); + + std::stringstream& stream(); + + private: + const std::string m_file; + const std::string m_function; + const log_level_t m_level; + const int m_line; + + std::stringstream m_stream; + }; + /** + * Register a log handler to receive the log output + */ + void register_handler(handler& h); private: + void write(const std::string& file, + const int line, + const std::string& function, + const log_level_t& level, + const std::string& message); + /** * the configured logging level */ log_level_t m_level; /** - * Opened file for debugging + * Pointer to a registered handler. Null if no handler registerd */ - std::ofstream m_file_stream; + handler* m_handler; +}; +class file_handler : public log_t::handler +{ +public: + file_handler(const std::string& ofile); + ~file_handler(); + + virtual void handle_message(const std::string& file, + const int line, + const std::string& function, + const log_level_t& level, + const std::string& message); + +private: /** - * Pointer to the output stream + * Opened file for debugging */ - std::ostream* m_o_stream; + std::ofstream m_file_stream; +}; + +class cout_handler : public log_t::handler +{ +public: + cout_handler() = default; + ~cout_handler() = default; + virtual void handle_message(const std::string& file, + const int line, + const std::string& function, + const log_level_t& level, + const std::string& message); }; /** @@ -93,7 +180,7 @@ log_t& logger(); #define VOM_LOG(lvl) \ if (lvl >= logger().level()) \ - logger().stream(__FILE__, __LINE__, lvl) + log_t::entry(__FILE__, __FUNCTION__, __LINE__, lvl).stream() }; /* -- cgit 1.2.3-korg