Skip to content

Commit 1f9387b

Browse files
impr: Skip evaluating log messages when not logged (#4028)
Skip evaluating log messages the SDK doesn't log. The SDK now only evaluates log messages when it's actually logging the log message. Previously, the SDK evaluated all log messages regardless of the log level. This improves the overall SDK performance slightly.
1 parent 5eaadc5 commit 1f9387b

File tree

7 files changed

+65
-5
lines changed

7 files changed

+65
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ to receive SIGTERM events, set the option `enableSigtermReporting = true`.
1717
### Improvements
1818

1919
- Stop FramesTracker when app is in background (#3979)
20+
- Skip evaluating log messages when not logged (#4028)
2021

2122
### Fixes
2223

Sentry.xcodeproj/project.pbxproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,7 @@
131131
62E081AB29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */; };
132132
62E146D02BAAE47600ED34FD /* LocalMetricsAggregator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E146CF2BAAE47600ED34FD /* LocalMetricsAggregator.swift */; };
133133
62E146D22BAAF55B00ED34FD /* LocalMetricsAggregatorTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62E146D12BAAF55B00ED34FD /* LocalMetricsAggregatorTests.swift */; };
134+
62F05D2B2C0DB1F100916E3F /* SentryLogTestHelper.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F05D2A2C0DB1F100916E3F /* SentryLogTestHelper.m */; };
134135
62F226B729A37C120038080D /* SentryBooleanSerialization.m in Sources */ = {isa = PBXBuildFile; fileRef = 62F226B629A37C120038080D /* SentryBooleanSerialization.m */; };
135136
62F4DDA12C04CB9700588890 /* SentryBaggageSerializationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62F4DDA02C04CB9700588890 /* SentryBaggageSerializationTests.swift */; };
136137
62FC69362BEDFF18002D3EF2 /* SentryLogExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 62FC69352BEDFF18002D3EF2 /* SentryLogExtensions.swift */; };
@@ -1111,6 +1112,8 @@
11111112
62E081AA29ED4322000F69FC /* SentryBreadcrumbTestDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBreadcrumbTestDelegate.swift; sourceTree = "<group>"; };
11121113
62E146CF2BAAE47600ED34FD /* LocalMetricsAggregator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalMetricsAggregator.swift; sourceTree = "<group>"; };
11131114
62E146D12BAAF55B00ED34FD /* LocalMetricsAggregatorTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalMetricsAggregatorTests.swift; sourceTree = "<group>"; };
1115+
62F05D292C0DB1C800916E3F /* SentryLogTestHelper.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryLogTestHelper.h; sourceTree = "<group>"; };
1116+
62F05D2A2C0DB1F100916E3F /* SentryLogTestHelper.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryLogTestHelper.m; sourceTree = "<group>"; };
11141117
62F226B629A37C120038080D /* SentryBooleanSerialization.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = SentryBooleanSerialization.m; sourceTree = "<group>"; };
11151118
62F226B829A37C270038080D /* SentryBooleanSerialization.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SentryBooleanSerialization.h; sourceTree = "<group>"; };
11161119
62F4DDA02C04CB9700588890 /* SentryBaggageSerializationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SentryBaggageSerializationTests.swift; sourceTree = "<group>"; };
@@ -3047,6 +3050,8 @@
30473050
15E0A8EF240F638200F044E3 /* SentrySerializationNilTests.m */,
30483051
7BA61EA525F21E660008CAA2 /* SentryLogTests.swift */,
30493052
7BA61EAB25F2206E0008CAA2 /* SentryLog+TestInit.h */,
3053+
62F05D292C0DB1C800916E3F /* SentryLogTestHelper.h */,
3054+
62F05D2A2C0DB1F100916E3F /* SentryLogTestHelper.m */,
30503055
7BBD18BA24530D2600427C76 /* SentryFileManagerTests.swift */,
30513056
7BD4E8E727FD95900086C410 /* SentryMigrateSessionInitTests.m */,
30523057
7BD4E8E527FD84480086C410 /* TestFileManagerDelegate.swift */,
@@ -4791,6 +4796,7 @@
47914796
7BF9EF742722A85B00B5BBEF /* SentryClassRegistrator.m in Sources */,
47924797
D8F67AF42BE10F9600C9197B /* UIRedactBuilderTests.swift in Sources */,
47934798
63B819141EC352A7002FDF4C /* SentryInterfacesTests.m in Sources */,
4799+
62F05D2B2C0DB1F100916E3F /* SentryLogTestHelper.m in Sources */,
47944800
7B68345128F7EB3D00FB7064 /* SentryMeasurementUnitTests.swift in Sources */,
47954801
7B14089A248791660035403D /* SentryCrashStackEntryMapperTests.swift in Sources */,
47964802
D85790292976A69F00C6AC1F /* TestDebugImageProvider.swift in Sources */,

