Skip to content

Commit

Permalink
Code format
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Oct 18, 2023
1 parent 8081237 commit f7e5be9
Show file tree
Hide file tree
Showing 26 changed files with 4,049 additions and 4,050 deletions.
41 changes: 20 additions & 21 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,24 @@
import PackageDescription

let package = Package(
name: "DLog",
platforms: [
.iOS(.v12),
.macOS(.v10_14),
.tvOS(.v12),
.watchOS(.v5)
],
products: [
.library(name: "DLog", targets: ["DLog"]),
.library(name: "DLogObjC", targets: ["DLogObjC"]),
.executable(name: "NetConsole", targets: ["NetConsole"])
],
targets: [
.target(name: "DLog"),
.target(name: "DLogObjC", dependencies: ["DLog"]),
.target(name: "NetConsole"),
.testTarget(name: "DLogTests", dependencies: ["DLog"]),
.testTarget(name: "DLogTestsObjC", dependencies: ["DLogObjC"]),
],
swiftLanguageVersions: [.v5]
name: "DLog",
platforms: [
.iOS(.v12),
.macOS(.v10_14),
.tvOS(.v12),
.watchOS(.v5)
],
products: [
.library(name: "DLog", targets: ["DLog"]),
.library(name: "DLogObjC", targets: ["DLogObjC"]),
.executable(name: "NetConsole", targets: ["NetConsole"])
],
targets: [
.target(name: "DLog"),
.target(name: "DLogObjC", dependencies: ["DLog"]),
.target(name: "NetConsole"),
.testTarget(name: "DLogTests", dependencies: ["DLog"]),
.testTarget(name: "DLogTestsObjC", dependencies: ["DLogObjC"]),
],
swiftLanguageVersions: [.v5]
)

38 changes: 19 additions & 19 deletions Sources/DLog/Atomic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ import Foundation

@discardableResult
func synchronized<T : AnyObject, U>(_ obj: T, closure: () -> U) -> U {
objc_sync_enter(obj)
defer {
objc_sync_exit(obj)
}
return closure()
objc_sync_enter(obj)
defer {
objc_sync_exit(obj)
}
return closure()
}

