Logger now fully binary

This commit is contained in:
Kilian Hofmann 2017-06-19 23:39:40 +02:00
parent 16ea155d0d
commit b42787cc1b
7 changed files with 290 additions and 85 deletions

View File

@ -2,4 +2,166 @@
<Bucket
type = "1"
version = "2.0">
<Breakpoints>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FileOperations.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "25"
endingLineNumber = "25"
landmarkName = "FileOperations"
landmarkType = "3">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FileOperations.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "31"
endingLineNumber = "31"
landmarkName = "decodeTimestamp(stamp:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FileOperations.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "70"
endingLineNumber = "70"
landmarkName = "logUpstream(data:toFrequencyLog:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Logger4/FrequencyOperation.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "241"
endingLineNumber = "241"
landmarkName = "logDummie(dir:element:date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Logger4/FrequencyOperation.swift"
timestampString = "519592197.249339"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "71"
endingLineNumber = "71"
landmarkName = "handleData(date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Logger4/FrequencyOperation.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "223"
endingLineNumber = "223"
landmarkName = "handleData(date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Logger4/FrequencyOperation.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "243"
endingLineNumber = "243"
landmarkName = "logDummie(dir:element:date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "Logger4/FrequencyOperation.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "242"
endingLineNumber = "242"
landmarkName = "logDummie(dir:element:date:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FileOperations.swift"
timestampString = "519600026.138018"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "26"
endingLineNumber = "26"
landmarkName = "decodeTimestamp(stamp:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
<BreakpointProxy
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
<BreakpointContent
shouldBeEnabled = "No"
ignoreCount = "0"
continueAfterRunningActions = "No"
filePath = "FileOperations.swift"
timestampString = "519600088.321308"
startingColumnNumber = "9223372036854775807"
endingColumnNumber = "9223372036854775807"
startingLineNumber = "95"
endingLineNumber = "95"
landmarkName = "logDownstream(data:toFrequencyLog:)"
landmarkType = "7">
</BreakpointContent>
</BreakpointProxy>
</Breakpoints>
</Bucket>

View File

