blob: 08abcb4519a64c41cdb521352c3050c4f4bd89e2 [file] [log] [blame]
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +01001/*
Georgios Pinitase874ef92019-09-09 17:40:33 +01002 * Copyright (c) 2016-2019 ARM Limited.
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +01003 *
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 Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H
25#define ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010026
Michalis Spyrou6bff1952019-10-02 17:22:11 +010027#include "arm_compute/core/Error.h"
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010028#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 Pinitase874ef92019-09-09 17:40:33 +010034#ifndef NO_MULTI_THREADING
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010035#include <thread>
Georgios Pinitase874ef92019-09-09 17:40:33 +010036#endif /* NO_MULTI_THREADING */
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +010037
38namespace arm_compute
39{
40namespace logging
41{
42/** Log message decorator interface */
43class IDecorator
44{
45public:
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 */
59class StringDecorator : public IDecorator
60{
61public:
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
78private:
79 std::string _str;
80};
81
82/** Date Decorator
83 *
84 * Appends the date and time in the log message
85 */
86class DateDecorator : public IDecorator
87{
88public:
89 // Inherited methods overridden:
90 void decorate(LogMsg &log_msg) override
91 {
92 log_msg.raw_ += angle_wrap_value(get_time());
93 }
94
95private:
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 */
116class ThreadIdDecorator : public IDecorator
117{
118public:
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 Spyrou6bff1952019-10-02 17:22:11 +0100124#else /* NO_MULTI_THREADING */
125 ARM_COMPUTE_UNUSED(log_msg);
Georgios Pinitas7d3d1b92017-10-12 17:34:20 +0100126#endif /* NO_MULTI_THREADING */
127 }
128};
129
130/** Log Level Decorator
131 *
132 * Appends the logging level in the log message
133 */
134class LogLevelDecorator : public IDecorator
135{
136public:
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 Spyrouf4643372019-11-29 16:17:13 +0000145#endif /* ARM_COMPUTE_LOGGING_LOG_MSG_DECORATORS_H */