@propertyWrapper
public class Atomic<T> {
private var value: T

public init(wrappedValue value: T) {
self.value = value
}

public var wrappedValue: T {
get {
synchronized(self) { value }
}
set {
synchronized(self) { value = newValue }
}
}
private var value: T
public init(wrappedValue value: T) {
self.value = value
}
public var wrappedValue: T {
get {
synchronized(self) { value }
}
set {
synchronized(self) { value = newValue }
}
}
}
242 changes: 121 additions & 121 deletions Sources/DLog/DLog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,135 +29,135 @@ import Foundation
/// The central class to emit log messages to specified outputs using one of the methods corresponding to a log level.
///
public class DLog: LogProtocol {

private let output: LogOutput?

/// The shared disabled logger.
///
/// Using this constant prevents from logging messages.
///
/// let logger = DLog.disabled
///
@objc
public static let disabled = DLog(nil)

/// Creates a logger object that assigns log messages to a specified category.
///
/// You can define category name to differentiate unique areas and parts of your app and DLog uses this value
/// to categorize and filter related log messages.
///
/// let logger = DLog()
/// let netLogger = logger["NET"]
/// let netLogger.log("Hello Net!")
///
/// - Parameters:
/// - name: Name of category.
@objc
public subscript(name: String) -> LogProtocol {
category(name: name)
}

private let output: LogOutput?

/// The shared disabled logger.
///
/// Using this constant prevents from logging messages.
///
/// let logger = DLog.disabled
///
@objc
public static let disabled = DLog(nil)

/// Creates a logger object that assigns log messages to a specified category.
///
/// You can define category name to differentiate unique areas and parts of your app and DLog uses this value
/// to categorize and filter related log messages.
///
/// let logger = DLog()
/// let netLogger = logger["NET"]
/// let netLogger.log("Hello Net!")
///
/// - Parameters:
/// - name: Name of category.
@objc
public subscript(name: String) -> LogProtocol {
category(name: name)
}

/// Creates a logger object with a configuration that assigns log messages to a specified category.
///
/// You can define category name to differentiate unique areas and parts of your app and DLog uses this value
/// to categorize and filter related log messages.
///
/// var config = LogConfig()
/// config.sign = ">"
///
/// let logger = DLog()
/// let netLogger = logger.category(name: "NET", config: config)
///
/// - Parameters:
/// - name: Name of category.
/// - config: Configuration of category.
public func category(name: String, config: LogConfig? = nil, metadata: Metadata? = nil) -> LogProtocol {
LogProtocol(logger: self, category: name, config: config ?? self.config, metadata: metadata ?? self.metadata.data)
}

/// Creates the logger instance with a target output object.
///
/// Create an instance and use it to log text messages about your app’s behaviour and to help you assess the state
/// of your app later. You also can choose a target output and a log level to indicate the severity of that message.
///
/// let logger = DLog()
/// logger.log("Hello DLog!")
///
/// - Parameters:
/// - output: A target output object. If it is omitted the logger uses `stdout` by default.
///
public init(_ output: LogOutput? = .stdout, config: LogConfig = LogConfig(), metadata: Metadata = Metadata()) {
self.output = output
super.init(logger: nil, category: "DLOG", config: config, metadata: metadata)
self.logger = self
}

/// Creates the logger instance with a list of linked outputs for both swift and objective-c code.
///
/// Swift:
///
/// let logger = DLog([.textPlain, .stdout])
///
/// Objective-C:
///
/// DLog* logger = [[DLog alloc] initWithOutputs:@[LogOutput.textPlain, filter, LogOutput.stdOut]];
///
/// - Parameters:
/// - outputs: An array of outputs.
///
@objc
public convenience init(outputs: [LogOutput]) {
var output: LogOutput?

/// Creates a logger object with a configuration that assigns log messages to a specified category.
///
/// You can define category name to differentiate unique areas and parts of your app and DLog uses this value
/// to categorize and filter related log messages.
///
/// var config = LogConfig()
/// config.sign = ">"
///
/// let logger = DLog()
/// let netLogger = logger.category(name: "NET", config: config)
///
/// - Parameters:
/// - name: Name of category.
/// - config: Configuration of category.
public func category(name: String, config: LogConfig? = nil, metadata: Metadata? = nil) -> LogProtocol {
LogProtocol(logger: self, category: name, config: config ?? self.config, metadata: metadata ?? self.metadata.data)
if outputs.count == 0 {
output = .stdout
}

/// Creates the logger instance with a target output object.
///
/// Create an instance and use it to log text messages about your app’s behaviour and to help you assess the state
/// of your app later. You also can choose a target output and a log level to indicate the severity of that message.
///
/// let logger = DLog()
/// logger.log("Hello DLog!")
///
/// - Parameters:
/// - output: A target output object. If it is omitted the logger uses `stdout` by default.
///
public init(_ output: LogOutput? = .stdout, config: LogConfig = LogConfig(), metadata: Metadata = Metadata()) {
self.output = output
super.init(logger: nil, category: "DLOG", config: config, metadata: metadata)
self.logger = self
}

/// Creates the logger instance with a list of linked outputs for both swift and objective-c code.
///
/// Swift:
///
/// let logger = DLog([.textPlain, .stdout])
///
/// Objective-C:
///
/// DLog* logger = [[DLog alloc] initWithOutputs:@[LogOutput.textPlain, filter, LogOutput.stdOut]];
///
/// - Parameters:
/// - outputs: An array of outputs.
///
@objc
public convenience init(outputs: [LogOutput]) {
var output: LogOutput?

if outputs.count == 0 {
output = .stdout
}
else {
output = outputs.count == 1
? outputs.first
: outputs.reduce(.textPlain, =>)
}

self.init(output)
else {
output = outputs.count == 1
? outputs.first
: outputs.reduce(.textPlain, =>)
}

/// Creates the default logger.
@objc
public convenience init() {
self.init(_:config:metadata:)()
}

// Scope

func enter(scope: LogScope) {
guard let out = output else { return }
self.init(output)
}

/// Creates the default logger.
@objc
public convenience init() {
self.init(_:config:metadata:)()
}

// Scope

func enter(scope: LogScope) {
guard let out = output else { return }
ScopeStack.shared.append(scope) {
out.scopeEnter(scope: scope)
}
}

func leave(scope: LogScope) {
guard let out = output else { return }
}
func leave(scope: LogScope) {
guard let out = output else { return }
ScopeStack.shared.remove(scope) {
out.scopeLeave(scope: scope)
}
}

// Interval
func begin(interval: LogInterval) {
guard let out = output else { return }
out.intervalBegin(interval: interval)
}

func end(interval: LogInterval) {
guard let out = output else { return }
out.intervalEnd(interval: interval)
}

func log(message: @escaping () -> LogMessage, type: LogType, category: String, config: LogConfig, scope: LogScope?, metadata: Metadata, file: String, function: String, line: UInt) -> String? {
guard let out = output else { return nil }
let item = LogItem(type: type, category: category, config: config, scope: scope, metadata: metadata, file: file, funcName: function, line: line, message: message)
return out.log(item: item)
}
}
// Interval
func begin(interval: LogInterval) {
guard let out = output else { return }
out.intervalBegin(interval: interval)
}
func end(interval: LogInterval) {
guard let out = output else { return }
out.intervalEnd(interval: interval)
}
func log(message: @escaping () -> LogMessage, type: LogType, category: String, config: LogConfig, scope: LogScope?, metadata: Metadata, file: String, function: String, line: UInt) -> String? {
guard let out = output else { return nil }
let item = LogItem(type: type, category: category, config: config, scope: scope, metadata: metadata, file: file, funcName: function, line: line, message: message)
return out.log(item: item)
}
}
36 changes: 18 additions & 18 deletions Sources/DLog/Dynamic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,25 @@ import os


typealias Swift_Demangle = @convention(c) (_ mangledName: UnsafePointer<UInt8>?,
_ mangledNameLength: Int,
_ outputBuffer: UnsafeMutablePointer<UInt8>?,
_ outputBufferSize: UnsafeMutablePointer<Int>?,
_ flags: UInt32) -> UnsafeMutablePointer<Int8>?
_ mangledNameLength: Int,
_ outputBuffer: UnsafeMutablePointer<UInt8>?,
_ outputBufferSize: UnsafeMutablePointer<Int>?,
_ flags: UInt32) -> UnsafeMutablePointer<Int8>?
/// Dynamic shared object
class Dynamic {

// Constants
static let dso = UnsafeMutableRawPointer(mutating: #dsohandle)
private static let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)

private static func dynamic<T>(symbol: String) -> T? {
guard let sym = dlsym(RTLD_DEFAULT, symbol) else {
return nil
}
return unsafeBitCast(sym, to: T.self)

// Constants
static let dso = UnsafeMutableRawPointer(mutating: #dsohandle)
private static let RTLD_DEFAULT = UnsafeMutableRawPointer(bitPattern: -2)

private static func dynamic<T>(symbol: String) -> T? {
guard let sym = dlsym(RTLD_DEFAULT, symbol) else {
return nil
}

// Functions
static let OS_ACTIVITY_CURRENT: os_activity_t? = dynamic(symbol: "_os_activity_current")
static let swift_demangle: Swift_Demangle? = dynamic(symbol: "swift_demangle")
return unsafeBitCast(sym, to: T.self)
}

// Functions
static let OS_ACTIVITY_CURRENT: os_activity_t? = dynamic(symbol: "_os_activity_current")
static let swift_demangle: Swift_Demangle? = dynamic(symbol: "swift_demangle")
}
Loading

0 comments on commit f7e5be9

Please sign in to comment.