blob: 32cb977bf28c99162a4db5921167e98384e371e8 [file] [log] [blame]
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +01001/*
2 * Copyright (c) 2016, 2017 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/utils/logging/Helpers.h"
28#include "arm_compute/core/utils/logging/Types.h"
29
30#include <chrono>
31#include <ctime>
32#include <string>
33#include <thread>
34
35namespace arm_compute
36{
37namespace logging
38{
39/** Log message decorator interface */
40class IDecorator
41{
42public:
43 /** Default Destructor */
44 virtual ~IDecorator() = default;
45 /** Decorates log message
46 *
47 * @param[in] log_msg Log message to decorate
48 */
49 virtual void decorate(LogMsg &log_msg) = 0;
50};
51
52/** String Decorator
53 *
54 * Appends a user defined string in the log message
55 */
56class StringDecorator : public IDecorator
57{
58public:
59 /** Defaults constructor
60 *
61 * @param str Sting to append
62 */
63 StringDecorator(const std::string &str)
64 : _str(str)
65 {
66 _str = angle_wrap_value(str);
67 }
68
69 // Inherited methods overridden:
70 void decorate(LogMsg &log_msg) override
71 {
72 log_msg.raw_ += _str;
73 }
74
75private:
76 std::string _str;
77};
78
79/** Date Decorator
80 *
81 * Appends the date and time in the log message
82 */
83class DateDecorator : public IDecorator
84{
85public:
86 // Inherited methods overridden:
87 void decorate(LogMsg &log_msg) override
88 {
89 log_msg.raw_ += angle_wrap_value(get_time());
90 }
91
92private:
93 /** Gets current system local time
94 *
95 * @return Local time
96 */
97 std::string get_time()
98 {
99 auto now = std::chrono::system_clock::now();
100 auto time = std::chrono::system_clock::to_time_t(now);
101
102 // TODO: use put_time for gcc > 4.9
103 char buf[100] = { 0 };
104 std::strftime(buf, sizeof(buf), "%d-%m-%Y %I:%M:%S", std::localtime(&time));
105 return buf;
106 }
107};
108
109/** Thread ID Decorator
110 *
111 * Appends the thread ID in the log message
112 */
113class ThreadIdDecorator : public IDecorator
114{
115public:
116 // Inherited methods overridden:
117 void decorate(LogMsg &log_msg) override
118 {
119#ifndef NO_MULTI_THREADING
120 log_msg.raw_ += angle_wrap_value(std::this_thread::get_id());
121#endif /* NO_MULTI_THREADING */
122 }
123};
124
125/** Log Level Decorator
126 *
127 * Appends the logging level in the log message
128 */
129class LogLevelDecorator : public IDecorator
130{
131public:
132 // Inherited methods overridden:
133 void decorate(LogMsg &log_msg) override
134 {
135 log_msg.raw_ += angle_wrap_value(string_from_log_level(log_msg.log_level_));
136 }
137};
138} // namespace logging
139} // namespace arm_compute
140#endif /* __ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H__ */