From 5269d20aec0dcd3f6b14f14eb6dc41a89af71f5a Mon Sep 17 00:00:00 2001 From: Philipp Hofmann Date: Wed, 18 Sep 2024 12:59:10 +0200 Subject: [PATCH] ref: Serialization use logging macros (#4345) Use the logging macros everywhere for consistency. --- Sources/Sentry/SentrySerialization.m | 101 ++++++++++----------------- 1 file changed, 37 insertions(+), 64 deletions(-) diff --git a/Sources/Sentry/SentrySerialization.m b/Sources/Sentry/SentrySerialization.m index 80d5ca61da..c43f047e2b 100644 --- a/Sources/Sentry/SentrySerialization.m +++ b/Sources/Sentry/SentrySerialization.m @@ -100,32 +100,33 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data error:&error]; if (nil != error) { SENTRY_LOG_ERROR(@"Failed to parse envelope header %@", error); - } else { - SentryId *eventId = nil; - NSString *eventIdAsString = headerDictionary[@"event_id"]; - if (nil != eventIdAsString) { - eventId = [[SentryId alloc] initWithUUIDString:eventIdAsString]; - } - - SentrySdkInfo *sdkInfo = nil; - if (nil != headerDictionary[@"sdk"]) { - sdkInfo = [[SentrySdkInfo alloc] initWithDict:headerDictionary]; - } - - SentryTraceContext *traceContext = nil; - if (nil != headerDictionary[@"trace"]) { - traceContext = - [[SentryTraceContext alloc] initWithDict:headerDictionary[@"trace"]]; - } - - envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:eventId - sdkInfo:sdkInfo - traceContext:traceContext]; - - if (headerDictionary[@"sent_at"] != nil) { - envelopeHeader.sentAt = sentry_fromIso8601String(headerDictionary[@"sent_at"]); - } + break; + } + + SentryId *eventId = nil; + NSString *eventIdAsString = headerDictionary[@"event_id"]; + if (nil != eventIdAsString) { + eventId = [[SentryId alloc] initWithUUIDString:eventIdAsString]; + } + + SentrySdkInfo *sdkInfo = nil; + if (nil != headerDictionary[@"sdk"]) { + sdkInfo = [[SentrySdkInfo alloc] initWithDict:headerDictionary]; } + + SentryTraceContext *traceContext = nil; + if (nil != headerDictionary[@"trace"]) { + traceContext = [[SentryTraceContext alloc] initWithDict:headerDictionary[@"trace"]]; + } + + envelopeHeader = [[SentryEnvelopeHeader alloc] initWithId:eventId + sdkInfo:sdkInfo + traceContext:traceContext]; + + if (headerDictionary[@"sent_at"] != nil) { + envelopeHeader.sentAt = sentry_fromIso8601String(headerDictionary[@"sent_at"]); + } + break; } } @@ -135,7 +136,6 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data return nil; } - NSAssert(envelopeHeaderIndex > 0, @"EnvelopeHeader was parsed, its index is expected."); if (envelopeHeaderIndex == 0) { SENTRY_LOG_ERROR(@"EnvelopeHeader was parsed, its index is expected."); return nil; @@ -155,38 +155,27 @@ + (SentryEnvelope *_Nullable)envelopeWithData:(NSData *)data #ifdef DEBUG NSString *itemHeaderString = [[NSString alloc] initWithData:itemHeaderData encoding:NSUTF8StringEncoding]; - [SentryLog - logWithMessage:[NSString stringWithFormat:@"Item Header %@", itemHeaderString] - andLevel:kSentryLevelDebug]; + SENTRY_LOG_DEBUG(@"Item Header %@", itemHeaderString); #endif NSError *error = nil; NSDictionary *headerDictionary = [NSJSONSerialization JSONObjectWithData:itemHeaderData options:0 error:&error]; if (nil != error) { - [SentryLog - logWithMessage:[NSString - stringWithFormat:@"Failed to parse envelope item header %@", - error] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Failed to parse envelope item header %@", error); return nil; } NSString *_Nullable type = [headerDictionary valueForKey:@"type"]; if (nil == type) { - [SentryLog - logWithMessage:[NSString stringWithFormat:@"Envelope item type is required."] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Envelope item type is required."); break; } NSNumber *bodyLengthNumber = [headerDictionary valueForKey:@"length"]; NSUInteger bodyLength = [bodyLengthNumber unsignedIntegerValue]; if (endOfEnvelope == i && bodyLength != 0) { - [SentryLog - logWithMessage:[NSString - stringWithFormat:@"Envelope item has no data but header " - @"indicates it's length is %d.", - (int)bodyLength] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR( + @"Envelope item has no data but header indicates it's length is %d.", + (int)bodyLength); break; } @@ -246,10 +235,7 @@ + (SentrySession *_Nullable)sessionWithData:(NSData *)sessionData options:0 error:&error]; if (nil != error) { - [SentryLog - logWithMessage:[NSString - stringWithFormat:@"Failed to deserialize session data %@", error] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Failed to deserialize session data %@", error); return nil; } SentrySession *session = [[SentrySession alloc] initWithJSONObject:sessionDictionary]; @@ -260,9 +246,7 @@ + (SentrySession *_Nullable)sessionWithData:(NSData *)sessionData } if (nil == session.releaseName || [session.releaseName isEqualToString:@""]) { - [SentryLog - logWithMessage:@"Deserialized session doesn't contain a release name. Dropping it." - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Deserialized session doesn't contain a release name. Dropping it."); return nil; } @@ -286,10 +270,7 @@ + (SentryAppState *_Nullable)appStateWithData:(NSData *)data options:0 error:&error]; if (nil != error) { - [SentryLog - logWithMessage:[NSString - stringWithFormat:@"Failed to deserialize app state data %@", error] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Failed to deserialize app state data %@", error); return nil; } @@ -303,11 +284,7 @@ + (NSDictionary *)deserializeDictionaryFromJsonData:(NSData *)data options:0 error:&error]; if (nil != error) { - [SentryLog - logWithMessage:[NSString - stringWithFormat:@"Failed to deserialize json item dictionary: %@", - error] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Failed to deserialize json item dictionary: %@", error); } return eventDictionary; @@ -320,11 +297,7 @@ + (SentryLevel)levelFromData:(NSData *)eventEnvelopeItemData options:0 error:&error]; if (nil != error) { - [SentryLog - logWithMessage:[NSString stringWithFormat: - @"Failed to retrieve event level from envelope item data: %@", - error] - andLevel:kSentryLevelError]; + SENTRY_LOG_ERROR(@"Failed to retrieve event level from envelope item data: %@", error); return kSentryLevelError; }