Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TokamakLogger #104

Merged
merged 20 commits into from Jun 4, 2019
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions Package.resolved
@@ -1,6 +1,15 @@
{
"object": {
"pins": [
{
"package": "swift-log",
"repositoryURL": "https://github.com/apple/swift-log.git",
"state": {
"branch": null,
"revision": "f4240bf022a69815241a883c03645444b58ac553",
"version": "1.1.0"
}
},
{
"package": "SwiftSyntax",
"repositoryURL": "https://github.com/apple/swift-syntax.git",
Expand Down
3 changes: 2 additions & 1 deletion Package.swift
Expand Up @@ -40,6 +40,7 @@ let package = Package(
// .package(url: /* package url */, from: "1.0.0"),
.package(url: "https://github.com/apple/swift-syntax.git", .exact("0.50000.0")),
.package(url: "https://github.com/jakeheis/SwiftCLI", from: "5.0.0"),
.package(url: "https://github.com/apple/swift-log.git", from: "1.1.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define
Expand Down Expand Up @@ -72,7 +73,7 @@ let package = Package(
),
.target(
name: "TokamakLint",
dependencies: ["SwiftSyntax"]
dependencies: ["SwiftSyntax", "Logging"]
),
.testTarget(
name: "TokamakTests",
Expand Down
6 changes: 3 additions & 3 deletions Sources/TokamakCLI/main.swift
Expand Up @@ -33,7 +33,7 @@ class LintCommand: Command {
}
}

let TokamakCLI = CLI(name: "TokamakCLI", version: "0.1.2", description: "Tokamak CLI tools")
let tokamakCLI = CLI(name: "TokamakCLI", version: "0.1.2", description: "Tokamak CLI tools")

TokamakCLI.commands = [LintCommand()]
TokamakCLI.goAndExit()
tokamakCLI.commands = [LintCommand()]
tokamakCLI.goAndExit()
132 changes: 132 additions & 0 deletions Sources/TokamakLint/Reporters/TokamakLogger.swift
@@ -0,0 +1,132 @@
//
// TokamakLogger.swift
// TokamakLint
//
// Created by Matvii Hodovaniuk on 5/30/19.
//

import Foundation
import Logging

enum Output {
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
case stdout
case file
}

public struct Outputs: OptionSet {
public init(rawValue: Outputs.RawValue) {
self.rawValue = rawValue
}

public let rawValue: Int

static let stdout = Outputs(rawValue: 1)
static let file = Outputs(rawValue: 2)
}

public struct TokamakLogger: LogHandler {
private static var overrideLogLevel: Logger.Level?
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved

// this holds the log level if not overridden
private var _logLevel: Logger.Level = .info

// metadata storage
public var metadata: Logger.Metadata = [:]

public init(label: String) {}

public var outputs = [Outputs.file]

public var _path: URL?

public var path: URL {
get {
guard let _path = _path else {
let fm = FileManager.default
guard let url = fm.urls(
for: FileManager.SearchPathDirectory.documentDirectory,
in: FileManager.SearchPathDomainMask.userDomainMask
)
.last?.appendingPathComponent("log.txt") else {
return URL(fileURLWithPath: "")
}
return url
}
return _path
}

set {
_path = newValue
}
}

public var logLevel: Logger.Level {
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
// when we get asked for the log level, we check if it was
// globally overridden or not
get {
return _logLevel
}
// we set the log level whenever we're asked (note: this might not
// have an effect if globally overridden)
set {
_logLevel = newValue
}
}

public func log(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function Parameter Count Violation: Function should have 5 parameters or less: it currently has 6 (function_parameter_count)

level: Logger.Level,
message: Logger.Message,
metadata: Logger.Metadata?,
file: String,
function: String,
line: UInt
) {
if outputs.contains(Outputs.stdout) {
print(message.description)
}

if outputs.contains(Outputs.file) {
write(message.description)
}
}

public func log(level: Logger.Level, message: Logger.Message) {
log(
level: level,
message: message,
metadata: .none,
file: "",
function: "",
line: 0
)
}

func write(_ string: String) {
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
let data = string.data(using: String.Encoding.utf8)
let fm = FileManager.default
if !fm.fileExists(atPath: path.absoluteString) {
fm.createFile(
atPath: path.absoluteString,
contents: data, attributes: nil
)
} else {
var file = FileHandle(forReadingAtPath: path.absoluteString)
_ = String(describing: file?.readDataToEndOfFile())
file = FileHandle(forWritingAtPath: path.absoluteString)
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
if file != nil {
file?.seek(toFileOffset: 10)
file?.write(data!)
file?.closeFile()
}
}
}

public subscript(metadataKey metadataKey: String) -> Logger.Metadata.Value? {
get {
return metadata[metadataKey]
}
set(newValue) {
metadata[metadataKey] = newValue
}
}
}
27 changes: 27 additions & 0 deletions Tests/TokamakCLITests/TokamakLint/TokamakLoggerTest.swift
@@ -0,0 +1,27 @@
//
// LoggerTest.swift
// TokamakCLITests
//
// Created by Matvii Hodovaniuk on 5/31/19.
//

import Logging
@testable import TokamakLint
import XCTest

final class TokamakLoggerTests: XCTestCase {
func testLogToFile() throws {
MaxDesiatov marked this conversation as resolved.
Show resolved Hide resolved
let fm = FileManager.default
guard let url = fm.urls(
for: FileManager.SearchPathDirectory.desktopDirectory,
in: FileManager.SearchPathDomainMask.userDomainMask
)
.last?.appendingPathComponent("log.txt")
else { return }

var logHandler = TokamakLogger(label: "TokamakCLI Output")
logHandler.outputs = [.stdout, .file]
logHandler.path = url
logHandler.log(level: .warning, message: "Msg")
}
}
25 changes: 25 additions & 0 deletions Tokamak.xcodeproj/Logging_Info.plist
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8"?>
<plist version="1.0">
<dict>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>
<string>6.0</string>
<key>CFBundleName</key>
<string>$(PRODUCT_NAME)</string>
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>$(CURRENT_PROJECT_VERSION)</string>
<key>NSPrincipalClass</key>
<string></string>
</dict>
</plist>
4 changes: 4 additions & 0 deletions Tokamak.xcodeproj/project.pbxproj
Expand Up @@ -33,6 +33,7 @@
/* End PBXAggregateTarget section */

/* Begin PBXBuildFile section */
A6BB978422A2BF68007A0102 /* TokamakLogger.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BB978322A2BF68007A0102 /* TokamakLogger.swift */; };
A6BFBFEC229D301000F2B06F /* GetComponents.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BFBFEB229D301000F2B06F /* GetComponents.swift */; };
A6BFBFEE229FFEFF00F2B06F /* ComponentIsStructRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6BFBFED229FFEFF00F2B06F /* ComponentIsStructRule.swift */; };
A6E7BC612293D7B20042E787 /* HooksRule.swift in Sources */ = {isa = PBXBuildFile; fileRef = A6E7BC602293D7B20042E787 /* HooksRule.swift */; };
Expand Down Expand Up @@ -400,6 +401,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
A6BB978322A2BF68007A0102 /* TokamakLogger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TokamakLogger.swift; sourceTree = "<group>"; };
A6BFBFEB229D301000F2B06F /* GetComponents.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GetComponents.swift; sourceTree = "<group>"; };
A6BFBFED229FFEFF00F2B06F /* ComponentIsStructRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComponentIsStructRule.swift; sourceTree = "<group>"; };
A6E7BC602293D7B20042E787 /* HooksRule.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HooksRule.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -899,6 +901,7 @@
isa = PBXGroup;
children = (
OBJ_147 /* XcodeReporter.swift */,
A6BB978322A2BF68007A0102 /* TokamakLogger.swift */,
);
path = Reporters;
sourceTree = "<group>";
Expand Down Expand Up @@ -1922,6 +1925,7 @@
OBJ_550 /* Location.swift in Sources */,
OBJ_551 /* Node.swift in Sources */,
OBJ_552 /* RuleDescription.swift in Sources */,
A6BB978422A2BF68007A0102 /* TokamakLogger.swift in Sources */,
A6BFBFEE229FFEFF00F2B06F /* ComponentIsStructRule.swift in Sources */,
OBJ_553 /* StyleViolation.swift in Sources */,
A6BFBFEC229D301000F2B06F /* GetComponents.swift in Sources */,
Expand Down
36 changes: 18 additions & 18 deletions Tokamak.xcodeproj/xcshareddata/xcschemes/Tokamak-Package.xcscheme
Expand Up @@ -28,9 +28,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Tokamak::TokamakCLI"
BuildableName = "TokamakCLI"
BlueprintName = "TokamakCLI"
BlueprintIdentifier = "Tokamak::TokamakTestRenderer"
BuildableName = "TokamakTestRenderer.framework"
BlueprintName = "TokamakTestRenderer"
ReferencedContainer = "container:Tokamak.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -42,9 +42,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Tokamak::TokamakDemo"
BuildableName = "TokamakDemo.framework"
BlueprintName = "TokamakDemo"
BlueprintIdentifier = "Tokamak::TokamakLint"
BuildableName = "TokamakLint.framework"
BlueprintName = "TokamakLint"
ReferencedContainer = "container:Tokamak.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -56,9 +56,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Tokamak::TokamakUIKit"
BuildableName = "TokamakUIKit.framework"
BlueprintName = "TokamakUIKit"
BlueprintIdentifier = "Tokamak::TokamakDemo"
BuildableName = "TokamakDemo.framework"
BlueprintName = "TokamakDemo"
ReferencedContainer = "container:Tokamak.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -70,9 +70,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Tokamak::Tokamak"
BuildableName = "Tokamak.framework"
BlueprintName = "Tokamak"
BlueprintIdentifier = "Tokamak::TokamakCLI"
BuildableName = "TokamakCLI"
BlueprintName = "TokamakCLI"
ReferencedContainer = "container:Tokamak.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -84,9 +84,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Tokamak::TokamakLint"
BuildableName = "TokamakLint.framework"
BlueprintName = "TokamakLint"
BlueprintIdentifier = "Tokamak::TokamakUIKit"
BuildableName = "TokamakUIKit.framework"
BlueprintName = "TokamakUIKit"
ReferencedContainer = "container:Tokamak.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand All @@ -98,9 +98,9 @@
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "Tokamak::TokamakTestRenderer"
BuildableName = "TokamakTestRenderer.framework"
BlueprintName = "TokamakTestRenderer"
BlueprintIdentifier = "Tokamak::Tokamak"
BuildableName = "Tokamak.framework"
BlueprintName = "Tokamak"
ReferencedContainer = "container:Tokamak.xcodeproj">
</BuildableReference>
</BuildActionEntry>
Expand Down