Skip to content

Commit 5a30d16

Browse files
authored
Customize log tag of SimpleLogDestination (#313)
Or default to "Partout" if nil. Also, fix an issue with Android log level mapping, whose fallthrough switch/case was always ending up with the fault level.
1 parent 879a107 commit 5a30d16

File tree

4 files changed

+24
-8
lines changed

4 files changed

+24
-8
lines changed

Sources/PartoutCore/Logging/SimpleLogDestination.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ internal import _PartoutCore_C
66

77
/// A ``LoggerDestination`` that prints messages to the default platform logger.
88
public struct SimpleLogDestination: LoggerDestination {
9-
public init() {
9+
private let tag: String?
10+
11+
public init(tag: String?) {
12+
self.tag = tag
1013
}
1114

1215
public func append(_ level: DebugLog.Level, _ msg: String) {
@@ -18,7 +21,13 @@ public struct SimpleLogDestination: LoggerDestination {
1821
#else
1922
let cLevel = pp_log_level(UInt32(level.rawValue))
2023
#endif
21-
pp_log_simple_append(cLevel, msg)
24+
if let tag {
25+
tag.withCString {
26+
pp_log_simple_append($0, cLevel, msg)
27+
}
28+
} else {
29+
pp_log_simple_append(nil, cLevel, msg)
30+
}
2231
#endif
2332
}
2433
}

Sources/PartoutCore_C/common.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,30 @@ void pp_clog_v(pp_log_category category,
2525

2626
#ifdef __ANDROID__
2727
#include <android/log.h>
28-
void pp_log_simple_append(pp_log_level level, const char *_Nonnull message) {
29-
const char *log_tag = "Partout";
28+
void pp_log_simple_append(const char *tag, pp_log_level level, const char *_Nonnull message) {
29+
const char *log_tag = tag ? tag : "Partout";
3030
int android_level = 0;
3131
switch (level) {
3232
case PPLogLevelDebug:
3333
android_level = ANDROID_LOG_VERBOSE;
34+
break;
3435
case PPLogLevelInfo:
3536
android_level = ANDROID_LOG_DEBUG;
37+
break;
3638
case PPLogLevelNotice:
3739
android_level = ANDROID_LOG_INFO;
40+
break;
3841
case PPLogLevelError:
3942
android_level = ANDROID_LOG_WARN;
43+
break;
4044
case PPLogLevelFault:
4145
android_level = ANDROID_LOG_FATAL;
46+
break;
4247
}
4348
__android_log_print(android_level, log_tag, "%s", message);
4449
}
4550
#else
46-
void pp_log_simple_append(pp_log_level level, const char *_Nonnull message) {
51+
void pp_log_simple_append(const char *tag, pp_log_level level, const char *_Nonnull message) {
4752
FILE *out = NULL;
4853
switch (level) {
4954
case PPLogLevelError:
@@ -54,6 +59,6 @@ void pp_log_simple_append(pp_log_level level, const char *_Nonnull message) {
5459
out = stdout;
5560
break;
5661
}
57-
fprintf(out, "Partout[%d]: %s\n", level, message);
62+
fprintf(out, "%s[%d]: %s\n", tag ? tag : "Partout", level, message);
5863
}
5964
#endif

Sources/PartoutCore_C/include/portable/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ void pp_clog_v(pp_log_category category,
3333
pp_log_level level,
3434
const char *_Nonnull fmt, ...);
3535

36-
void pp_log_simple_append(pp_log_level level, const char *_Nonnull message);
36+
void pp_log_simple_append(const char *_Nullable tag,
37+
pp_log_level level,
38+
const char *_Nonnull message);
3739

3840
// Use inline rather than #define to make available to Swift
3941

Tests/PartoutCoreTests/Helpers.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import Testing
99

1010
func setUpLogging() {
1111
var logger = PartoutLogger.Builder()
12-
logger.setDestination(SimpleLogDestination(), for: [.core])
12+
logger.setDestination(SimpleLogDestination(tag: nil), for: [.core])
1313
PartoutLogger.register(logger.build())
1414
}
1515

0 commit comments

Comments
 (0)