-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathLogger.cpp
More file actions
85 lines (77 loc) · 2.05 KB
/
Logger.cpp
File metadata and controls
85 lines (77 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "Logger.hpp"
#include <cstdarg>
#include <cstdio>
#if defined(__ANDROID__)
#include <android/log.h>
#define LOG_TAG "NodeApiHost"
#elif defined(__APPLE__)
#include <TargetConditionals.h>
#endif
namespace {
constexpr auto LineFormat = "[%s] [NodeApiHost] ";
enum class LogLevel { Debug, Warning, Error };
constexpr std::string_view levelToString(LogLevel level) {
switch (level) {
case LogLevel::Debug:
return "DEBUG";
case LogLevel::Warning:
return "WARNING";
case LogLevel::Error:
return "ERROR";
default:
return "UNKNOWN";
}
}
#if defined(__ANDROID__)
constexpr int androidLogLevel(LogLevel level) {
switch (level) {
case LogLevel::Debug:
return ANDROID_LOG_DEBUG;
case LogLevel::Warning:
return ANDROID_LOG_WARN;
case LogLevel::Error:
return ANDROID_LOG_ERROR;
default:
return ANDROID_LOG_UNKNOWN;
}
}
#endif
void log_message_internal(LogLevel level, const char *format, va_list args) {
#if defined(__ANDROID__)
__android_log_vprint(androidLogLevel(level), LOG_TAG, format, args);
#elif defined(__APPLE__)
// iOS or macOS
const auto level_str = levelToString(level);
fprintf(stderr, LineFormat, level_str.data());
vfprintf(stderr, format, args);
fprintf(stderr, "\n");
#else
// Fallback for other platforms
const auto level_str = levelToString(level);
fprintf(stdout, LineFormat, level_str.data());
vfprintf(stdout, format, args);
fprintf(stdout, "\n");
#endif
}
} // anonymous namespace
namespace callstack::react_native_node_api {
void log_debug(const char *format, ...) {
// TODO: Disable logging in release builds
va_list args;
va_start(args, format);
log_message_internal(LogLevel::Debug, format, args);
va_end(args);
}
void log_warning(const char *format, ...) {
va_list args;
va_start(args, format);
log_message_internal(LogLevel::Warning, format, args);
va_end(args);
}
void log_error(const char *format, ...) {
va_list args;
va_start(args, format);
log_message_internal(LogLevel::Error, format, args);
va_end(args);
}
} // namespace callstack::react_native_node_api