Skip to content

Commit 935c931

Browse files
committed
add logs
1 parent 1941cf3 commit 935c931

File tree

24 files changed

+116
-1
lines changed

24 files changed

+116
-1
lines changed

iOS/APIExample/Common/AgoraExtension.swift

+56
Original file line numberDiff line numberDiff line change
@@ -257,3 +257,59 @@ extension UIAlertController {
257257
self.addAction(UIAlertAction(title: "Cancel".localized, style: .cancel, handler: nil))
258258
}
259259
}
260+
261+
extension UIApplication {
262+
/// The top most view controller
263+
static var topMostViewController: UIViewController? {
264+
return UIApplication.shared.keyWindow?.rootViewController?.visibleViewController
265+
}
266+
}
267+
268+
extension UIViewController {
269+
/// The visible view controller from a given view controller
270+
var visibleViewController: UIViewController? {
271+
if let navigationController = self as? UINavigationController {
272+
return navigationController.topViewController?.visibleViewController
273+
} else if let tabBarController = self as? UITabBarController {
274+
return tabBarController.selectedViewController?.visibleViewController
275+
} else if let presentedViewController = presentedViewController {
276+
return presentedViewController.visibleViewController
277+
} else {
278+
return self
279+
}
280+
}
281+
}
282+
283+
extension OutputStream {
284+
285+
/// Write `String` to `OutputStream`
286+
///
287+
/// - parameter string: The `String` to write.
288+
/// - parameter encoding: The `String.Encoding` to use when writing the string. This will default to `.utf8`.
289+
/// - parameter allowLossyConversion: Whether to permit lossy conversion when writing the string. Defaults to `false`.
290+
///
291+
/// - returns: Return total number of bytes written upon success. Return `-1` upon failure.
292+
293+
func write(_ string: String, encoding: String.Encoding = .utf8, allowLossyConversion: Bool = false) -> Int {
294+
295+
if let data = string.data(using: encoding, allowLossyConversion: allowLossyConversion) {
296+
let ret = data.withUnsafeBytes {
297+
write($0, maxLength: data.count)
298+
}
299+
if(ret < 0) {
300+
print("write fail: \(streamError.debugDescription)")
301+
}
302+
}
303+
304+
return -1
305+
}
306+
307+
}
308+
309+
extension Date {
310+
func getFormattedDate(format: String) -> String {
311+
let dateformat = DateFormatter()
312+
dateformat.dateFormat = format
313+
return dateformat.string(from: self)
314+
}
315+
}

iOS/APIExample/Common/LogViewController.swift

