Skip to content

Commit

Permalink
[iOS Example]: Adding logger override example + Updating file headers (
Browse files Browse the repository at this point in the history
  • Loading branch information
mliao95 authored Oct 16, 2024
1 parent a3fa1d4 commit eb7270a
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
F72A640E2BD3160000881A24 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72A640D2BD315FF00881A24 /* Notifications.swift */; };
F72A640F2BD3160000881A24 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72A640D2BD315FF00881A24 /* Notifications.swift */; };
F72A64102BD3160000881A24 /* Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = F72A640D2BD315FF00881A24 /* Notifications.swift */; };
F796A3D42CBF4EA000EC5FCE /* CustomLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = F796A3D32CBF4EA000EC5FCE /* CustomLogger.swift */; };
F7B4A1242C115455005C7921 /* AttachmentButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B4A1232C115455005C7921 /* AttachmentButton.swift */; };
F7B4A1252C115455005C7921 /* AttachmentButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B4A1232C115455005C7921 /* AttachmentButton.swift */; };
F7B4A1262C115455005C7921 /* AttachmentButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = F7B4A1232C115455005C7921 /* AttachmentButton.swift */; };
Expand Down Expand Up @@ -134,6 +135,7 @@
7EFF832C2C30045F00865E09 /* AmazonConnectChatIOSDemo.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = AmazonConnectChatIOSDemo.entitlements; sourceTree = "<group>"; };
F712DCA52BCF4A28009FC568 /* NetworkConnectionManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkConnectionManager.swift; sourceTree = "<group>"; };
F72A640D2BD315FF00881A24 /* Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Notifications.swift; sourceTree = "<group>"; };
F796A3D32CBF4EA000EC5FCE /* CustomLogger.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomLogger.swift; sourceTree = "<group>"; };
F7B4A1232C115455005C7921 /* AttachmentButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentButton.swift; sourceTree = "<group>"; };
F7B4A1292C13B7D5005C7921 /* UIView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -331,6 +333,7 @@
7E91594F2BBE67D500821C05 /* Models */ = {
isa = PBXGroup;
children = (
F796A3D32CBF4EA000EC5FCE /* CustomLogger.swift */,
F712DCA52BCF4A28009FC568 /* NetworkConnectionManager.swift */,
7E9159512BBE67D500821C05 /* CreateStartChatResponse.swift */,
7E9159532BBE67D500821C05 /* NetworkManager.swift */,
Expand Down Expand Up @@ -516,6 +519,7 @@
7E91596A2BBE67D500821C05 /* AppDelegate.swift in Sources */,
7E9159642BBE67D500821C05 /* ChatMessageView.swift in Sources */,
7E9159612BBE67D500821C05 /* TestView.swift in Sources */,
F796A3D42CBF4EA000EC5FCE /* CustomLogger.swift in Sources */,
7E829D4E2C24CF2000DCB00E /* ChatManager.swift in Sources */,
F72A640E2BD3160000881A24 /* Notifications.swift in Sources */,
7E9159662BBE67D500821C05 /* ContentView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

import Foundation
import SwiftUI
import AmazonConnectChatIOS
Expand Down Expand Up @@ -35,6 +38,9 @@ class ChatManager: ObservableObject {
chatSession.configure(config: globalConfig)
// Setup handlers for various chat session events
setupChatSessionHandlers(chatSession: chatSession)

let customLogger = CustomLogger()
SDKLogger.configureLogger(customLogger)
}

// MARK: - Init Chat
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

import Foundation
import AmazonConnectChatIOS

class CustomLogger: SDKLoggerProtocol {
private var outputDir: URL
private var loggerCreationDateAndTime: String

init() {
self.outputDir = FileManager.default.temporaryDirectory

let currentDate = Date()
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd_HH-mm"
self.loggerCreationDateAndTime = dateFormatter.string(from: currentDate)
}

func logVerbose(_ message: @autoclosure () -> String) {
let logMessage = "VERBOSE: \(message())"
print(logMessage)
writeToAppTempFile(content: logMessage)
}

func logInfo(_ message: @autoclosure () -> String) {
let logMessage = "INFO: \(message())"
print(logMessage)
writeToAppTempFile(content: logMessage)
}

func logDebug(_ message: @autoclosure () -> String) {
let logMessage = "DEBUG: \(message())"
print(logMessage)
writeToAppTempFile(content: logMessage)
}

func logFault(_ message: @autoclosure () -> String) {
let logMessage = "FAULT: \(message())"
print(logMessage)
writeToAppTempFile(content: logMessage)
}

func logError(_ message: @autoclosure () -> String) {
let logMessage = "ERROR: \(message())"
print(logMessage)
writeToAppTempFile(content: logMessage)
}

func setOutputFileDir(to directory: URL) {
outputDir = directory
}

func writeToAppTempFile(content: String) -> Void {
DispatchQueue.global(qos: .background).async {
do {
let fileName = "\(self.loggerCreationDateAndTime)-amazon-connect-logs.txt"
let filePath = self.outputDir.appendingPathComponent(fileName)

if !FileManager.default.fileExists(atPath: filePath.path) {
// Create the file if it doesn't exist
FileManager.default.createFile(atPath: filePath.path, contents: nil, attributes: nil)
}

// Append content to the file
let fileHandle = try FileHandle(forWritingTo: filePath)
defer { fileHandle.closeFile() }

// Move to the end of the file before appending
fileHandle.seekToEndOfFile()
let timestamp = Date().description
if let data = "[\(timestamp)] \(content) \n".data(using: .utf8) {
fileHandle.write(data)
}
} catch {
print("ERROR")
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//
// NetworkConnectionManager.swift
// iOSChatExample
//
// Created by Liao, Michael on 4/16/24.
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

import Foundation
import Network
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
//
// Notifications.swift
// iOSChatExample
//
// Created by Liao, Michael on 4/19/24.
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

import Foundation

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
//
// AttachmentButton.swift
// iOSChatExample
//
// Created by Liao, Michael on 6/5/24.
//
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0

import SwiftUI

struct AttachmentButton: View {
Expand Down

0 comments on commit eb7270a

Please sign in to comment.