Skip to content

Commit 47ba685

Browse files
committed
add logs
1 parent f20cac7 commit 47ba685

24 files changed

Lines changed: 119 additions & 1 deletion

File tree

iOS/APIExample/Common/AgoraExtension.swift

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,3 +208,59 @@ extension UIAlertController {
208208
self.addAction(UIAlertAction(title: "Cancel".localized, style: .cancel, handler: nil))
209209
}
210210
}
211+
212+
extension UIApplication {
213+
/// The top most view controller
214+
static var topMostViewController: UIViewController? {
215+
return UIApplication.shared.keyWindow?.rootViewController?.visibleViewController
216+
}
217+
}
218+
219+
extension UIViewController {
220+
/// The visible view controller from a given view controller
221+
var visibleViewController: UIViewController? {
222+
if let navigationController = self as? UINavigationController {
223+
return navigationController.topViewController?.visibleViewController
224+
} else if let tabBarController = self as? UITabBarController {
225+
return tabBarController.selectedViewController?.visibleViewController
226+
} else if let presentedViewController = presentedViewController {
227+
return presentedViewController.visibleViewController
228+
} else {
229+
return self
230+
}
231+
}
232+
}
233+
234+
extension OutputStream {
235+
236+
/// Write `String` to `OutputStream`
237+
///
238+
/// - parameter string: The `String` to write.
239+
/// - parameter encoding: The `String.Encoding` to use when writing the string. This will default to `.utf8`.
240+
/// - parameter allowLossyConversion: Whether to permit lossy conversion when writing the string. Defaults to `false`.
241+
///
242+
/// - returns: Return total number of bytes written upon success. Return `-1` upon failure.
243+
244+
func write(_ string: String, encoding: String.Encoding = .utf8, allowLossyConversion: Bool = false) -> Int {
245+
246+
if let data = string.data(using: encoding, allowLossyConversion: allowLossyConversion) {
247+
let ret = data.withUnsafeBytes {
248+
write($0, maxLength: data.count)
249+
}
250+
if(ret < 0) {
251+
print("write fail: \(streamError.debugDescription)")
252+
}
253+
}
254+
255+
return -1
256+
}
257+
258+
}
259+
260+
extension Date {
261+
func getFormattedDate(format: String) -> String {
262+
let dateformat = DateFormatter()
263+
dateformat.dateFormat = format
264+
return dateformat.string(from: self)
265+
}
266+
}

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)