Skip to content

Commit

Permalink
Merge pull request #5 from kostiakoval/0.2.0
Browse files Browse the repository at this point in the history
0.2.0
  • Loading branch information
kostiakoval committed Nov 28, 2015
2 parents 543e609 + 60159a3 commit d843635
Show file tree
Hide file tree
Showing 19 changed files with 395 additions and 69 deletions.
20 changes: 20 additions & 0 deletions Example/Example-iOS/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
myFunc()
colorLog()
return true
}

Expand All @@ -32,7 +33,26 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
SpeedLog.print("Show FunctionName and File name")

SpeedLog.mode = [.FuncName, .FileName, .Line]
SpeedLog.print("Show 3 options :)")

SpeedLog.mode = .FullCodeLocation
SpeedLog.print("Show fullCode, same as above")

SpeedLog.mode = [.Date, .FuncName, .FileName, .Line]
SpeedLog.print("Show all 3 options :)")
}

func colorLog() {
SpeedLog.mode = .None
SpeedLog.print("\nNice UIColor :) \n")
let c = UIColor.redColor()
print("Original:", c)

SpeedLog.enableVisualColorLog()
SpeedLog.print("Visual:", c)

SpeedLog.disableVisualColorLog()
SpeedLog.print("Original Restored:", c)
}
}

8 changes: 0 additions & 8 deletions Example/Example-iOS/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,6 @@ class ViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}


}

11 changes: 2 additions & 9 deletions Example/Example.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,6 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/DerivedData/Example/Build/Products/Debug-iphoneos",
);
INFOPLIST_FILE = "Example-iOS/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -414,10 +410,6 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
"CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer";
EMBEDDED_CONTENT_CONTAINS_SWIFT = YES;
FRAMEWORK_SEARCH_PATHS = (
"$(inherited)",
"$(PROJECT_DIR)/DerivedData/Example/Build/Products/Debug-iphoneos",
);
INFOPLIST_FILE = "Example-iOS/Info.plist";
IPHONEOS_DEPLOYMENT_TARGET = 9.1;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
Expand All @@ -431,14 +423,15 @@
B5BA46DE1BFF668700EC5A0D /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
OTHER_SWIFT_FLAGS = "-D ENABLE_LOG";
OTHER_SWIFT_FLAGS = "";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Debug;
};
B5BA46DF1BFF668700EC5A0D /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
OTHER_SWIFT_FLAGS = "-D DISABLE_LOG";
PRODUCT_NAME = "$(TARGET_NAME)";
};
name = Release;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//: [Previous](@previous)

import SpeedLog
SpeedLog.print("Hello")
SpeedLog.print(["Super"], ["Speed"])

// ### Log output Styling

SpeedLog.mode = .FuncName
SpeedLog.print("Show only FunctionName")
//myFunc(): Show only FunctionName

SpeedLog.mode = [.FuncName, .FileName]
SpeedLog.print("Show FunctionName and File name")
//AppDelegate.myFunc(): Show FunctionName and File name

SpeedLog.mode = [.FuncName, .FileName, .Line]
SpeedLog.print("Show 3 options :)")
//AppDelegate.myFunc()[36]: Show 3 options :)

SpeedLog.mode = .FullCodeLocation
SpeedLog.print("Show fullCode, same as above")
//AppDelegate.myFunc()[39]: Show fullCode, same as above

SpeedLog.mode = .AllOptions
SpeedLog.print("Enable All Features")
//2015-11-26 19:32:33.687 AppDelegate.myFunc()[27]: Enable All Features
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Timeline
version = "3.0">
<TimelineItems>
</TimelineItems>
</Timeline>
7 changes: 5 additions & 2 deletions Example/MyPlayground.playground/contents.xcplayground
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<playground version='5.0' target-platform='ios'>
<timeline fileName='timeline.xctimeline'/>
<playground version='6.0' target-platform='ios'>
<pages>
<page name='Playground'/>
<page name='Readme'/>
</pages>
</playground>
Binary file added Images/RGB-colors-log.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 34 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ The SpeedLog allows you to remove `print` when the logs are disabled. This allow
- [x] Improves Swift code optimization
- [x] Easy to disable
- [x] Reach formatting
- [x] Coloured console output
- [x] Custom `UIColor` representation

