Skip to content

Commit

Permalink
it starts
Browse files Browse the repository at this point in the history
  • Loading branch information
paulofaria committed Feb 16, 2016
0 parents commit bf51190
Show file tree
Hide file tree
Showing 10 changed files with 731 additions and 0 deletions.
30 changes: 30 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Created by https://www.gitignore.io/api/xcode

### Xcode ###
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
.build/
build/
DerivedData
Carthage/
Packages/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata

## Other
*.xccheckout
*.moved-aside
*.xcuserstate
*.xcscmblueprint
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2015 Zewo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import PackageDescription

let package = Package(
name: "Log",
dependencies: [
.Package(url: "https://github.com/Zewo/File.git", majorVersion: 0, minor: 2),
.Package(url: "https://github.com/Zewo/CLibvenice.git", majorVersion: 0, minor: 2)
]
)
97 changes: 97 additions & 0 deletions Source/Log.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// Log.swift
//
// The MIT License (MIT)
//
// Copyright (c) 2015 Zewo
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

@_exported import File

public final class Log {
public struct Level: OptionSetType {
public let rawValue: Int32

public init(rawValue: Int32) {
self.rawValue = rawValue
}

public static let Trace = Level(rawValue: 1 << 0)
public static let Debug = Level(rawValue: 1 << 1)
public static let Info = Level(rawValue: 1 << 2)
public static let Warning = Level(rawValue: 1 << 3)
public static let Error = Level(rawValue: 1 << 4)
public static let Fatal = Level(rawValue: 1 << 5)
public static let All = Level(rawValue: ~0)
}

public var stream: File
public var levels: Level
private let messageChannel = Channel<String>()
private let once = Once()

public init(stream: File = standardErrorStream, levels: Level = .All) {
self.stream = stream
self.levels = levels
}

deinit {
messageChannel.close()
}

public func log(level: Level, item: Any, terminator: String = "\n", flush: Bool = true) {
if levels.contains(level) {
once.runInBackground {
for message in self.messageChannel {
do {
try self.stream.write(message)
} catch {
print("Log error: \(error)")
print("Log message: \(message)")
}
}
}
messageChannel.send(String(item) + terminator)
}
}

public func trace(item: Any, terminator: String = "\n", flush: Bool = true) {
log(.Trace, item: item, terminator: terminator, flush: flush)
}

public func debug(item: Any, terminator: String = "\n", flush: Bool = true) {
log(.Debug, item: item, terminator: terminator, flush: flush)
}

public func info(item: Any, terminator: String = "\n", flush: Bool = true) {
log(.Info, item: item, terminator: terminator, flush: flush)
}

public func warning(item: Any, terminator: String = "\n", flush: Bool = true) {
log(.Warning, item: item, terminator: terminator, flush: flush)
}

public func error(item: Any, terminator: String = "\n", flush: Bool = true) {
log(.Error, item: item, terminator: terminator, flush: flush)
}

public func fatal(item: Any, terminator: String = "\n", flush: Bool = true) {
log(.Fatal, item: item, terminator: terminator, flush: flush)
}
}
Loading

0 comments on commit bf51190

Please sign in to comment.