SkyWay Linux SDK
読み取り中…
検索中…
一致する文字列を見つけられません
logger.hpp
1//
2// © NTT DOCOMO BUSINESS, Inc. All Rights Reserved.
3//
4
5#ifndef SKYWAY_GLOBAL_INTERFACE_LOGGER_HPP_
6#define SKYWAY_GLOBAL_INTERFACE_LOGGER_HPP_
7
8#include <boost/format.hpp>
9#include <memory>
10#include <mutex>
11
12#define __SKW_FILE__ skyway::global::interface::Logger::GetFileName(__FILE__)
13
14#define SKW_TRACE(msg, ...) \
15 if (skyway::global::interface::Logger::Shared()) { \
16 std::string formatted = \
17 skyway::global::interface::Logger::Shared()->Format(msg, ##__VA_ARGS__); \
18 skyway::global::interface::Logger::Shared()->Trace( \
19 formatted, __SKW_FILE__, __FUNCTION__, __LINE__); \
20 }
21
22#define SKW_DEBUG(msg, ...) \
23 if (skyway::global::interface::Logger::Shared()) { \
24 std::string formatted = \
25 skyway::global::interface::Logger::Shared()->Format(msg, ##__VA_ARGS__); \
26 skyway::global::interface::Logger::Shared()->Debug( \
27 formatted, __SKW_FILE__, __FUNCTION__, __LINE__); \
28 }
29
30#define SKW_INFO(msg, ...) \
31 if (skyway::global::interface::Logger::Shared()) { \
32 std::string formatted = \
33 skyway::global::interface::Logger::Shared()->Format(msg, ##__VA_ARGS__); \
34 skyway::global::interface::Logger::Shared()->Info( \
35 formatted, __SKW_FILE__, __FUNCTION__, __LINE__); \
36 }
37
38#define SKW_WARN(msg, ...) \
39 if (skyway::global::interface::Logger::Shared()) { \
40 std::string formatted = \
41 skyway::global::interface::Logger::Shared()->Format(msg, ##__VA_ARGS__); \
42 skyway::global::interface::Logger::Shared()->Warn( \
43 formatted, __SKW_FILE__, __FUNCTION__, __LINE__); \
44 }
45
46#define SKW_ERROR(msg, ...) \
47 if (skyway::global::interface::Logger::Shared()) { \
48 std::string formatted = \
49 skyway::global::interface::Logger::Shared()->Format(msg, ##__VA_ARGS__); \
50 skyway::global::interface::Logger::Shared()->Error( \
51 formatted, __SKW_FILE__, __FUNCTION__, __LINE__); \
52 }
53
54namespace skyway {
55namespace global {
56namespace interface {
57
59class Logger {
60public:
62 enum Level { kOff, kTrace, kDebug, kInfo, kWarn, kError };
64 virtual ~Logger() = default;
65 virtual void Trace(const std::string& msg,
66 const std::string& filename,
67 const std::string& function,
68 int line) = 0;
69 virtual void Debug(const std::string& msg,
70 const std::string& filename,
71 const std::string& function,
72 int line) = 0;
73 virtual void Info(const std::string& msg,
74 const std::string& filename,
75 const std::string& function,
76 int line) = 0;
77 virtual void Warn(const std::string& msg,
78 const std::string& filename,
79 const std::string& function,
80 int line) = 0;
81 virtual void Error(const std::string& msg,
82 const std::string& filename,
83 const std::string& function,
84 int line) = 0;
85
86 void BuildFormat() {}
87
88 template <class Head, class... Tail>
89 void BuildFormat(Head&& head, Tail&&... tail) {
90 fmt_ = fmt_ % head;
91 this->BuildFormat(std::forward<Tail>(tail)...);
92 }
93
94 template <class FormatString, class... Args>
95 std::string Format(FormatString fmt_str, Args&&... args) {
96 std::lock_guard<std::mutex> lg(fmt_mtx_);
97 fmt_ = boost::format(fmt_str);
98 this->BuildFormat(args...);
99 return fmt_.str();
100 }
101
102 static inline std::string GetFileName(const std::string& path) {
103 size_t pos1;
104
105 pos1 = path.rfind('\\');
106 if (pos1 != std::string::npos) {
107 return path.substr(pos1 + 1, path.size() - pos1 - 1);
108 }
109
110 pos1 = path.rfind('/');
111 if (pos1 != std::string::npos) {
112 return path.substr(pos1 + 1, path.size() - pos1 - 1);
113 }
114
115 return path;
116 }
117
118 // Set value when Context::Setup()
119 static void SetSharedInstance(std::unique_ptr<Logger> logger);
120 static Logger* Shared();
122
123private:
124 static std::unique_ptr<Logger> shared_;
125
126 std::mutex fmt_mtx_;
127 boost::format fmt_;
128};
129
130} // namespace interface
131} // namespace global
132} // namespace skyway
133
134#endif /* SKYWAY_GLOBAL_INTERFACE_LOGGER_HPP_ */
SkyWayのログを処理するクラス
Definition logger.hpp:59
Level
ログレベル
Definition logger.hpp:62
SkyWayで発生したエラーを示す構造体
Definition error.hpp:14