## Usage

Expand All @@ -28,22 +30,40 @@ SpeedLog.print(["Super"], ["Speed"])
### Log output Styling

```swift
SpeedLog.mode = LogMode.FuncName
SpeedLog.mode = .FuncName
SpeedLog.print("Show only FunctionName")
//myFunc(): Show only FunctionName

SpeedLog.mode = LogMode.FuncName | LogMode.FileName
SpeedLog.mode = [.FuncName, .FileName]
SpeedLog.print("Show FunctionName and File name")
//AppDelegate.myFunc(): Show FunctionName and File name

SpeedLog.mode = LogMode.FuncName | LogMode.FileName | LogMode.Line
SpeedLog.print("Show all 3 options :)")
//AppDelegate.myFunc()[35]: Show all 3 options :)
SpeedLog.mode = [.FuncName, .FileName, .Line]
SpeedLog.print("Show 3 options :)")
//AppDelegate.myFunc()[36]: Show 3 options :)

SpeedLog.mode = LogMode.AllOptions
SpeedLog.mode = .FullCodeLocation
SpeedLog.print("Show fullCode, same as above")
//AppDelegate.myFunc()[39]: Show fullCode, same as above

SpeedLog.mode = .AllOptions
SpeedLog.print("Enable All Features")
//AppDelegate.myFunc()[26]: Enable All Features
//2015-11-26 19:32:33.687 AppDelegate.myFunc()[27]: Enable All Features
```
### UIColor log
`SpeedLog` has nice `UIColor` log style.
All you need to do is enable it once by calling `SpeedLog.enableVisualColorLog()`

![](/Images/RGB-colors-log.png)

