Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 1 | /* |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 ARM Limited. |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 24 | #ifndef ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H |
| 25 | #define ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 26 | |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 27 | #include "arm_compute/core/Error.h" |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/utils/logging/Helpers.h" |
| 29 | #include "arm_compute/core/utils/logging/Types.h" |
| 30 | |
| 31 | #include <chrono> |
| 32 | #include <ctime> |
| 33 | #include <string> |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 34 | #ifndef NO_MULTI_THREADING |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 35 | #include <thread> |
Georgios Pinitas | e874ef9 | 2019-09-09 17:40:33 +0100 | [diff] [blame] | 36 | #endif /* NO_MULTI_THREADING */ |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 37 | |
| 38 | namespace arm_compute |
| 39 | { |
| 40 | namespace logging |
| 41 | { |
| 42 | /** Log message decorator interface */ |
| 43 | class IDecorator |
| 44 | { |
| 45 | public: |
| 46 | /** Default Destructor */ |
| 47 | virtual ~IDecorator() = default; |
| 48 | /** Decorates log message |
| 49 | * |
| 50 | * @param[in] log_msg Log message to decorate |
| 51 | */ |
| 52 | virtual void decorate(LogMsg &log_msg) = 0; |
| 53 | }; |
| 54 | |
| 55 | /** String Decorator |
| 56 | * |
| 57 | * Appends a user defined string in the log message |
| 58 | */ |
| 59 | class StringDecorator : public IDecorator |
| 60 | { |
| 61 | public: |
| 62 | /** Defaults constructor |
| 63 | * |
| 64 | * @param str Sting to append |
| 65 | */ |
| 66 | StringDecorator(const std::string &str) |
| 67 | : _str(str) |
| 68 | { |
| 69 | _str = angle_wrap_value(str); |
| 70 | } |
| 71 | |
| 72 | // Inherited methods overridden: |
| 73 | void decorate(LogMsg &log_msg) override |
| 74 | { |
| 75 | log_msg.raw_ += _str; |
| 76 | } |
| 77 | |
| 78 | private: |
| 79 | std::string _str; |
| 80 | }; |
| 81 | |
| 82 | /** Date Decorator |
| 83 | * |
| 84 | * Appends the date and time in the log message |
| 85 | */ |
| 86 | class DateDecorator : public IDecorator |
| 87 | { |
| 88 | public: |
| 89 | // Inherited methods overridden: |
| 90 | void decorate(LogMsg &log_msg) override |
| 91 | { |
| 92 | log_msg.raw_ += angle_wrap_value(get_time()); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | /** Gets current system local time |
| 97 | * |
| 98 | * @return Local time |
| 99 | */ |
| 100 | std::string get_time() |
| 101 | { |
| 102 | auto now = std::chrono::system_clock::now(); |
| 103 | auto time = std::chrono::system_clock::to_time_t(now); |
| 104 | |
| 105 | // TODO: use put_time for gcc > 4.9 |
| 106 | char buf[100] = { 0 }; |
| 107 | std::strftime(buf, sizeof(buf), "%d-%m-%Y %I:%M:%S", std::localtime(&time)); |
| 108 | return buf; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | /** Thread ID Decorator |
| 113 | * |
| 114 | * Appends the thread ID in the log message |
| 115 | */ |
| 116 | class ThreadIdDecorator : public IDecorator |
| 117 | { |
| 118 | public: |
| 119 | // Inherited methods overridden: |
| 120 | void decorate(LogMsg &log_msg) override |
| 121 | { |
| 122 | #ifndef NO_MULTI_THREADING |
| 123 | log_msg.raw_ += angle_wrap_value(std::this_thread::get_id()); |
Michalis Spyrou | 6bff195 | 2019-10-02 17:22:11 +0100 | [diff] [blame] | 124 | #else /* NO_MULTI_THREADING */ |
| 125 | ARM_COMPUTE_UNUSED(log_msg); |
Georgios Pinitas | 7d3d1b9 | 2017-10-12 17:34:20 +0100 | [diff] [blame] | 126 | #endif /* NO_MULTI_THREADING */ |
| 127 | } |
| 128 | }; |
| 129 | |
| 130 | /** Log Level Decorator |
| 131 | * |
| 132 | * Appends the logging level in the log message |
| 133 | */ |
| 134 | class LogLevelDecorator : public IDecorator |
| 135 | { |
| 136 | public: |
| 137 | // Inherited methods overridden: |
| 138 | void decorate(LogMsg &log_msg) override |
| 139 | { |
| 140 | log_msg.raw_ += angle_wrap_value(string_from_log_level(log_msg.log_level_)); |
| 141 | } |
| 142 | }; |
| 143 | } // namespace logging |
| 144 | } // namespace arm_compute |
Michalis Spyrou | f464337 | 2019-11-29 16:17:13 +0000 | [diff] [blame] | 145 | #endif /* ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H */ |