1 | // |
---|
2 | // LogViewController.swift |
---|
3 | // I2PLauncher |
---|
4 | // |
---|
5 | // Created by Mikal Villa on 18/09/2018. |
---|
6 | // Copyright © 2018 The I2P Project. All rights reserved. |
---|
7 | // |
---|
8 | |
---|
9 | import Foundation |
---|
10 | import AppKit |
---|
11 | |
---|
12 | class LogViewerViewController : NSTabViewItem { |
---|
13 | |
---|
14 | @IBOutlet var scrollView: NSScrollView? |
---|
15 | @IBOutlet var textFieldView: NSTextView? |
---|
16 | |
---|
17 | private var outputPipe : Pipe? |
---|
18 | |
---|
19 | override init(identifier: Any?) { |
---|
20 | super.init(identifier: identifier) |
---|
21 | self.captureStandardOutputAndRouteToTextView() |
---|
22 | } |
---|
23 | |
---|
24 | required init?(coder aDecoder: NSCoder) { |
---|
25 | super.init(coder: aDecoder) |
---|
26 | self.captureStandardOutputAndRouteToTextView() |
---|
27 | } |
---|
28 | |
---|
29 | |
---|
30 | func captureStandardOutputAndRouteToTextView() { |
---|
31 | outputPipe = RouterManager.shared().getRouterTask()?.processPipe |
---|
32 | outputPipe?.fileHandleForReading.waitForDataInBackgroundAndNotify() |
---|
33 | |
---|
34 | NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outputPipe?.fileHandleForReading , queue: nil) { |
---|
35 | notification in |
---|
36 | |
---|
37 | let output = self.outputPipe?.fileHandleForReading.availableData |
---|
38 | let outputString = String(data: output!, encoding: String.Encoding.utf8) ?? "" |
---|
39 | |
---|
40 | DispatchQueue.main.async(execute: { |
---|
41 | let previousOutput = self.textFieldView?.string ?? "" |
---|
42 | let nextOutput = previousOutput + "\n" + outputString |
---|
43 | self.textFieldView?.string = nextOutput |
---|
44 | |
---|
45 | let range = NSRange(location:nextOutput.characters.count,length:0) |
---|
46 | self.textFieldView?.scrollRangeToVisible(range) |
---|
47 | |
---|
48 | }) |
---|
49 | |
---|
50 | self.outputPipe?.fileHandleForReading.waitForDataInBackgroundAndNotify() |
---|
51 | } |
---|
52 | |
---|
53 | } |
---|
54 | } |
---|
55 | |
---|
56 | |
---|