Skip to content

Commit

Permalink
Merge pull request #6 from czechboy0/feature/lumberjack
Browse files Browse the repository at this point in the history
Logging into file and console
  • Loading branch information
Honza Dvorsky committed Apr 12, 2015
2 parents ebe7972 + d33633a commit 47c9b10
Show file tree
Hide file tree
Showing 20 changed files with 181 additions and 72 deletions.
2 changes: 1 addition & 1 deletion BuildaCIServer/XcodeServerEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public class XcodeServerEndPoints {
let data = NSJSONSerialization.dataWithJSONObject(body, options: .allZeros, error: &error)
if let error = error {
//parsing error
println("Parsing error \(error.description)")
Log.error("Parsing error \(error.description)")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion BuildaGitServer/GitHubEndpoints.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public class GitHubEndpoints {
let data = NSJSONSerialization.dataWithJSONObject(body, options: .allZeros, error: &error)
if let error = error {
//parsing error
println("Parsing error \(error.description)")
Log.error("Parsing error \(error.description)")
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions BuildaGitServerTests/GitHubServerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class GitHubSourceTests: XCTestCase {
XCTAssertNotNil(body, "Body must be non-nil")
let prs: [PullRequest] = GitHubArray(body as! NSArray)
XCTAssertGreaterThan(prs.count, 0, "We need > 0 items to test parsing")
println("Parsed PRs: \(prs)")
Log.verbose("Parsed PRs: \(prs)")
}
}

Expand All @@ -73,7 +73,7 @@ class GitHubSourceTests: XCTestCase {
XCTAssertNotNil(body, "Body must be non-nil")
let branches: [Branch] = GitHubArray(body as! NSArray)
XCTAssertGreaterThan(branches.count, 0, "We need > 0 items to test parsing")
println("Parsed branches: \(branches)")
Log.verbose("Parsed branches: \(branches)")
}
}

Expand Down
88 changes: 88 additions & 0 deletions BuildaUtils/Logging.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//
// Logging.swift
// Buildasaur
//
// Created by Honza Dvorsky on 12/04/2015.
// Copyright (c) 2015 Honza Dvorsky. All rights reserved.
//

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) {
Log.log("[VERBOSE]: " + message)
}

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

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

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

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 @@ -22,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 @@ -34,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 @@ -44,7 +45,7 @@ class Persistence {
return url
}

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

let fm = NSFileManager.defaultManager()

Expand All @@ -53,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 @@ -66,22 +67,22 @@ class Persistence {
return buildaAppSupport

} else {
println("Failed to create Builda's Application Support folder, error \(error)")
Log.error("Failed to create Builda's Application Support folder, error \(error)")
}
}

assertionFailure("Couldn't access Builda's persistence folder, aborting")
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?
if let contents = fm.contentsOfDirectoryAtURL(folderUrl, includingPropertiesForKeys: nil, options: NSDirectoryEnumerationOptions.SkipsHiddenFiles | NSDirectoryEnumerationOptions.SkipsSubdirectoryDescendants, error: &error) as? [NSURL] {
contents.map { visit(url: $0) }
} else {
println("Couldn't read folder \(folderUrl), error \(error)")
Log.error("Couldn't read folder \(folderUrl), error \(error)")
}

}
Expand Down
2 changes: 1 addition & 1 deletion BuildaUtils/UIUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public class UIUtils {
// alert.beginSheetModalForWindow(window, completionHandler: completion)
} else {
//no window to present in, at least print
println("Alert: \(alert.messageText)")
Log.info("Alert: \(alert.messageText)")
}
}
}
2 changes: 1 addition & 1 deletion BuildaUtils/XcodeProjectParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ public class XcodeProjectParser {
return absolutePaths
}
}
println("Couldn't load contents of workspace \(url)")
Log.error("Couldn't load contents of workspace \(url)")
return nil
}

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
2 changes: 1 addition & 1 deletion Buildasaur/BuildTemplateViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ class BuildTemplateViewController: SetupViewController, NSComboBoxDelegate, NSTa
self.schemesComboBox.delegate = self

self.fetchDevices { () -> () in
println("Finished fetching devices")
Log.verbose("Finished fetching devices")
}
}

Expand Down
Loading

0 comments on commit 47c9b10

Please sign in to comment.