Skip to content

Commit

Permalink
rolled my own logging in the end
Browse files Browse the repository at this point in the history
  • Loading branch information
Honza Dvorsky committed Apr 12, 2015
1 parent 029bc60 commit 21f42ad
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 21 deletions.
68 changes: 64 additions & 4 deletions BuildaUtils/Logging.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,81 @@

import Foundation

public protocol Logger {

func description() -> String
func log(message: String)
}

public class FileLogger: Logger {

let filePath: NSURL
let stream: NSOutputStream

public init(filePath: NSURL) {
self.filePath = filePath
self.stream = NSOutputStream(URL: filePath, append: true)!
self.stream.open()
}

deinit {
self.stream.close()
}

public func description() -> String {
return "File logger into file at path \(self.filePath)"
}

public func log(message: String) {
let data: NSData = "\(message)\n".dataUsingEncoding(NSUTF8StringEncoding)!
self.stream.write(UnsafePointer<UInt8>(data.bytes), maxLength: data.length)
}
}

public class ConsoleLogger: Logger {

public init() {
//
}

public func description() -> String {
return "Console logger"
}

public func log(message: String) {
println(message)
}
}

public class Log {

static private var _loggers = [Logger]()
public class func addLoggers(loggers: [Logger]) {
for i in loggers {
_loggers.append(i)
println("Added logger: \(i)")
}
}

private class func log(message: String) {
for i in _loggers {
i.log(message)
}
}

public class func verbose(message: String) {
println("[VERBOSE]: " + message)
Log.log("[VERBOSE]: " + message)
}

public class func info(message: String) {
println("[INFO]: " + message)
Log.log("[INFO]: " + message)
}

public class func error(message: String) {
println("[ERROR]: " + message)
Log.log("[ERROR]: " + message)
}

public class func untouched(message: String) {
println(message)
Log.log(message)
}
}
14 changes: 7 additions & 7 deletions Buildasaur/Persistence.swift → BuildaUtils/Persistence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import Foundation
import BuildaUtils