@ -10,13 +10,33 @@ import Cocoa
class FileOperations: NSObject {
// MARK: - Timestamp related
// 15 in threshold indicates some error
static func encodeTimeStamp(stamp: String, threshold: String) -> UInt32 {
let mappingEncode: Dictionary<String, UInt32> = ["-" : 0, "62.81": 1, "58.21": 2, "57": 3, "59.81": 4, "55.21": 5, "54": 6, "56.81": 7, "52.21": 8, "51": 9]
let separated: Array<String> = stamp.components(separatedBy: ":")
let hours: UInt32 = UInt32(separated[0])! << 16
let minutes: UInt32 = UInt32(separated[1])! << 10
let seconds: UInt32 = UInt32(separated[2])! << 4
let thresholdI: UInt32 = mappingEncode[threshold] ?? 15
return hours | seconds | minutes | thresholdI
}
static func decodeTimestamp(stamp: UInt32) -> (String, String) {
let mappingDecode: Dictionary<UInt32, String> = [0: "-", 1: "62.81", 2: "58.21", 3: "57", 4: "59.81", 5: "55.21", 6: "54", 7: "56.81", 8: "52.21", 9: "51"]
let threshold: UInt32 = stamp & 0b1111
let seconds: UInt32 = (stamp >> 4) & 0b111111
let minutes: UInt32 = (stamp >> 10) & 0b111111
let hours: UInt32 = (stamp >> 16) & 0b11111
return ("\(hours):\(minutes):\(seconds)", mappingDecode[threshold] ?? "\(15)")
}
// MARK: - Loss log related
static func log(data: String, toLog path: String) {
let seperated: Array<String> = data.components(separatedBy: ":")
let hours: UInt32 = UInt32(seperated[0])!
let minutes: UInt32 = UInt32(seperated[1])!
let seconds: UInt32 = UInt32(seperated[2])!
var dataToWrite: UInt32 = 0
dataToWrite = ((((dataToWrite | hours) << 6) | minutes) << 8) | seconds
let dataToWrite = encodeTimeStamp(stamp: data, threshold: "")
let file: UnsafeMutableRawPointer = UnsafeMutableRawPointer.allocate(bytes: 3, alignedTo: 1)
file.storeBytes(of: dataToWrite, as: UInt32.self)
do {
@ -36,11 +56,7 @@ class FileOperations: NSObject {
while i < Data.length - 3 {
let data: UnsafeMutableRawPointer = UnsafeMutableRawPointer.allocate(bytes: 3, alignedTo: 1)
Data.getBytes(data, range: NSRange(location: i, length: 3))
let compound: UInt32 = data.load(as: UInt32.self)
let seconds: UInt32 = (compound) & 0b111111
let minutes: UInt32 = (compound >> 8) & 0b111111
let hours: UInt32 = (compound >> 14) & 0b11111
returnString?.append("\(hours):\(minutes):\(seconds)\n")
returnString?.append("\(decodeTimestamp(stamp: data.load(as: UInt32.self)).0)\n")
i += 3
}
if returnString != "" {
@ -48,4 +64,57 @@ class FileOperations: NSObject {
}
return nil
}
// MARK: - Frequency logging (encoding)
static func logUpstream(data: String, toFrequencyLog path: String) {
let separated: Array<String> = data.components(separatedBy: ";")
var encodedTime: UInt32 = encodeTimeStamp(stamp: separated[0], threshold: separated[2])
// NaN Used for -
var encodedPower: Int32 = Int32.min
if (Double(separated[1]) != nil) {
encodedPower = Int32(Double(Double(separated[1])! * 100.0))
}
var stringSize: UInt8 = UInt8(separated[3].lengthOfBytes(using: .ascii))
let fileContent: NSMutableData = NSMutableData()
fileContent.append(&stringSize, length: 1)
fileContent.append(&encodedTime, length: 3)
fileContent.append(&encodedPower, length: 4)
fileContent.append(separated[3].data(using: .ascii)!)
do {
let fileData: NSMutableData = try NSMutableData(contentsOf: URL(fileURLWithPath: path))
fileData.append(fileContent as Data)
fileData.write(to: URL(fileURLWithPath: path), atomically: true)
} catch _ {
fileContent.write(to: URL(fileURLWithPath: path), atomically: true)
}
}
static func logDownstream(data: String, toFrequencyLog path: String) {
let separated: Array<String> = data.components(separatedBy: ";")
var encodedTime: UInt32 = encodeTimeStamp(stamp: separated[0], threshold: "")
// NaN Used for -
var encodedPower: Int32 = Int32.min
if (Double(separated[1]) != nil) {
encodedPower = Int32(Double(Double(separated[1])! * 100.0))
}
var encodedSNR = UInt8(separated[2]) ?? UInt8.max
let fileContent: NSMutableData = NSMutableData()
fileContent.append(&encodedTime, length: 3)
fileContent.append(&encodedPower, length: 4)
fileContent.append(&encodedSNR, length: 1)
do {
let fileData: NSMutableData = try NSMutableData(contentsOf: URL(fileURLWithPath: path))
fileData.append(fileContent as Data)
fileData.write(to: URL(fileURLWithPath: path), atomically: true)
} catch _ {
fileContent.write(to: URL(fileURLWithPath: path), atomically: true)
}
}
// MARK: - Frequency loading (decoding)
}

View File

@ -42,9 +42,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>2.0</string>
<string>2.1</string>
<key>CFBundleVersion</key>
<string>417</string>
<string>418</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>

View File

@ -1,2 +1,2 @@
version 2.0
build 417
version 2.1
build 418

View File

@ -125,18 +125,18 @@ class FrequencyOperation: Operation {
entry += "NaN"
}
// Add ranging status
entry += ";\(channel["ranging_status"] as! String)\n"
entry += ";\(channel["ranging_status"] as! String)"
// Add file name to path
file = dir.appending("Upstream \(channel["frequency"]!).csv") as NSString
file = dir.appending("Upstream \(channel["frequency"]!).hex") as NSString
// Track modification
filesModified.append("Upstream \(channel["frequency"]!).csv")
filesModified.append("Upstream \(channel["frequency"]!).hex")
// Write to file
logFreq(header: ";Power Level;Threshold;Ranging Status\n", file: file, entry: entry)
FileOperations.logUpstream(data: entry, toFrequencyLog: file.expandingTildeInPath)
}
}
}
}
// We have a single channel
// We have a single channel
else if channels is Dictionary<String,AnyObject> {
// Grab channel
let channel: Dictionary<String,AnyObject> = channels as! Dictionary<String,AnyObject>
@ -157,13 +157,13 @@ class FrequencyOperation: Operation {
}
let adjustInt: Double = Double(adjust)!
// Add data to entry
entry = "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));\(String(powerInt! + adjustInt));\(upstreamOne[modulationAdjust.allKeys(for: adjust)[0] as! String]!);\(channel["ranging_status"] as! String)\n"
entry = "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));\(String(powerInt! + adjustInt));\(upstreamOne[modulationAdjust.allKeys(for: adjust)[0] as! String]!);\(channel["ranging_status"] as! String)"
// Add file name to path
file = dir.appending("Upstream \(channel["frequency"]!).csv") as NSString
file = dir.appending("Upstream \(channel["frequency"]!).hex") as NSString
// Track modifiction
filesModified.append("Upstream \(channel["frequency"]!).csv")
filesModified.append("Upstream \(channel["frequency"]!).hex")
// Write to file
logFreq(header: ";Power Level;Threshold;Ranging Status\n", file: file, entry: entry)
FileOperations.logUpstream(data: entry, toFrequencyLog: file.expandingTildeInPath)
}
}
}
@ -185,17 +185,17 @@ class FrequencyOperation: Operation {
// We do not have an error
if channel["frequency"] as! String != "status_error" {
// Add data to entry
entry = "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));\(channel["power_level"]!);\(channel["snr"]!)\n"
entry = "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));\(channel["power_level"]!);\(channel["snr"]!)"
// Add file name to path
file = dir.appending("Downstream \(channel["frequency"]!).csv") as NSString
file = dir.appending("Downstream \(channel["frequency"]!).hex") as NSString
// Tracking modification
filesModified.append("Downstream \(channel["frequency"]!).csv")
filesModified.append("Downstream \(channel["frequency"]!).hex")
// Write to file
logFreq(header: ";Power;SNR\n", file: file, entry: entry)
FileOperations.logDownstream(data: entry, toFrequencyLog: file.expandingTildeInPath)
}
}
}
// We have a single channel
// We have a single channel
else if channels is Dictionary<String,AnyObject> {
// Grab channel
let channel: Dictionary<String,AnyObject> = channels as! Dictionary<String,AnyObject>
@ -204,72 +204,46 @@ class FrequencyOperation: Operation {
// Add data to entry
entry = "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));\(channel["power_level"]!);\(channel["snr"]!)\n"
// Add file name to path
file = dir.appending("Downstream \(channel["frequency"]!).csv") as NSString
file = dir.appending("Downstream \(channel["frequency"]!).hex") as NSString
// Tracking modification
filesModified.append("Downstream \(channel["frequency"]!).csv")
filesModified.append("Downstream \(channel["frequency"]!).hex")
// Write to file
logFreq(header: ";Power;SNR\n", file: file, entry: entry)
}
}
// Update if not modified
let fileManager = FileManager.default
// Get all filenames in directory
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: dir.expandingTildeInPath)!
// Step through files
for element in enumerator {
// Check if modified
if !filesModified.contains(element as! String) && (element as! String).contains(".csv") {
logDummie(dir: dir, element: element as! String, date: date)
FileOperations.logDownstream(data: entry, toFrequencyLog: file.expandingTildeInPath)
}
}
}
// Update if both nil (no connection to modem possible)
else if (upstreamBool && downstreamBool) {
upstreamBool = false
downstreamBool = false
let fileManager = FileManager.default
// Get all filenames in directory
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: dir.expandingTildeInPath)!
// Step through files
for element in enumerator {
// Update if not modified
let fileManager = FileManager.default
// Get all filenames in directory
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: dir.expandingTildeInPath)!
// Step through files
for element in enumerator {
// Check if modified
if !filesModified.contains(element as! String) && (element as! String).contains(".hex") {
logDummie(dir: dir, element: element as! String, date: date)
}
}
}
}
func logFreq(header: String, file: NSString, entry: String) {
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: file.expandingTildeInPath) {
do {
try header.write(toFile: file.expandingTildeInPath, atomically: true, encoding: .utf8)
}
catch {
NSLog("WRITE TO FILE \(file.expandingTildeInPath) FAILED\n");
return
// Update if both nil (no connection to modem possible)
else if (upstreamBool && downstreamBool) {
upstreamBool = false
downstreamBool = false
let fileManager = FileManager.default
// Get all filenames in directory
let enumerator:FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: dir.expandingTildeInPath)!
// Step through files
for element in enumerator {
logDummie(dir: dir, element: element as! String, date: date)
}
}
let fileH: FileHandle? = FileHandle(forUpdatingAtPath: file.expandingTildeInPath)
fileH?.seekToEndOfFile()
let data: Data = (entry as NSString).data(using: String.Encoding.utf8.rawValue)!
fileH?.write(data)
fileH?.closeFile()
}
func logDummie(dir: NSString, element: String, date: Date) {
// Get file
let file: FileHandle? = FileHandle(forUpdatingAtPath: dir.expandingTildeInPath.appending(element))
if file != nil {
// Write appropriate null entry to file
file?.seekToEndOfFile();
var data: Data?;
if element.contains("Upstream") {
data = ("\((NSApp.delegate as! AppDelegate).justTime.string(from: date));-;-;-\n" as NSString).data(using: String.Encoding.utf8.rawValue)
} else {
data = ("\((NSApp.delegate as! AppDelegate).justTime.string(from: date));-;-\n" as NSString).data(using: String.Encoding.utf8.rawValue)
}
file?.write(data!)
file?.closeFile()
if element.contains("Upstream") {
FileOperations.logUpstream(data: "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));-;-;-", toFrequencyLog: (dir.appending(element) as NSString).expandingTildeInPath)
} else {
FileOperations.logDownstream(data: "\((NSApp.delegate as! AppDelegate).justTime.string(from: date));-;-",
toFrequencyLog: (dir.appending(element) as NSString).expandingTildeInPath)
}
}
}

View File

@ -15,9 +15,9 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>4.0</string>
<string>4.1</string>
<key>CFBundleVersion</key>
<string>39</string>
<string>47</string>
<key>LSApplicationCategoryType</key>
<string>public.app-category.utilities</string>
<key>LSMinimumSystemVersion</key>

View File

@ -1,2 +1,2 @@
version 4.0
build 39
version 4.1
build 47