|
1 | 1 | /*
|
2 |
| - * NSLogger.swift |
3 |
| - * |
4 |
| - * version 1.8.0 26-MARCH-2017 |
5 |
| - * |
6 |
| - * Part of NSLogger (client side) |
7 |
| - * https://github.com/fpillet/NSLogger |
8 |
| - * |
9 |
| - * BSD license follows (http://www.opensource.org/licenses/bsd-license.php) |
10 |
| - * |
11 |
| - * Copyright (c) 2010-2017 Florent Pillet All Rights Reserved. |
12 |
| - * |
13 |
| - * Redistribution and use in source and binary forms, with or without modification, |
14 |
| - * are permitted provided that the following conditions are met: |
15 |
| - * |
16 |
| - * Redistributions of source code must retain the above copyright notice, |
17 |
| - * this list of conditions and the following disclaimer. Redistributions in |
18 |
| - * binary form must reproduce the above copyright notice, this list of |
19 |
| - * conditions and the following disclaimer in the documentation and/or other |
20 |
| - * materials provided with the distribution. Neither the name of Florent |
21 |
| - * Pillet nor the names of its contributors may be used to endorse or promote |
22 |
| - * products derived from this software without specific prior written |
23 |
| - * permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
24 |
| - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
25 |
| - * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
26 |
| - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
27 |
| - * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
28 |
| - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
29 |
| - * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
30 |
| - * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
31 |
| - * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
32 |
| - * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
33 |
| - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
34 |
| - * |
35 |
| - */ |
| 2 | +* NSLogger.swift |
| 3 | +* |
| 4 | +* version 1.8.0 26-MARCH-2017 |
| 5 | +* |
| 6 | +* Part of NSLogger (client side) |
| 7 | +* https://github.com/fpillet/NSLogger |
| 8 | +* |
| 9 | +* BSD license follows (http://www.opensource.org/licenses/bsd-license.php) |
| 10 | +* |
| 11 | +* Copyright (c) 2010-2017 Florent Pillet All Rights Reserved. |
| 12 | +* |
| 13 | +* Redistribution and use in source and binary forms, with or without modification, |
| 14 | +* are permitted provided that the following conditions are met: |
| 15 | +* |
| 16 | +* Redistributions of source code must retain the above copyright notice, |
| 17 | +* this list of conditions and the following disclaimer. Redistributions in |
| 18 | +* binary form must reproduce the above copyright notice, this list of |
| 19 | +* conditions and the following disclaimer in the documentation and/or other |
| 20 | +* materials provided with the distribution. Neither the name of Florent |
| 21 | +* Pillet nor the names of its contributors may be used to endorse or promote |
| 22 | +* products derived from this software without specific prior written |
| 23 | +* permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 24 | +* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT |
| 25 | +* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 26 | +* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 27 | +* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 28 | +* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 29 | +* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 30 | +* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 31 | +* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 32 | +* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | +* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | +* |
| 35 | +*/ |
36 | 36 |
|
37 | 37 | import Foundation
|
38 |
| -import UIKit |
39 |
| - |
| 38 | +#if os(iOS) || os(tvOS) |
| 39 | + import UIKit |
| 40 | +#endif |
| 41 | +#if os(OSX) |
| 42 | + import Cocoa |
| 43 | +#endif |
40 | 44 |
|
41 | 45 | public enum LoggerDomain {
|
42 |
| - case App |
43 |
| - case View |
44 |
| - case Layout |
45 |
| - case Controller |
46 |
| - case Routing |
47 |
| - case Service |
48 |
| - case Network |
49 |
| - case Model |
50 |
| - case Cache |
51 |
| - case DB |
52 |
| - case IO |
53 |
| - case Custom(String) |
54 |
| - |
55 |
| - var rawValue: String { |
56 |
| - switch self { |
57 |
| - case .App: return "App" |
58 |
| - case .View: return "View" |
59 |
| - case .Layout: return "Layout" |
60 |
| - case .Controller: return "Controller" |
61 |
| - case .Routing: return "Routing" |
62 |
| - case .Service: return "Service" |
63 |
| - case .Network: return "Network" |
64 |
| - case .Model: return "Model" |
65 |
| - case .Cache: return "Cache" |
66 |
| - case .DB: return "DB" |
67 |
| - case .IO: return "IO" |
68 |
| - case let .Custom(customDomain): return customDomain |
69 |
| - } |
70 |
| - } |
| 46 | + case App |
| 47 | + case View |
| 48 | + case Layout |
| 49 | + case Controller |
| 50 | + case Routing |
| 51 | + case Service |
| 52 | + case Network |
| 53 | + case Model |
| 54 | + case Cache |
| 55 | + case DB |
| 56 | + case IO |
| 57 | + case Custom(String) |
| 58 | + |
| 59 | + var rawValue: String { |
| 60 | + switch self { |
| 61 | + case .App: return "App" |
| 62 | + case .View: return "View" |
| 63 | + case .Layout: return "Layout" |
| 64 | + case .Controller: return "Controller" |
| 65 | + case .Routing: return "Routing" |
| 66 | + case .Service: return "Service" |
| 67 | + case .Network: return "Network" |
| 68 | + case .Model: return "Model" |
| 69 | + case .Cache: return "Cache" |
| 70 | + case .DB: return "DB" |
| 71 | + case .IO: return "IO" |
| 72 | + case let .Custom(customDomain): return customDomain |
| 73 | + } |
| 74 | + } |
71 | 75 | }
|
72 | 76 |
|
73 |
| - |
74 | 77 | public enum LoggerLevel: Int32 {
|
75 |
| - case Error = 0 |
76 |
| - case Warning = 1 |
77 |
| - case Important = 2 |
78 |
| - case Info = 3 |
79 |
| - case Debug = 4 |
80 |
| - case Verbose = 5 |
81 |
| - case Noise = 6 |
| 78 | + case Error = 0 |
| 79 | + case Warning = 1 |
| 80 | + case Important = 2 |
| 81 | + case Info = 3 |
| 82 | + case Debug = 4 |
| 83 | + case Verbose = 5 |
| 84 | + case Noise = 6 |
82 | 85 | }
|
83 | 86 |
|
84 |
| - |
| 87 | +/* |
| 88 | +* Log a string to display in the viewer |
| 89 | +* |
| 90 | +*/ |
85 | 91 | public func Log(_ domain: LoggerDomain, _ level: LoggerLevel, _ format: @autoclosure () -> String,
|
86 | 92 | _ filename: String = #file, lineNumber: Int32 = #line, fnName: String = #function) {
|
87 |
| - #if !NSLOGGER_DISABLED |
88 |
| - let vaArgs = getVaList([format()]) |
89 |
| - |
90 |
| - let fileNameCstr = stringToCStr(filename) |
91 |
| - let fnNameCstr = stringToCStr(fnName) |
92 |
| - |
93 |
| - LogMessageF_va(fileNameCstr, lineNumber, fnNameCstr, |
94 |
| - domain.rawValue, level.rawValue, |
95 |
| - "%@", vaArgs) |
96 |
| - |
97 |
| - #endif |
| 93 | + #if DEBUG || NSLOGGER_ENABLED |
| 94 | + let vaArgs = getVaList([format()]) |
| 95 | + |
| 96 | + let fileNameCstr = stringToCStr(filename) |
| 97 | + let fnNameCstr = stringToCStr(fnName) |
| 98 | + |
| 99 | + LogMessageF_va(fileNameCstr, lineNumber, fnNameCstr, |
| 100 | + domain.rawValue, level.rawValue, |
| 101 | + "%@", vaArgs) |
| 102 | + |
| 103 | + #endif |
98 | 104 | }
|
99 | 105 |
|
| 106 | +/* |
| 107 | +* Log an iOS / tvOS UIImage to display in the viewer |
| 108 | +* |
| 109 | +*/ |
| 110 | +#if os(iOS) || os(tvOS) |
| 111 | + public func LogImage(_ domain: LoggerDomain, _ level: LoggerLevel, _ image: @autoclosure () -> UIImage, |
| 112 | + _ filename: String = #file, lineNumber: Int32 = #line, fnName: String = #function) { |
| 113 | + #if DEBUG || NSLOGGER_ENABLED |
| 114 | + let image = image() |
| 115 | + let imageData = UIImagePNGRepresentation(image) |
| 116 | + let fileNameCstr = stringToCStr(filename) |
| 117 | + let fnNameCstr = stringToCStr(fnName) |
| 118 | + LogImageDataF(fileNameCstr, lineNumber, fnNameCstr, |
| 119 | + domain.rawValue, level.rawValue, |
| 120 | + Int32(image.size.width), Int32(image.size.height), imageData) |
| 121 | + #endif |
| 122 | + } |
| 123 | +#endif |
100 | 124 |
|
101 |
| -public func LogImage(_ domain: LoggerDomain, _ level: LoggerLevel, _ image: @autoclosure () -> UIImage, |
102 |
| - _ filename: String = #file, lineNumber: Int32 = #line, fnName: String = #function) { |
103 |
| - #if !NSLOGGER_DISABLED |
104 |
| - let image = image() |
105 |
| - let imageData = UIImagePNGRepresentation(image) |
106 |
| - |
107 |
| - let fileNameCstr = stringToCStr(filename) |
108 |
| - let fnNameCstr = stringToCStr(fnName) |
109 |
| - LogImageDataF(fileNameCstr, lineNumber, fnNameCstr, |
110 |
| - domain.rawValue, level.rawValue, |
111 |
| - Int32(image.size.width), Int32(image.size.height), imageData) |
112 |
| - #endif |
113 |
| -} |
114 |
| - |
| 125 | +/* |
| 126 | +* Log a macOS NSImage to display in the viewer |
| 127 | +* |
| 128 | +*/ |
| 129 | +#if os(OSX) |
| 130 | + public func LogImage(_ domain: LoggerDomain, _ level: LoggerLevel, _ image: @autoclosure () -> NSImage, |
| 131 | + _ filename: String = #file, lineNumber: Int32 = #line, fnName: String = #function) { |
| 132 | + #if DEBUG || NSLOGGER_ENABLED |
| 133 | + let image = image() |
| 134 | + let width = image.size.width |
| 135 | + let height = image.size.height |
| 136 | + guard |
| 137 | + let tiff = image.tiffRepresentation, |
| 138 | + let bitmapRep = NSBitmapImageRep(data: tiff), |
| 139 | + let imageData = bitmapRep.representation(using: NSPNGFileType, properties: nil) else { |
| 140 | + return |
| 141 | + } |
| 142 | + let fileNameCstr = stringToCStr(filename) |
| 143 | + let fnNameCstr = stringToCStr(fnName) |
| 144 | + LogImageDataF(fileNameCstr, lineNumber, fnNameCstr, |
| 145 | + domain.rawValue, level.rawValue, |
| 146 | + Int32(width), Int32(height), imageData) |
| 147 | + #endif |
| 148 | + } |
| 149 | +#endif |
115 | 150 |
|
| 151 | +/* |
| 152 | +* Log a binary block of data to a binary representation in the viewer |
| 153 | +* |
| 154 | +*/ |
116 | 155 | public func LogData(filename: String, lineNumber: Int32, functionName: String,
|
117 | 156 | domain: LoggerDomain, level: LoggerLevel, data: @autoclosure () -> Data) {
|
118 |
| - #if !NSLOGGER_DISABLED |
119 |
| - let fileNameCstr = stringToCStr(filename) |
120 |
| - let functionNameCstr = stringToCStr(functionName) |
121 |
| - LogDataF(fileNameCstr, lineNumber, functionNameCstr, |
122 |
| - domain.rawValue, level.rawValue, data()) |
123 |
| - #endif |
| 157 | + #if DEBUG || NSLOGGER_ENABLED |
| 158 | + let fileNameCstr = stringToCStr(filename) |
| 159 | + let functionNameCstr = stringToCStr(functionName) |
| 160 | + LogDataF(fileNameCstr, lineNumber, functionNameCstr, |
| 161 | + domain.rawValue, level.rawValue, data()) |
| 162 | + #endif |
124 | 163 | }
|
125 | 164 |
|
126 | 165 |
|
127 | 166 | fileprivate func stringToCStr(_ string: String) -> UnsafePointer<Int8> {
|
128 |
| - let cfStr = string as NSString |
129 |
| - return cfStr.cString(using: String.Encoding.ascii.rawValue)! |
| 167 | + let cfStr = string as NSString |
| 168 | + return cfStr.cString(using: String.Encoding.ascii.rawValue)! |
130 | 169 | }
|
131 | 170 |
|
132 | 171 |
|
0 commit comments