class Persistence {
public class Persistence {

class func loadJSONFromUrl(url: NSURL) -> (AnyObject?, NSError?) {
public class func loadJSONFromUrl(url: NSURL) -> (AnyObject?, NSError?) {

var error: NSError?
if let data = NSData(contentsOfURL: url, options: NSDataReadingOptions.allZeros, error: &error) {
Expand All @@ -23,7 +23,7 @@ class Persistence {
return (nil, error)
}

class func saveJSONToUrl(json: AnyObject, url: NSURL) -> (Bool, NSError?) {
public class func saveJSONToUrl(json: AnyObject, url: NSURL) -> (Bool, NSError?) {

var error: NSError?
if let data = NSJSONSerialization.dataWithJSONObject(json, options: NSJSONWritingOptions.PrettyPrinted, error: &error) {
Expand All @@ -35,7 +35,7 @@ class Persistence {
return (false, error)
}

class func getFileInAppSupportWithName(name: String, isDirectory: Bool) -> NSURL {
public class func getFileInAppSupportWithName(name: String, isDirectory: Bool) -> NSURL {

let root = self.buildaApplicationSupportFolderURL()
let url = root.URLByAppendingPathComponent(name, isDirectory: isDirectory)
Expand All @@ -45,7 +45,7 @@ class Persistence {
return url
}

class func createFolderIfNotExists(url: NSURL) {
public class func createFolderIfNotExists(url: NSURL) {

let fm = NSFileManager.defaultManager()

Expand All @@ -54,7 +54,7 @@ class Persistence {
assert(success, "Failed to create a folder in Builda's Application Support folder \(url), error \(error)")
}

class func buildaApplicationSupportFolderURL() -> NSURL {
public class func buildaApplicationSupportFolderURL() -> NSURL {

let fm = NSFileManager.defaultManager()
if let appSupport = fm.URLsForDirectory(NSSearchPathDirectory.ApplicationSupportDirectory, inDomains:NSSearchPathDomainMask.UserDomainMask).first as? NSURL {
Expand All @@ -75,7 +75,7 @@ class Persistence {
return NSURL()
}

class func iterateThroughFilesInFolder(folderUrl: NSURL, visit: (url: NSURL) -> ()) {
public class func iterateThroughFilesInFolder(folderUrl: NSURL, visit: (url: NSURL) -> ()) {

let fm = NSFileManager.defaultManager()
var error: NSError?
Expand Down
12 changes: 8 additions & 4 deletions Buildasaur.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,13 @@
3A7B91391A3E44100060A21A /* XcodeServerEntity.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7B91381A3E44100060A21A /* XcodeServerEntity.swift */; };
3A7B913B1A3E450D0060A21A /* Bot.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7B913A1A3E450D0060A21A /* Bot.swift */; };
3A7B913D1A3E455A0060A21A /* BotConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A7B913C1A3E455A0060A21A /* BotConfiguration.swift */; };
3A808A1B1ADB03640073145D /* Logging.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A808A1A1ADB03640073145D /* Logging.swift */; };
3A808A1D1ADB063F0073145D /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A808A1C1ADB063F0073145D /* Persistence.swift */; };
3A90C4C01A90220F0048C040 /* HDGitHubXCBotSyncer.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3A90C4BF1A90220F0048C040 /* HDGitHubXCBotSyncer.swift */; };
3AA04CB31A62DAC600350811 /* SourceControlBlueprint.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA04CB21A62DAC600350811 /* SourceControlBlueprint.swift */; };
3AA04CB51A62E1D600350811 /* Integration.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AA04CB41A62E1D600350811 /* Integration.swift */; };
3AAA1B661AAB6E9800FA1598 /* SeparatorView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAA1B651AAB6E9800FA1598 /* SeparatorView.swift */; };
3AAA1B681AAB722600FA1598 /* StatusProjectViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAA1B671AAB722600FA1598 /* StatusProjectViewController.swift */; };
3AAA1B6C1AAB9D2A00FA1598 /* Persistence.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAA1B6B1AAB9D2A00FA1598 /* Persistence.swift */; };
3AAA1B721AABBBB200FA1598 /* NetworkUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAA1B711AABBBB200FA1598 /* NetworkUtils.swift */; };
3AAA1B741AABBD3A00FA1598 /* Errors.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAA1B731AABBD3A00FA1598 /* Errors.swift */; };
3AAA1B761AAC504700FA1598 /* StatusServerViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3AAA1B751AAC504700FA1598 /* StatusServerViewController.swift */; };
Expand Down Expand Up @@ -187,13 +188,14 @@
3A7B91381A3E44100060A21A /* XcodeServerEntity.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XcodeServerEntity.swift; sourceTree = "<group>"; };
3A7B913A1A3E450D0060A21A /* Bot.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Bot.swift; sourceTree = "<group>"; };
3A7B913C1A3E455A0060A21A /* BotConfiguration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BotConfiguration.swift; sourceTree = "<group>"; };
3A808A1A1ADB03640073145D /* Logging.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Logging.swift; sourceTree = "<group>"; };
3A808A1C1ADB063F0073145D /* Persistence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
3A90C4BF1A90220F0048C040 /* HDGitHubXCBotSyncer.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = HDGitHubXCBotSyncer.swift; sourceTree = "<group>"; };
3A9DEC7A1A3BDA6C008C8270 /* PullRequestBranch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PullRequestBranch.swift; path = BuildaGitServer/PullRequestBranch.swift; sourceTree = SOURCE_ROOT; };
3AA04CB21A62DAC600350811 /* SourceControlBlueprint.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SourceControlBlueprint.swift; sourceTree = "<group>"; };
3AA04CB41A62E1D600350811 /* Integration.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Integration.swift; sourceTree = "<group>"; };
3AAA1B651AAB6E9800FA1598 /* SeparatorView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SeparatorView.swift; sourceTree = "<group>"; };
3AAA1B671AAB722600FA1598 /* StatusProjectViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusProjectViewController.swift; sourceTree = "<group>"; };
3AAA1B6B1AAB9D2A00FA1598 /* Persistence.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Persistence.swift; sourceTree = "<group>"; };
3AAA1B711AABBBB200FA1598 /* NetworkUtils.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NetworkUtils.swift; sourceTree = "<group>"; };
3AAA1B731AABBD3A00FA1598 /* Errors.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Errors.swift; sourceTree = "<group>"; };
3AAA1B751AAC504700FA1598 /* StatusServerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = StatusServerViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -273,7 +275,6 @@
3A2F9D841A8FE64900B0DB68 /* LocalSource.swift */,
3A5591551A913C0A00FB19F2 /* XcodeLocalSource.swift */,
3A4770A11A745F470016E170 /* StorageUtils.swift */,
3AAA1B6B1AAB9D2A00FA1598 /* Persistence.swift */,
3AAA1B711AABBBB200FA1598 /* NetworkUtils.swift */,
);
name = Model;
Expand Down Expand Up @@ -411,6 +412,8 @@
3AAF6E751A3CE4CC00C657FB /* BuildaUtils */ = {
isa = PBXGroup;
children = (
3A808A1C1ADB063F0073145D /* Persistence.swift */,
3A808A1A1ADB03640073145D /* Logging.swift */,
3AF1B1211AAC621800917EF3 /* UIUtils.swift */,
3A4770A31A745FFA0016E170 /* XcodeProjectParser.swift */,
3A6355DC1A3BC19800545BF9 /* HTTPUtils.swift */,
Expand Down Expand Up @@ -728,7 +731,6 @@
3A5B04AC1AB4AC0F00F60536 /* SetupViewController.swift in Sources */,
3A5B04AA1AB4ABEB00F60536 /* TriggerViewController.swift in Sources */,
3AF1B1241AAC7CA500917EF3 /* StatusSyncerViewController.swift in Sources */,
3AAA1B6C1AAB9D2A00FA1598 /* Persistence.swift in Sources */,
3A5B04B21AB5C42A00F60536 /* XcodeServerSyncerUtils.swift in Sources */,
3A5B04B01AB5144700F60536 /* ManualBotManagementViewController.swift in Sources */,
3A5687761A3B93BD0066DB2B /* AppDelegate.swift in Sources */,
Expand All @@ -751,8 +753,10 @@
buildActionMask = 2147483647;
files = (
3AAF6F091A3CE75600C657FB /* HTTPUtils.swift in Sources */,
3A808A1D1ADB063F0073145D /* Persistence.swift in Sources */,
3AAA1B741AABBD3A00FA1598 /* Errors.swift in Sources */,
3A7B91361A3E41980060A21A /* Server.swift in Sources */,
3A808A1B1ADB03640073145D /* Logging.swift in Sources */,
3AF1B1221AAC621800917EF3 /* UIUtils.swift in Sources */,
3A4770A41A745FFA0016E170 /* XcodeProjectParser.swift in Sources */,
3AAF6F0A1A3CE75600C657FB /* JSON.swift in Sources */,
Expand Down
31 changes: 25 additions & 6 deletions Buildasaur/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,42 @@
import Cocoa

/*
TODO: Keychain: GitHub token

Please report any crashes on GitHub, I may optionally ask you to email them to me. Thanks!
You can find them at ~/Library/Logs/DiagnosticReports/Buildasaur-*

Also, you can find the log at ~/Library/Application Support/Buildasaur/Builda.log
*/

import BuildaCIServer
import BuildaUtils

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

var server: XcodeServer?

func applicationDidFinishLaunching(aNotification: NSNotification) {
// Insert code here to initialize your application

self.setupLogging()
}

func setupLogging() {

let path = Persistence.buildaApplicationSupportFolderURL().URLByAppendingPathComponent("Builda.log", isDirectory: false)
let fileLogger = FileLogger(filePath: path)
let consoleLogger = ConsoleLogger()
let loggers: [Logger] = [
consoleLogger,
fileLogger
]
Log.addLoggers(loggers)
let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
let ascii =
" ____ _ _ _\n" +
"| _ \\ (_) | | |\n" +
"| |_) |_ _ _| | __| | __ _ ___ __ _ _ _ _ __\n" +
"| _ <| | | | | |/ _` |/ _` / __|/ _` | | | | '__|\n" +
"| |_) | |_| | | | (_| | (_| \\__ \\ (_| | |_| | |\n" +
"|____/ \\__,_|_|_|\\__,_|\\__,_|___/\\__,_|\\__,_|_|\n"

Log.untouched("*\n*\n*\n\(ascii)\nBuildasaur \(version) launched at \(NSDate()).\n*\n*\n*\n")
}

func applicationWillTerminate(aNotification: NSNotification) {
Expand Down

0 comments on commit 21f42ad

Please sign in to comment.