@@ -6,42 +6,40 @@ import SystemPackage
66
77
88/**
9- A logger designed for Command Line Tools.
10-
11- A few things:
12- - Output is UTF8. Always.
13- - There is no buffering. We use `write(2)`.
14- - Ouptuts to stderr by default. The idea is: “usable” text (text that is
15- actually what the user asked for when launching your tool) should be outputted
16- to stdout, presumably using `print`, the rest should be on stderr. If needed you
17- can setup the logger to use any fd, the logger will simply `write(2)` to it.
18- - Ouptut has special control chars for colors if program is not compiled w/
19- Xcode and output fd is a tty. You can force using or force not using colors.
20- - If the write syscall fails, the log is lost (or partially lost; interrupts are
21- retried; see SystemPackage for more info).
22- - You can configure the logger not to automatically add a new line after each
23- message. By default, new lines are added.
24- - As this logger is specially dedicated to CLT programs, the text it outputs is
25- as small as possible on purpose: only the message is displayed, w/ a potential
26- prefix indicating the log level (or a color if colors are allowed).
27-
28- A § (Xcode is dumb and needs a § here for the comment to be properly formatted).
29-
30- - Note: An interesting logger is `Adorkable/swift-log-format-and-pipe`. I almost
31- used it (by creating extensions for a clt format and co), but ultimately
32- dismissed it because:
33- - Despite its name (which contains formatter), you are not free to choose the
34- log format: every message is ended w/ a `\n` (the LoggerTextOutputStreamPipe
35- adds the new-line directly). The only way to bypass this would be to create a
36- new pipe.
37- - It does not seem to be updated anymore (latest commit is from 2 years ago and
38- some code they duplicated from `apple/swift-log` has not been updated).
39- - To log w/o buffering (as one should for a logger?) you also have to create a
40- new pipe.
41- - Overall I love the idea of the project, but I’m not fond of the realization.
42- It is a feeling; I’m not sure of the reasons behind it. Might be related to the
43- fact that we cannot use the project as-is, or that the genericity the Adorkable
44- logger introduces is not really needed (creating a log handler is not complex). */
9+ A logger designed for Command Line Tools.
10+
11+ A few things:
12+ + Output is UTF8. Always.
13+ + There is no buffering. We use `write(2)`.
14+ + Ouptuts to stderr by default. The idea is: “usable” text (text that is
15+ actually what the user asked for when launching your tool) should be output to
16+ stdout, presumably using `print`, the rest should be on stderr. If needed you
17+ can setup the logger to use any fd, the logger will simply `write(2)` to it.
18+ + Ouptut has special control chars for colors if program is not compiled w/
19+ Xcode and output fd is a tty. You can force using or force not using colors.
20+ + If the write syscall fails, the log is lost (or partially lost; interrupts
21+ are retried; see SystemPackage for more info).
22+ + You can configure the logger not to automatically add a new line after each
23+ message. By default, new lines are added.
24+ + As this logger is specially dedicated to CLT programs, the text it outputs is
25+ as small as possible on purpose: only the message is displayed, w/ a potential
26+ prefix indicating the log level (or a color if colors are allowed).
27+
28+ - Note: An interesting logger is `Adorkable/swift-log-format-and-pipe`. I
29+ almost used it (by creating extensions for a clt format and co), but ultimately
30+ dismissed it because:
31+ + Despite its name (which contains formatter), you are not free to choose the
32+ log format: every message is ended w/ a `\n` (the LoggerTextOutputStreamPipe
33+ adds the new-line directly). The only way to bypass this would be to create a
34+ new pipe.
35+ + It does not seem to be updated anymore (latest commit is from 2 years ago
36+ and some code they duplicated from `apple/swift-log` has not been updated).
37+ + To log w/o buffering (as one should for a logger?) you also have to create a
38+ new pipe.
39+ + Overall I love the idea of the project, but I’m not fond of the realization.
40+ It is a feeling; I’m not sure of the reasons behind it. Might be related to the
41+ fact that we cannot use the project as-is, or that the genericity the Adorkable
42+ logger introduces is not really needed (creating a log handler is not complex). */
4543public struct CLTLogger : LogHandler {
4644
4745 public static var defaultTextPrefixesByLogLevel : [ Logger . Level : ( text: String , textContinuation: String , metadata: String ) ] = {
@@ -65,12 +63,12 @@ public struct CLTLogger : LogHandler {
6563 public static var defaultEmojiPrefixesByLogLevel : [ Logger . Level : ( text: String , textContinuation: String , metadata: String ) ] = {
6664 func addMeta( _ str: String , _ padding: String ) -> ( text: String , textContinuation: String , metadata: String ) {
6765 let linkPadding : String
68- #if TERMINAL_EMOJI
66+ #if TERMINAL_EMOJI
6967 let str = str + padding
7068 linkPadding = " "
71- #else
69+ #else
7270 linkPadding = " "
73- #endif
71+ #endif
7472 return ( " " + str + " • " , " " + str + " ◦ " , " " + str + " ⛓ " + linkPadding)
7573 }
7674 /* The padding correct alignment issues. */
@@ -169,10 +167,10 @@ public struct CLTLogger : LogHandler {
169167 let data = Data ( fullString. utf8)
170168
171169 /* We lock, because the writeAll function might split the write in more
172- * than 1 write (if the write system call only writes a part of the data).
173- * If another part of the program writes to fd, we might get interleaved
174- * data, because they cannot be aware of our lock (and we cannot be aware
175- * of theirs if they have one). */
170+ * than 1 write (if the write system call only writes a part of the data).
171+ * If another part of the program writes to fd, we might get interleaved
172+ * data, because they cannot be aware of our lock (and we cannot be aware
173+ * of theirs if they have one). */
176174 CLTLogger . lock. lock ( )
177175
178176 /* Is there a better idea than silently drop the message in case of fail? */
@@ -182,16 +180,16 @@ public struct CLTLogger : LogHandler {
182180 }
183181
184182 private static func shouldEnableColors( for fd: FileDescriptor ) -> Bool {
185- #if Xcode
183+ #if Xcode
186184 /* Xcode runs program in a tty, but does not support colors */
187185 return false
188- #else
186+ #else
189187 return isatty ( fd. rawValue) != 0
190- #endif
188+ #endif
191189 }
192190
193191 /* Do _not_ use os_unfair_lock, apparently it is bad in Swift:
194- * https://twitter.com/grynspan/status/1392080373752995849 */
192+ * https://twitter.com/grynspan/status/1392080373752995849 */
195193 private static var lock = NSLock ( )
196194
197195 private var flatMetadataCache = [ String] ( )
@@ -207,8 +205,8 @@ public struct CLTLogger : LogHandler {
207205 guard !metadata. isEmpty else { return " [:] " }
208206
209207 /* Basically we’ll return "\(metadata) ", but keys will be sorted.
210- * Most of the implem was stolen from Swift source code:
211- * https://github.com/apple/swift/blob/swift-5.3.3-RELEASE/stdlib/public/core/Dictionary.swift#L1681*/
208+ * Most of the implem was stolen from Swift source code:
209+ * https://github.com/apple/swift/blob/swift-5.3.3-RELEASE/stdlib/public/core/Dictionary.swift#L1681 */
212210 var result = " [ "
213211 var first = true
214212 for (k, v) in metadata. lazy. sorted ( by: { $0. key < $1. key } ) {
0 commit comments