Skip to content

Commit 827cf52

Browse files
author
Nacho Bonafonte
committed
Add an extra-debug mode DD_TRACE_DEBUG_CALLSTACK , that also prints callstacks after each log message
1 parent b72d07f commit 827cf52

File tree

6 files changed

+26
-8
lines changed

6 files changed

+26
-8
lines changed

Sources/DatadogSDKTesting/DDEnvironmentValues.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ internal enum ConfigurationValues: String, CaseIterable {
3535
case DD_ENDPOINT
3636
case DD_DONT_EXPORT
3737
case DD_TRACE_DEBUG
38+
case DD_TRACE_DEBUG_CALLSTACK
3839
}
3940

4041
// These configuration values must not be passed to the child app in an UI test
@@ -134,6 +135,7 @@ internal struct DDEnvironmentValues {
134135

135136
/// The framework has been launched with extra debug information
136137
let extraDebug: Bool
138+
let extraDebugCallstack: Bool
137139

138140
/// Intelligent test runner related environment
139141
let gitUploadEnabled: Bool
@@ -288,8 +290,11 @@ internal struct DDEnvironmentValues {
288290
let envReportHostname = DDEnvironmentValues.getEnvVariable(ExtraConfigurationValues.DD_CIVISIBILITY_REPORT_HOSTNAME.rawValue) as NSString?
289291
reportHostname = envReportHostname?.boolValue ?? false
290292

293+
let envExtraDebugCallStack = DDEnvironmentValues.getEnvVariable(ConfigurationValues.DD_TRACE_DEBUG_CALLSTACK.rawValue) as NSString?
294+
extraDebugCallstack = envExtraDebugCallStack?.boolValue ?? false
295+
291296
let envExtraDebug = DDEnvironmentValues.getEnvVariable(ConfigurationValues.DD_TRACE_DEBUG.rawValue) as NSString?
292-
extraDebug = envExtraDebug?.boolValue ?? false
297+
extraDebug = envExtraDebug?.boolValue ?? extraDebugCallstack
293298

294299
/// CI values
295300
var branchEnv: String?

Sources/DatadogSDKTesting/DDTestModule.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public class DDTestModule: NSObject, Encodable {
9999
Log.debug("Module loading time interval: \(DDTestMonitor.clock.now.timeIntervalSince(beforeLoadingTime))")
100100
}
101101

102-
func internalEnd(endTime: Date? = nil) {
102+
private func internalEnd(endTime: Date? = nil) {
103103
if DDTestMonitor.instance?.crashedModuleInfo != nil && anyTestExecuted == false {
104104
//The module started after a crash and no test was executed because of ITR dont report it
105105
DDTestMonitor.tracer.flush()

Sources/DatadogSDKTesting/DDTestObserver.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,20 @@ class DDTestObserver: NSObject, XCTestObservation {
3030

3131
func testBundleWillStart(_ testBundle: Bundle) {
3232
let bundleName = testBundle.bundleURL.deletingPathExtension().lastPathComponent
33+
Log.debug("testBundleWillStart: \(bundleName)")
3334
module = DDTestModule.start(bundleName: bundleName)
3435
module?.testFramework = "XCTest"
3536
}
3637

3738
func testBundleDidFinish(_ testBundle: Bundle) {
3839
/// We need to wait for all the traces to be written to the backend before exiting
3940
module?.end()
41+
Log.debug("testBundleDidFinish: \(testBundle.bundleURL.deletingPathExtension().lastPathComponent)")
4042
}
4143

4244
func testSuiteWillStart(_ testSuite: XCTestSuite) {
4345
if module?.configError ?? false {
46+
Log.debug("testSuiteWillStart: Failed, module config error")
4447
testSuite.testRun?.stop()
4548
exit(1)
4649
}
@@ -51,7 +54,6 @@ class DDTestObserver: NSObject, XCTestObservation {
5154
else {
5255
return
5356
}
54-
5557
Log.measure(name: "waiting itrWorkQueue") {
5658
DDTestMonitor.instance?.itrWorkQueue.waitUntilAllOperationsAreFinished()
5759
}
@@ -62,6 +64,7 @@ class DDTestObserver: NSObject, XCTestObservation {
6264
testSuite.setValue(finalTests, forKey: "_mutableTests")
6365
if !finalTests.isEmpty {
6466
suite = module.suiteStart(name: testSuite.name)
67+
Log.debug("testSuiteWillStart: \(testSuite.name)")
6568
}
6669
let testsToSkip = tests.count - finalTests.count
6770
Log.print("ITR skipped \(testsToSkip) tests")
@@ -70,6 +73,7 @@ class DDTestObserver: NSObject, XCTestObservation {
7073
}
7174
} else {
7275
suite = module.suiteStart(name: testSuite.name)
76+
Log.debug("testSuiteWillStart: \(testSuite.name)")
7377
}
7478
}
7579

@@ -78,6 +82,7 @@ class DDTestObserver: NSObject, XCTestObservation {
7882
tests.firstObject is XCTestCase
7983
{
8084
suite?.end()
85+
Log.debug("testSuiteDidFinish: \(testSuite.name)")
8186
}
8287
}
8388

Sources/DatadogSDKTesting/DDTestSuite.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public class DDTestSuite: NSObject, Encodable {
3838
}
3939
}
4040

41-
func internalEnd(endTime: Date? = nil) {
41+
private func internalEnd(endTime: Date? = nil) {
4242
duration = (endTime ?? DDTestMonitor.clock.now).timeIntervalSince(startTime).toNanoseconds
4343
/// Export module event
4444

Sources/DatadogSDKTesting/ImageSymbols/DDSymbolicator.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -295,11 +295,15 @@ enum DDSymbolicator {
295295
Spawn.commandToFile("/usr/bin/symbols -fullSourcePath -lazy \"\(imagePath)\"", outputPath: symbolsOutputURL.path)
296296
return symbolsOutputURL
297297
}
298-
299298

300-
static func getCallStack() -> [String] {
299+
static func getCallStack(hidesLibrarySymbols: Bool = true) -> [String] {
301300
let callStackSymbols = Thread.callStackSymbols
302-
let index = callStackSymbols.firstIndex { !$0.contains(exactWord: "DatadogSDKTesting") } ?? 0
301+
let index: Array<String>.Index
302+
if hidesLibrarySymbols {
303+
index = callStackSymbols.firstIndex { !$0.contains(exactWord: "DatadogSDKTesting") } ?? callStackSymbols.startIndex
304+
} else {
305+
index = callStackSymbols.index(0, offsetBy: 2)
306+
}
303307

304308
let demangled: [String] = callStackSymbols.dropFirst(index)
305309
.map {

Sources/DatadogSDKTesting/Utils/Log.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Foundation
99

1010
struct Log {
1111
private static var debugTracer = DDTestMonitor.env.extraDebug
12+
private static var debugTracerCallstack = DDTestMonitor.env.extraDebugCallstack
1213

1314
private static func swiftPrint(_ string: String) {
1415
Swift.print(string)
@@ -18,7 +19,7 @@ struct Log {
1819
NSLog(string)
1920
}
2021

21-
private static var printMethod: () -> (String) -> () = {
22+
private static var printMethod: () -> (String) -> Void = {
2223
let osActivityMode = DDEnvironmentValues.getEnvVariable("OS_ACTIVITY_MODE") ?? ""
2324
if osActivityMode == "disable" {
2425
return swiftPrint
@@ -30,6 +31,9 @@ struct Log {
3031
static func debug(_ string: @autoclosure () -> String) {
3132
if debugTracer {
3233
Log.printMethod()("[Debug][DatadogSDKTesting] " + string() + "\n")
34+
if debugTracerCallstack {
35+
Swift.print("Callstack:\n" + DDSymbolicator.getCallStack(hidesLibrarySymbols: false).joined(separator: "\n") + "\n")
36+
}
3337
}
3438
}
3539

0 commit comments

Comments
 (0)