Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ref: Log for Swift #4201

Merged
merged 8 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 deletions Sentry.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion SentryTestUtils/ClearTestState.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Foundation
import Sentry
@testable import Sentry

public func clearTestState() {
TestCleanup.clearTestState()
Expand Down
1 change: 1 addition & 0 deletions SentryTestUtils/SentryLogExtensions.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
@testable import Sentry

extension SentryLog {
public static func setTestDefaultLogLevel() {
Expand Down
13 changes: 2 additions & 11 deletions Sources/Sentry/SentryLevelHelper.m
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
#import "SentryLevelHelper.h"
#import "SentryBreadcrumb+Private.h"
#import "SentryLevelMapper.h"

@implementation SentryLevelHelper

+ (NSUInteger)breadcrumbLevel:(SentryBreadcrumb *)breadcrumb
NSUInteger
sentry_breadcrumbLevel(SentryBreadcrumb *breadcrumb)
{
return breadcrumb.level;
}

+ (NSString *_Nonnull)getNameFor:(NSUInteger)level
{
return nameForSentryLevel(level);
}

@end
44 changes: 2 additions & 42 deletions Sources/Sentry/SentryLevelMapper.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,56 +2,16 @@
#import "SentrySwift.h"
NS_ASSUME_NONNULL_BEGIN

NSString *const kSentryLevelNameNone = @"none";
NSString *const kSentryLevelNameDebug = @"debug";
NSString *const kSentryLevelNameInfo = @"info";
NSString *const kSentryLevelNameWarning = @"warning";
NSString *const kSentryLevelNameError = @"error";
NSString *const kSentryLevelNameFatal = @"fatal";

SentryLevel
sentryLevelForString(NSString *string)
{
if ([string isEqualToString:kSentryLevelNameNone]) {
return kSentryLevelNone;
}
if ([string isEqualToString:kSentryLevelNameDebug]) {
return kSentryLevelDebug;
}
if ([string isEqualToString:kSentryLevelNameInfo]) {
return kSentryLevelInfo;
}
if ([string isEqualToString:kSentryLevelNameWarning]) {
return kSentryLevelWarning;
}
if ([string isEqualToString:kSentryLevelNameError]) {
return kSentryLevelError;
}
if ([string isEqualToString:kSentryLevelNameFatal]) {
return kSentryLevelFatal;
}

// Default is error, see https://develop.sentry.dev/sdk/event-payloads/#optional-attributes
return kSentryLevelError;
return [SentryLevelHelper levelForName:string];
}

NSString *
nameForSentryLevel(SentryLevel level)
{
switch (level) {
case kSentryLevelNone:
return kSentryLevelNameNone;
case kSentryLevelDebug:
return kSentryLevelNameDebug;
case kSentryLevelInfo:
return kSentryLevelNameInfo;
case kSentryLevelWarning:
return kSentryLevelNameWarning;
case kSentryLevelError:
return kSentryLevelNameError;
case kSentryLevelFatal:
return kSentryLevelNameFatal;
}
return [SentryLevelHelper nameForLevel:level];
}

NS_ASSUME_NONNULL_END
99 changes: 0 additions & 99 deletions Sources/Sentry/SentryLog.m

This file was deleted.

29 changes: 29 additions & 0 deletions Sources/Sentry/SentryLogC.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#import "SentryAsyncSafeLog.h"
#import "SentryFileManager.h"
#import "SentryInternalCDefines.h"
#import "SentryLevelMapper.h"
#import "SentryLog.h"

NS_ASSUME_NONNULL_BEGIN

void
sentry_initializeAsyncLogFile(void)
{
const char *asyncLogPath =
[[sentryApplicationSupportPath() stringByAppendingPathComponent:@"async.log"] UTF8String];

NSError *error;
if (!createDirectoryIfNotExists(sentryApplicationSupportPath(), &error)) {
SENTRY_LOG_ERROR(@"Failed to initialize directory for async log file: %@", error);
return;

Check warning on line 18 in Sources/Sentry/SentryLogC.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryLogC.m#L18

Added line #L18 was not covered by tests
}

if (SENTRY_LOG_ERRNO(
sentry_asyncLogSetFileName(asyncLogPath, true /* overwrite existing log */))
!= 0) {
SENTRY_LOG_ERROR(
@"Could not open a handle to specified path for async logging %s", asyncLogPath);

Check warning on line 25 in Sources/Sentry/SentryLogC.m

View check run for this annotation

Codecov / codecov/patch

Sources/Sentry/SentryLogC.m#L25

Added line #L25 was not covered by tests
};
}

NS_ASSUME_NONNULL_END
14 changes: 0 additions & 14 deletions Sources/Sentry/SentryLogOutput.m

This file was deleted.

8 changes: 1 addition & 7 deletions Sources/Sentry/include/SentryLevelHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,6 @@ NS_ASSUME_NONNULL_BEGIN
/**
* This is a workaround to access SentryLevel value from swift
*/
@interface SentryLevelHelper : NSObject

+ (NSUInteger)breadcrumbLevel:(SentryBreadcrumb *)breadcrumb;

+ (NSString *_Nonnull)getNameFor:(NSUInteger)level;

@end
NSUInteger sentry_breadcrumbLevel(SentryBreadcrumb *breadcrumb);

NS_ASSUME_NONNULL_END
7 changes: 0 additions & 7 deletions Sources/Sentry/include/SentryLevelMapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,6 @@

NS_ASSUME_NONNULL_BEGIN

FOUNDATION_EXPORT NSString *const kSentryLevelNameNone;
FOUNDATION_EXPORT NSString *const kSentryLevelNameDebug;
FOUNDATION_EXPORT NSString *const kSentryLevelNameInfo;
FOUNDATION_EXPORT NSString *const kSentryLevelNameWarning;
FOUNDATION_EXPORT NSString *const kSentryLevelNameError;
FOUNDATION_EXPORT NSString *const kSentryLevelNameFatal;

/**
* Maps a string to a SentryLevel. If the passed string doesn't match any level this defaults to
* the 'error' level. See https://develop.sentry.dev/sdk/event-payloads/#optional-attributes
Expand Down
20 changes: 0 additions & 20 deletions Sources/Sentry/include/SentryLog.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
#import "SentryDefines.h"
#import "SentrySwift.h"

@class SentryLogOutput;

NS_ASSUME_NONNULL_BEGIN

@interface SentryLog : NSObject
SENTRY_NO_INIT

+ (void)configure:(BOOL)debug diagnosticLevel:(SentryLevel)level;

+ (void)logWithMessage:(NSString *)message andLevel:(SentryLevel)level;

/**
* @return @c YES if the current logging configuration will log statements at the current level,
* @c NO if not.
*/
+ (BOOL)willLogAtLevel:(SentryLevel)level;

@end

NS_ASSUME_NONNULL_END
#define SENTRY_LOG(_SENTRY_LOG_LEVEL, ...) \
if ([SentryLog willLogAtLevel:_SENTRY_LOG_LEVEL]) { \
[SentryLog logWithMessage:[NSString stringWithFormat:@"[%@:%d] %@", \
Expand Down
4 changes: 4 additions & 0 deletions Sources/Sentry/include/SentryLogC.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#ifndef SentryLogC_h
#define SentryLogC_h
void sentry_initializeAsyncLogFile(void);
#endif
11 changes: 0 additions & 11 deletions Sources/Sentry/include/SentryLogOutput.h

This file was deleted.

1 change: 1 addition & 0 deletions Sources/Sentry/include/SentryPrivate.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "SentryDateUtil.h"
#import "SentryDisplayLinkWrapper.h"
#import "SentryLevelHelper.h"
#import "SentryLogC.h"
#import "SentryRandom.h"
#import "SentrySdkInfo.h"
#import "SentrySession.h"
29 changes: 29 additions & 0 deletions Sources/Swift/Helper/Log/SentryLevel.swift
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
@_implementationOnly import _SentryPrivate
import Foundation

@objc
public enum SentryLevel: UInt {
static let levelNames = ["none", "debug", "info", "warning", "error", "fatal"]

@objc(kSentryLevelNone)
case none = 0

Expand All @@ -21,3 +24,29 @@ public enum SentryLevel: UInt {
@objc(kSentryLevelFatal)
case fatal = 5
}

extension SentryLevel: CustomStringConvertible {
public var description: String {
return SentryLevel.levelNames[Int(self.rawValue)]
}

static func fromName(_ name: String) -> SentryLevel {
guard let index = SentryLevel.levelNames.firstIndex(of: name) else { return .error }
return SentryLevel(rawValue: UInt(index)) ?? .error
}
}

@objcMembers
class SentryLevelHelper: NSObject {
static func nameForLevel(_ level: SentryLevel) -> String {
return level.description
}

static func levelForName(_ name: String) -> SentryLevel {
.fromName(name)
}

static func breadcrumbLevel(_ breadcrumb: Breadcrumb) -> SentryLevel? {
SentryLevel(rawValue: sentry_breadcrumbLevel(breadcrumb))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import Foundation
class SentryRRWebBreadcrumbEvent: SentryRRWebCustomEvent {
init(timestamp: Date, category: String, message: String? = nil, level: SentryLevel = .none, data: [String: Any]? = nil) {

var payload: [String: Any] = ["type": "default", "category": category, "level": SentryLevelHelper.getNameFor(level.rawValue), "timestamp": timestamp.timeIntervalSince1970 ]
var payload: [String: Any] = ["type": "default", "category": category, "level": level.description, "timestamp": timestamp.timeIntervalSince1970 ]

if let message = message {
payload["message"] = message
Expand Down
Loading
Loading