Skip to content

Commit c7074e2

Browse files
authored
ref: asserts and warnings (#3139)
1 parent 2b55154 commit c7074e2

File tree

4 files changed

+39
-9
lines changed

4 files changed

+39
-9
lines changed

Sources/Sentry/SentryNSTimerFactory.m

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
#import "SentryNSTimerFactory.h"
2+
#import "SentryInternalDefines.h"
23

34
@implementation SentryNSTimerFactory
45

56
- (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
67
repeats:(BOOL)repeats
78
block:(void (^)(NSTimer *timer))block
89
{
10+
SENTRY_ASSERT([NSThread isMainThread],
11+
@"Timers must be scheduled from the main thread, or they may never fire. See the attribute "
12+
@"on the declaration in NSTimer.h. See "
13+
@"https://stackoverflow.com/questions/8304702/"
14+
@"how-do-i-create-a-nstimer-on-a-background-thread for more info.");
915
return [NSTimer scheduledTimerWithTimeInterval:interval repeats:repeats block:block];
1016
}
1117

@@ -15,6 +21,11 @@ - (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti
1521
userInfo:(nullable id)userInfo
1622
repeats:(BOOL)yesOrNo
1723
{
24+
SENTRY_ASSERT([NSThread isMainThread],
25+
@"Timers must be scheduled from the main thread, or they may never fire. See the attribute "
26+
@"on the declaration in NSTimer.h. See "
27+
@"https://stackoverflow.com/questions/8304702/"
28+
@"how-do-i-create-a-nstimer-on-a-background-thread for more info.");
1829
return [NSTimer scheduledTimerWithTimeInterval:ti
1930
target:aTarget
2031
selector:aSelector

Sources/Sentry/SentryProfileTimeseries.mm

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
logSlicingFailureWithArray(
1919
NSArray<SentrySample *> *array, SentryTransaction *transaction, BOOL start)
2020
{
21-
if (!SENTRY_CASSERT(array.count > 0, @"Should not have attempted to slice an empty array.")) {
21+
if (!SENTRY_CASSERT_RETURN(
22+
array.count > 0, @"Should not have attempted to slice an empty array.")) {
2223
return;
2324
}
2425

Sources/Sentry/SentryTracerConcurrency.mm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#if SENTRY_TARGET_PROFILING_SUPPORTED
44

55
# import "SentryId.h"
6+
# import "SentryInternalDefines.h"
67
# import "SentryLog.h"
78
# import "SentryProfiler+Private.h"
89
# import "SentryTracer.h"
@@ -41,7 +42,7 @@
4142
SENTRY_LOG_DEBUG(
4243
@"Tracking relationship between profiler id %@ and tracer id %@", profilerKey, tracerKey);
4344

44-
NSCAssert((_gProfilersToTracers == nil && _gTracersToProfilers == nil)
45+
SENTRY_CASSERT((_gProfilersToTracers == nil && _gTracersToProfilers == nil)
4546
|| (_gProfilersToTracers != nil && _gTracersToProfilers != nil),
4647
@"Both structures must be initialized simultaneously.");
4748

@@ -69,16 +70,14 @@
6970
{
7071
std::lock_guard<std::mutex> l(_gStateLock);
7172

72-
NSCAssert(_gTracersToProfilers != nil && _gProfilersToTracers != nil,
73+
SENTRY_CASSERT(_gTracersToProfilers != nil && _gProfilersToTracers != nil,
7374
@"Structures should have already been initialized by the time they are being queried");
7475

7576
const auto tracerKey = tracer.traceId.sentryIdString;
7677
const auto profiler = _gTracersToProfilers[tracerKey];
7778

78-
NSCAssert(
79-
profiler != nil, @"Expected a profiler to be associated with tracer id %@.", tracerKey);
80-
if (profiler == nil) {
81-
SENTRY_LOG_WARN(@"Could not find a profiler associated with tracer id %@.", tracerKey);
79+
if (!SENTRY_CASSERT_RETURN(profiler != nil,
80+
@"Expected a profiler to be associated with tracer id %@.", tracerKey)) {
8281
return nil;
8382
}
8483

Sources/Sentry/include/SentryInternalDefines.h

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1+
#import "SentryLog.h"
12
#import <Foundation/Foundation.h>
23

34
static NSString *const SentryDebugImageType = @"macho";
45

6+
/**
7+
* Abort if assertion fails in debug, and log a warning if it fails in production.
8+
*/
9+
#define SENTRY_ASSERT(cond, ...) \
10+
if (!(cond)) { \
11+
SENTRY_LOG_WARN(__VA_ARGS__); \
12+
NSAssert(NO, __VA_ARGS__); \
13+
}
14+
15+
/**
16+
* Abort if assertion fails in debug, and log a warning if it fails in production.
17+
*/
18+
#define SENTRY_CASSERT(cond, ...) \
19+
if (!(cond)) { \
20+
SENTRY_LOG_WARN(__VA_ARGS__); \
21+
NSCAssert(NO, __VA_ARGS__); \
22+
}
23+
524
/**
625
* Abort if assertion fails in debug, and log a warning if it fails in production.
726
* @return The result of the assertion condition, so it can be used to e.g. early return from the
827
* point of it's check if that's also desirable in production.
928
*/
10-
#define SENTRY_ASSERT(cond, ...) \
29+
#define SENTRY_ASSERT_RETURN(cond, ...) \
1130
({ \
1231
const auto __cond_result = (cond); \
1332
if (!__cond_result) { \
@@ -22,7 +41,7 @@ static NSString *const SentryDebugImageType = @"macho";
2241
* @return The result of the assertion condition, so it can be used to e.g. early return from the
2342
* point of it's check if that's also desirable in production.
2443
*/
25-
#define SENTRY_CASSERT(cond, ...) \
44+
#define SENTRY_CASSERT_RETURN(cond, ...) \
2645
({ \
2746
const auto __cond_result = (cond); \
2847
if (!__cond_result) { \

0 commit comments

Comments
 (0)