Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 24 additions & 40 deletions platform/jvm/capture/src/main/kotlin/io/bitdrift/capture/Capture.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ package io.bitdrift.capture

import android.content.Context
import android.util.Log
import io.bitdrift.capture.common.IBackgroundThreadHandler
import io.bitdrift.capture.common.MainThreadHandler
import io.bitdrift.capture.events.span.Span
import io.bitdrift.capture.events.span.SpanResult
Expand All @@ -22,7 +21,6 @@ import io.bitdrift.capture.providers.SystemDateProvider
import io.bitdrift.capture.providers.session.SessionStrategy
import io.bitdrift.capture.reports.FatalIssueMechanism
import io.bitdrift.capture.reports.FatalIssueReporter
import io.bitdrift.capture.threading.CaptureDispatchers
import okhttp3.HttpUrl
import java.util.UUID
import java.util.concurrent.atomic.AtomicReference
Expand All @@ -36,17 +34,14 @@ internal sealed class LoggerState {

/**
* The logger is in the process of being started. Subsequent attempts to start the logger will be ignored.
* Any calls to Logger.log() meanwhile Logger.start() is in process will be cached in memory
*/
class Starting(
val preInitInMemoryLogger: PreInitInMemoryLogger,
) : LoggerState()
data object Starting : LoggerState()

/**
* The logger has been successfully started and is ready for use. Subsequent attempts to start the logger will be ignored.
*/
class Started(
val loggerImpl: LoggerImpl,
val logger: LoggerImpl,
) : LoggerState()

/**
Expand All @@ -62,7 +57,6 @@ object Capture {
internal const val LOG_TAG = "BitdriftCapture"
private val default: AtomicReference<LoggerState> = AtomicReference(LoggerState.NotStarted)
private val fatalIssueReporter = FatalIssueReporter()
private val preInitInMemoryLogger by lazy { PreInitInMemoryLogger() }

/**
* Returns a handle to the underlying logger instance, if Capture has been started.
Expand All @@ -72,8 +66,8 @@ object Capture {
fun logger(): ILogger? =
when (val state = default.get()) {
is LoggerState.NotStarted -> null
is LoggerState.Starting -> state.preInitInMemoryLogger
is LoggerState.Started -> state.loggerImpl
is LoggerState.Starting -> null
is LoggerState.Started -> state.logger
is LoggerState.StartFailure -> null
}

Expand Down Expand Up @@ -163,7 +157,6 @@ object Capture {
fieldProviders: List<FieldProvider> = listOf(),
dateProvider: DateProvider? = null,
apiUrl: HttpUrl = defaultCaptureApiUrl,
context: Context? = null,
) {
start(
apiKey,
Expand All @@ -173,7 +166,6 @@ object Capture {
dateProvider,
apiUrl,
CaptureJniLibrary,
context,
)
}

Expand All @@ -188,14 +180,12 @@ object Capture {
dateProvider: DateProvider? = null,
apiUrl: HttpUrl = defaultCaptureApiUrl,
bridge: IBridge,
context: Context? = null,
backgroundThreadHandler: IBackgroundThreadHandler = CaptureDispatchers.CommonBackground,
) {
// Note that we need to use @Synchronized to prevent multiple loggers from being initialized,
// while subsequent logger access relies on volatile reads.

// There's nothing we can do if we don't have yet access to the application context.
if (context == null && !ContextHolder.isInitialized) {
if (!ContextHolder.isInitialized) {
Log.w(
LOG_TAG,
"Attempted to initialize Capture with a null context",
Expand All @@ -204,32 +194,26 @@ object Capture {
}

// Ideally we would use `getAndUpdate` in here but it's available for API 24 and up only.
if (default.compareAndSet(LoggerState.NotStarted, LoggerState.Starting(preInitInMemoryLogger))) {
backgroundThreadHandler.runAsync {
try {
val unwrappedContext = context?.applicationContext ?: ContextHolder.APP_CONTEXT
if (configuration.enableFatalIssueReporting) {
fatalIssueReporter.initBuiltInMode(unwrappedContext)
}
val loggerImpl =
LoggerImpl(
apiKey = apiKey,
apiUrl = apiUrl,
context = unwrappedContext,
fieldProviders = fieldProviders,
dateProvider = dateProvider ?: SystemDateProvider(),
configuration = configuration,
sessionStrategy = sessionStrategy,
bridge = bridge,
fatalIssueReporter = fatalIssueReporter,
preInitLogFlusher = preInitInMemoryLogger,
)
default.set(LoggerState.Started(loggerImpl))
} catch (e: Throwable) {
Log.w(LOG_TAG, "Failed to start Capture", e)
preInitInMemoryLogger.clear()
default.set(LoggerState.StartFailure)
if (default.compareAndSet(LoggerState.NotStarted, LoggerState.Starting)) {
try {
if (configuration.enableFatalIssueReporting) {
fatalIssueReporter.initBuiltInMode(ContextHolder.APP_CONTEXT)
}
val loggerImpl =
LoggerImpl(
apiKey = apiKey,
apiUrl = apiUrl,
fieldProviders = fieldProviders,
dateProvider = dateProvider ?: SystemDateProvider(),
configuration = configuration,
sessionStrategy = sessionStrategy,
bridge = bridge,
fatalIssueReporter = fatalIssueReporter,
)
default.set(LoggerState.Started(loggerImpl))
} catch (e: Throwable) {
Log.w(LOG_TAG, "Failed to start Capture", e)
default.set(LoggerState.StartFailure)
}
} else {
Log.w(LOG_TAG, "Multiple attempts to start Capture")
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ internal class LoggerImpl(
dateProvider: DateProvider,
private val errorHandler: ErrorHandler = ErrorHandler(),
sessionStrategy: SessionStrategy,
context: Context,
context: Context = ContextHolder.APP_CONTEXT,
clientAttributes: ClientAttributes =
ClientAttributes(
context,
Expand All @@ -84,7 +84,6 @@ internal class LoggerImpl(
private val eventListenerDispatcher: CaptureDispatchers.CommonBackground = CaptureDispatchers.CommonBackground,
windowManager: IWindowManager = WindowManager(errorHandler),
private val fatalIssueReporter: IFatalIssueReporter,
private val preInitLogFlusher: IPreInitLogFlusher,
) : ILogger {
private val metadataProvider: MetadataProvider
private val batteryMonitor = BatteryMonitor(context)
Expand Down Expand Up @@ -262,8 +261,6 @@ internal class LoggerImpl(
appExitLogger.installAppExitLogger()

CaptureJniLibrary.startLogger(this.loggerId)

preInitLogFlusher.flushToNative(this)
}

writeSdkStartLog(context, clientAttributes, initDuration = duration)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,4 @@ class Span internal constructor(
}
logger = null
}

/**
* This is only needed during [io.bitdrift.capture.PreInitInMemoryLogger]
*/
internal fun setLoggerImpl(loggerImpl: LoggerImpl) {
this.logger = loggerImpl
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.nhaarman.mockitokotlin2.any
import com.nhaarman.mockitokotlin2.anyOrNull
import com.nhaarman.mockitokotlin2.mock
import com.nhaarman.mockitokotlin2.whenever
import io.bitdrift.capture.fakes.FakePreInitLogFlusher
import io.bitdrift.capture.providers.DateProvider
import io.bitdrift.capture.providers.session.SessionStrategy
import io.bitdrift.capture.reports.FatalIssueReporter
Expand Down Expand Up @@ -52,7 +51,6 @@ class CaptureLoggerSessionOverrideTest {
private lateinit var logger: LoggerImpl
private var testServerPort: Int? = null
private val fatalIssueReporter = FatalIssueReporter()
private val preInitLogFlusher = FakePreInitLogFlusher()

@Before
fun setUp() {
Expand Down Expand Up @@ -91,7 +89,6 @@ class CaptureLoggerSessionOverrideTest {
context = ContextHolder.APP_CONTEXT,
preferences = preferences,
fatalIssueReporter = fatalIssueReporter,
preInitLogFlusher = preInitLogFlusher,
)

CaptureTestJniLibrary.stopTestApiServer()
Expand Down Expand Up @@ -126,7 +123,6 @@ class CaptureLoggerSessionOverrideTest {
preferences = preferences,
activityManager = activityManager,
fatalIssueReporter = fatalIssueReporter,
preInitLogFlusher = preInitLogFlusher,
)
val newStreamId = CaptureTestJniLibrary.awaitNextApiStream()
assertThat(newStreamId).isNotEqualTo(-1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import io.bitdrift.capture.attributes.DeviceAttributes
import io.bitdrift.capture.attributes.NetworkAttributes
import io.bitdrift.capture.common.RuntimeFeature
import io.bitdrift.capture.fakes.FakeFatalIssueReporter
import io.bitdrift.capture.fakes.FakePreInitLogFlusher
import io.bitdrift.capture.network.HttpRequestInfo
import io.bitdrift.capture.network.HttpResponse
import io.bitdrift.capture.network.HttpResponseInfo
Expand Down Expand Up @@ -64,7 +63,6 @@ class CaptureLoggerTest {
private lateinit var logger: LoggerImpl
private var testServerPort: Int? = null
private val fatalIssueReporter: IFatalIssueReporter = FakeFatalIssueReporter()
private val preInitLogFlusher = FakePreInitLogFlusher()

@Before
fun setUp() {
Expand All @@ -76,7 +74,6 @@ class CaptureLoggerTest {

testServerPort = CaptureTestJniLibrary.startTestApiServer(-1)

preInitLogFlusher.reset()
logger = buildLogger(dateProvider = systemDateProvider)
}

Expand Down Expand Up @@ -444,7 +441,6 @@ class CaptureLoggerTest {
)

assertThat(JniRuntime(logger.loggerId).isEnabled(RuntimeFeature.SESSION_REPLAY_COMPOSE)).isFalse
assertThat(preInitLogFlusher.wasFlushed).isTrue()
}

private fun testServerUrl(): HttpUrl =
Expand Down Expand Up @@ -474,7 +470,6 @@ class CaptureLoggerTest {
dateProvider = dateProvider,
configuration = Configuration(),
fatalIssueReporter = fatalIssueReporter,
preInitLogFlusher = preInitLogFlusher,
)
}

Expand Down
Loading
Loading