Lugdunum  0.1.0
Formatter.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <chrono>
4 #include <memory>
5 #include <vector>
6 #include <string>
7 #include <lug/System/Export.hpp>
8 
9 namespace lug {
10 namespace System {
11 namespace Logger {
12 
16 namespace priv {
17 class Message;
18 } // priv
23 class Formatter;
24 
25 using FlagHandlerPointer = std::string (Formatter::*)(const std::tm* now);
26 
30 namespace priv {
31 
32 class Formattable {
33 public:
34  virtual std::string format(Message* = nullptr) const = 0;
35 };
36 
37 class UserChars : public Formattable {
38 public:
39  void addChar(char c);
40  virtual std::string format(Message* = nullptr) const;
41 
42 private:
43  std::string _chars;
44 };
45 
46 class LevelFlag : public Formattable {
47 public:
48  virtual std::string format(Message* = nullptr) const;
49 };
50 
51 class MessageFlag : public Formattable {
52 public:
53  virtual std::string format(Message* = nullptr) const;
54 };
55 
56 struct Token {
57  Token() = default;
58  Token(FlagHandlerPointer flagHandle) : basic(flagHandle) {}
59  Token(std::unique_ptr<Formattable> formattable) : advanced(std::move(formattable)) {}
60 
61  FlagHandlerPointer basic{nullptr};
62  std::unique_ptr<Formattable> advanced{nullptr};
63 };
64 
65 } // priv
71 public:
72  Formatter(const std::string& pattern);
73 
74  Formatter(const Formatter&) = delete;
75  Formatter(Formatter&&) = delete;
76 
77  Formatter& operator=(const Formatter&) = delete;
78  Formatter& operator=(Formatter&&) = delete;
79 
80  virtual ~Formatter ();
81 
82  virtual void format(priv::Message& msg);
83  virtual void format(priv::Message& msg, const std::tm* now);
84 
85 private:
86  void handleFlag(char c);
87  void compilePattern(const std::string& pattern);
88 
89  std::string handleFlagy(const std::tm* now); // Year 2 digits
90  std::string handleFlagY(const std::tm* now); // Year 4 digits
91  std::string handleFlagm(const std::tm* now); // Month from 1 to 12
92  std::string handleFlagd(const std::tm* now); // Day from 1 to 31
93  std::string handleFlagH(const std::tm* now); // Hour from 0 to 24
94  std::string handleFlagM(const std::tm* now); // Minute from 0 to 59
95  std::string handleFlagS(const std::tm* now); // Sec from 0 to 61
96 
97  std::vector<priv::Token> _formatChain;
98 };
99 
100 } // Logger
101 } // System
102 } // lug
#define LUG_SYSTEM_API
Definition: Export.hpp:11
std::vector< priv::Token > _formatChain
Definition: Formatter.hpp:97
std::string(Formatter::*)(const std::tm *now) FlagHandlerPointer
Definition: Formatter.hpp:25