Serial output logger based on the LoggerInterface

Dependencies:   LoggerInterface

Fork of LogIt by Sille Van Landschoot

Committer:
Nico De Witte
Date:
Wed Dec 14 18:06:34 2016 +0100
Revision:
4:9c5143891c8a
Refactor LogIt to SerialLogger and also add Logable interface and NullLogger.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nico De Witte 4:9c5143891c8a 1 #pragma once
Nico De Witte 4:9c5143891c8a 2
Nico De Witte 4:9c5143891c8a 3 #include "LoggerInterface.h"
Nico De Witte 4:9c5143891c8a 4
Nico De Witte 4:9c5143891c8a 5 // NullLogger logs nothing but it allows us to inject other logger when necessary.
Nico De Witte 4:9c5143891c8a 6 // With NullLogger we dont have to check if logger is set to actual logger.
Nico De Witte 4:9c5143891c8a 7 // Kudos to Sandi Metz and her seminar on how to avoid if statements
Nico De Witte 4:9c5143891c8a 8
Nico De Witte 4:9c5143891c8a 9 // Source: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern
Nico De Witte 4:9c5143891c8a 10 namespace LogIt {
Nico De Witte 4:9c5143891c8a 11
Nico De Witte 4:9c5143891c8a 12 class NullLogger : public Log::LoggerInterface
Nico De Witte 4:9c5143891c8a 13 {
Nico De Witte 4:9c5143891c8a 14 public:
Nico De Witte 4:9c5143891c8a 15 static NullLogger * get_instance()
Nico De Witte 4:9c5143891c8a 16 {
Nico De Witte 4:9c5143891c8a 17 static NullLogger instance; // Guaranteed to be destroyed.
Nico De Witte 4:9c5143891c8a 18 // Instantiated on first use.
Nico De Witte 4:9c5143891c8a 19 return &instance;
Nico De Witte 4:9c5143891c8a 20 }
Nico De Witte 4:9c5143891c8a 21
Nico De Witte 4:9c5143891c8a 22 private:
Nico De Witte 4:9c5143891c8a 23 NullLogger() {} // Constructor? (the {} brackets) are needed here.
Nico De Witte 4:9c5143891c8a 24
Nico De Witte 4:9c5143891c8a 25 // C++ 03
Nico De Witte 4:9c5143891c8a 26 // ========
Nico De Witte 4:9c5143891c8a 27 // Dont forget to declare these two. You want to make sure they
Nico De Witte 4:9c5143891c8a 28 // are unacceptable otherwise you may accidentally get copies of
Nico De Witte 4:9c5143891c8a 29 // your singleton appearing.
Nico De Witte 4:9c5143891c8a 30 NullLogger(NullLogger const&); // Don't Implement
Nico De Witte 4:9c5143891c8a 31 void operator=(NullLogger const&); // Don't implement
Nico De Witte 4:9c5143891c8a 32
Nico De Witte 4:9c5143891c8a 33 // C++ 11
Nico De Witte 4:9c5143891c8a 34 // =======
Nico De Witte 4:9c5143891c8a 35 // We can use the better technique of deleting the methods
Nico De Witte 4:9c5143891c8a 36 // we don't want.
Nico De Witte 4:9c5143891c8a 37 public:
Nico De Witte 4:9c5143891c8a 38 // NullLogger(NullLogger const&) = delete;
Nico De Witte 4:9c5143891c8a 39 // void operator=(NullLogger const&) = delete;
Nico De Witte 4:9c5143891c8a 40
Nico De Witte 4:9c5143891c8a 41 // Note: Scott Meyers mentions in his Effective Modern
Nico De Witte 4:9c5143891c8a 42 // C++ book, that deleted functions should generally
Nico De Witte 4:9c5143891c8a 43 // be public as it results in better error messages
Nico De Witte 4:9c5143891c8a 44 // due to the compilers behavior to check accessibility
Nico De Witte 4:9c5143891c8a 45 // before deleted status
Nico De Witte 4:9c5143891c8a 46
Nico De Witte 4:9c5143891c8a 47 public:
Nico De Witte 4:9c5143891c8a 48 void emergency(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 49 void alert(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 50 void critical(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 51 void error(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 52 void warning(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 53 void notice(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 54 void info(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 55 void debug(const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 56 void log(Level level, const char * message, ...) {};
Nico De Witte 4:9c5143891c8a 57 };
Nico De Witte 4:9c5143891c8a 58
Nico De Witte 4:9c5143891c8a 59 };