Help us understand the problem. What is going on with this article?

Logging function with automatic call path/function with CustomDebugStringConvertible

More than 1 year has passed since last update.

It is convenient to use a static logging function which automatically prints both the values of the parameters, but also details about where the log call was made, to aid debugging.

e.g. call at line 999 in function someFunctionName() in file User/QiitaUser/project/someFileName.swift

log(someDouble, someCustomObject) 

The Xcode Application output is:

someFileName -> line 999 -> someFunctionName -> someDouble, someCustomObject values...

Here, we take advantage the protocol CustomDebugStringConvertible. All native types (string, double) conform to this protocol, and so will show useful information when printing e.g. someDouble.debugDescription, String(reflecting: someDouble). It is also simple to conform custom objects to this protocol in their definition.

See https://developer.apple.com/documentation/swift/customdebugstringconvertible for official implementation details for CustomDebugStringConvertible

// call the function with the "args" paremter only - the other paramters are assigned automatically 
static func log(_ args: Any?..., tag: String = "", file: String = #file, function: String = #function, line: Int = #line) {

// define a value for DEBUG_LOGGING in Build Settings -> Active Compilation Conditions for the debug target
#if DEBUG_LOGGING

   // extract the calling filename from the complete path, or the complete path, as necessary
   let parsedFileName = String(file.split(separator: "/").last?.split(separator: ".").first ?? "")
   let fileName = (parsedFileName.isEmpty) ? file : parsedFileName

   // extract the calling function name from a string containing the arguments and enclosing brackets
   let functionName = String(function.split(separator: "(").first ?? "")

   // this line defines the location of the log statement
   let header = fileName + "-> line " + String(line) + " -> " + functionName 

   // extract the arguments which conform to CustomDebugStringConvertible, and combine their
   // descriptions via reflection
   let argsAsString = args
      .compactMap{$0 as? CustomDebugStringConvertible}
      .map{
         String(reflecting: $0).replacingOccurrences(of: "Optional", with: "")
      }
      .joined(separator: " ")

   // print to the Application Output
   Swift.print(text + " " + argsAsString)

#endif
}
cayozin
iOS/Android development (Swift/C++/Java/Kotlin/XML/Javascript), backend (php/mySQL) and Machine Learning (Python/Scala)
ayudante
いつもユーザー中心で技術者とコンサルタントがとことん考え抜く それがアユダンテです
https://ayudante.jp/
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした