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