AlexaClientSDK  1.25.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Logger.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 
16 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LOGGER_LOGGER_H_
17 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LOGGER_LOGGER_H_
18 
19 #include <atomic>
20 #include <chrono>
21 #include <mutex>
22 #include <sstream>
23 #include <vector>
24 
26 
31 
37 #define ACSDK_STRINGIFY_INNER(expression) #expression
38 
44 #define ACSDK_STRINGIFY(macro) ACSDK_STRINGIFY_INNER(macro)
45 
52 #define ACSDK_CONCATENATE_INNER(lhs, rhs) lhs##rhs
53 
60 #define ACSDK_CONCATENATE(lhs, rhs) ACSDK_CONCATENATE_INNER(lhs, rhs)
61 
62 namespace alexaClientSDK {
63 namespace avsCommon {
64 namespace utils {
65 namespace logger {
66 
150 class Logger {
151 public:
157  Logger(Level level);
158 
160  virtual ~Logger() = default;
161 
167  virtual void setLevel(Level level);
168 
175  inline bool shouldLog(Level level) const;
176 
183  void log(Level level, const LogEntry& entry);
184 
196  void logAtExit(Level level, const LogEntry& entry);
197 
208  virtual void emit(
209  Level level,
210  std::chrono::system_clock::time_point time,
211  const char* threadMoniker,
212  const char* text);
213 
221 
229 
230 protected:
236  void init(const configuration::ConfigurationNode configuration);
237 
239  std::atomic<Level> m_level;
240 
241 private:
248  bool initLogLevel(const configuration::ConfigurationNode configuration);
249 
253  void notifyObserversOnLogLevelChanged();
254 
256  std::vector<LogLevelObserverInterface*> m_observers;
257 
259  std::mutex m_observersMutex;
260 };
261 
262 bool Logger::shouldLog(Level level) const {
263  return level >= m_level;
264 }
265 
271 #define ACSDK_GET_LOGGER_FUNCTION_NAME(type) ACSDK_CONCATENATE(ACSDK_CONCATENATE(get, type), Logger)
272 
273 // If @c ACSDK_LOG_SINK was not defined, default to logging to console.
274 #ifndef ACSDK_LOG_SINK
275 #define ACSDK_LOG_SINK Console
276 #endif
277 
279 #define ACSDK_GET_SINK_LOGGER ACSDK_GET_LOGGER_FUNCTION_NAME(ACSDK_LOG_SINK)
280 
286 std::shared_ptr<Logger> ACSDK_GET_SINK_LOGGER();
287 
288 } // namespace logger
289 } // namespace utils
290 } // namespace avsCommon
291 } // namespace alexaClientSDK
292 
293 #ifdef ACSDK_LOG_MODULE
294 
296 
297 namespace alexaClientSDK {
298 namespace avsCommon {
299 namespace utils {
300 namespace logger {
301 
307 #define ACSDK_GET_LOGGER_FUNCTION ACSDK_GET_LOGGER_FUNCTION_NAME(ACSDK_LOG_MODULE)
308 
312 inline std::shared_ptr<Logger> ACSDK_GET_LOGGER_FUNCTION() {
313  static std::shared_ptr<ModuleLogger> moduleLogger =
314  std::make_shared<ModuleLogger>(ACSDK_STRINGIFY(ACSDK_LOG_MODULE));
315  return moduleLogger;
316 }
317 
318 } // namespace logger
319 } // namespace utils
320 } // namespace avsCommon
321 } // namespace alexaClientSDK
322 
323 #else // ACSDK_LOG_MODULE
324 
325 namespace alexaClientSDK {
326 namespace avsCommon {
327 namespace utils {
328 namespace logger {
329 
335 inline std::shared_ptr<Logger> ACSDK_GET_LOGGER_FUNCTION() {
336  static std::shared_ptr<Logger> logger = ACSDK_GET_SINK_LOGGER();
337  return logger;
338 }
339 
340 } // namespace logger
341 } // namespace utils
342 } // namespace avsCommon
343 } // namespace alexaClientSDK
344 
345 #endif
346 
348 #ifdef ACSDK_LOG_ENABLED
349 
355 #define ACSDK_LOG(level, entry) \
356  do { \
357  auto loggerInstance = alexaClientSDK::avsCommon::utils::logger::ACSDK_GET_LOGGER_FUNCTION(); \
358  if (loggerInstance->shouldLog(level)) { \
359  loggerInstance->log(level, entry); \
360  } \
361  } while (false)
362 #else
363 
369 #define ACSDK_LOG(level, entry) \
370  do { \
371  (void)level; \
372  (void)entry; \
373  } while (false)
374 #endif
375 
376 #ifdef ACSDK_DEBUG_LOG_ENABLED
377 
384 #define ACSDK_DEBUG9(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG9, entry)
385 
392 #define ACSDK_DEBUG8(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG8, entry)
393 
400 #define ACSDK_DEBUG7(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG7, entry)
401 
408 #define ACSDK_DEBUG6(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG6, entry)
409 
416 #define ACSDK_DEBUG5(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG5, entry)
417 
424 #define ACSDK_DEBUG4(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG4, entry)
425 
432 #define ACSDK_DEBUG3(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG3, entry)
433 
440 #define ACSDK_DEBUG2(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG2, entry)
441 
448 #define ACSDK_DEBUG1(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG1, entry)
449 
456 #define ACSDK_DEBUG0(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG0, entry)
457 
464 #define ACSDK_DEBUG(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::DEBUG0, entry)
465 
466 #else // ACSDK_DEBUG_LOG_ENABLED
467 
474 #define ACSDK_DEBUG9(entry)
475 
482 #define ACSDK_DEBUG8(entry)
483 
490 #define ACSDK_DEBUG7(entry)
491 
498 #define ACSDK_DEBUG6(entry)
499 
506 #define ACSDK_DEBUG5(entry)
507 
514 #define ACSDK_DEBUG4(entry)
515 
522 #define ACSDK_DEBUG3(entry)
523 
530 #define ACSDK_DEBUG2(entry)
531 
538 #define ACSDK_DEBUG1(entry)
539 
546 #define ACSDK_DEBUG0(entry)
547 
554 #define ACSDK_DEBUG(entry)
555 
556 #endif // ACSDK_DEBUG_LOG_ENABLED
557 
564 #define ACSDK_INFO(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::INFO, entry)
565 
572 #define ACSDK_WARN(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::WARN, entry)
573 
580 #define ACSDK_ERROR(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::ERROR, entry)
581 
587 #define ACSDK_CRITICAL(entry) ACSDK_LOG(alexaClientSDK::avsCommon::utils::logger::Level::CRITICAL, entry)
588 
589 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_LOGGER_LOGGER_H_
#define ACSDK_STRINGIFY(macro)
Definition: Logger.h:44
bool shouldLog(Level level) const
Definition: Logger.h:262
void log(Level level, const LogEntry &entry)
Definition: Logger.cpp:37
void init(const configuration::ConfigurationNode configuration)
Definition: Logger.cpp:43
void logAtExit(Level level, const LogEntry &entry)
Definition: Logger.cpp:123
std::shared_ptr< Logger > ACSDK_GET_LOGGER_FUNCTION()
Definition: Logger.h:335
std::shared_ptr< Logger > ACSDK_GET_SINK_LOGGER()
void addLogLevelObserver(LogLevelObserverInterface *observer)
Definition: Logger.cpp:86
virtual void emit(Level level, std::chrono::system_clock::time_point time, const char *threadMoniker, const char *text)
Definition: Logger.cpp:129
Logger(Level level)
Definition: Logger.cpp:34
void removeLogLevelObserver(LogLevelObserverInterface *observer)
Definition: Logger.cpp:102
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
std::atomic< Level > m_level
The lowest severity level of logs to be output by this Logger.
Definition: Logger.h:239
virtual void setLevel(Level level)
Definition: Logger.cpp:64
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:32

AlexaClientSDK 1.25.0 - Copyright 2016-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0