DaveWoodCom / XCGLogger

A debug log framework for use in Swift projects. Allows you to log details to the console (and optionally a file), just like you would have with NSLog() or print(), but with additional information, such as the date, function name, filename and line number.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Log as variadic function just like `print()`

ladislas opened this issue · comments

Hi there!

First of all, let me wish you a very happy new year and thank you for the awesome work you've put together building XCGLogger. I've been using it for some years now, have tried some competitors, but they cannot compete ;)

One thing I find missing though, is the inability to log variadic parameters, like print() would do:

print("Hello", "World", 42, {3 + 4}()) // --> "Hello World 42 7"

log.debug("Hello", "World", 42, {3 + 4}()) // --> Error "Missing argument labels 'fileName:lineNumber:' in call"

I can of course change it to this, but it's not IMO as user-friendly:

let num = 42
log.debug("Hello World \(num)")

It there a particular reason this has not been implement? And it this something that people might find useful? If so, I'd be happy to give it a shot and provide you with a PR.

Let me know!
Best,
-- Ladislas

After digging through your code and reading this discussion on Swift's Forum, I've learned about @autoclosure.

If I understand correctly, it is used as a performance boost to defer the execution of closures to a later time and not when the argument is passed, for example, to the log.debug() function.

And it is not possible to have variadic @autoclosure.

So that might answer my previous question.

@ladislas please close if there is no more question

Adding function with same name but Any... in addition from one with autoclosure, I think some conflicts will occurs

as extension with other name I think it is possible, create its own closure

   open class func print(_ items: Any..., functionName: StaticString = #function, fileName: StaticString = #file, lineNumber: Int = #line, userInfo: [String: Any] = [:]) {
       self.default.logln(.debug, functionName: functionName, fileName: fileName, lineNumber: lineNumber, userInfo: userInfo) {
           return items.map { String(describing: $0) }.joined(separator: " ")
       }
   }

or you can log with your closure
XCGLogger.debug { ["test", "test1", "test2"].joined(separator: " ") }
or with other type from string (extension on sequence could be done)
XCGLogger.debug { ["test", "test1", 5].map { String(describing: $0) }.joined(separator: " ") }

Error "Missing argument labels 'fileName:lineNumber:' in call"

Just to demystify the error: a logger is not a simple print and have some features like showing line of code and fileName , lineNumber are parameters filled automatically to get your log location

thanks @phimage for the clarification!