1
1
// logger.h
2
2
//
3
- // Copyright (C) 2021, Celestia Development Team
3
+ // Copyright (C) 2021-present , Celestia Development Team
4
4
//
5
5
// Logging functions.
6
6
//
11
11
12
12
#pragma once
13
13
14
+ #include < celcompat/filesystem.h>
15
+ #include < fmt/format.h>
14
16
#include < iosfwd>
15
17
#include < string>
18
+ #include < string_view>
16
19
17
- #include < fmt/format.h>
18
-
19
- #include < celcompat/filesystem.h>
20
-
21
- template <>
22
- struct fmt ::formatter<fs::path> : formatter<std::string>
20
+ template <> struct fmt ::formatter<fs::path> : formatter<std::string>
23
21
{
24
- template <typename FormatContext>
25
- auto format (const fs::path &path, FormatContext &ctx)
22
+ template <typename FormatContext> auto format (const fs::path &path, FormatContext &ctx)
26
23
{
27
- return formatter<std::string>::format (path.string (), ctx);
24
+ return formatter<std::string>::format (path.u8string (), ctx);
28
25
}
29
26
};
30
27
@@ -42,91 +39,111 @@ enum class Level
42
39
43
40
class Logger
44
41
{
45
- public:
42
+ public:
46
43
using Stream = std::basic_ostream<char >;
47
44
45
+ /* * Create a default logger with Info verbocity writing to std::clog and std::cerr */
48
46
Logger ();
49
- Logger (Level level, Stream &log, Stream &err) :
50
- m_log (log),
51
- m_err (err),
52
- m_level (level)
53
- {}
54
47
55
- void setLevel (Level level)
56
- {
57
- m_level = level;
58
- }
48
+ /* *
49
+ * Create a logger with custom verbocity and output streams
50
+ * @param level verbosibility level
51
+ * @param log stream used to output normal messages
52
+ * @param err stream used to output errors, warnings and debug messages
53
+ */
54
+ Logger (Level level, Stream &log, Stream &err);
59
55
60
- template <typename ... Args> inline void
61
- debug (const char *format, const Args&... args) const ;
56
+ /* *
57
+ * Set verbocity level
58
+ *
59
+ * @param level verbosibility level
60
+ */
61
+ void setLevel (Level level);
62
62
63
- template <typename ... Args> inline void
64
- info ( const char * format, const Args&... args) const ;
63
+ template <typename ... Args>
64
+ inline void debug (std::string_view format, const Args &...args) const ;
65
65
66
- template <typename ... Args> inline void
67
- verbose ( const char * format, const Args&... args) const ;
66
+ template <typename ... Args>
67
+ inline void info (std::string_view format, const Args &...args) const ;
68
68
69
- template <typename ... Args> inline void
70
- warn ( const char * format, const Args&... args) const ;
69
+ template <typename ... Args>
70
+ inline void verbose (std::string_view format, const Args &...args) const ;
71
71
72
- template <typename ... Args> inline void
73
- error ( const char * format, const Args&... args) const ;
72
+ template <typename ... Args>
73
+ inline void warn (std::string_view format, const Args &...args) const ;
74
74
75
- template <typename ... Args> void
76
- log (Level, const char * format, const Args&... args) const ;
75
+ template <typename ... Args>
76
+ inline void error (std::string_view format, const Args &...args) const ;
77
77
78
- static Logger* g_logger;
78
+ template <typename ... Args>
79
+ void log (Level, std::string_view format, const Args &...args) const ;
79
80
80
- private:
81
- void vlog (Level level, fmt ::string_view format, fmt::format_args args) const ;
81
+ private:
82
+ void vlog (Level level, std ::string_view format, fmt::format_args args) const ;
82
83
83
84
Stream &m_log;
84
85
Stream &m_err;
85
- Level m_level { Level::Info } ;
86
+ Level m_level;
86
87
};
87
88
88
- template <typename ... Args> void
89
- Logger::log (Level level, const char * format, const Args&... args) const
89
+ template <typename ... Args> void
90
+ Logger::log (Level level, std::string_view format, const Args &...args) const
90
91
{
91
92
if (level <= m_level)
92
- vlog (level, fmt::string_view ( format) , fmt::make_format_args (args...));
93
+ vlog (level, format, fmt::make_format_args (args...));
93
94
}
94
95
95
- template <typename ... Args> void
96
- Logger::debug (const char * format, const Args&... args) const
96
+ template <typename ... Args> void
97
+ Logger::debug (std::string_view format, const Args &...args) const
97
98
{
98
99
log (Level::Debug, format, args...);
99
100
}
100
101
101
- template <typename ... Args> inline void
102
- Logger::info (const char * format, const Args&... args) const
102
+ template <typename ... Args> inline void
103
+ Logger::info (std::string_view format, const Args &...args) const
103
104
{
104
105
log (Level::Info, format, args...);
105
106
}
106
107
107
- template <typename ... Args> inline void
108
- Logger::verbose (const char * format, const Args&... args) const
108
+ template <typename ... Args> inline void
109
+ Logger::verbose (std::string_view format, const Args &...args) const
109
110
{
110
111
log (Level::Verbose, format, args...);
111
112
}
112
113
113
- template <typename ... Args> inline void
114
- Logger::warn (const char * format, const Args&... args) const
114
+ template <typename ... Args> inline void
115
+ Logger::warn (std::string_view format, const Args &...args) const
115
116
{
116
117
log (Level::Warning, format, args...);
117
118
}
118
119
119
- template <typename ... Args> inline void
120
- Logger::error (const char * format, const Args&... args) const
120
+ template <typename ... Args> inline void
121
+ Logger::error (std::string_view format, const Args &...args) const
121
122
{
122
123
log (Level::Error, format, args...);
123
124
}
124
125
125
- Logger* GetLogger ();
126
- Logger* CreateLogger (Level level = Level::Info);
127
- Logger* CreateLogger (Level level,
128
- Logger::Stream &log,
129
- Logger::Stream &err);
126
+ /* * Return a pointer to the default global Logger instance */
127
+ Logger *GetLogger ();
128
+
129
+ /* *
130
+ * Initiliaze the default global Logger instance writing to std::clog and std::cerr
131
+ *
132
+ * @param level verbosibility level
133
+ * @return pointer to the default global Logger instance
134
+ */
135
+ Logger *CreateLogger (Level level = Level::Info);
136
+
137
+ /* *
138
+ * Initiliaze the default global Logger instance writing to custom streams
139
+ *
140
+ * @param level verbosibility level
141
+ * @param log stream used to output normal messages
142
+ * @param err stream used to output errors, warnings and debug messages
143
+ */
144
+ Logger *CreateLogger (Level level, Logger::Stream &log, Logger::Stream &err);
145
+
146
+ /* * Destroy the default global Logger instance */
130
147
void DestroyLogger ();
131
148
132
149
} // end namespace celestia::util
0 commit comments