Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities.

This paper also participates inProject DigginTo win the creative gift package and challenge the creative incentive money.

preface

In an old project I did before, I did a security check and found that the App was printing logs under Release mode. In Release mode, you need to cancel log printing.

A look at the project, OC code, used a lot of NSlogs to print logs:

NSLog(@" hit OK ");Copy the code

NSLog works fine, but we need to tweak it.

Use macros in one step

By defining macros to distinguish Debug from Release mode, you can guarantee that no line of print logs will be left under Release mode. This is the most violent, easiest, and minimal way to change:

#ifdef DEBUG #define NSLog(...) NSLog(__VA_ARGS__) #else #define NSLog(...) #endifCopy the code

In Release mode, the macro NSLog(…) There is no implementation, so there can be no data left.

This is indeed a good method, and it is less time-consuming and less invasive. But when we have a certain need for the printed log, what should we do?

But this is only for OC, so you can try the same idea in Swift, and it doesn’t work that well, so you can try it.

Public func swiftPrint(_ items: Any... , separator: String = " ", terminator: String = "\n") { #if DEBUG print(items, separator: separator, terminator: terminator) #endif }Copy the code

If there is a printing requirement, it is recommended to use the library.

CocoaLumberjack

The log library is very old, with a 1.0 version of Tag dating back to 2011, when I think most people hadn’t started iOS development.

This log library provides many functions, log level, custom output format, log write to file and so on, everything, although the main body of the library is written in OC, but has done support for Swift, and very friendly.

If you are interested, you can take a look at this layer of encapsulation from OC to Swift, which can also inspire your own code.

XCGLogger

This library is a logging library written in Swift that feels a bit like the Swift version of CocoaLumberjack.

The last update to this library was two years ago.

SwiftPrettyPrint

Compared with the above two libraries on GitHub, it only has more than 100 stars, which is really small, but I tried to use it and felt good.

Definition:

enum Enum { case foo(Int) } struct ID { let id: Int } struct Struct { var array: [Int?]  var dictionary: [String: Int] var tuple: (Int, string: String) var `enum`: Enum var id: ID } let value = Struct(array: [1, 2, nil], dictionary: ["one": 1, "two": 2], tuple: (1, string: "string"), enum: .foo(42), id: ID(id: 7))Copy the code

Print:

Pretty.print(value)
// Struct(array: [1, 2, nil], dictionary: ["one": 1, "two": 2], tuple: (1, string: "string"), enum: .foo(42), id: 7)

Pretty.prettyPrint(value)
// Struct(
//     array: [
//         1,
//         2,
//         nil
//     ],
//     dictionary: [
//         "one": 1,
//         "two": 2
//     ],
//     tuple: (
//         1,
//         string: "string"
//     ),
//     enum: .foo(42),
//     id: 7
// )

Copy the code

Not only can it print in general and set breakpoints, but it can also print through the library so that debugging of some problems can be made clearer.

Reference documentation

CocoaLumberjack

CocoaLumberJack Integrates the log library

XCGLogger

SwiftPrettyPrint

conclusion

My personal recommendation is that the log print library should be identified at the beginning of the project, so as to avoid replacement and modification later, which is easy to do, but very mechanical.

Alternatively, consider wrapping a layer in your own project so that if you want to replace the logging library, you only need to change it in the packaging layer.

Radish green vegetables have their own preferences, I have listed the log print library is not necessarily suitable for all people to use, we can choose according to the actual situation of their App or their own packaging is ok.

We’ll see you next time.