1- #pragma once
2-
1+ #include < android/log.h >
2+ # include < format >
33#include < string>
4+ #include < string_view>
5+
46namespace pl {
57namespace log {
68
9+ inline constexpr const char *LOG_TAG = " LeviLogger" ;
10+
711class Logger {
812public:
9- explicit Logger (const std::string &name);
13+ explicit Logger (std::string name) : loggerName(std::move(name)) {}
14+
15+ template <typename ... Args>
16+ void info (std::string_view fmt_str, Args &&...args) const {
17+ log (ANDROID_LOG_INFO, fmt_str, std::forward<Args>(args)...);
18+ }
1019
11- void info (const char *fmt, ...);
12- void debug (const char *fmt, ...);
13- void warn (const char *fmt, ...);
14- void error (const char *fmt, ...);
20+ template <typename ... Args>
21+ void debug (std::string_view fmt_str, Args &&...args) const {
22+ log (ANDROID_LOG_DEBUG, fmt_str, std::forward<Args>(args)...);
23+ }
24+
25+ template <typename ... Args>
26+ void warn (std::string_view fmt_str, Args &&...args) const {
27+ log (ANDROID_LOG_WARN, fmt_str, std::forward<Args>(args)...);
28+ }
29+
30+ template <typename ... Args>
31+ void error (std::string_view fmt_str, Args &&...args) const {
32+ log (ANDROID_LOG_ERROR, fmt_str, std::forward<Args>(args)...);
33+ }
1534
1635private:
1736 std::string loggerName;
18- void log (int level, const char *fmt, va_list args);
37+
38+ template <typename ... Args>
39+ void log (int android_level, std::string_view fmt_str, Args &&...args) const {
40+ auto msg =
41+ std::format (" [{}] {}" , loggerName,
42+ std::vformat (fmt_str, std::make_format_args (args...)));
43+ __android_log_print (android_level, LOG_TAG, " %s" , msg.c_str ());
44+ }
1945};
46+
2047} // namespace log
2148} // namespace pl
0 commit comments