2 * Copyright (C) 2015 Canonical, Ltd.
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.
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.
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/>.
18 #ifndef ANBOX_LOGGER_H_
19 #define ANBOX_LOGGER_H_
21 #include <boost/optional.hpp>
26 #include "anbox/do_not_copy_or_move.h"
27 #include "anbox/utils.h"
30 // A Logger enables persisting of messages describing & explaining the
31 // state of the system.
32 class Logger : public DoNotCopyOrMove {
34 // Severity enumerates all known severity levels
35 // applicable to log messages.
36 enum class Severity { kTrace,
43 // A Location describes the origin of a log message.
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.
50 virtual void Init(const Severity& severity = Severity::kWarning) = 0;
52 bool SetSeverityFromString(const std::string &severity);
53 virtual void SetSeverity(const Severity& severity) = 0;
54 virtual Severity GetSeverity() = 0;
56 virtual void Log(Severity severity, const std::string& message,
57 const boost::optional<Location>& location) = 0;
60 const std::string& message,
61 const boost::optional<Location>& location = boost::optional<Location>{});
63 const std::string& message,
64 const boost::optional<Location>& location = boost::optional<Location>{});
66 const std::string& message,
67 const boost::optional<Location>& location = boost::optional<Location>{});
69 const std::string& message,
70 const boost::optional<Location>& location = boost::optional<Location>{});
72 const std::string& message,
73 const boost::optional<Location>& location = boost::optional<Location>{});
75 const std::string& message,
76 const boost::optional<Location>& location = boost::optional<Location>{});
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);
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);
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);
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);
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);
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);
118 // operator<< inserts severity into out.
119 std::ostream& operator<<(std::ostream& out, Logger::Severity severity);
121 // operator<< inserts location into out.
122 std::ostream& operator<<(std::ostream& out, const Logger::Location& location);
124 // Log returns the mcs-wide configured logger instance.
125 // Save to call before/after main.
127 // SetLog installs the given logger as mcs-wide default logger.
128 void SetLogger(const std::shared_ptr<Logger>& logger);
132 anbox::Log().Tracef( \
133 anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
135 anbox::Log().Debugf( \
136 anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
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__)
144 anbox::Log().Errorf( \
145 anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
147 anbox::Log().Fatalf( \
148 anbox::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)