```swift
SpeedLog.enableVisualColorLog()
SpeedLog.print("Visual:", c)

SpeedLog.disableVisualColorLog()
SpeedLog.print("Original Restored:", c)
```
P.S - You also need to install [XcodeColors](https://github.com/robbiehanson/XcodeColors) Xcode Plugin. Use [Alcatraz](http://alcatraz.io) to install it.

###Enables Log

Expand Down Expand Up @@ -84,6 +104,13 @@ github "kostiakoval/SpeedLog"

Copy the `SpeedLog.swift` file into your project

### Colors

In order to use colorised console you need to install [XcodeColors](https://github.com/robbiehanson/XcodeColors) Xcode plugin.
Use [Alcatraz](http://alcatraz.io) to install it. Go to:
Xcode -> Window -> Package Manager -> Search for `XcodeColors` and install it


## Future Features
There are many logging libs I like:

Expand Down
52 changes: 52 additions & 0 deletions Source/ColorLog.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//
// Colors.swift
// SpeedLog
//
// Created by Kostiantyn Koval on 23/11/15.
// Copyright © 2015 Kostiantyn Koval. All rights reserved.
//

import Foundation

/// RGBColorType representation
protocol RGBColorType {

/// Return RGB color represenation.
/// Example R: 200, G: 125, G: 255
var colorCode: String { get }
}

struct ColorLog {
struct Key {
private static let Escape = "\u{001b}["
private static let Fg = "fg"
static let Bg = "bg"

static let StartFg = "\(Escape)\(Fg)"
static let StartBg = "\(Escape)\(Bg)"

static let ResetFG = Escape + "fg;" // Clear any foreground color
static let ResetBG = Escape + "bg;" // Clear any background color
static let Reset = Escape + ";" // Clear any foreground or background color
}

/// String with a Font color
static func font<T>(color: RGBColorType, object: T) -> String {
return "\(Key.StartFg)\(color.colorCode);\(object)\(Key.Reset)"
}

/// String with a Background color
static func background<T>(color: RGBColorType, object: T) -> String {
return "\(Key.StartBg)\(color.colorCode);\(object)\(Key.Reset)"
}

/// String with both Background and Font color
static func colored<T>(font: RGBColorType, background: RGBColorType, object: T) -> String {
let string =
"\(Key.Escape)fg\(font.colorCode);" +
"\(Key.Escape)bg\(background.colorCode);" +
"\(object)\(Key.Reset)"

return string
}
}
54 changes: 37 additions & 17 deletions Source/SpeedLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,19 @@ typealias SLog = SpeedLog
///LogMode type. Specify what details should be included to the log
public struct LogMode : OptionSetType {

private var value: UInt = 0
public var rawValue: UInt { return value }

public init(rawValue value: UInt) { self.value = value }
public init(_ value: UInt) { self.value = value }
public let rawValue: UInt
public init(rawValue: UInt) { self.rawValue = rawValue }

//MARK:- Options
public static var None = LogMode(rawValue: 0)
public static var FileName = LogMode(rawValue: 1 << 0)
public static var FuncName = LogMode(rawValue: 1 << 1)
public static var Line = LogMode(rawValue: 1 << 2)
public static let None = LogMode(rawValue: 0)
public static let FileName = LogMode(rawValue: 1 << 0)
public static let FuncName = LogMode(rawValue: 1 << 1)
public static let Line = LogMode(rawValue: 1 << 2)
public static let Date = LogMode(rawValue: 1 << 3)

/// AllOptions - Enable all options, [FileName, FuncName, Line]
public static var AllOptions: LogMode = [FileName, FuncName, Line]
public static let AllOptions: LogMode = [Date, FileName, FuncName, Line]
public static let FullCodeLocation: LogMode = [FileName, FuncName, Line]
}


Expand All @@ -44,8 +43,8 @@ public struct SpeedLog {
*/

public static func print(items: Any..., separator: String = " ", terminator: String = "\n", _ file: String = __FILE__, _ function: String = __FUNCTION__, _ line: Int = __LINE__) {
#if ENABLE_LOG
let prefix = modePrefix(file, function: function, line: line)
#if !DISABLE_LOG
let prefix = modePrefix(NSDate(), file: file, function: function, line: line)
let stringItem = items.map {"\($0)"} .joinWithSeparator(separator)
Swift.print("\(prefix)\(stringItem)", terminator: terminator)
#endif
Expand All @@ -54,14 +53,19 @@ public struct SpeedLog {

extension SpeedLog {

/**
Creates an output string for the currect log Mode
*/
static func modePrefix(file: String, function: String, line: Int) -> String {
/// Create an output string for the currect log Mode
static func modePrefix(date: NSDate, file: String, function: String, line: Int) -> String {
var result: String = ""
if mode.contains(.Date) {
let formatter = NSDateFormatter()
formatter.dateFormat = "yyyy-MM-dd HH:mm:ss.SSS "

let s = formatter.stringFromDate(date)
result += s
}
if mode.contains(.FileName) {
let filename = file.lastPathComponent.stringByDeletingPathExtension
result = "\(filename)."
result += "\(filename)."
}
if mode.contains(.FuncName) {
result += "\(function)"
Expand All @@ -71,13 +75,29 @@ extension SpeedLog {
}

if !result.isEmpty {
result = result.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceCharacterSet())
result += ": "
}

return result
}
}

// MARK: - ColorLog

public extension SpeedLog {

/// Use custom UIColor desription
static func enableVisualColorLog() {
UIColor.swizzleDescription()
}

/// Restore default UIColor desription
static func disableVisualColorLog() {
UIColor.undoDesriptionSwizzling()
}
}

/// String syntax sugar extension
extension String {
var ns: NSString {
Expand Down
24 changes: 24 additions & 0 deletions Source/Swizzling.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// Swizzling.swift
// SpeedLog
//
// Created by Kostiantyn Koval on 24/11/15.
// Copyright © 2015 Kostiantyn Koval. All rights reserved.
//

import Foundation

extension NSObject {

class func swizzleMethods(origSelector: Selector, withSelector: Selector, forClass: AnyClass) {
let originalMethod = class_getInstanceMethod(forClass, origSelector)
let swizzledMethod = class_getInstanceMethod(forClass, withSelector)

method_exchangeImplementations(originalMethod, swizzledMethod)
}

func swizzleMethods(origSelector: Selector, withSelector: Selector) {
let aClass: AnyClass! = object_getClass(self)
NSObject.swizzleMethods(origSelector, withSelector: withSelector, forClass: aClass)
}
}
Loading

0 comments on commit d843635

Please sign in to comment.