From 83f764c5f4feaea8997b533fe4035bf6b39fd467 Mon Sep 17 00:00:00 2001
From: kongr45gpen <electrovesta@gmail.com>
Date: Sat, 31 Aug 2019 02:21:31 +0300
Subject: [PATCH] General Logger fixes

Use a non-templated version of etl::string

Set the etl format as a static variable for the Logger

This prevents wasting time recreating the format variable every time the
LOG function is called.

Force inlining the LOG function

This forces the compiler to check the `if constexpr` structure, and
disable the logging line entirely if needed.

Never redefine the log level

Move `disabled` logging level to ease level checks
---
 inc/Logger.hpp              | 16 ++++++++++------
 src/Logger.cpp              |  6 +++---
 src/Platform/x86/Logger.cpp |  2 +-
 test/TestPlatform.cpp       |  2 +-
 4 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/inc/Logger.hpp b/inc/Logger.hpp
index f4b6bdd8..ec0e4142 100644
--- a/inc/Logger.hpp
+++ b/inc/Logger.hpp
@@ -4,7 +4,7 @@
 #include <cstdint>
 #include <etl/String.hpp>
 #include <etl/to_string.h>
-#include <ECSS_Definitions.hpp>
+#include "ECSS_Definitions.hpp"
 
 #if defined LOGLEVEL_TRACE
 #define LOGLEVEL Logger::trace // Ignore-MISRA
@@ -20,7 +20,7 @@
 #define LOGLEVEL Logger::error // Ignore-MISRA
 #elif defined LOGLEVEL_EMERGENCY
 #define LOGLEVEL Logger::emergency // Ignore-MISRA
-#else
+#elif !defined LOGLEVEL
 #define LOGLEVEL Logger::disabled // Ignore-MISRA
 #endif
 
@@ -51,6 +51,11 @@ public:
 	 */
 	typedef uint8_t LogLevelType;
 
+	/**
+	 * ETL's string format specification, to be used for all logged messages
+	 */
+	static etl::format_spec format;
+
 	/**
 	 * Log levels supported by the logger. Each level represents a different severity of the logged Message,
 	 * and messages of lower severities can be filtered on top of more significant ones.
@@ -58,7 +63,6 @@ public:
 	 * Each severity is tied to a number. The higher the number, the higher the severity.
 	 */
 	enum LogLevel : LogLevelType {
-		disabled = 0, ///< Use this log level to disable logging entirely. No message should be logged as disabled.
 		trace = 32, ///< Very detailed information, useful for tracking the individual steps of an operation
 		debug = 64, ///< General debugging information
 		info = 96, ///< Noteworthy or periodical events
@@ -66,6 +70,7 @@ public:
 		warning = 160, ///< Unexpected events that do not compromise the operability of a function
 		error = 192, ///< Unexpected failure of an operation
 		emergency = 254, ///< Unexpected failure that renders the entire system unusable
+		disabled = 255, ///< Use this log level to disable logging entirely. No message should be logged as disabled.
 	};
 
 	/**
@@ -84,7 +89,6 @@ public:
 	 */
 	struct LogEntry {
 		String<LOGGER_MAX_MESSAGE_SIZE> message = ""; ///< The current log message itself, starting from a blank slate
-		etl::format_spec format; ///< ETL's string format specification
 		LogLevel level; ///< The log level of this message
 
 		explicit LogEntry(LogLevel level); ///< Create a new LogEntry
@@ -131,7 +135,7 @@ public:
 	/**
 	 * Store a new log message
 	 */
-	static void log(LogLevel level, String<LOGGER_MAX_MESSAGE_SIZE>& message);
+	static void log(LogLevel level, etl::istring & message);
 };
 
 /**
@@ -190,7 +194,7 @@ public:
  * message will not be logged. This is determined at compile-time.
  */
 template <Logger::LogLevel level>
-constexpr inline auto LOG() {
+constexpr __attribute__((always_inline)) inline auto LOG() {
 	if constexpr (Logger::isLogged(level)) {
 		return Logger::LogEntry(level);
 	} else {
diff --git a/src/Logger.cpp b/src/Logger.cpp
index 64bb617f..65584a94 100644
--- a/src/Logger.cpp
+++ b/src/Logger.cpp
@@ -1,5 +1,7 @@
 #include <Logger.hpp>
 
+etl::format_spec Logger::format;
+
 // Reimplementation of the function for variable C strings
 template <>
 Logger::LogEntry& Logger::LogEntry::operator<<(char* value) noexcept {
@@ -14,9 +16,7 @@ Logger::LogEntry& Logger::LogEntry::operator<<(const char* value) noexcept {
 	return *this;
 }
 
-Logger::LogEntry::LogEntry(LogLevel level) : level(level) {
-	format.precision(3); // Set precision to 3 decimal digits
-}
+Logger::LogEntry::LogEntry(LogLevel level) : level(level) {}
 
 Logger::LogEntry::~LogEntry() {
 	// When the destructor is called, the log message is fully "designed". Now we can finally "display" it to the user.
diff --git a/src/Platform/x86/Logger.cpp b/src/Platform/x86/Logger.cpp
index 5f6c839f..f5ec73d0 100644
--- a/src/Platform/x86/Logger.cpp
+++ b/src/Platform/x86/Logger.cpp
@@ -7,7 +7,7 @@
 #include <iomanip>
 
 // The implementation of this function appends ANSI codes that should add colours to a compatible terminal
-void Logger::log(Logger::LogLevel level, String<LOGGER_MAX_MESSAGE_SIZE> & message) {
+void Logger::log(Logger::LogLevel level, etl::istring & message) {
 	// Get the current time & date
 	std::time_t t = std::time(nullptr);
 	std::tm tm = *std::localtime(&t);
diff --git a/test/TestPlatform.cpp b/test/TestPlatform.cpp
index 1e50ae0f..beb76de1 100644
--- a/test/TestPlatform.cpp
+++ b/test/TestPlatform.cpp
@@ -35,7 +35,7 @@ void ErrorHandler::logError(ErrorType errorType) {
 	ServiceTests::addError(ErrorHandler::findErrorSource(errorType), errorType);
 }
 
-void Logger::log(Logger::LogLevel level, String<LOGGER_MAX_MESSAGE_SIZE> & message) {
+void Logger::log(Logger::LogLevel level, etl::istring & message) {
 	// Logs while testing are completely ignored
 }
 
-- 
GitLab