Skip to content

Commit 5bc0cf9

Browse files
committed
fix or test!
1 parent a1de9dc commit 5bc0cf9

1 file changed

Lines changed: 54 additions & 28 deletions

File tree

src/pl/Logger.h

Lines changed: 54 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,71 @@
44
#include <format>
55
#include <string>
66
#include <string_view>
7+
#include <unordered_map>
8+
#include <memory>
9+
#include <mutex>
710

811
namespace pl::log {
912

10-
class Logger {
11-
public:
12-
explicit Logger(std::string name) : loggerName(std::move(name)) {}
13+
class Logger {
14+
public:
15+
explicit Logger(std::string name) : loggerName(std::move(name)) {}
1316

14-
template <typename... Args>
15-
void info(std::string_view fmt_str, Args &&...args) const {
16-
log(ANDROID_LOG_INFO, fmt_str, std::forward<Args>(args)...);
17-
}
17+
static Logger& getOrCreate(std::string name) {
18+
std::lock_guard lock(loggerMutex);
1819

19-
template <typename... Args>
20-
void debug(std::string_view fmt_str, Args &&...args) const {
21-
log(ANDROID_LOG_DEBUG, fmt_str, std::forward<Args>(args)...);
20+
auto it = loggers.find(name);
21+
if (it != loggers.end()) {
22+
return *it->second;
2223
}
2324

24-
template <typename... Args>
25-
void warn(std::string_view fmt_str, Args &&...args) const {
26-
log(ANDROID_LOG_WARN, fmt_str, std::forward<Args>(args)...);
27-
}
25+
auto logger = std::make_unique<Logger>(std::move(name));
26+
auto& ref = *logger;
27+
loggers[ref.loggerName] = std::move(logger);
2828

29-
template <typename... Args>
30-
void error(std::string_view fmt_str, Args &&...args) const {
31-
log(ANDROID_LOG_ERROR, fmt_str, std::forward<Args>(args)...);
32-
}
29+
return ref;
30+
}
3331

34-
private:
35-
std::string loggerName;
32+
template <typename... Args>
33+
void info(std::string_view fmt_str, Args&&... args) const {
34+
log(ANDROID_LOG_INFO, fmt_str, std::forward<Args>(args)...);
35+
}
3636

37-
template <typename... Args>
38-
void log(int android_level, std::string_view fmt_str, Args &&...args) const {
39-
auto msg = std::vformat(fmt_str, std::make_format_args(args...));
37+
template <typename... Args>
38+
void debug(std::string_view fmt_str, Args&&... args) const {
39+
log(ANDROID_LOG_DEBUG, fmt_str, std::forward<Args>(args)...);
40+
}
4041

41-
__android_log_print(android_level, loggerName.c_str(), "%s %s",
42-
loggerName.c_str(), msg.c_str());
43-
}
44-
};
42+
template <typename... Args>
43+
void warn(std::string_view fmt_str, Args&&... args) const {
44+
log(ANDROID_LOG_WARN, fmt_str, std::forward<Args>(args)...);
45+
}
46+
47+
template <typename... Args>
48+
void error(std::string_view fmt_str, Args&&... args) const {
49+
log(ANDROID_LOG_ERROR, fmt_str, std::forward<Args>(args)...);
50+
}
51+
52+
private:
53+
std::string loggerName;
54+
55+
inline static std::unordered_map<std::string, std::unique_ptr<Logger>> loggers{};
56+
inline static std::mutex loggerMutex{};
57+
58+
template <typename... Args>
59+
void log(int android_level, std::string_view fmt_str, Args&&... args) const {
60+
std::string msg = std::vformat(fmt_str, std::make_format_args(args...));
61+
62+
__android_log_print(
63+
android_level,
64+
loggerName.c_str(),
65+
"%s",
66+
msg.c_str()
67+
);
68+
}
69+
};
4570

4671
} // namespace pl::log
4772

48-
inline pl::log::Logger preloader_logger("Preloader");
73+
74+
inline auto& preloader_logger = pl::log::Logger::getOrCreate("Preloader");

0 commit comments

Comments
 (0)