0

Problem Statement: I have a program running in Xcode w/ a bunch of print() statement which prints outputs to the debug console just fine. However, I want to be able to also redirect these outputs to a file such that I can get users to send them to me as a way to debug.

Solutions I've found on SO enables me to redirect outputs to file but debug console output would be missing.

The Ask: I want my cake and eat it to. I want to be able to redirect print() statements BOTH to debug console AND file.

SO's I've reference: https://stackoverflow.com/a/46339657/14414215 https://stackoverflow.com/a/53392944/14414215

app4g
  • 670
  • 4
  • 24
  • 1
    You could use the built-in log functionality, https://developer.apple.com/documentation/os/logging – Joakim Danielson Mar 07 '21 at 10:11
  • @JoakimDanielson I did look at apple default/recommended logging. my understanding was that its more involved and needed the users to do more things to get the logs back to me. (like connecting the app to a Mac and navigating somewhere to get it?) – app4g Mar 07 '21 at 23:16
  • I don’t know about that but you should always think twice before reinventing the wheel. With a log framework you have the possibility to have different log levels and to configure your app which one to use for instance, you could also expect good performance for the actual logging and so on. – Joakim Danielson Mar 08 '21 at 08:05
  • @JoakimDanielson yes. agree. For my side, my priority was to reduce the friction to get users to send me the logs and I didn't know any better way to reduce that. Having said that, will definitely explore more. – app4g Mar 08 '21 at 14:00

1 Answers1

2

Using the 2 linked SO in the question, I managed to alter my CSVoutput function like below. Using this, I just call the function each time.

import Foundation

struct CSVfuncs {

  static func writeLog(_ string: String){
      let filename = "Log_DebugLog.txt"
      let fileURL = FileManager.default.urls(for: .documentDirectory, in: .allDomainsMask).appendingPathComponent(filename)
 
        let data = "\(string)\n"
        
        do {
          print("\(string)")                     // Print to DebugConsole
          try data.appendToURL(fileURL: fileURL) // Redirect to File
        }
        catch {
          print("CSVfuncs writeLog: Could not write data to file")
        }
    }
}

example:

CVSFuncs.writeLog("this is printed to Debug Console & File")
app4g
  • 670
  • 4
  • 24