Skip to content

Commit 9845106

Browse files
committed
feat: add logger api
1 parent 309870d commit 9845106

File tree

5 files changed

+68
-34
lines changed

5 files changed

+68
-34
lines changed

src/pl/Logger.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#pragma once
2+
3+
#include <string>
4+
5+
class Logger {
6+
public:
7+
explicit Logger(const std::string &name);
8+
9+
void info(const char *fmt, ...);
10+
void debug(const char *fmt, ...);
11+
void warn(const char *fmt, ...);
12+
void error(const char *fmt, ...);
13+
14+
private:
15+
std::string loggerName;
16+
void log(int level, const char *fmt, va_list args);
17+
};

src/pl/PreLoader.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
#include "Logger.h"
12
#include "internal/AndroidUtils.h"
2-
#include "internal/Logger.h"
33
#include "internal/ModManager.h"
44
#include <android/native_activity.h>
55
#include <dlfcn.h>

src/pl/internal/Logger.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#pragma once
2+
3+
#include <android/log.h>
4+
#include <fmt/core.h>
5+
#include <string>
6+
#include <string_view>
7+
8+
namespace pl {
9+
namespace log {
10+
11+
inline constexpr const char *LOG_TAG = "LeviLogger";
12+
13+
class Logger {
14+
public:
15+
explicit Logger(std::string name) : loggerName(std::move(name)) {}
16+
17+
template <typename... Args>
18+
void info(std::string_view fmt_str, Args &&...args) const {
19+
log(ANDROID_LOG_INFO, fmt_str, std::forward<Args>(args)...);
20+
}
21+
22+
template <typename... Args>
23+
void debug(std::string_view fmt_str, Args &&...args) const {
24+
log(ANDROID_LOG_DEBUG, fmt_str, std::forward<Args>(args)...);
25+
}
26+
27+
template <typename... Args>
28+
void warn(std::string_view fmt_str, Args &&...args) const {
29+
log(ANDROID_LOG_WARN, fmt_str, std::forward<Args>(args)...);
30+
}
31+
32+
template <typename... Args>
33+
void error(std::string_view fmt_str, Args &&...args) const {
34+
log(ANDROID_LOG_ERROR, fmt_str, std::forward<Args>(args)...);
35+
}
36+
37+
private:
38+
std::string loggerName;
39+
40+
template <typename... Args>
41+
void log(int android_level, std::string_view fmt_str, Args &&...args) const {
42+
auto msg =
43+
fmt::format("[{}] {}", loggerName,
44+
fmt::vformat(fmt_str, fmt::make_format_args(args...)));
45+
__android_log_print(android_level, LOG_TAG, "%s", msg.c_str());
46+
}
47+
};
48+
49+
} // namespace log
50+
} // namespace pl

src/pl/internal/Logger.h

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/pl/internal/ModManager.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#include "ModManager.h"
2-
#include "Logger.h"
32
#include <dlfcn.h>
43
#include <fstream>
54
#include <nlohmann/json.hpp>

0 commit comments

Comments
 (0)