Skip to content

Commit

Permalink
Move OutlineSentryLogger to OutlineAppleLib (#1681)
Browse files Browse the repository at this point in the history
  • Loading branch information
fortuna authored Jul 25, 2023
1 parent af3f51b commit e368323
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 448 deletions.
10 changes: 9 additions & 1 deletion src/cordova/apple/OutlineAppleLib/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ let package = Package(
products: [
.library(
name: "OutlineAppleLib",
targets: ["Tun2socks", "OutlineTunnel"]),
targets: ["Tun2socks", "OutlineSentryLogger", "OutlineTunnel"]),
.library(
name: "PacketTunnelProvider",
targets: ["PacketTunnelProvider"]),
],
dependencies: [
.package(url: "https://github.com/CocoaLumberjack/CocoaLumberjack.git", from: "3.7.4"),
.package(url: "https://github.com/getsentry/sentry-cocoa", from: "7.31.3"),
],
targets: [
.target(
Expand All @@ -29,6 +30,13 @@ let package = Package(
.headerSearchPath("Internal"),
]
),
.target(
name: "OutlineSentryLogger",
dependencies: [
.product(name: "CocoaLumberjackSwift", package: "CocoaLumberjack"),
.product(name: "Sentry", package: "sentry-cocoa")
]
),
.target(
name: "OutlineTunnel",
dependencies: [
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// Copyright 2018 The Outline Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import CocoaLumberjack
import CocoaLumberjackSwift
import Sentry

// Custom CocoaLumberjack logger that logs messages to Sentry.
public class OutlineSentryLogger: DDAbstractLogger {
private static let kDateFormat = "yyyy/MM/dd HH:mm:ss:SSS"
private static let kDatePattern = "[0-9]{4}/[0-9]{2}/[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3}"

private var logsDirectory: String!

// Initializes CocoaLumberjack, adding itself as a logger.
public init(forAppGroup appGroup: String) {
super.init()
guard let containerUrl = FileManager.default.containerURL(
forSecurityApplicationGroupIdentifier: appGroup) else {
DDLogError("Failed to retrieve app container directory")
return
}
self.logsDirectory = containerUrl.appendingPathComponent("Logs").path

DDLog.add(self)
DDLog.add(DDOSLogger.sharedInstance)
dynamicLogLevel = DDLogLevel.info
}

// Adds |logMessage| to Sentry as a breadcrumb.
public override func log(message logMessage: DDLogMessage) {
let breadcrumb = Breadcrumb(level: ddLogLevelToSentryLevel(logMessage.level), category:"App")
breadcrumb.message = logMessage.message
breadcrumb.timestamp = logMessage.timestamp
SentrySDK.addBreadcrumb(crumb: breadcrumb)
}

private func ddLogLevelToSentryLevel(_ level: DDLogLevel) -> SentryLevel {
switch level {
case .error:
return .error
case .warning:
return .warning
case .info:
return .info
default:
return .debug
}
}

// Reads VpnExtension logs and adds them to Sentry as breadcrumbs.
public func addVpnExtensionLogsToSentry(maxBreadcrumbsToAdd: Int) {
var logs: [String]
do {
logs = try FileManager.default.contentsOfDirectory(atPath: self.logsDirectory)
} catch {
DDLogError("Failed to list logs directory. Not sending VPN logs")
return
}

let dateFormatter = DateFormatter()
dateFormatter.dateFormat = OutlineSentryLogger.kDateFormat
var numBreadcrumbsAdded: UInt = 0
// Log files are named by date, get the most recent.
for logFile in logs.sorted().reversed() {
let logFilePath = (self.logsDirectory as NSString).appendingPathComponent(logFile)
DDLogDebug("Reading log file: \(String(describing: logFilePath))")
do {
let logContents = try String(contentsOf: NSURL.fileURL(withPath: logFilePath))
// Order log lines descending by time.
let logLines = logContents.components(separatedBy: "\n").reversed()
for line in logLines {
if numBreadcrumbsAdded >= maxBreadcrumbsToAdd {
return
}
if let (timestamp, message) = parseTimestamp(in: line) {
let breadcrumb = Breadcrumb(level: .info, category: "VpnExtension")
breadcrumb.timestamp = dateFormatter.date(from: timestamp)
breadcrumb.message = message
SentrySDK.addBreadcrumb(crumb: breadcrumb)
numBreadcrumbsAdded += 1
}
}
} catch let error {
DDLogError("Failed to read logs: \(error)")
}
}
}

private func parseTimestamp(in log:String) -> (String, String)? {
do {
let regex = try NSRegularExpression(pattern: OutlineSentryLogger.kDatePattern)
let logNsString = log as NSString // Cast to access NSString length and substring methods.
let results = regex.matches(in: log, range: NSRange(location: 0, length: logNsString.length))
if !results.isEmpty {
let timestamp = logNsString.substring(with: results[0].range)
let message = logNsString.substring(from: timestamp.count)
.trimmingCharacters(in: .whitespacesAndNewlines)
return (timestamp, message)
}
} catch let error {
DDLogError("Failed to parse timestamp: \(error)")
}
return nil
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,7 @@ - (id)init {
id<DDLogFileManager> logFileManager = [[DDLogFileManagerDefault alloc]
initWithLogsDirectory:logsDirectory];
_fileLogger = [[DDFileLogger alloc] initWithLogFileManager:logFileManager];
#if TARGET_OS_IPHONE
[DDLog addLogger:[DDOSLogger sharedInstance]];
#else
if (@available(macOS 10.12, *)) {
[DDLog addLogger:[DDOSLogger sharedInstance]];
} else {
[DDLog addLogger:[DDASLLogger sharedInstance]];
}
#endif
[DDLog addLogger:_fileLogger];

_tunnelStore = [[OutlineTunnelStore alloc] initWithAppGroup:appGroup];
Expand Down
4 changes: 0 additions & 4 deletions src/cordova/apple/xcode/ios/Outline.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
5F7F90AE0E924FD7B065C415 /* CDVStatusBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 0394302BA6114B2AB648D4FF /* CDVStatusBar.m */; };
6AFF5BF91D6E424B00AB3073 /* CDVLaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 6AFF5BF81D6E424B00AB3073 /* CDVLaunchScreen.storyboard */; };
F63DC2182970AFFA00D92E0A /* OutlineAppleLib in Frameworks */ = {isa = PBXBuildFile; productRef = F63DC2172970AFFA00D92E0A /* OutlineAppleLib */; };
FC8C31091FAA8032004262BE /* OutlineSentryLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC8C31081FAA8032004262BE /* OutlineSentryLogger.swift */; };
FC8C310B1FAA814A004262BE /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FC8C310A1FAA814A004262BE /* NetworkExtension.framework */; };
FC8C310C1FAA88FB004262BE /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FC8C310A1FAA814A004262BE /* NetworkExtension.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -116,7 +115,6 @@
F63DC2162970AFE600D92E0A /* OutlineAppleLib */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = OutlineAppleLib; path = ../../src/cordova/apple/OutlineAppleLib; sourceTree = "<group>"; };
F840E1F0165FE0F500CFE078 /* config.xml */ = {isa = PBXFileReference; lastKnownFileType = text.xml; name = config.xml; path = Outline/config.xml; sourceTree = "<group>"; };
FC55AB411F4F960A0056F12C /* VpnExtension-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "VpnExtension-Info.plist"; path = "Outline/VpnExtension-Info.plist"; sourceTree = SOURCE_ROOT; };
FC8C31081FAA8032004262BE /* OutlineSentryLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = OutlineSentryLogger.swift; path = "cordova-plugin-outline/OutlineSentryLogger.swift"; sourceTree = "<group>"; };
FC8C310A1FAA814A004262BE /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = System/Library/Frameworks/NetworkExtension.framework; sourceTree = SDKROOT; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -244,7 +242,6 @@
307C750510C5A3420062BCA9 /* Plugins */ = {
isa = PBXGroup;
children = (
FC8C31081FAA8032004262BE /* OutlineSentryLogger.swift */,
0394302BA6114B2AB648D4FF /* CDVStatusBar.m */,
0168F53D3AFF46F5B346C874 /* CDVStatusBar.h */,
AAFAFA54943F490EAF4CD5BC /* OutlinePlugin.swift */,
Expand Down Expand Up @@ -468,7 +465,6 @@
302D95F114D2391D003F00A1 /* MainViewController.m in Sources */,
5F7F90AE0E924FD7B065C415 /* CDVStatusBar.m in Sources */,
2A617D29B96942E58B082FAC /* OutlinePlugin.swift in Sources */,
FC8C31091FAA8032004262BE /* OutlineSentryLogger.swift in Sources */,
1273B4E700B84E31B2528701 /* CDVClipboard.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
FC5FF9501F3E1FD40032A745 /* NetworkExtension.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = FC5FF9461F3E1E8B0032A745 /* NetworkExtension.framework */; };
FC6E7F8E204DC1BE003CB365 /* CDVMacOsUrlHandler.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC6E7F8D204DC1BE003CB365 /* CDVMacOsUrlHandler.swift */; };
FC7D56051F86969E00ABD5CA /* EventMonitor.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC7D56041F86969E00ABD5CA /* EventMonitor.swift */; };
FC8C30FC1FA7DDCB004262BE /* OutlineSentryLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC8C30FB1FA7DDCB004262BE /* OutlineSentryLogger.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -132,7 +131,6 @@
FC5FF9461F3E1E8B0032A745 /* NetworkExtension.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = NetworkExtension.framework; path = System/Library/Frameworks/NetworkExtension.framework; sourceTree = SDKROOT; };
FC6E7F8D204DC1BE003CB365 /* CDVMacOsUrlHandler.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CDVMacOsUrlHandler.swift; sourceTree = "<group>"; };
FC7D56041F86969E00ABD5CA /* EventMonitor.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EventMonitor.swift; sourceTree = "<group>"; };
FC8C30FB1FA7DDCB004262BE /* OutlineSentryLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = OutlineSentryLogger.swift; path = "cordova-plugin-outline/OutlineSentryLogger.swift"; sourceTree = "<group>"; };
FCB2DED41F3E3CAD000C6A44 /* VpnExtension.entitlements */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.entitlements; path = VpnExtension.entitlements; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -178,7 +176,6 @@
isa = PBXGroup;
children = (
AA09EA80E0C54DFFB24A1810 /* OutlinePlugin.swift */,
FC8C30FB1FA7DDCB004262BE /* OutlineSentryLogger.swift */,
469466FF1BDB44C081741BF5 /* CDVClipboard.m */,
19209C3AABCC45AB8CFDD974 /* CDVClipboard.h */,
);
Expand Down Expand Up @@ -530,7 +527,6 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
FC8C30FC1FA7DDCB004262BE /* OutlineSentryLogger.swift in Sources */,
70BD683318FFB02D00A1EFCF /* main.m in Sources */,
70BD686D18FFB0BF00A1EFCF /* AppDelegate.m in Sources */,
FC6E7F8E204DC1BE003CB365 /* CDVMacOsUrlHandler.swift in Sources */,
Expand Down
Loading

0 comments on commit e368323

Please sign in to comment.