Sources/Sentry/include/SentryLog.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ SENTRY_NO_INIT
2222

2323
NS_ASSUME_NONNULL_END
2424
#define SENTRY_LOG(_SENTRY_LOG_LEVEL, ...) \
25-
[SentryLog logWithMessage:[NSString stringWithFormat:@"[%@:%d] %@", \
26-
[[[NSString stringWithUTF8String:__FILE__] \
27-
lastPathComponent] stringByDeletingPathExtension], \
28-
__LINE__, [NSString stringWithFormat:__VA_ARGS__]] \
29-
andLevel:_SENTRY_LOG_LEVEL]
25+
if ([SentryLog willLogAtLevel:_SENTRY_LOG_LEVEL]) { \
26+
[SentryLog logWithMessage:[NSString stringWithFormat:@"[%@:%d] %@", \
27+
[[[NSString stringWithUTF8String:__FILE__] \
28+
lastPathComponent] stringByDeletingPathExtension], \
29+
__LINE__, [NSString stringWithFormat:__VA_ARGS__]] \
30+
andLevel:_SENTRY_LOG_LEVEL]; \
31+
}
3032
#define SENTRY_LOG_DEBUG(...) SENTRY_LOG(kSentryLevelDebug, __VA_ARGS__)
3133
#define SENTRY_LOG_INFO(...) SENTRY_LOG(kSentryLevelInfo, __VA_ARGS__)
3234
#define SENTRY_LOG_WARN(...) SENTRY_LOG(kSentryLevelWarning, __VA_ARGS__)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#import "SentryLog.h"
2+
3+
NS_ASSUME_NONNULL_BEGIN
4+
5+
void sentryLogDebugWithMacroArgsNotEvaluated(void);
6+
void sentryLogErrorWithMacro(NSString *message);
7+
8+
NS_ASSUME_NONNULL_END
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#import "SentryLogTestHelper.h"
2+
#import <Foundation/Foundation.h>
3+
#import <XCTest/XCTest.h>
4+
5+
NSString *
6+
doNotCallMe(void)
7+
{
8+
XCTFail("The args for the log macro must not be evaluated.");
9+
return @"Don't call me";
10+
}
11+
12+
void
13+
sentryLogDebugWithMacroArgsNotEvaluated(void)
14+
{
15+
SENTRY_LOG_DEBUG(@"%@", doNotCallMe());
16+
}
17+
18+
void
19+
sentryLogErrorWithMacro(NSString *message)
20+
{
21+
SENTRY_LOG_ERROR(@"%@", message);
22+
}

Tests/SentryTests/Helper/SentryLogTests.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,24 @@ class SentryLogTests: XCTestCase {
6969
"[Sentry] [info] 3",
7070
"[Sentry] [debug] 4"], logOutput.loggedMessages)
7171
}
72+
73+
func testMacroLogsErrorMessage() {
74+
let logOutput = TestLogOutput()
75+
SentryLog.setLogOutput(logOutput)
76+
SentryLog.configure(true, diagnosticLevel: SentryLevel.error)
77+
78+
sentryLogErrorWithMacro("error")
79+
80+
XCTAssertEqual(["[Sentry] [error] [SentryLogTestHelper:21] error"], logOutput.loggedMessages)
81+
}
82+
83+
func testMacroDoesNotEvaluateArgs_WhenNotMessageNotLogged() {
84+
let logOutput = TestLogOutput()
85+
SentryLog.setLogOutput(logOutput)
86+
SentryLog.configure(true, diagnosticLevel: SentryLevel.info)
87+
88+
sentryLogDebugWithMacroArgsNotEvaluated()
89+
90+
XCTAssertTrue(logOutput.loggedMessages.isEmpty)
91+
}
7292
}

Tests/SentryTests/SentryTests-Bridging-Header.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
#import "SentryLog+TestInit.h"
140140
#import "SentryLog.h"
141141
#import "SentryLogOutput.h"
142+
#import "SentryLogTestHelper.h"
142143
#import "SentryMeasurementValue.h"
143144
#import "SentryMechanism.h"
144145
#import "SentryMechanismMeta.h"

0 commit comments

Comments
 (0)