ThingsBoard Client SDK 0.16.0
Client SDK to connect with ThingsBoard IoT Platform from IoT devices (Arduino, Espressif, etc.)
Loading...
Searching...
No Matches
DefaultLogger.h
Go to the documentation of this file.
1#ifndef Default_Logger_h
2#define Default_Logger_h
3
4// Local includes.
5#include "Helper.h"
6
7
8// Log messages.
9char constexpr FAILED_MESSAGE[] = "Invalid arguments passed to format specifiers (%) in Logger::printfln";
10char constexpr LOG_MESSAGE_FORMAT[] = "[TB] %s\n";
11
12
15 public:
23 template<typename ...Args>
24 static int printfln(char const * format, Args const &... args) {
25 // Check if variadic template contains any actual additional arguments that should be inserted into format message.
26 // If it does, use constexpr if statement for C++20 supported devices to optimize away the branch instruction at compile time
27 // or alternatively runtime for devices that do not support C++20
28#if THINGSBOARD_ENABLE_CXX20
29 if constexpr (sizeof...(Args) == 0U) {
30#else
31 if (sizeof...(Args) == 0U) {
32#endif // THINGSBOARD_ENABLE_CXX20
33 return printf(LOG_MESSAGE_FORMAT, format);
34 }
35 else {
36 auto const size = static_cast<int>(Helper::Calculate_Print_Size(format, args...));
37 char arguments[size] = {};
38 auto const written_characters = snprintf(arguments, size, format, args...);
39 // Written characters is expected to be one less, because of the null termination character
40 bool const result = (written_characters == size - 1U);
41 return printf(LOG_MESSAGE_FORMAT, result ? arguments : FAILED_MESSAGE);
42 }
43 }
44};
45
46#endif // Default_Logger_h
char constexpr FAILED_MESSAGE[]
Definition: DefaultLogger.h:9
char constexpr LOG_MESSAGE_FORMAT[]
Definition: DefaultLogger.h:10
Default logger class used by the ThingsBoard class to log messages into the console output.
Definition: DefaultLogger.h:14
static int printfln(char const *format, Args const &... args)
Prints the given format message containing format specifiers (subsequences beginning with %) as well ...
Definition: DefaultLogger.h:24
static size_t Calculate_Print_Size(char const *format, Args const &... args)
Returns the total amount of bytes needed to store the formatted string with null termination,...
Definition: Helper.h:32