Skip to content

Commit

Permalink
Migration to Swift 4, minor updates.
Browse files Browse the repository at this point in the history
  • Loading branch information
Rien committed Apr 6, 2018
1 parent c35231a commit 8eb337d
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 54 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ build
/Packages
/DerivedData
Package.pins
Package.resolved
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ let package = Package(
name: "SwifterLog",
dependencies: [
.Package(url: "https://github.com/Balancingrock/CAsl", "0.1.0"),
.Package(url: "https://github.com/Balancingrock/VJson", "0.10.9"),
.Package(url: "https://github.com/Balancingrock/SwifterSockets", "0.10.10")
.Package(url: "https://github.com/Balancingrock/VJson", "0.10.10"),
.Package(url: "https://github.com/Balancingrock/SwifterSockets", "0.10.11")
]
)
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ Maintenance updates are updates due to management of the (SPM) package hierarchy
- Allow custom targets
- Allow custom formatting

#### v1.1.1 (Current)
#### v1.1.2 (Current)

- Migration to Swift 4, minor changes.
- Updated user manual

#### v1.1.1

- Updated dependecies

Expand Down
8 changes: 5 additions & 3 deletions Sources/Source.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// File: Source.swift
// Project: SwifterLog
//
// Version: 1.1.0
// Version: 1.1.2
//
// Author: Marinus van der Lugt
// Company: http://balancingrock.nl
Expand Down Expand Up @@ -51,7 +51,9 @@
// =====================================================================================================================
//
// History:
// 1.1.0 - Initial release in preperation for v2.0.0
//
// 1.1.2 - Migration to Swift 4, minor changes.
// 1.1.0 - Initial release in preperation for v2.0.0
//
// =====================================================================================================================

Expand All @@ -78,7 +80,7 @@ public struct Source: Hashable {
self.type = type
self.function = function
self.line = line
let arr: [Int] = [id?.hashValue, file?.hashValue, type?.hashValue, function?.hashValue, line?.hashValue].flatMap { $0 }
let arr: [Int] = [id?.hashValue, file?.hashValue, type?.hashValue, function?.hashValue, line?.hashValue].compactMap { $0 }
self.hashValue = arr.reduce(5381) { return ($0 << 5) &+ $0 &+ $1 }
}

