1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* Copyright (c) 2017-2019 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 <memory>
#include <sstream>
#include <string>
#include <vector>
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> 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<std::shared_ptr<Printer>> 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<std::shared_ptr<Printer>> printers,
std::vector<std::unique_ptr<IDecorator>> 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 <typename... Ts>
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> printer);
/** Adds a log message decorator to the logger
*
* @warning Not thread-safe
*
* @param[in] decorator
*/
void add_decorator(std::unique_ptr<IDecorator> 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<std::shared_ptr<Printer>> _printers;
std::vector<std::unique_ptr<IDecorator>> _decorators;
};
template <typename... Ts>
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 */
|