TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / src / anbox / logger.h
1 /*
2  * Copyright (C) 2015 Canonical, Ltd.
3  *
4  * This program is free software: you can redistribute it and/or modify it
5  * under the terms of the GNU General Public License version 3, as published
6  * by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranties of
10  * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
11  * PURPOSE.  See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program.  If not, see <http://www.gnu.org/licenses/>.
15  *
16  */
17
18 #ifndef ANBOX_LOGGER_H_
19 #define ANBOX_LOGGER_H_
20
21 #include <boost/optional.hpp>
22
23 #include <memory.h>
24 #include <string>
25
26 #include "anbox/do_not_copy_or_move.h"
27 #include "anbox/utils.h"
28
29 namespace anbox {
30 // A Logger enables persisting of messages describing & explaining the
31 // state of the system.
32 class Logger : public DoNotCopyOrMove {
33  public:
34   // Severity enumerates all known severity levels
35   // applicable to log messages.
36   enum class Severity { kTrace,
37                         kDebug,
38                         kInfo,
39                         kWarning,
40                         kError,
41                         kFatal };
42
43   // A Location describes the origin of a log message.
44   struct Location {
45     std::string file;      // The name of the file that contains the log message.
46     std::string function;  // The function that contains the log message.
47     std::uint32_t line;    // The line in file that resulted in the log message.
48   };
49
50   virtual void Init(const Severity& severity = Severity::kWarning) = 0;
51
52   bool SetSeverityFromString(const std::string &severity);
53   virtual void SetSeverity(const Severity& severity) = 0;
54   virtual Severity GetSeverity() = 0;
55
56   virtual void Log(Severity severity, const std::string& message,
57                    const boost::optional<Location>& location) = 0;
58
59   virtual void Trace(
60       const std::string& message,
61       const boost::optional<Location>& location = boost::optional<Location>{});
62   virtual void Debug(
63       const std::string& message,
64       const boost::optional<Location>& location = boost::optional<Location>{});
65   virtual void Info(
66       const std::string& message,
67       const boost::optional<Location>& location = boost::optional<Location>{});
68   virtual void Warning(
69       const std::string& message,
70       const boost::optional<Location>& location = boost::optional<Location>{});
71   virtual void Error(
72       const std::string& message,
73       const boost::optional<Location>& location = boost::optional<Location>{});
74   virtual void Fatal(
75       const std::string& message,
76       const boost::optional<Location>& location = boost::optional<Location>{});
77
78   template <typename... T>
79   void Tracef(const boost::optional<Location>& location,
80               const std::string& pattern, T&&... args) {
81     Trace(utils::string_format(pattern, std::forward<T>(args)...), location);
82   }
83
84   template <typename... T>
85   void Debugf(const boost::optional<Location>& location,
86               const std::string& pattern, T&&... args) {
87     Debug(utils::string_format(pattern, std::forward<T>(args)...), location);
88   }
89
90   template <typename... T>
91   void Infof(const boost::optional<Location>& location,
92              const std::string& pattern, T&&... args) {
93     Info(utils::string_format(pattern, std::forward<T>(args)...), location);
94   }
95
96   template <typename... T>
97   void Warningf(const boost::optional<Location>& location,
98                 const std::string& pattern, T&&... args) {
99     Warning(utils::string_format(pattern, std::forward<T>(args)...), location);
100   }
101
102   template <typename... T>
103   void Errorf(const boost::optional<Location>& location,
104               const std::string& pattern, T&&... args) {
105     Error(utils::string_format(pattern, std::forward<T>(args)...), location);
106   }
107
108   template <typename... T>
109   void Fatalf(const boost::optional<Location>& location,
110               const std::string& pattern, T&&... args) {
111     Fatal(utils::string_format(pattern, std::forward<T>(args)...), location);
112   }
113
114  protected:
115   Logger() = default;
116 };
117
118 // operator<< inserts severity into out.
119 std::ostream& operator<<(std::ostream& out, Logger::Severity severity);
120
121 // operator<< inserts location into out.
122 std::ostream& operator<<(std::ostream& out, const Logger::Location& location);
123
124 // Log returns the mcs-wide configured logger instance.
125 // Save to call before/after main.
126 Logger& Log();
127 // SetLog installs the given logger as mcs-wide default logger.
128 void SetLogger(const std::shared_ptr<Logger>& logger);
129 }
130
131 #define TRACE(...)     \
132   anbox::Log().Tracef( \
133       anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
134 #define DEBUG(...)     \
135   anbox::Log().Debugf( \
136       anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
137 #define INFO(...)     \
138   anbox::Log().Infof( \
139       anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
140 #define WARNING(...)     \
141   anbox::Log().Warningf( \
142       anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
143 #define ERROR(...)     \
144   anbox::Log().Errorf( \
145       anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
146 #define FATAL(...)     \
147   anbox::Log().Fatalf( \
148       anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
149
150 #endif