Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
signalapp
GitHub Repository: signalapp/Signal-iOS
Path: blob/main/SignalServiceKit/Debugging/OWSAsserts.h
1 views
1
//
2
// Copyright 2024 Signal Messenger, LLC
3
// SPDX-License-Identifier: AGPL-3.0-only
4
//
5
6
#import <SignalServiceKit/DebuggerUtils.h>
7
#import <SignalServiceKit/OWSLogs.h>
8
9
NS_ASSUME_NONNULL_BEGIN
10
11
#ifndef OWSAssert
12
13
#define CONVERT_TO_STRING(X) #X
14
#define CONVERT_EXPR_TO_STRING(X) CONVERT_TO_STRING(X)
15
16
#define OWSAssertDebugUnlessRunningTests(X) \
17
do { \
18
if (!CurrentAppContext().isRunningTests) { \
19
OWSAssertDebug(X); \
20
} \
21
} while (NO)
22
23
#ifdef DEBUG
24
25
#define USE_ASSERTS
26
27
// OWSAssertDebug() and OWSFailDebug() should be used in Obj-C methods.
28
// OWSCAssertDebug() and OWSCFailDebug() should be used in free functions.
29
30
#define OWSAssertDebug(X) \
31
do { \
32
if (!(X)) { \
33
OWSLogError(@"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
34
OWSLogFlush(); \
35
NSAssert(0, @"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
36
} \
37
} while (NO)
38
39
#define OWSCAssertDebug(X) \
40
do { \
41
if (!(X)) { \
42
OWSLogError(@"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
43
OWSLogFlush(); \
44
NSCAssert(0, @"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
45
} \
46
} while (NO)
47
48
#define OWSFailWithoutLogging(message, ...) \
49
do { \
50
NSString *formattedMessage = [NSString stringWithFormat:message, ##__VA_ARGS__]; \
51
if (IsDebuggerAttached()) { \
52
TrapDebugger(); \
53
} else { \
54
NSAssert(0, formattedMessage); \
55
} \
56
} while (NO)
57
58
#define OWSCFailWithoutLogging(message, ...) \
59
do { \
60
NSString *formattedMessage = [NSString stringWithFormat:message, ##__VA_ARGS__]; \
61
NSCAssert(0, formattedMessage); \
62
} while (NO)
63
64
#define OWSFailNoFormat(message) \
65
do { \
66
OWSLogError(@"%@", message); \
67
OWSLogFlush(); \
68
NSAssert(0, message); \
69
} while (NO)
70
71
#define OWSCFailNoFormat(message) \
72
do { \
73
OWSLogError(@"%@", message); \
74
OWSLogFlush(); \
75
NSCAssert(0, message); \
76
} while (NO)
77
78
#else
79
80
#define OWSAssertDebug(X)
81
#define OWSCAssertDebug(X)
82
#define OWSFailWithoutLogging(message, ...)
83
#define OWSCFailWithoutLogging(message, ...)
84
#define OWSFailNoFormat(X)
85
#define OWSCFailNoFormat(X)
86
87
#endif
88
89
#endif
90
91
// Like OWSAssertDebug, but will fail in production, terminating the app
92
#define OWSPrecondition(X) \
93
do { \
94
if (!(X)) { \
95
OWSFail(@"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
96
} \
97
} while (NO)
98
99
#define OWSCPrecondition(X) \
100
do { \
101
if (!(X)) { \
102
OWSCFail(@"Assertion failed: %s", CONVERT_EXPR_TO_STRING(X)); \
103
} \
104
} while (NO)
105
106
#define OWSAbstractMethod() OWSFail(@"Method needs to be implemented by subclasses.")
107
108
// This macro is intended for use in Objective-C.
109
#define OWSAssertIsOnMainThread() OWSCAssertDebug([NSThread isMainThread])
110
111
#define OWSFailDebug(_messageFormat, ...) \
112
do { \
113
OWSLogError(_messageFormat, ##__VA_ARGS__); \
114
OWSFailWithoutLogging(_messageFormat, ##__VA_ARGS__); \
115
} while (0)
116
117
#define OWSCFailDebug(_messageFormat, ...) \
118
do { \
119
OWSLogError(_messageFormat, ##__VA_ARGS__); \
120
OWSCFailWithoutLogging(_messageFormat, ##__VA_ARGS__); \
121
} while (NO)
122
123
void SwiftExit(NSString *message, const char *file, const char *function, int line);
124
125
#define OWSFail(_messageFormat, ...) \
126
do { \
127
OWSFailDebug(_messageFormat, ##__VA_ARGS__); \
128
\
129
NSString *_message = [NSString stringWithFormat:_messageFormat, ##__VA_ARGS__]; \
130
SwiftExit(_message, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
131
} while (0)
132
133
#define OWSCFail(_messageFormat, ...) \
134
do { \
135
OWSCFailDebug(_messageFormat, ##__VA_ARGS__); \
136
\
137
NSString *_message = [NSString stringWithFormat:_messageFormat, ##__VA_ARGS__]; \
138
SwiftExit(_message, __FILE__, __PRETTY_FUNCTION__, __LINE__); \
139
} while (NO)
140
141
__attribute__((annotate("returns_localized_nsstring"))) static inline NSString *LocalizationNotNeeded(NSString *s)
142
{
143
return s;
144
}
145
146
NS_ASSUME_NONNULL_END
147
148