Problems with Unified Logging, Staticstring, Customstringconvertible and Description

Problems with Unified Logging, StaticString, CustomStringConvertible and description

A string doesn't have to be static, as long as the format includes %{public}@. Here's a complete example:

import UIKit
import os
let mylog = OSLog(subsystem: "com.neuburg.matt", category: "testing")
class Foo: CustomStringConvertible {
var bar:Int = 1
var description: String {
return "<\(type(of: self)): bar = \(bar)>"
}
}
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let foo = Foo()
os_log("%{public}@", log: mylog, String(describing:foo))
}
}

Prints:

[testing] <Foo: bar = 1>

...which I believe was the goal.

Using os_log to log function arguments, or other dynamic data

See Logging:

Formatting Log Messages

To format a log message, use a standard NSString or printf format string, ...

and String Format Specifiers for the standard format string specifiers, such as %@ and %d.

In your case:

os_log("foo: %@ %@", log: .default, type: .debug, x, y.description)

The format string is restricted to static strings to prevent
(unintentional) expansion of format string specifiers. Here is an example demonstrating the
problem, using NSLog() because that does not restrict the format
to constant strings:

let s = "50%"
NSLog("\(s)percent")
// Output: 500x0ercent

The %p expects a pointer on the variable argument list, which is
not provided. This is undefined behavior, it can lead to crashes
or unexpected output.

os_log - use of unresolved identifier error

Because you forgot to

import os 

at the start of this file.

Passing variadic args in Swift 4 for os_log

Did some more research on this. Turns out that os_log is actually a C macro. This created all sorts of problems with how it maps in to Swifts variadic args.

However, that macro also captures other debugging info and is probably not safe to wrap up anyways.

App crashing when fetching 800 contacts using iOS 9 Contacts framework

I would check if thumbnailImageData is nil and ignore it in that case. It's possible that a record is marked as having a thumbnail but it can't be loaded for some reason, like an incompatible image type.

How to print out the method name and line number in swift

Literal        Type     Value

#file String The name of the file in which it appears.
#line Int The line number on which it appears.
#column Int The column number in which it begins.
#function String The name of the declaration in which it appears.
#dsohandle UnsafeMutablePointer The dso handle.

Example

print("Function: \(#function), line: \(#line)") 

With default values in parameters you can also create a function

public func track(_ message: String, file: String = #file, function: String = #function, line: Int = #line ) { 
print("\(message) called from \(function) \(file):\(line)")
}

which can be used like this

track("enters app")

In Swift 2.1

 Literal        Type     Value

__FILE__ String The name of the file in which it appears.
__LINE__ Int The line number on which it appears.
__COLUMN__ Int The column number in which it begins.
__FUNCTION__ String The name of the declaration in which it appears.

for more info see the documentation



Related Topics



Leave a reply



Submit