Diag-Client-Lib
logger.h
Go to the documentation of this file.
1 /* Diagnostic Client library
2  * Copyright (C) 2024 Avijit Dey
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 #ifndef DIAGNOSTIC_CLIENT_LIB_LIB_UTILITY_UTILITY_LOGGER_H
9 #define DIAGNOSTIC_CLIENT_LIB_LIB_UTILITY_UTILITY_LOGGER_H
10 
11 #ifdef ENABLE_DLT_LOGGER
12 #include <dlt/dlt.h>
13 #endif
14 #include <iostream>
15 #include <memory>
16 #include <sstream>
17 #include <string>
18 #include <string_view>
19 #include <utility>
20 
21 #define UNUSED_PARAM(expr) \
22  do { (void) (expr); } while (0)
23 
24 namespace utility {
25 namespace logger {
26 
32 class Logger final {
33  public:
47  template<typename Func>
48  auto LogFatal(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept
49  -> void {
50 #ifdef ENABLE_DLT_LOGGER
51  LogDltMessage(DLT_LOG_FATAL, file_name, func_name, line_no, std::forward<Func>(func));
52 #else
53  UNUSED_PARAM(file_name);
54  UNUSED_PARAM(line_no);
55  UNUSED_PARAM(func_name);
56  UNUSED_PARAM(func);
57 #endif
58  std::abort(); // abort in case of fatal issue
59  }
60 
74  template<typename Func>
75  auto LogError(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept
76  -> void {
77 #ifdef ENABLE_DLT_LOGGER
78  LogDltMessage(DLT_LOG_ERROR, file_name, func_name, line_no, std::forward<Func>(func));
79 #else
80  UNUSED_PARAM(file_name);
81  UNUSED_PARAM(line_no);
82  UNUSED_PARAM(func_name);
83  UNUSED_PARAM(func);
84 #endif
85  }
86 
100  template<typename Func>
101  auto LogWarn(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept
102  -> void {
103 #ifdef ENABLE_DLT_LOGGER
104  LogDltMessage(DLT_LOG_WARN, file_name, func_name, line_no, std::forward<Func>(func));
105 #else
106  UNUSED_PARAM(file_name);
107  UNUSED_PARAM(line_no);
108  UNUSED_PARAM(func_name);
109  UNUSED_PARAM(func);
110 #endif
111  }
112 
126  template<typename Func>
127  auto LogInfo(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept
128  -> void {
129 #ifdef ENABLE_DLT_LOGGER
130  LogDltMessage(DLT_LOG_INFO, file_name, func_name, line_no, std::forward<Func>(func));
131 #else
132  UNUSED_PARAM(file_name);
133  UNUSED_PARAM(line_no);
134  UNUSED_PARAM(func_name);
135  UNUSED_PARAM(func);
136 #endif
137  }
138 
152  template<typename Func>
153  auto LogDebug(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept
154  -> void {
155 #ifdef ENABLE_DLT_LOGGER
156  LogDltMessage(DLT_LOG_DEBUG, file_name, func_name, line_no, std::forward<Func>(func));
157 #else
158  UNUSED_PARAM(file_name);
159  UNUSED_PARAM(line_no);
160  UNUSED_PARAM(func_name);
161  UNUSED_PARAM(func);
162 #endif
163  }
164 
178  template<typename Func>
179  auto LogVerbose(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept
180  -> void {
181 #ifdef ENABLE_DLT_LOGGER
182  LogDltMessage(DLT_LOG_VERBOSE, file_name, func_name, line_no, std::forward<Func>(func));
183 #else
184  UNUSED_PARAM(file_name);
185  UNUSED_PARAM(line_no);
186  UNUSED_PARAM(func_name);
187  UNUSED_PARAM(func);
188 #endif
189  }
190 
191  public:
197  explicit Logger(std::string_view context_id);
198 
206  Logger(std::string_view app_id, std::string_view context_id);
207 
211  ~Logger();
212 
213  private:
227  template<typename Func>
228  auto CreateLoggingMessage(const std::string_view file_name, const std::string_view /* func_name */, int line_no,
229  Func &&func) noexcept -> std::stringstream {
230  std::stringstream msg{};
231  func(msg);
232  msg << " [" << file_name << ":" << line_no << "]";
233  return msg;
234  }
235 
249 #ifdef ENABLE_DLT_LOGGER
250  template<typename Func>
251  void LogDltMessage(DltLogLevelType log_level, const std::string_view file_name, const std::string_view func_name,
252  int line_no, Func &&func) {
253 
254  DLT_LOG(contxt_, log_level,
255  DLT_CSTRING(CreateLoggingMessage(file_name, func_name, line_no, std::forward<Func>(func)).str().c_str()));
256  }
257 #endif
258 
259 #ifdef ENABLE_DLT_LOGGER
260  // Declare the context
261  DLT_DECLARE_CONTEXT(contxt_)
262 #else
263  std::string contxt_;
264 #endif
265  // Stores application id
266  std::string app_id_;
267 
268  // Stores context id
269  std::string context_id_;
270 
271  // store the information about registration with app id
273 };
274 } // namespace logger
275 } // namespace utility
276 #endif // DIAGNOSTIC_CLIENT_LIB_LIB_UTILITY_UTILITY_LOGGER_H
Logger class that is used to log Dlt messages from the component.
Definition: logger.h:32
auto LogDebug(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept -> void
Log debug message.
Definition: logger.h:153
~Logger()
Destruct an instance of Logger.
Definition: logger.cpp:39
std::string app_id_
Definition: logger.h:266
Logger(std::string_view context_id)
Construct an instance of Logger.
Definition: logger.cpp:14
bool registration_with_app_id_
Definition: logger.h:272
std::string context_id_
Definition: logger.h:269
std::string contxt_
Function to send the messages to dlt infrastructure.
Definition: logger.h:263
auto LogVerbose(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept -> void
Log verbose message.
Definition: logger.h:179
auto LogError(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept -> void
Log error message.
Definition: logger.h:75
auto LogInfo(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept -> void
Log info message.
Definition: logger.h:127
auto LogFatal(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept -> void
Log fatal message and abort.
Definition: logger.h:48
auto CreateLoggingMessage(const std::string_view file_name, const std::string_view, int line_no, Func &&func) noexcept -> std::stringstream
Function to create the final logging message.
Definition: logger.h:228
auto LogWarn(const std::string_view file_name, int line_no, const std::string_view func_name, Func &&func) noexcept -> void
Log warning message.
Definition: logger.h:101
#define UNUSED_PARAM(expr)
Definition: logger.h:21