From 7d3d1b923b7793f1cf5e29c78bfda2582522cf25 Mon Sep 17 00:00:00 2001 From: Georgios Pinitas Date: Thu, 12 Oct 2017 17:34:20 +0100 Subject: COMPMID-619: Logging interface Change-Id: I73d1433ee7a682aeabb7540aa2ea1f6564f90aae Reviewed-on: http://mpd-gerrit.cambridge.arm.com/91775 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- arm_compute/core/utils/logging/Logger.h | 176 ++++++++++++++++++++++++++++++++ 1 file changed, 176 insertions(+) create mode 100644 arm_compute/core/utils/logging/Logger.h (limited to 'arm_compute/core/utils/logging/Logger.h') diff --git a/arm_compute/core/utils/logging/Logger.h b/arm_compute/core/utils/logging/Logger.h new file mode 100644 index 0000000000..eb9bdd2e36 --- /dev/null +++ b/arm_compute/core/utils/logging/Logger.h @@ -0,0 +1,176 @@ +/* + * Copyright (c) 2017 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_LOGGING_LOGGER_H__ +#define __ARM_COMPUTE_LOGGING_LOGGER_H__ + +#include "arm_compute/core/utils/logging/Helpers.h" +#include "arm_compute/core/utils/logging/IPrinter.h" +#include "arm_compute/core/utils/logging/LogMsgDecorators.h" +#include "arm_compute/core/utils/logging/Types.h" + +#include +#include +#include +#include + +namespace arm_compute +{ +namespace logging +{ +/** Logger class */ +class Logger +{ +public: + /** Default Constructor + * + * @param[in] name Name of the logger + * @param[in] log_level Logger log level + * @param[in] printer Printer to push the messages + */ + Logger(std::string name, LogLevel log_level, std::shared_ptr printer); + /** Default Constructor + * + * @param[in] name Name of the logger + * @param[in] log_level Logger log level + * @param[in] printers Printers to push the messages + */ + Logger(std::string name, LogLevel log_level, std::vector> printers = {}); + /** Default Constructor + * + * @param[in] name Name of the logger + * @param[in] log_level Logger log level + * @param[in] printers Printers to push the messages + * @param[in] decorators Message decorators, which append information in the logged message + */ + Logger(std::string name, + LogLevel log_level, + std::vector> printers, + std::vector> decorators); + /** Allow instances of this class to be moved */ + Logger(Logger &&) = default; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + Logger(const Logger &) = delete; + /** Prevent instances of this class from being copied (As this class contains pointers) */ + Logger &operator=(const Logger &) = delete; + /** Allow instances of this class to be moved */ + Logger &operator=(Logger &&) = default; + /** Logs a message + * + * @param[in] log_level Log level of the message + * @param[in] msg Message to log + */ + void log(LogLevel log_level, const std::string &msg); + /** Logs a formatted message + * + * @param[in] log_level Log level of the message + * @param[in] fmt Message format + * @param[in] args Message arguments + */ + template + void log(LogLevel log_level, const std::string &fmt, Ts &&... args); + /** Sets log level of the logger + * + * @warning Not thread-safe + * + * @param[in] log_level Log level to set + */ + void set_log_level(LogLevel log_level); + /** Returns logger's log level + * + * @return Logger's log level + */ + LogLevel log_level() const; + /** Returns logger's name + * + * @return Logger's name + */ + std::string name() const; + /** Adds a printer to the logger + * + * @warning Not thread-safe + * + * @param[in] printer + */ + void add_printer(std::shared_ptr printer); + /** Adds a log message decorator to the logger + * + * @warning Not thread-safe + * + * @param[in] decorator + */ + void add_decorator(std::unique_ptr decorator); + +private: + /** Set default message decorators */ + void set_default_decorators(); + /** Checks if a message should be logged depending + * on the message log level and the loggers one + * + * @param[in] log_level Log level + * + * @return True if message should be logged else false + */ + bool is_loggable(LogLevel log_level); + /** Decorate log message + * + * @param[in] Log message to decorate + */ + void decorate_log_msg(LogMsg &msg); + /** Creates final log message by creating the prefix + * + * @param[in] str Log message + * @param[in] log_level Message's log level + * + * @return Final log message to print + */ + std::string create_log_msg(const std::string &str, LogLevel log_level); + /** Prints the message to all the printers + * + * @param[in] msg Message to print + */ + void print_all(const std::string &msg); + +private: + std::string _name; + LogLevel _log_level; + std::vector> _printers; + std::vector> _decorators; +}; + +template +inline void Logger::log(LogLevel log_level, const std::string &fmt, Ts &&... args) +{ + // Return if message shouldn't be logged + // i.e. if log level does not match the logger's + if(!is_loggable(log_level)) + { + return; + } + + // Print message to all printers + print_all(create_log_msg(string_with_format(fmt, args...), log_level)); +} +} // namespace logging +} // namespace arm_compute +#endif /* __ARM_COMPUTE_LOGGING_LOGGER_H__ */ -- cgit v1.2.1