Expand Down
6 changes: 4 additions & 2 deletions Sources/Target.Logfiles.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// File: Target.File.swift
// Project: SwifterLog
//
// Version: 1.1.0
// Version: 1.1.2
//
// Author: Marinus van der Lugt
// Company: http://balancingrock.nl
Expand Down Expand Up @@ -53,6 +53,8 @@
// =====================================================================================================================
//
// History:
//
// 1.1.2 - Migration to Swift 4, minor changes.
// 1.1.0 - Initial release in preperation for v2.0.0
//
// =====================================================================================================================
Expand Down Expand Up @@ -189,7 +191,7 @@ public class Logfiles: Target {

let logfileUrl = URL(fileURLWithPath: logdir).appendingPathComponent(filename)

if FileManager.default.createFile(atPath: logfileUrl.path, contents: nil, attributes: [FileAttributeKey.posixPermissions.rawValue : NSNumber(value: 0o640)]) {
if FileManager.default.createFile(atPath: logfileUrl.path, contents: nil, attributes: [FileAttributeKey(rawValue: FileAttributeKey.posixPermissions.rawValue) : NSNumber(value: 0o640)]) {

return FileHandle(forUpdatingAtPath: logfileUrl.path)

Expand Down
17 changes: 0 additions & 17 deletions Tests/SwifterLogTests/SwifterLogTests.swift

This file was deleted.

40 changes: 11 additions & 29 deletions docs/UserManual.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

## Jump start

The framework defines a singleton called `theLogger`. This variable can be use this variable to access all logging functions. For ease of use, it is recommened to create a global variable from this:
The framework defines a singleton called `singleton`. This variable can be use this variable to access all logging functions. For ease of use, it is recommened to create a global variable from this:

let log = SwifterLog.theLogger
let log = SwifterLog.singleton

Before using the logging functions, first setup the log level thresholds for the targets. In code this is done as follows:

Expand All @@ -18,19 +18,10 @@ Of course the default is `.none`, thus it is not necessary to assign `.none` to

After the threshold levels are set, use the log as follows:

log.atLevelError(id: logId, source: "My source identifier", message: "Error message")
log.atError(message: "Informative text or variable", from: Source(id: -1, file: #file, type: "textual", function: #function, line: #line), to: myTargets)
or

log.atLevelDebug(id: logId, source: #file.source(#function, #line), message: myVariable)

or alternatively without the id parameter:

log.atLevelInfo(source: "My source identifier", message: myObject)
or

log.atLevelNotice(source: #file.source(#function, #line), message: "Error message")

For best layout in the log, either always use the ID parameter, or never us it.
log.atDebug(message: myVariable, from: Source(id: -1, file: #file, type: "myType", function: #function, line:#line))

The ID parameter can be used to differentiate between objects, sockets, threads etc. Set it to -1 if IDs are used but there is no such id present in a specific case.

Expand All @@ -46,22 +37,22 @@ Consider the following:

import SwifterLog
typealias Log = SwifterLog
let log = Log.theLogger
let log = Log.singleton

log.stdoutPrintAtAndAboveLevel = .info
extension MyGreatVariable: ReflectedStringConvertable {}
let myGreatVariable = MyGreatVariable()
log.atLevelDebug(source: "Here", message: myGreatVariable)
log.atDebug(message: myGreatVariable, from: Source(...))

As said before, this works fine. When there is no target with a threshold below `.info` no log information is written. However the call `atLevelDebug` is always made. And hence all parameters must be evaluated and the information must be placed on the stack. That overhead is always incurred. Wether the debug level is used or not.
As said before, this works fine. When there is no target with a threshold below `.info` no log information is written. However the call `atDebug` is always made. And hence all parameters must be evaluated and the information must be placed on the stack. That overhead is always incurred. Wether the debug level is used or not.

The solution is to avoid the evaluation/preparation when the debug level is not used.

For this purpose there are additional optional (level dependent) loggers available. Because they are optionals they will be nil when the debug level is not in use. For example the optional debug logger is SwifterLog.atDebug

The last line of the above example then becomes:

Log.atDebug?.log(source: "Here", message: myGreatVariable)
Log.atDebug?.log(message: myGreatVariable, from: Source(...))

While the readability has suffered slightly, this has the big advantage of not evaluating the parameters of the call if the debug level is not used in any target. Under circumstances this can either give a performance boost to the application, or alternatively, it becomes unneccesary to comment-out all logger call at the debug and info level before shipping.

Expand Down Expand Up @@ -175,7 +166,7 @@ The information is transmitted as a small JSON record. An example:

All of the data fields are in fact strings.

__Warning__: If a network target is too slow, logging messages will stack up in the logger. Leading to increased use of system resources that could eventually crash the applications.
__Warning__: If a network target is too slow, logging messages will stack up in the logger. Leading to increased use of system resources that could eventually crash the application.

Be aware of privacy issues when using a network target!

Expand All @@ -197,7 +188,7 @@ It can be uninstalled by:

Note that from within the callback there should be no logging entries made that include the callback target!

__Warning__: If a callback target is too slow, logging messages will stack up in the logger. Leading to increased use of system resources that could eventually crash the applications.
__Warning__: If a callback target is too slow, logging messages will stack up in the logger. Leading to increased use of system resources that could eventually crash the application.

## Levels

Expand Down Expand Up @@ -353,19 +344,10 @@ Caveat: The end user cannot change the plist entries without invalidating the ap

## Extra's

### Source
To make life easier, SwifterLog has an extension on String to create a very readable `source` identifier.

The Swift language has three literals that make sense to use as a `source` identifier: #file, #function and #line.

However the value of these is not very readable if they are simply concatenated. Therefore the String extension "source" can be used to transform the value of these literals into a shorter more readable form:

log.atLevelNotice(source: #file.source(#function, #line), message: "Error message")

### ReflectedStringConvertable

Logging calls accept as a message the argument "Any". To retrieve the information the "description" method is used. This works fine for most build in types and for structs.

However for classes this does not work very well. This where the ReflectedStringConvertible can help. By simply extending a class with it ReflectedStringConvertible protocol, classes will provide a neatly formatted output without any additional work from the developer.

Thanks to Matt Comi for this [idea](https://github.com/mattcomi/ReflectedStringConvertible)
Thanks to Matt Comi for this [idea](https://github.com/mattcomi/ReflectedStringConvertible)

0 comments on commit 8eb337d

Please sign in to comment.