Music Hub ..
A session-wide music playback service
logger.h
Go to the documentation of this file.
1/*
2 * Copyright © 2016 Canonical Ltd.
3 *
4 * This program is free software: you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License version 3,
6 * as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *
16 */
17
18#ifndef LOGGER_H_
19#define LOGGER_H_
20
23
24#include <boost/optional.hpp>
25
26#include <string>
27
28namespace core {
29namespace ubuntu {
30namespace media {
31// A Logger enables persisting of messages describing & explaining the
32// state of the system.
34public:
35 // Severity enumerates all known severity levels
36 // applicable to log messages.
37 enum class Severity {
38 kTrace,
39 kDebug,
40 kInfo,
41 kWarning,
42 kError,
43 kFatal
44 };
45
46 // A Location describes the origin of a log message.
47 struct Location {
48 std::string file; // The name of the file that contains the log message.
49 std::string function; // The function that contains the log message.
50 std::uint32_t line; // The line in file that resulted in the log message.
51 };
52
54
55 virtual void Log(Severity severity, const std::string &message, const boost::optional<Location>& location) = 0;
56
57 virtual void Trace(const std::string& message, const boost::optional<Location>& location = boost::optional<Location>{});
58 virtual void Debug(const std::string& message, const boost::optional<Location>& location = boost::optional<Location>{});
59 virtual void Info(const std::string& message, const boost::optional<Location>& location = boost::optional<Location>{});
60 virtual void Warning(const std::string& message, const boost::optional<Location>& location = boost::optional<Location>{});
61 virtual void Error(const std::string& message, const boost::optional<Location>& location = boost::optional<Location>{});
62 virtual void Fatal(const std::string& message, const boost::optional<Location>& location = boost::optional<Location>{});
63
64
65 template<typename... T>
66 void Tracef(const boost::optional<Location>& location, const std::string& pattern, T&&...args) {
67 Trace(Utils::Sprintf(pattern, std::forward<T>(args)...), location);
68 }
69
70 template<typename... T>
71 void Debugf(const boost::optional<Location>& location, const std::string& pattern, T&&...args) {
72 Debug(Utils::Sprintf(pattern, std::forward<T>(args)...), location);
73 }
74
75 template<typename... T>
76 void Infof(const boost::optional<Location>& location, const std::string& pattern, T&&...args) {
77 Info(Utils::Sprintf(pattern, std::forward<T>(args)...), location);
78 }
79
80 template<typename... T>
81 void Warningf(const boost::optional<Location>& location, const std::string& pattern, T&&...args) {
82 Warning(Utils::Sprintf(pattern, std::forward<T>(args)...), location);
83 }
84
85 template<typename... T>
86 void Errorf(const boost::optional<Location>& location, const std::string& pattern, T&&...args) {
87 Error(Utils::Sprintf(pattern, std::forward<T>(args)...), location);
88 }
89
90 template<typename... T>
91 void Fatalf(const boost::optional<Location>& location, const std::string& pattern, T&&...args) {
92 Fatal(Utils::Sprintf(pattern, std::forward<T>(args)...), location);
93 }
94
95protected:
96 Logger() = default;
97};
98
99// operator<< inserts severity into out.
100std::ostream& operator<<(std::ostream& out, Logger::Severity severity);
101
102// operator<< inserts location into out.
103std::ostream& operator<<(std::ostream& out, const Logger::Location &location);
104
105// Log returns the core::ubuntu::media-wide configured logger instance.
106// Save to call before/after main.
107Logger& Log();
108// SetLog installs the given logger as core::ubuntu::media-wide default logger.
109void SetLogger(const std::shared_ptr<Logger>& logger);
110
111#define TRACE(...) Log().Tracef(Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
112#define DEBUG(...) Log().Debugf(Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
113#define INFO(...) Log().Infof(Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
114#define WARNING(...) Log().Warningf(Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
115#define ERROR(...) Log().Errorf(Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
116#define FATAL(...) Log().Fatalf(Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
117} // namespace media
118} // namespace ubuntu
119} // namespace core
120
121#define MH_TRACE(...) core::ubuntu::media::Log().Tracef(\
122 core::ubuntu::media::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
123#define MH_DEBUG(...) core::ubuntu::media::Log().Debugf(\
124 core::ubuntu::media::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
125#define MH_INFO(...) core::ubuntu::media::Log().Infof(\
126 core::ubuntu::media::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
127#define MH_WARNING(...) core::ubuntu::media::Log().Warningf(core::ubuntu::media::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
128#define MH_ERROR(...) core::ubuntu::media::Log().Errorf(core::ubuntu::media::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
129#define MH_FATAL(...) core::ubuntu::media::Log().Fatalf(core::ubuntu::media::Logger::Location{__FILE__, __FUNCTION__, __LINE__}, __VA_ARGS__)
130
131#endif
void Fatalf(const boost::optional< Location > &location, const std::string &pattern, T &&...args)
Definition: logger.h:91
virtual void Debug(const std::string &message, const boost::optional< Location > &location=boost::optional< Location >{})
Definition: logger.cpp:111
virtual void Trace(const std::string &message, const boost::optional< Location > &location=boost::optional< Location >{})
Definition: logger.cpp:107
void Tracef(const boost::optional< Location > &location, const std::string &pattern, T &&...args)
Definition: logger.h:66
virtual void Warning(const std::string &message, const boost::optional< Location > &location=boost::optional< Location >{})
Definition: logger.cpp:119
void Warningf(const boost::optional< Location > &location, const std::string &pattern, T &&...args)
Definition: logger.h:81
virtual void Log(Severity severity, const std::string &message, const boost::optional< Location > &location)=0
void Errorf(const boost::optional< Location > &location, const std::string &pattern, T &&...args)
Definition: logger.h:86
virtual void Fatal(const std::string &message, const boost::optional< Location > &location=boost::optional< Location >{})
Definition: logger.cpp:127
virtual void Error(const std::string &message, const boost::optional< Location > &location=boost::optional< Location >{})
Definition: logger.cpp:123
void Debugf(const boost::optional< Location > &location, const std::string &pattern, T &&...args)
Definition: logger.h:71
virtual void Init(const core::ubuntu::media::Logger::Severity &severity=core::ubuntu::media::Logger::Severity::kWarning)=0
void Infof(const boost::optional< Location > &location, const std::string &pattern, T &&...args)
Definition: logger.h:76
virtual void Info(const std::string &message, const boost::optional< Location > &location=boost::optional< Location >{})
Definition: logger.cpp:115
std::ostream & operator<<(std::ostream &out, Player::PlaybackStatus status)
Definition: player.h:217
Logger & Log()
Definition: logger.cpp:151
void SetLogger(const std::shared_ptr< Logger > &logger)
Definition: logger.cpp:155
Definition: player.h:34
static std::string Sprintf(const std::string &fmt_str, Types &&... args)
Definition: utils.h:64