Compute Library
 23.11
LogMsgDecorators.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016-2019 Arm Limited.
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  */
24 #ifndef ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H
25 #define ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H
26 
27 #include "arm_compute/core/Error.h"
30 
31 #include <chrono>
32 #include <ctime>
33 #include <string>
34 #ifndef NO_MULTI_THREADING
35 #include <thread>
36 #endif /* NO_MULTI_THREADING */
37 
38 namespace arm_compute
39 {
40 namespace logging
41 {
42 /** Log message decorator interface */
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  */
60 {
61 public:
62  /** Defaults constructor
63  *
64  * @param str Sting to append
65  */
66  StringDecorator(const std::string &str) : _str(str)
67  {
68  _str = angle_wrap_value(str);
69  }
70 
71  // Inherited methods overridden:
72  void decorate(LogMsg &log_msg) override
73  {
74  log_msg.raw_ += _str;
75  }
76 
77 private:
78  std::string _str;
79 };
80 
81 /** Date Decorator
82  *
83  * Appends the date and time in the log message
84  */
85 class DateDecorator : public IDecorator
86 {
87 public:
88  // Inherited methods overridden:
89  void decorate(LogMsg &log_msg) override
90  {
91  log_msg.raw_ += angle_wrap_value(get_time());
92  }
93 
94 private:
95  /** Gets current system local time
96  *
97  * @return Local time
98  */
99  std::string get_time()
100  {
101  auto now = std::chrono::system_clock::now();
102  auto time = std::chrono::system_clock::to_time_t(now);
103 
104  // TODO: use put_time for gcc > 4.9
105  char buf[100] = {0};
106  std::strftime(buf, sizeof(buf), "%d-%m-%Y %I:%M:%S", std::localtime(&time));
107  return buf;
108  }
109 };
110 
111 /** Thread ID Decorator
112  *
113  * Appends the thread ID in the log message
114  */
116 {
117 public:
118  // Inherited methods overridden:
119  void decorate(LogMsg &log_msg) override
120  {
121 #ifndef NO_MULTI_THREADING
122  log_msg.raw_ += angle_wrap_value(std::this_thread::get_id());
123 #else /* NO_MULTI_THREADING */
124  ARM_COMPUTE_UNUSED(log_msg);
125 #endif /* NO_MULTI_THREADING */
126  }
127 };
128 
129 /** Log Level Decorator
130  *
131  * Appends the logging level in the log message
132  */
134 {
135 public:
136  // Inherited methods overridden:
137  void decorate(LogMsg &log_msg) override
138  {
140  }
141 };
142 } // namespace logging
143 } // namespace arm_compute
144 #endif /* ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H */
arm_compute::logging::DateDecorator
Date Decorator.
Definition: LogMsgDecorators.h:85
arm_compute::logging::LogMsg
Log message.
Definition: Types.h:44
arm_compute::logging::StringDecorator
String Decorator.
Definition: LogMsgDecorators.h:59
arm_compute::logging::LogMsg::raw_
std::string raw_
Log message.
Definition: Types.h:60
arm_compute::logging::IDecorator::decorate
virtual void decorate(LogMsg &log_msg)=0
Decorates log message.
arm_compute::logging::StringDecorator::StringDecorator
StringDecorator(const std::string &str)
Defaults constructor.
Definition: LogMsgDecorators.h:66
arm_compute::logging::ThreadIdDecorator::decorate
void decorate(LogMsg &log_msg) override
Decorates log message.
Definition: LogMsgDecorators.h:119
caffe_mnist_image_extractor.str
str
Definition: caffe_mnist_image_extractor.py:21
Error.h
arm_compute::logging::StringDecorator::decorate
void decorate(LogMsg &log_msg) override
Decorates log message.
Definition: LogMsgDecorators.h:72
arm_compute::logging::DateDecorator::decorate
void decorate(LogMsg &log_msg) override
Decorates log message.
Definition: LogMsgDecorators.h:89
ARM_COMPUTE_UNUSED
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:151
Types.h
arm_compute::logging::LogLevelDecorator
Log Level Decorator.
Definition: LogMsgDecorators.h:133
arm_compute
Copyright (c) 2017-2023 Arm Limited.
Definition: introduction.dox:24
arm_compute::logging::LogMsg::log_level_
LogLevel log_level_
Logging level.
Definition: Types.h:62
arm_compute::logging::angle_wrap_value
std::string angle_wrap_value(const T &val)
Wraps a value with angles and returns the string.
Definition: Helpers.h:63
Helpers.h
arm_compute::logging::IDecorator
Log message decorator interface.
Definition: LogMsgDecorators.h:43
arm_compute::logging::LogLevelDecorator::decorate
void decorate(LogMsg &log_msg) override
Decorates log message.
Definition: LogMsgDecorators.h:137
arm_compute::logging::ThreadIdDecorator
Thread ID Decorator.
Definition: LogMsgDecorators.h:115
arm_compute::logging::IDecorator::~IDecorator
virtual ~IDecorator()=default
Default Destructor.
arm_compute::logging::string_from_log_level
const std::string & string_from_log_level(LogLevel log_level)
Translates a given log level to a string.
Definition: Helpers.cpp:31