Compute Library
 21.02
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)
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  */
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());
124 #else /* NO_MULTI_THREADING */
125  ARM_COMPUTE_UNUSED(log_msg);
126 #endif /* NO_MULTI_THREADING */
127  }
128 };
129 
130 /** Log Level Decorator
131  *
132  * Appends the logging level in the log message
133  */
135 {
136 public:
137  // Inherited methods overridden:
138  void decorate(LogMsg &log_msg) override
139  {
141  }
142 };
143 } // namespace logging
144 } // namespace arm_compute
145 #endif /* ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H */
void decorate(LogMsg &log_msg) override
Decorates log message.
std::string angle_wrap_value(const T &val)
Wraps a value with angles and returns the string.
Definition: Helpers.h:62
Copyright (c) 2017-2021 Arm Limited.
void decorate(LogMsg &log_msg) override
Decorates log message.
const std::string & string_from_log_level(LogLevel log_level)
Translates a given log level to a string.
Definition: Helpers.cpp:31
StringDecorator(const std::string &str)
Defaults constructor.
virtual ~IDecorator()=default
Default Destructor.
LogLevel log_level_
Logging level.
Definition: Types.h:64
#define ARM_COMPUTE_UNUSED(...)
To avoid unused variables warnings.
Definition: Error.h:152
virtual void decorate(LogMsg &log_msg)=0
Decorates log message.
void decorate(LogMsg &log_msg) override
Decorates log message.
void decorate(LogMsg &log_msg) override
Decorates log message.
std::string raw_
Log message.
Definition: Types.h:62
Log message decorator interface.