Skip to content

Commit

Permalink
Code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ikhvorost committed Jan 16, 2024
1 parent 8b3c7a0 commit 8b69b1a
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 48 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/swift.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ jobs:
- name: Xcode 15.2
run: |
sudo xcode-select -s '/Applications/Xcode_15.2.app/Contents/Developer'
xcodebuild -version
- name: Build
run: sh build.sh DLog-Package
run: swift build

- name: Test
run: |
swift build
swift test --enable-code-coverage
xcrun llvm-cov export -format="lcov" .build/debug/DLogPackageTests.xctest/contents/macos/DLogPackageTests -instr-profile .build/debug/codecov/default.profdata -ignore-filename-regex='Tests' -ignore-filename-regex='Net' > info.lcov
Expand Down
5 changes: 2 additions & 3 deletions Sources/DLog/LogInterval.swift
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,6 @@ public class LogInterval: LogItem {
private let logger: DLog
private let id: Int
private let name: String
private let _metadata: Metadata
@Atomic
private var begun = false

Expand All @@ -148,7 +147,6 @@ public class LogInterval: LogItem {
self.name = name
self.id = "\(location.file):\(location.function):\(location.line)".hash
self.staticName = staticName
self._metadata = metadata

let message = { LogMessage(stringLiteral: name) }
super.init(message: message, type: .interval, category: category, config: config, scope: scope, metadata: {[metadata]}, location: location)
Expand Down Expand Up @@ -216,7 +214,8 @@ public class LogInterval: LogItem {
(.min, "min", { stringFromTimeInterval(statistics.min) }),
(.total, "total", { stringFromTimeInterval(statistics.total) }),
]
metadata = [_metadata, Metadata.metadata(from: items, options: config.intervalConfig.options)]
let metadata = Metadata.metadata(from: items, options: config.intervalConfig.options)
self.metadata = _metadata() + [metadata]

logger.end(interval: self)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/DLog/LogItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public class LogItem: NSObject {

let config: LogConfig

private let _metadata: () -> [Metadata]
let _metadata: () -> [Metadata]
/// Metadata of log message
public lazy var metadata: [Metadata] = { _metadata() }()

Expand Down
3 changes: 1 addition & 2 deletions Sources/DLog/LogPrivacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ public enum LogPrivacy {
var info = kinfo_proc()
var size = MemoryLayout.size(ofValue: info)
var mib: [Int32] = [CTL_KERN, KERN_PROC, KERN_PROC_PID, getpid()]
return sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0) == 0 &&
(info.kp_proc.p_flag & P_TRACED) != 0
return sysctl(&mib, UInt32(mib.count), &info, &size, nil, 0) == 0 && (info.kp_proc.p_flag & P_TRACED) != 0
}()

private static let isXCTest: Bool = { NSClassFromString("XCTest") != nil }()
Expand Down
4 changes: 2 additions & 2 deletions Sources/DLog/LogProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public class LogProtocol: NSObject {
///
@discardableResult
public func scope(_ name: String, metadata: Metadata? = nil, closure: ((LogScope) -> Void)? = nil) -> LogScope {
let scope = LogScope(name: name, logger: logger, category: category, config: self.config, metadata: metadata ?? self.logger.metadata.data)
let scope = LogScope(name: name, logger: logger, category: category, config: self.config, metadata: metadata ?? logger.metadata.data)
if let block = closure {
scope.enter()
block(scope)
Expand All @@ -262,7 +262,7 @@ public class LogProtocol: NSObject {
config.intervalConfig = intervalConfig
}

let interval = LogInterval(logger: logger, name: name, staticName: staticName, category: category, config: config, scope: _scope, metadata: self.metadata.data, location: location)
let interval = LogInterval(logger: logger, name: name, staticName: staticName, category: category, config: config, scope: _scope, metadata: metadata.data, location: location)
if let block = closure {
interval.begin()
block()
Expand Down
13 changes: 6 additions & 7 deletions Sources/DLog/Mach.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ struct TaskInfo {
}

static var power: task_power_info { taskInfo(task_power_info(), TASK_POWER_INFO) }
static var power_v2: task_power_info_v2 { taskInfo(task_power_info_v2(), TASK_POWER_INFO_V2) }
static var vm: task_vm_info { taskInfo(task_vm_info(), TASK_VM_INFO) }
}

Expand All @@ -65,18 +64,18 @@ struct ThreadInfo {
func threadsInfo() -> (cpuUsage: Int32, threadsCount: UInt32) {
var thread_list: thread_act_array_t?
var thread_count: mach_msg_type_number_t = 0
guard task_threads(mach_task_self_, &thread_list, &thread_count) == KERN_SUCCESS, let threads = thread_list else {
return (0, 0)
}
task_threads(mach_task_self_, &thread_list, &thread_count)

defer {
let size = MemoryLayout<thread_t>.size * Int(thread_count)
vm_deallocate(mach_task_self_, vm_address_t(threads.pointee), vm_size_t(size))
if thread_count > 0 {
let size = MemoryLayout<thread_t>.size * Int(thread_count)
vm_deallocate(mach_task_self_, vm_address_t(thread_list!.pointee), vm_size_t(size))
}
}

var cpu_usage: Int32 = 0
for i in 0..<Int(thread_count) {
let basic = ThreadInfo.basic(thread: threads[i])
let basic = ThreadInfo.basic(thread: thread_list![i])
if basic.flags & TH_FLAGS_IDLE == 0 {
cpu_usage += basic.cpu_usage
}
Expand Down
9 changes: 3 additions & 6 deletions Sources/DLog/TraceStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,12 @@ public struct StackConfig {
}

fileprivate func swift_demangle(_ mangled: String) -> String? {
guard mangled.hasPrefix("$s") else {
guard mangled.hasPrefix("$s"), let cString = Dynamic.swift_demangle?(mangled, mangled.count, nil, nil, 0) else {
return nil
}

if let cString = Dynamic.swift_demangle?(mangled, mangled.count, nil, nil, 0) {
defer { cString.deallocate() }
return String(cString: cString)
}
return nil
defer { cString.deallocate() }
return String(cString: cString)
}

func stackMetadata(moduleName: String, stackAddresses: ArraySlice<NSNumber>, config: StackConfig) -> [Metadata] {
Expand Down
1 change: 1 addition & 0 deletions Tests/DLogTests/DLogTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1370,6 +1370,7 @@ final class TraceTests: XCTestCase {
func test_trace_config_all() {
var config = LogConfig()
config.traceConfig.options = .all
config.traceConfig.stackConfig.view = .all
let logger = DLog(config: config)
let text = logger.trace()
XCTAssert(text?.match(#"\#(Location) \{func:test_trace_config_all,process:\{cpu:\d+%,memory:\d+MB,pid:\d+,threads:\d+\},queue:com\.apple\.main-thread,stack:\[\{frame:\d+,symbol:DLogTests\.TraceTests\.test_trace_config_all\(\) -> \(\)\}"#) == true)
Expand Down
24 changes: 0 additions & 24 deletions build.sh

This file was deleted.

0 comments on commit 8b69b1a

Please sign in to comment.