+27
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,42 @@ struct LogItem {
2929

3030
class LogUtils {
3131
static var logs:[LogItem] = []
32+
static var appLogPath:String = "\(logFolder())/app-\(Date().getFormattedDate(format: "yyyy-MM-dd")).log"
3233

3334
static func log(message: String, level: LogLevel) {
3435
LogUtils.logs.append(LogItem(message: message, level: level, dateTime: Date()))
3536
print("\(level.description): \(message)")
3637
}
3738

39+
static func logFolder() -> String {
40+
return "\(NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0])/logs"
41+
}
42+
static func sdkLogPath() -> String {
43+
return "\(logFolder())/agorasdk.log"
44+
}
45+
3846
static func removeAll() {
3947
LogUtils.logs.removeAll()
4048
}
49+
50+
static func writeAppLogsToDisk() {
51+
if let outputStream = OutputStream(url: URL(fileURLWithPath: LogUtils.appLogPath), append: true) {
52+
outputStream.open()
53+
for log in LogUtils.logs {
54+
let msg = "\(log.level.description) \(log.dateTime.getFormattedDate(format: "yyyy-MM-dd HH:mm:ss")) \(log.message)\n"
55+
let bytesWritten = outputStream.write(msg)
56+
if bytesWritten < 0 { print("write failure") }
57+
}
58+
outputStream.close()
59+
LogUtils.removeAll()
60+
} else {
61+
print("Unable to open file")
62+
}
63+
}
64+
65+
static func cleanUp() {
66+
try? FileManager.default.removeItem(at: URL(fileURLWithPath: LogUtils.logFolder(), isDirectory: true))
67+
}
4168
}
4269

4370
class LogViewController: AGViewController {

iOS/APIExample/Examples/Advanced/AudioMixing/AudioMixing.swift

+1
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ class AudioMixingMain: BaseViewController {
9898
config.appId = KeyCenter.AppId
9999
config.areaCode = GlobalSettings.shared.area.rawValue
100100
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
101+
agoraKit.setLogFile(LogUtils.sdkLogPath())
101102

102103
guard let channelName = configs["channelName"] as? String,
103104
let audioProfile = configs["audioProfile"] as? AgoraAudioProfile,

iOS/APIExample/Examples/Advanced/CustomAudioRender/CustomAudioRender.swift

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class CustomAudioRenderMain: BaseViewController {
5353
config.appId = KeyCenter.AppId
5454
config.areaCode = GlobalSettings.shared.area.rawValue
5555
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
56+
agoraKit.setLogFile(LogUtils.sdkLogPath())
5657

5758
guard let channelName = configs["channelName"] as? String else {return}
5859

iOS/APIExample/Examples/Advanced/CustomAudioSource/CustomAudioSource.swift

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class CustomAudioSourceMain: BaseViewController {
5353
config.appId = KeyCenter.AppId
5454
config.areaCode = GlobalSettings.shared.area.rawValue
5555
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
56+
agoraKit.setLogFile(LogUtils.sdkLogPath())
5657

5758
guard let channelName = configs["channelName"] as? String else {return}
5859

iOS/APIExample/Examples/Advanced/CustomVideoRender/CustomVideoRender.swift

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ class CustomVideoRenderMain: BaseViewController {
5555
config.appId = KeyCenter.AppId
5656
config.areaCode = GlobalSettings.shared.area.rawValue
5757
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
58+
agoraKit.setLogFile(LogUtils.sdkLogPath())
5859

5960
// get channel name from configs
6061
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/CustomVideoSourceMediaIO/CustomVideoSourceMediaIO.swift

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class CustomVideoSourceMediaIOMain: BaseViewController {
5656
config.appId = KeyCenter.AppId
5757
config.areaCode = GlobalSettings.shared.area.rawValue
5858
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
59+
agoraKit.setLogFile(LogUtils.sdkLogPath())
5960

6061
// get channel name from configs
6162
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/CustomVideoSourcePush/CustomVideoSourcePush.swift

+1
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class CustomVideoSourcePushMain: BaseViewController {
7272
config.appId = KeyCenter.AppId
7373
config.areaCode = GlobalSettings.shared.area.rawValue
7474
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
75+
agoraKit.setLogFile(LogUtils.sdkLogPath())
7576

7677
// get channel name from configs
7778
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/JoinMultiChannel/JoinMultiChannel.swift

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class JoinMultiChannelMain: BaseViewController {
5858
config.appId = KeyCenter.AppId
5959
config.areaCode = GlobalSettings.shared.area.rawValue
6060
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
61+
agoraKit.setLogFile(LogUtils.sdkLogPath())
6162

6263
// get channel name from configs
6364
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/MediaChannelRelay/MediaChannelRelay.swift

+1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ class MediaChannelRelayMain: BaseViewController {
6666
config.appId = KeyCenter.AppId
6767
config.areaCode = GlobalSettings.shared.area.rawValue
6868
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
69+
agoraKit.setLogFile(LogUtils.sdkLogPath())
6970

7071
// get channel name from configs
7172
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/MediaPlayer/MediaPlayer.swift

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ class MediaPlayerMain: BaseViewController {
6363
config.appId = KeyCenter.AppId
6464
config.areaCode = GlobalSettings.shared.area.rawValue
6565
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
66+
agoraKit.setLogFile(LogUtils.sdkLogPath())
6667

6768
// get channel name from configs
6869
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/PrecallTest/PrecallTest.swift

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class PrecallTestEntry : UIViewController
2929
config.appId = KeyCenter.AppId
3030
config.areaCode = GlobalSettings.shared.area.rawValue
3131
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
32+
agoraKit.setLogFile(LogUtils.sdkLogPath())
3233

3334
// have to be a broadcaster for doing echo test
3435
agoraKit.setChannelProfile(.liveBroadcasting)

iOS/APIExample/Examples/Advanced/QuickSwitchChannel/QuickSwitchChannel.swift

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class QuickSwitchChannel: BaseViewController {
7979
config.appId = KeyCenter.AppId
8080
config.areaCode = GlobalSettings.shared.area.rawValue
8181
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
82+
agoraKit.setLogFile(LogUtils.sdkLogPath())
8283

8384
// get channel name from configs
8485
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/RTMPInjection/RTMPInjection.swift

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ class RTMPInjectionMain: BaseViewController {
6767
config.appId = KeyCenter.AppId
6868
config.areaCode = GlobalSettings.shared.area.rawValue
6969
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
70+
agoraKit.setLogFile(LogUtils.sdkLogPath())
7071

7172
guard let channelName = configs["channelName"] as? String else {return}
7273

iOS/APIExample/Examples/Advanced/RTMPStreaming/RTMPStreaming.swift

+1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ class RTMPStreamingMain: BaseViewController {
8484
config.appId = KeyCenter.AppId
8585
config.areaCode = GlobalSettings.shared.area.rawValue
8686
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
87+
agoraKit.setLogFile(LogUtils.sdkLogPath())
8788

8889
guard let channelName = configs["channelName"] as? String else {return}
8990

iOS/APIExample/Examples/Advanced/RawMediaData/RawMediaData.swift

+1
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class RawMediaDataMain: BaseViewController {
5656
config.appId = KeyCenter.AppId
5757
config.areaCode = GlobalSettings.shared.area.rawValue
5858
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
59+
agoraKit.setLogFile(LogUtils.sdkLogPath())
5960

6061
// get channel name from configs
6162
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/ScreenShare/ScreenShare.swift

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class ScreenShareMain: BaseViewController {
6161
config.appId = KeyCenter.AppId
6262
config.areaCode = GlobalSettings.shared.area.rawValue
6363
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
64+
agoraKit.setLogFile(LogUtils.sdkLogPath())
6465

6566
// get channel name from configs
6667
guard let channelName = configs["channelName"] as? String else {return}

iOS/APIExample/Examples/Advanced/StreamEncryption/StreamEncryption.swift

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ class StreamEncryptionMain: BaseViewController {
8585
config.appId = KeyCenter.AppId
8686
config.areaCode = GlobalSettings.shared.area.rawValue
8787
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
88+
agoraKit.setLogFile(LogUtils.sdkLogPath())
8889

8990
// get channel name from configs
9091
guard let channelName = configs["channelName"] as? String,

iOS/APIExample/Examples/Advanced/VideoMetadata/VideoMetadata.swift

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class VideoMetadataMain: BaseViewController {
6868
config.appId = KeyCenter.AppId
6969
config.areaCode = GlobalSettings.shared.area.rawValue
7070
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
71+
agoraKit.setLogFile(LogUtils.sdkLogPath())
7172

7273
// register metadata delegate and datasource
7374
agoraKit.setMediaMetadataDataSource(self, with: .video)

iOS/APIExample/Examples/Advanced/VoiceChanger/VoiceChanger.swift

+1
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,7 @@ class VoiceChangerMain: BaseViewController {
354354
config.appId = KeyCenter.AppId
355355
config.areaCode = GlobalSettings.shared.area.rawValue
356356
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
357+
agoraKit.setLogFile(LogUtils.sdkLogPath())
357358

358359
guard let channelName = configs["channelName"] as? String else {return}
359360
self.title = channelName

iOS/APIExample/Examples/Basic/JoinChannelAudio/JoinChannelAudio.swift

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class JoinChannelAudioMain: BaseViewController {
9393
config.appId = KeyCenter.AppId
9494
config.areaCode = GlobalSettings.shared.area.rawValue
9595
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
96+
agoraKit.setLogFile(LogUtils.sdkLogPath())
9697

9798
guard let channelName = configs["channelName"] as? String,
9899
let audioProfile = configs["audioProfile"] as? AgoraAudioProfile,

iOS/APIExample/Examples/Basic/JoinChannelVideo/JoinChannelVideo.swift

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ class JoinChannelVideoMain: BaseViewController {
118118
config.appId = KeyCenter.AppId
119119
config.areaCode = GlobalSettings.shared.area.rawValue
120120
agoraKit = AgoraRtcEngineKit.sharedEngine(with: config, delegate: self)
121+
agoraKit.setLogFile(LogUtils.sdkLogPath())
121122

122123
// get channel name from configs
123124
guard let channelName = configs["channelName"] as? String,

iOS/APIExample/ViewController.swift

+12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
//
88

99
import UIKit
10+
import Floaty
1011

1112
struct MenuSection {
1213
var name: String
@@ -52,6 +53,17 @@ class ViewController: AGViewController {
5253

5354
override func viewDidLoad() {
5455
super.viewDidLoad()
56+
Floaty.global.button.addItem(title: "Send Logs", handler: {item in
57+
LogUtils.writeAppLogsToDisk()
58+
let activity = UIActivityViewController(activityItems: [NSURL(fileURLWithPath: LogUtils.logFolder(), isDirectory: true)], applicationActivities: nil)
59+
UIApplication.topMostViewController?.present(activity, animated: true, completion: nil)
60+
})
61+
62+
Floaty.global.button.addItem(title: "Clean Up", handler: {item in
63+
LogUtils.cleanUp()
64+
})
65+
Floaty.global.button.isDraggable = true
66+
Floaty.global.show()
5567
}
5668
}
5769

iOS/Podfile

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
target 'APIExample' do
55

66
use_frameworks!
7-
7+
pod 'Floaty', '~> 4.2.0'
88
pod 'AGEVideoLayout', '~> 1.0.2'
99
# use this if you don't need encryption feature
1010
# pod 'AgoraRtcEngine_iOS', '~> 3.1.1'

0 commit comments

Comments
 (0)