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

4
  • 1
    You could use the built-in log functionality, developer.apple.com/documentation/os/logging Commented Mar 7, 2021 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?) Commented Mar 7, 2021 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. Commented Mar 8, 2021 at 8: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. Commented Mar 8, 2021 at 14:00

1 Answer 1

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")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.