-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogDNA.swift
180 lines (146 loc) · 5.98 KB
/
LogDNA.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
//
// LogDNA.swift
// TradingApp
//
// Created by Nevin Jethmalani on 11/2/17.
// Copyright © 2017 Nevin Jethmalani. All rights reserved.
//
import Cocoa
import Alamofire
import SwiftyJSON
class LogDNA: NSObject {
var ingestionKey:String!
var hostName:String!
var appName:String!
var deviceIpAddress:String!
var deviceMacAddress:String?
var verbose:Bool = false
static var shared = LogDNA()
class func setup(withIngestionKey ingestionKey:String, hostName:String, appName:String, includeNetworkData:Bool){
self.shared.ingestionKey = ingestionKey
self.shared.hostName = hostName
self.shared.appName = appName
if includeNetworkData == true {
self.shared.deviceMacAddress = self.getMacAddress()
self.shared.deviceIpAddress = self.getIPAddress()
}
}
class func log(line:String, level:LogDNALevel, meta:[String:Any]){
var headers: HTTPHeaders = [
"content-type": "application/json; charset=UTF-8"
]
guard let hostName = self.shared.hostName else {return}
guard let credentials = self.shared.ingestionKey else {return}
let credentialData = "\(credentials):\(credentials)".data(using: String.Encoding.utf8)!
let base64Credentials = credentialData.base64EncodedString(options: [])
headers["Authorization"] = "Basic \(base64Credentials)"
var parameters:Parameters = [String : Any]()
var mainLine = [String:Any]()
mainLine["line"] = line
mainLine["app"] = self.shared.appName
mainLine["level"] = level.value
mainLine["meta"] = meta
parameters["lines"] = [mainLine]
var url = "https://logs.logdna.com/logs/ingest?hostname=\(hostName)&now=\(Date().timeIntervalSince1970)"
if let macAddressAvailable = self.shared.deviceMacAddress {
url += "&mac=\(macAddressAvailable)"
}
if let ipAddressAvailable = self.shared.deviceIpAddress {
url += "&ip=\(ipAddressAvailable)"
}
Alamofire.request(url, method: .post, parameters: parameters , encoding: JSONEncoding.default, headers: headers)
.responseJSON { (response) in
switch response.result {
case .success(let value):
if self.shared.verbose == true {
print ("return: \(value)")
}
case .failure(let error):
if self.shared.verbose == true {
print ("error: \(error)")
}
}
}
}
class func getIPAddress() -> String? {
var address: String?
var ifaddr: UnsafeMutablePointer<ifaddrs>? = nil
if getifaddrs(&ifaddr) == 0 {
var ptr = ifaddr
while ptr != nil {
defer { ptr = ptr?.pointee.ifa_next }
let interface = ptr?.pointee
let addrFamily = interface?.ifa_addr.pointee.sa_family
if addrFamily == UInt8(AF_INET) || addrFamily == UInt8(AF_INET6) {
if let name: String = String(cString: (interface?.ifa_name)!), name == "en0" {
var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
getnameinfo(interface?.ifa_addr, socklen_t((interface?.ifa_addr.pointee.sa_len)!), &hostname, socklen_t(hostname.count), nil, socklen_t(0), NI_NUMERICHOST)
address = String(cString: hostname)
}
}
}
freeifaddrs(ifaddr)
}
return address
}
//These are all used for the mac address
class func FindEthernetInterfaces() -> io_iterator_t? {
let matchingDict = IOServiceMatching("IOEthernetInterface") as NSMutableDictionary
matchingDict["IOPropertyMatch"] = [ "IOPrimaryInterface" : true]
var matchingServices : io_iterator_t = 0
if IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &matchingServices) != KERN_SUCCESS {
return nil
}
return matchingServices
}
class func getMACAddress(_ intfIterator : io_iterator_t) -> [UInt8]? {
var macAddress : [UInt8]?
var intfService = IOIteratorNext(intfIterator)
while intfService != 0 {
var controllerService : io_object_t = 0
if IORegistryEntryGetParentEntry(intfService, "IOService", &controllerService) == KERN_SUCCESS {
let dataUM = IORegistryEntryCreateCFProperty(controllerService, "IOMACAddress" as CFString, kCFAllocatorDefault, 0)
if let data = dataUM?.takeRetainedValue() as? NSData {
macAddress = [0, 0, 0, 0, 0, 0]
data.getBytes(&macAddress!, length: macAddress!.count)
}
IOObjectRelease(controllerService)
}
IOObjectRelease(intfService)
intfService = IOIteratorNext(intfIterator)
}
return macAddress
}
class func getMacAddress()-> String?{
if let intfIterator = FindEthernetInterfaces() {
if let macAddress = getMACAddress(intfIterator) {
let macAddressAsString = macAddress.map( { String(format:"%02x", $0) } )
.joined(separator: ":")
return macAddressAsString
}
IOObjectRelease(intfIterator)
}
return nil
}
}
enum LogDNALevel {
case debug
case info
case warn
case error
case fatal
var value: String {
switch self {
case .debug:
return "DEBUG"
case .info:
return "INFO"
case .warn:
return "WARN"
case .error:
return "ERROR"
case .fatal:
return "FATAL"
}
}
}