|
9 | 9 | #include <TargetConditionals.h>
|
10 | 10 | #endif
|
11 | 11 |
|
12 |
| -namespace callstack::nodeapihost { |
13 |
| -void log_debug(const char *format, ...) { |
14 |
| - // TODO: Disable logging in release builds |
| 12 | +namespace { |
| 13 | +constexpr auto LineFormat = "[%s] [NodeApiHost] "; |
15 | 14 |
|
16 |
| - va_list args; |
17 |
| - va_start(args, format); |
| 15 | +enum class LogLevel { Debug, Warning, Error }; |
| 16 | + |
| 17 | +constexpr std::string_view levelToString(LogLevel level) { |
| 18 | + switch (level) { |
| 19 | + case LogLevel::Debug: |
| 20 | + return "DEBUG"; |
| 21 | + case LogLevel::Warning: |
| 22 | + return "WARNING"; |
| 23 | + case LogLevel::Error: |
| 24 | + return "ERROR"; |
| 25 | + default: |
| 26 | + return "UNKNOWN"; |
| 27 | + } |
| 28 | +} |
18 | 29 |
|
19 | 30 | #if defined(__ANDROID__)
|
20 |
| - __android_log_vprint(ANDROID_LOG_DEBUG, LOG_TAG, format, args); |
| 31 | +constexpr int androidLogLevel(LogLevel level) { |
| 32 | + switch (level) { |
| 33 | + case LogLevel::Debug: |
| 34 | + return ANDROID_LOG_DEBUG; |
| 35 | + case LogLevel::Warning: |
| 36 | + return ANDROID_LOG_WARN; |
| 37 | + case LogLevel::Error: |
| 38 | + return ANDROID_LOG_ERROR; |
| 39 | + default: |
| 40 | + return ANDROID_LOG_UNKNOWN; |
| 41 | + } |
| 42 | +} |
| 43 | +#endif |
| 44 | + |
| 45 | +void log_message_internal(LogLevel level, const char* format, va_list args) { |
| 46 | +#if defined(__ANDROID__) |
| 47 | + __android_log_vprint(androidLogLevel(level), LOG_TAG, format, args); |
21 | 48 | #elif defined(__APPLE__)
|
22 | 49 | // iOS or macOS
|
23 |
| - fprintf(stderr, "[NodeApiHost] "); |
| 50 | + const auto level_str = levelToString(level); |
| 51 | + fprintf(stderr, LineFormat, level_str.data()); |
24 | 52 | vfprintf(stderr, format, args);
|
25 | 53 | fprintf(stderr, "\n");
|
26 | 54 | #else
|
27 | 55 | // Fallback for other platforms
|
28 |
| - fprintf(stdout, "[NodeApiHost] "); |
| 56 | + const auto level_str = levelToString(level); |
| 57 | + fprintf(stdout, LineFormat, level_str.data()); |
29 | 58 | vfprintf(stdout, format, args);
|
30 | 59 | fprintf(stdout, "\n");
|
31 | 60 | #endif
|
| 61 | +} |
| 62 | +} // anonymous namespace |
| 63 | + |
| 64 | +namespace callstack::nodeapihost { |
32 | 65 |
|
| 66 | +void log_debug(const char* format, ...) { |
| 67 | + // TODO: Disable logging in release builds |
| 68 | + va_list args; |
| 69 | + va_start(args, format); |
| 70 | + log_message_internal(LogLevel::Debug, format, args); |
| 71 | + va_end(args); |
| 72 | +} |
| 73 | +void log_warning(const char* format, ...) { |
| 74 | + va_list args; |
| 75 | + va_start(args, format); |
| 76 | + log_message_internal(LogLevel::Warning, format, args); |
| 77 | + va_end(args); |
| 78 | +} |
| 79 | +void log_error(const char* format, ...) { |
| 80 | + va_list args; |
| 81 | + va_start(args, format); |
| 82 | + log_message_internal(LogLevel::Error, format, args); |
33 | 83 | va_end(args);
|
34 | 84 | }
|
35 |
| -} // namespace callstack::nodeapihost |
| 85 | +} // namespace callstack::nodeapihost |
0 commit comments