-
Notifications
You must be signed in to change notification settings - Fork 944
/
Copy pathLog.swift
180 lines (154 loc) · 4.44 KB
/
Log.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// LogManager.swift
// PerfectLib
//
// Created by Kyle Jessup on 7/21/15.
// Copyright (C) 2015 PerfectlySoft, Inc.
//
//===----------------------------------------------------------------------===//
//
// This source file is part of the Perfect.org open source project
//
// Copyright (c) 2015 - 2016 PerfectlySoft Inc. and the Perfect project authors
// Licensed under Apache License v2.0
//
// See http://perfect.org/licensing.html for license information
//
//===----------------------------------------------------------------------===//
//
#if os(Linux)
import SwiftGlibc
import LinuxBridge
#else
import Darwin
import os
#endif
/// Placeholder functions for logging system
public protocol Logger {
func debug(message: String, _ even: Bool)
func info(message: String, _ even: Bool)
func warning(message: String, _ even: Bool)
func error(message: String, _ even: Bool)
func critical(message: String, _ even: Bool)
func terminal(message: String, _ even: Bool)
}
public struct ConsoleLogger: Logger {
public init(){}
public func debug(message: String, _ even: Bool) {
print((even ? "[DBG] " : "[DBG] ") + message)
}
public func info(message: String, _ even: Bool) {
print((even ? "[INFO] " : "[INFO] ") + message)
}
public func warning(message: String, _ even: Bool) {
print((even ? "[WARN] " : "[WARN] ") + message)
}
public func error(message: String, _ even: Bool) {
print((even ? "[ERR] " : "[ERR] ") + message)
}
public func critical(message: String, _ even: Bool) {
print((even ? "[CRIT] " : "[CRIT] ") + message)
}
public func terminal(message: String, _ even: Bool) {
print((even ? "[TERM] " : "[TERM] ") + message)
}
}
public struct SysLogger: Logger {
let consoleEcho = ConsoleLogger()
public init(){}
func syslog(priority: Int32, _ args: CVarArg...) {
#if os(Linux)// || os(macOS)
withVaList(args) {
vsyslog(priority, "%s", $0)
}
#else
// nerdo: Using unified logging (https://developer.apple.com/documentation/os/logging)
// os_log isn't very well documented... but this seems like a step in the right direction.
let osLogType: OSLogType
switch priority {
case 0, 1, 2:
osLogType = .fault
case 3:
osLogType = .error
case 4, 5, 6:
osLogType = .info
case 7:
osLogType = .debug
default:
osLogType = .default
}
os_log("%s", log: .default, type: osLogType, args)
#endif
}
public func debug(message: String, _ even: Bool) {
consoleEcho.debug(message: message, even)
message.withCString {
f in
syslog(priority: LOG_DEBUG, f)
}
}
public func info(message: String, _ even: Bool) {
consoleEcho.info(message: message, even)
message.withCString {
f in
syslog(priority: LOG_INFO, f)
}
}
public func warning(message: String, _ even: Bool) {
consoleEcho.warning(message: message, even)
message.withCString {
f in
syslog(priority: LOG_WARNING, f)
}
}
public func error(message: String, _ even: Bool) {
consoleEcho.error(message: message, even)
message.withCString {
f in
syslog(priority: LOG_ERR, f)
}
}
public func critical(message: String, _ even: Bool) {
consoleEcho.critical(message: message, even)
message.withCString {
f in
syslog(priority: LOG_CRIT, f)
}
}
public func terminal(message: String, _ even: Bool) {
consoleEcho.terminal(message: message, even)
message.withCString {
f in
syslog(priority: LOG_EMERG, f)
}
}
}
/// Placeholder functions for logging system
public struct Log {
private init(){}
public static var logger: Logger = ConsoleLogger()
/// Whether or not to even off the log messages
/// If set to true log messages will be inline with each other
public static var even = false
public static func debug(message: @autoclosure () -> String) {
// #if DEBUG
Log.logger.debug(message: message(), even)
// #endif
}
public static func info(message: String, evenIdents: Bool = even) {
Log.logger.info(message: message, evenIdents)
}
public static func warning(message: String, evenIdents: Bool = even) {
Log.logger.warning(message: message, evenIdents)
}
public static func error(message: String, evenIdents: Bool = even) {
Log.logger.error(message: message, evenIdents)
}
public static func critical(message: String, evenIdents: Bool = even) {
Log.logger.critical(message: message, evenIdents)
}
public static func terminal(message: String, evenIdents: Bool = even) -> Never {
Log.logger.terminal(message: message, evenIdents)
fatalError(message)
}
}