Skip to content

Commit 935c931

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

24 files changed

Lines changed: 116 additions & 1 deletion

File tree

iOS/APIExample/Common/AgoraExtension.swift

Lines changed: 56 additions & 0 deletions
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

Lines changed: 27 additions & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 0 deletions
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}

0 commit comments

Comments
 (0)