From 3897eeda02230eb9096f466efc3f4c0fbfcb2231 Mon Sep 17 00:00:00 2001 From: Patches Date: Thu, 9 Jan 2020 23:42:18 -0600 Subject: [PATCH] issue141 - Become agnostic to logging implementation (#142) --- .../java/com/isupatches/wisefy/WiseFyTests.kt | 17 -- .../wisefy/internal/TestWiseFyLogger.kt | 38 +++++ .../internal/base/BaseInstrumentationTest.kt | 7 +- .../main/java/com/isupatches/wisefy/WiseFy.kt | 64 +++----- .../com/isupatches/wisefy/WisePublicApi.kt | 15 +- .../connection/AbstractWiseFyConnection.kt | 14 +- .../connection/WiseFyConnectionLegacy.kt | 19 ++- .../connection/WiseFyConnectionSDK23.kt | 29 ++-- .../isupatches/wisefy/logging/WiseFyLogger.kt | 119 ++++++-------- .../logging/WiseFyLoggerImplementation.kt | 147 ------------------ .../wisefy/logging/WiseFyLoggingStrategy.kt | 33 ---- .../wisefy/search/AbstractWiseFySearch.kt | 34 ++-- .../wisefy/search/WiseFySearchLegacy.kt | 12 +- .../wisefy/search/WiseFySearchSDK23.kt | 14 +- .../wisefy/main/PublicApiVisibilityTests.kt | 6 - .../internal/logging/WiseFySampleLogger.kt | 66 ++++++++ .../internal/util/WiseFyFactory.kt | 5 +- 17 files changed, 260 insertions(+), 379 deletions(-) create mode 100644 wisefy/src/androidTest/java/com/isupatches/wisefy/internal/TestWiseFyLogger.kt delete mode 100644 wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggerImplementation.kt delete mode 100644 wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggingStrategy.kt create mode 100644 wisefysample/src/main/java/com/isupatches/wisefysample/internal/logging/WiseFySampleLogger.kt diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTests.kt b/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTests.kt index 6401f1e9..8b6b44c3 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTests.kt +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/WiseFyTests.kt @@ -1,6 +1,5 @@ package com.isupatches.wisefy -import androidx.test.platform.app.InstrumentationRegistry import com.isupatches.wisefy.internal.base.BaseInstrumentationTest import org.junit.Assert.assertEquals import org.junit.Assert.assertNotNull @@ -19,22 +18,6 @@ internal class WiseFyTests : BaseInstrumentationTest() { private const val EXPECTED_SIGNAL_RESULT = 35 } - @Test - fun brains_loggingFalse() { - val wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext) - .logging(false) - .getSmarts() - assertEquals(false, wisefy.isLoggingEnabled()) - } - - @Test - fun brains_loggingTrue() { - val wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext) - .logging(true) - .getSmarts() - assertEquals(true, wisefy.isLoggingEnabled()) - } - @Test fun calculateBars() { val result = wisefy.calculateBars(TEST_RSSI_LEVEL_HIGH, TEST_NUMBER_OF_BARS) diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/TestWiseFyLogger.kt b/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/TestWiseFyLogger.kt new file mode 100644 index 00000000..b6b908e1 --- /dev/null +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/TestWiseFyLogger.kt @@ -0,0 +1,38 @@ +package com.isupatches.wisefy.internal + +import com.isupatches.wisefy.logging.WiseFyLogger + +class TestWiseFyLogger : WiseFyLogger { + + override fun i(tag: String, message: String, vararg args: Any) { + // No-op + } + + override fun v(tag: String, message: String, vararg args: Any) { + // No-op + } + + override fun d(tag: String, message: String, vararg args: Any) { + // No-op + } + + override fun w(tag: String, message: String, vararg args: Any) { + // No-op + } + + override fun e(tag: String, message: String, vararg args: Any) { + // No-op + } + + override fun e(tag: String, throwable: Throwable, message: String, vararg args: Any) { + // No-op + } + + override fun wtf(tag: String, message: String, vararg args: Any) { + // No-op + } + + override fun wtf(tag: String, throwable: Throwable, message: String, vararg args: Any) { + // No-op + } +} diff --git a/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/base/BaseInstrumentationTest.kt b/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/base/BaseInstrumentationTest.kt index e4447b65..c34c1519 100644 --- a/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/base/BaseInstrumentationTest.kt +++ b/wisefy/src/androidTest/java/com/isupatches/wisefy/internal/base/BaseInstrumentationTest.kt @@ -10,6 +10,7 @@ import com.isupatches.wisefy.WiseFyPrechecks import com.isupatches.wisefy.callbacks.AddNetworkCallbacks import com.isupatches.wisefy.connection.WiseFyConnection import com.isupatches.wisefy.internal.NullCallbackUtil +import com.isupatches.wisefy.internal.TestWiseFyLogger import com.isupatches.wisefy.internal.VerificationUtil import com.isupatches.wisefy.internal.mock.MockNetworkUtil import com.isupatches.wisefy.internal.mock.MockWiseFyConnectionUtil @@ -51,13 +52,12 @@ internal abstract class BaseInstrumentationTest { mockWifiManager = mock(WifiManager::class.java) mockConnectivityManager = mock(ConnectivityManager::class.java) - wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext) + wisefy = WiseFy.Brains(InstrumentationRegistry.getInstrumentation().targetContext, TestWiseFyLogger()) .customConnectivityManager(mockConnectivityManager) .customWifiManager(mockWifiManager) .customWiseFyConnection(mockWiseFyConnection) .customWiseFyPrechecks(mockWiseFyPrechecks) .customWiseFySearch(mockWiseFySearch) - .logging(true) .getSmarts() wisefy.setupWiseFyThread(true) @@ -73,7 +73,8 @@ internal abstract class BaseInstrumentationTest { verificationUtil = VerificationUtil(mockConnectivityManager, mockWifiManager) } - @After open fun tearDown() { + @After + open fun tearDown() { wisefy.dump() } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.kt b/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.kt index 4f8419ca..0e3b5eec 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/WiseFy.kt @@ -94,7 +94,8 @@ class WiseFy private constructor( private val wifiManager: WifiManager, private val wisefyConnection: WiseFyConnection, private val wisefyPrechecks: WiseFyPrechecks, - private val wisefySearch: WiseFySearch + private val wisefySearch: WiseFySearch, + private val logger: WiseFyLogger? ) : WiseFyPublicApi { companion object { @@ -147,11 +148,12 @@ class WiseFy private constructor( */ class Brains @JvmOverloads constructor( context: Context, + logger: WiseFyLogger? = null, useLegacyConnection: Boolean = false, useLegacySearch: Boolean = false ) { - private var loggingEnabled: Boolean = false + private var logger: WiseFyLogger? = null private var connectivityManager: ConnectivityManager private var wifiManager: WifiManager private var wisefyConnection: WiseFyConnection @@ -168,33 +170,21 @@ class WiseFy private constructor( // and "useLegacyConnection" option is not enabled wisefyConnection = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || useLegacyConnection) { - WiseFyConnectionLegacy.create(connectivityManager, wifiManager) + WiseFyConnectionLegacy.create(connectivityManager, wifiManager, logger) } else { - WiseFyConnectionSDK23.create(connectivityManager, wifiManager) + WiseFyConnectionSDK23.create(connectivityManager, wifiManager, logger) } // We'll use SDK 23 logic for WiseFySearch if client is on at least an SDK 23 device // and "useLegacySearch" option is not enabled wisefySearch = if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M || useLegacySearch) { - WiseFySearchLegacy.create(wifiManager) + WiseFySearchLegacy.create(wifiManager, logger) } else { - WiseFySearchSDK23.create(wifiManager) + WiseFySearchSDK23.create(wifiManager, logger) } wisefyPrechecks = WiseFyPrechecksImpl.create(wisefySearch) } - /** - * Used to enable/or disable logging for a WiseFy instance. - * - * @param loggingEnabled If logging should be enabled for the WiseFy instance. - * - * @author Patches - * @since 3.0 - */ - fun logging(loggingEnabled: Boolean): Brains = apply { - this.loggingEnabled = loggingEnabled - } - /** * Used internally to set ConnectivityManager in tests. * @@ -268,24 +258,24 @@ class WiseFy private constructor( /** * Uses a private constructor and returns a WiseFy instance. * - * @see [WiseFyLogger.configureWiseFyLoggerImplementation] * @see [WiseFyConnection.init] * * Updates * - 05/12/2019: Added new call to [WiseFyConnection.init] + * - 01/07/2019: Added logger * * @author Patches * @since 3.0 */ fun getSmarts(): WiseFy { - WiseFyLogger.configureWiseFyLoggerImplementation(loggingEnabled) wisefyConnection.init() return WiseFy( connectivityManager = connectivityManager, wifiManager = wifiManager, wisefyConnection = wisefyConnection, wisefyPrechecks = wisefyPrechecks, - wisefySearch = wisefySearch + wisefySearch = wisefySearch, + logger = logger ) } } @@ -736,7 +726,7 @@ class WiseFy private constructor( it.quit() } if (it.isAlive) { - WiseFyLogger.warn( + logger?.w( TAG, "WiseFy Thread is still alive. Current status: isAlive(): %b, getState(): %s", it.isAlive, @@ -744,7 +734,7 @@ class WiseFy private constructor( ) it.interrupt() } - WiseFyLogger.debug( + logger?.d( TAG, "WiseFy Thread isAlive(): %b, getState(): %s", it.isAlive, @@ -754,7 +744,7 @@ class WiseFy private constructor( } wisefyHandler = null wisefyConnection.destroy() - WiseFyLogger.debug(TAG, "Cleaned up WiseFy Thread") + logger?.d(TAG, "Cleaned up WiseFy Thread") } /** @@ -1062,7 +1052,7 @@ class WiseFy private constructor( try { return InetAddress.getByAddress(ipAddress).hostAddress } catch (uhe: UnknownHostException) { - WiseFyLogger.error(TAG, uhe, "UnknownHostException while gathering IP (sync)") + logger?.e(TAG, uhe, "UnknownHostException while gathering IP (sync)") } return null } @@ -1099,7 +1089,7 @@ class WiseFy private constructor( try { callbacks?.retrievedIP(InetAddress.getByAddress(ipAddress).hostAddress) } catch (uhe: UnknownHostException) { - WiseFyLogger.error(TAG, uhe, "UnknownHostException while gathering IP (async)") + logger?.e(TAG, uhe, "UnknownHostException while gathering IP (async)") callbacks?.failureRetrievingIP() } } @@ -1435,20 +1425,6 @@ class WiseFy private constructor( return wisefyConnection.isDeviceRoaming() } - /** - * To query if logging is enabled or disabled for a WiseFy instance. - * - * @return boolean - If logging is enabled for the WiseFy instance - * - * @see [WiseFyLogger.isLoggingEnabled] - * - * @author Patches - * @since 3.0 - */ - @Sync - @CallingThread - override fun isLoggingEnabled(): Boolean = WiseFyLogger.isLoggingEnabled() - /** * To check if the device's current network is 5gHz. * @@ -1681,7 +1657,7 @@ class WiseFy private constructor( if (wifiConfiguration != null) { return removeNetworkConfiguration(wifiConfiguration) } else { - WiseFyLogger.warn(TAG, "SSID to remove: %s was not found in list to remove network", ssidToRemove) + logger?.w(TAG, "SSID to remove: %s was not found in list to remove network", ssidToRemove) } return false } @@ -2211,9 +2187,9 @@ class WiseFy private constructor( */ private fun addNetworkConfiguration(wifiConfiguration: WifiConfiguration): Int { val result = wifiManager.addNetwork(wifiConfiguration) - WiseFyLogger.debug(TAG, "Adding network with SSID: %s had result: %d", wifiConfiguration.SSID, result) + logger?.d(TAG, "Adding network with SSID: %s had result: %d", wifiConfiguration.SSID, result) if (result == WIFI_MANAGER_FAILURE) { - WiseFyLogger.error(TAG, "Error adding network configuration.") + logger?.e(TAG, "Error adding network configuration.") } return result } @@ -2289,7 +2265,7 @@ class WiseFy private constructor( private fun removeNetworkConfiguration(wifiConfiguration: WifiConfiguration): Boolean { wifiManager.disconnect() val result = wifiManager.removeNetwork(wifiConfiguration.networkId) - WiseFyLogger.debug(TAG, "Removing network with SSID: %s had result: %b", wifiConfiguration.SSID, result) + logger?.d(TAG, "Removing network with SSID: %s had result: %b", wifiConfiguration.SSID, result) wifiManager.reconnect() return result } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/WisePublicApi.kt b/wisefy/src/main/java/com/isupatches/wisefy/WisePublicApi.kt index 61ae6dbf..2954403e 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/WisePublicApi.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/WisePublicApi.kt @@ -59,6 +59,9 @@ import com.isupatches.wisefy.callbacks.SearchForSavedNetworksCallbacks * @see [SignalStrengthApi] * @see [WiseFy] * + * Updates + * - 01/07/2020: Removed isLoggingEnabled + * * @author Patches * @since 3.0 */ @@ -87,18 +90,6 @@ interface WiseFyPublicApi : AccessPointApi, AddNetworkApi, ConnectionApi, Device * @since 3.0 */ fun getWiseFyLock(): WiseFyLock - - /** - * To query if logging is enabled or disabled for a WiseFy instance. - * - * @return boolean - If logging is enabled for the WiseFy instance - * - * @see [WiseFy.isLoggingEnabled] - * - * @author Patches - * @since 3.0 - */ - fun isLoggingEnabled(): Boolean } /** diff --git a/wisefy/src/main/java/com/isupatches/wisefy/connection/AbstractWiseFyConnection.kt b/wisefy/src/main/java/com/isupatches/wisefy/connection/AbstractWiseFyConnection.kt index fc92ee14..63746a8b 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/connection/AbstractWiseFyConnection.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/connection/AbstractWiseFyConnection.kt @@ -27,11 +27,15 @@ import com.isupatches.wisefy.utils.rest * @see [WifiManager] * @see [WiseFyConnection] * + * Updates + * - 01/07/2020: Added WiseFyLogger + * * @author Patches * @since 4.0 */ internal abstract class AbstractWiseFyConnection( - private val wifiManager: WifiManager + private val wifiManager: WifiManager, + private val logger: WiseFyLogger? ) : WiseFyConnection { internal companion object { @@ -65,9 +69,9 @@ internal abstract class AbstractWiseFyConnection( connectionInfo?.let { if (!it.ssid.isNullOrEmpty()) { val currentSSID = it.ssid.replace(QUOTE, "") - WiseFyLogger.debug(TAG, "Current SSID: %s, Desired SSID: %s", currentSSID, ssid) + logger?.d(TAG, "Current SSID: %s, Desired SSID: %s", currentSSID, ssid) if (currentSSID.equals(ssid, ignoreCase = true) && isNetworkConnected()) { - WiseFyLogger.debug(TAG, "Network is connected") + logger?.d(TAG, "Network is connected") return true } } @@ -94,7 +98,7 @@ internal abstract class AbstractWiseFyConnection( */ @WaitsForTimeout override fun waitToConnectToSSID(ssid: String?, timeoutInMillis: Int): Boolean { - WiseFyLogger.debug( + logger?.d( TAG, "Waiting %d milliseconds to connect to network with ssid %s", timeoutInMillis, @@ -108,7 +112,7 @@ internal abstract class AbstractWiseFyConnection( } rest() currentTime = System.currentTimeMillis() - WiseFyLogger.debug(TAG, "Current time: %d, End time: %d (waitToConnectToSSID)", currentTime, endTime) + logger?.d(TAG, "Current time: %d, End time: %d (waitToConnectToSSID)", currentTime, endTime) } while (currentTime < endTime) return false } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionLegacy.kt b/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionLegacy.kt index 24567b3c..53d45ad9 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionLegacy.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionLegacy.kt @@ -31,14 +31,18 @@ import com.isupatches.wisefy.logging.WiseFyLogger * @see [WifiManager] * @see [AbstractWiseFyConnection] * + * Updates + * - 01/07/2020: Added WiseFyLogger + * * @author Patches * @since 3.0 */ @Suppress("deprecation") internal class WiseFyConnectionLegacy private constructor( private val connectivityManager: ConnectivityManager, - wifiManager: WifiManager -) : AbstractWiseFyConnection(wifiManager) { + wifiManager: WifiManager, + private val logger: WiseFyLogger? +) : AbstractWiseFyConnection(wifiManager, logger) { internal companion object { private val TAG = WiseFyConnectionLegacy::class.java.simpleName @@ -55,12 +59,17 @@ internal class WiseFyConnectionLegacy private constructor( * * Updates * - 01/04/2020: Formatting update + * - 01/07/2020: Added WiseFyLogger * * @author Patches * @since 4.0 */ - fun create(connectivityManager: ConnectivityManager, wifiManager: WifiManager): WiseFyConnection { - return WiseFyConnectionLegacy(connectivityManager, wifiManager) + fun create( + connectivityManager: ConnectivityManager, + wifiManager: WifiManager, + logger: WiseFyLogger? = null + ): WiseFyConnection { + return WiseFyConnectionLegacy(connectivityManager, wifiManager, logger) } } @@ -152,7 +161,7 @@ internal class WiseFyConnectionLegacy private constructor( */ override fun isNetworkConnected(): Boolean { val networkInfo = connectivityManager.activeNetworkInfo - WiseFyLogger.debug(TAG, "networkInfo: %s", networkInfo ?: "") + logger?.d(TAG, "networkInfo: %s", networkInfo ?: "") return networkInfo?.isConnectedAndAvailable() ?: false } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionSDK23.kt b/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionSDK23.kt index f52cf7e0..fe25fb0a 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionSDK23.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/connection/WiseFyConnectionSDK23.kt @@ -36,14 +36,18 @@ import com.isupatches.wisefy.logging.WiseFyLogger * @see [WifiManager] * @see [AbstractWiseFyConnection] * + * Updates + * - 01/07/2020: Added WiseFyLogger + * * @author Patches * @since 4.0 */ @RequiresApi(Build.VERSION_CODES.M) internal class WiseFyConnectionSDK23 private constructor( private val connectivityManager: ConnectivityManager, - wifiManager: WifiManager -) : AbstractWiseFyConnection(wifiManager) { + wifiManager: WifiManager, + private val logger: WiseFyLogger? +) : AbstractWiseFyConnection(wifiManager, logger) { internal companion object { // Internal to avoid SyntheticAccessor error within networkChangeCallback @@ -61,12 +65,17 @@ internal class WiseFyConnectionSDK23 private constructor( * * Updates * - 01/04/2020: Formatting update + * - 01/07/2020: Added WiseFyLogger * * @author Patches * @since 4.0 */ - fun create(connectivityManager: ConnectivityManager, wifiManager: WifiManager): WiseFyConnection { - return WiseFyConnectionSDK23(connectivityManager, wifiManager) + fun create( + connectivityManager: ConnectivityManager, + wifiManager: WifiManager, + logger: WiseFyLogger? = null + ): WiseFyConnection { + return WiseFyConnectionSDK23(connectivityManager, wifiManager, logger) } } @@ -78,13 +87,13 @@ internal class WiseFyConnectionSDK23 private constructor( object : ConnectivityManager.NetworkCallback() { override fun onAvailable(network: Network?) { super.onAvailable(network) - WiseFyLogger.debug(TAG, "onAvailable, $network") + logger?.d(TAG, "onAvailable, $network") this@WiseFyConnectionSDK23.connectionStatus = WiseFyConnectionStatus.AVAILABLE } override fun onCapabilitiesChanged(network: Network?, networkCapabilities: NetworkCapabilities?) { super.onCapabilitiesChanged(network, networkCapabilities) - WiseFyLogger.debug( + logger?.d( TAG, "onCapabilitiesChanged, network: $network, networkCapabilities: $networkCapabilities" ) @@ -92,24 +101,24 @@ internal class WiseFyConnectionSDK23 private constructor( override fun onLinkPropertiesChanged(network: Network?, linkProperties: LinkProperties?) { super.onLinkPropertiesChanged(network, linkProperties) - WiseFyLogger.debug(TAG, "onLinkPropertiesChanged, network: $network, linkProperties: $linkProperties") + logger?.d(TAG, "onLinkPropertiesChanged, network: $network, linkProperties: $linkProperties") } override fun onLosing(network: Network?, maxMsToLive: Int) { super.onLosing(network, maxMsToLive) - WiseFyLogger.debug(TAG, "onLosing, network: $network, maxMsToLive: $maxMsToLive") + logger?.d(TAG, "onLosing, network: $network, maxMsToLive: $maxMsToLive") this@WiseFyConnectionSDK23.connectionStatus = WiseFyConnectionStatus.LOSING } override fun onLost(network: Network?) { super.onLost(network) - WiseFyLogger.debug(TAG, "onLost, network: $network") + logger?.d(TAG, "onLost, network: $network") this@WiseFyConnectionSDK23.connectionStatus = WiseFyConnectionStatus.LOST } override fun onUnavailable() { super.onUnavailable() - WiseFyLogger.debug(TAG, "onUnavailable") + logger?.d(TAG, "onUnavailable") this@WiseFyConnectionSDK23.connectionStatus = WiseFyConnectionStatus.UNAVAILABLE } } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLogger.kt b/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLogger.kt index 5cab81b7..c2e8aa07 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLogger.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLogger.kt @@ -1,5 +1,5 @@ /* - * Copyright 2018 Patches Klinefelter + * Copyright 2020 Patches Klinefelter * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,135 +16,108 @@ package com.isupatches.wisefy.logging /** - * Logging helper class. + * Interface for clients for logging * * @author Patches - * @since 3.0 + * @since 5.0 */ -@Suppress("SpreadOperator") -internal object WiseFyLogger { - - private var wisefyLoggerImplementation: WiseFyLoggerImplementation? = null +interface WiseFyLogger { /** - * To return if logging is enabled for the WiseFyLoggerImplementation. - * - * @return bool - If logging is enabled for the instance of WiseFy + * Logs an info message * - * @see [ensureWiseFyLoggerImplementationExists] - * @see [WiseFyLoggerImplementation.isLoggingEnabled] + * @param tag The tag for the log message + * @param message The message to log (can include placeholders) + * @param args The formatting arguments for the log message * * @author Patches - * @since 3.0 + * @since 5.0 */ - fun isLoggingEnabled(): Boolean { - ensureWiseFyLoggerImplementationExists() - return wisefyLoggerImplementation?.isLoggingEnabled ?: false - } + fun i(tag: String, message: String, vararg args: Any) /** - * Creates and sets the WiseFyLoggerImplementation to use when logging. - * - * @param loggingEnabled - Whether logging should be enabled for the WiseFyLoggerImplementation + * Logs an verbose message * - * @see [WiseFyLoggerImplementation] + * @param tag The tag for the log message + * @param message The message to log (can include placeholders) + * @param args The formatting arguments for the log message * * @author Patches - * @since 3.0 + * @since 5.0 */ - fun configureWiseFyLoggerImplementation(loggingEnabled: Boolean) { - wisefyLoggerImplementation = WiseFyLoggerImplementation(loggingEnabled) - } + fun v(tag: String, message: String, vararg args: Any) /** - * Logs a debug message. + * Logs a debug message * * @param tag The tag for the log message * @param message The message to log (can include placeholders) * @param args The formatting arguments for the log message * - * @see [ensureWiseFyLoggerImplementationExists] - * @see [WiseFyLoggerImplementation.debug] - * * @author Patches - * @since 3.0 + * @since 5.0 */ - fun debug(tag: String, message: String, vararg args: Any) { - ensureWiseFyLoggerImplementationExists() - wisefyLoggerImplementation?.debug(tag, message, *args) - } + fun d(tag: String, message: String, vararg args: Any) /** - * Logs an error message. + * Logs a warning message * * @param tag The tag for the log message * @param message The message to log (can include placeholders) * @param args The formatting arguments for the log message * - * @see [ensureWiseFyLoggerImplementationExists] - * @see [WiseFyLoggerImplementation.error] - * * @author Patches - * @since 3.0 + * @since 5.0 */ - fun error(tag: String, message: String, vararg args: Any) { - ensureWiseFyLoggerImplementationExists() - wisefyLoggerImplementation?.error(tag, message, *args) - } + fun w(tag: String, message: String, vararg args: Any) /** - * Logs an error message with a throwable. + * Logs an error message * * @param tag The tag for the log message - * @param throwable A throwable to log with the message * @param message The message to log (can include placeholders) * @param args The formatting arguments for the log message * - * @see [ensureWiseFyLoggerImplementationExists] - * @see [WiseFyLoggerImplementation.error] - * * @author Patches - * @since 3.0 + * @since 5.0 */ - fun error(tag: String, throwable: Throwable, message: String, vararg args: Any) { - ensureWiseFyLoggerImplementationExists() - wisefyLoggerImplementation?.error(tag, throwable, message, *args) - } + fun e(tag: String, message: String, vararg args: Any) /** - * Logs a warning message. + * Logs an error message with throwable * * @param tag The tag for the log message + * @param throwable A throwable to log with the message * @param message The message to log (can include placeholders) * @param args The formatting arguments for the log message * - * @see [ensureWiseFyLoggerImplementationExists] - * @see [WiseFyLoggerImplementation.warn] - * * @author Patches - * @since 3.0 + * @since 5.0 */ - fun warn(tag: String, message: String, vararg args: Any) { - ensureWiseFyLoggerImplementationExists() - wisefyLoggerImplementation?.warn(tag, message, *args) - } + fun e(tag: String, throwable: Throwable, message: String, vararg args: Any) - /* - * HELPERS + /** + * Logs a terrible failure message + * + * @param tag The tag for the log message + * @param message The message to log (can include placeholders) + * @param args The formatting arguments for the log message + * + * @author Patches + * @since 5.0 */ + fun wtf(tag: String, message: String, vararg args: Any) /** - * If a WiseFyLoggerImplementation is not already configured or set, it will create - * a new instance with the default value of false for logging enabled. + * Logs a terrible failure message with throwable * - * @see [configureWiseFyLoggerImplementation] + * @param tag The tag for the log message + * @param throwable A throwable to log with the message + * @param message The message to log (can include placeholders) + * @param args The formatting arguments for the log message * * @author Patches - * @since 3.0 + * @since 5.0 */ - private fun ensureWiseFyLoggerImplementationExists() { - if (wisefyLoggerImplementation == null) { - configureWiseFyLoggerImplementation(false) - } - } + fun wtf(tag: String, throwable: Throwable, message: String, vararg args: Any) } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggerImplementation.kt b/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggerImplementation.kt deleted file mode 100644 index 021d1efc..00000000 --- a/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggerImplementation.kt +++ /dev/null @@ -1,147 +0,0 @@ -/* - * Copyright 2018 Patches Klinefelter - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.isupatches.wisefy.logging - -import android.util.Log -import java.util.Locale - -/** - * A class that implements the logging strategy desired for WiseFy. - * - * @see WiseFyLoggingStrategy - * - * @author Patches - * @since 3.0 - */ -@Suppress("SpreadOperator") -internal class WiseFyLoggerImplementation constructor( - internal val isLoggingEnabled: Boolean -) : WiseFyLoggingStrategy { - - companion object { - private const val MAX_TAG_LENGTH = 23 - } - - /** - * Logs a debug message. - * - * @param tag The tag for the log message - * @param message The message to log (can include placeholders) - * @param args The formatting arguments for the log message - * - * @see [isLoggable] - * @see [Log.d] - * - * @author Patches - * @since 3.0 - */ - override fun debug(tag: String, message: String, vararg args: Any) { - if (isLoggable(tag, Log.DEBUG)) { - Log.d(tag, message.format(Locale.US, *args)) - } - } - - /** - * Logs an error message. - * - * @param tag The tag for the log message - * @param message The message to log (can include placeholders) - * @param args The formatting arguments for the log message - * - * @see [isLoggable] - * @see[Log.e] - * - * @author Patches - * @since 3.0 - */ - override fun error(tag: String, message: String, vararg args: Any) { - if (isLoggable(tag, Log.ERROR)) { - Log.e(tag, message.format(Locale.US, *args)) - } - } - - /** - * Logs an error message with a throwable. - * - * @param tag The tag for the log message - * @param throwable A throwable to log with the message - * @param message The message to log (can include placeholders) - * @param args The formatting arguments for the log message - * - * @see [isLoggable] - * @see [Log.e] - * - * @author Patches - * @since 3.0 - */ - override fun error(tag: String, throwable: Throwable, message: String, vararg args: Any) { - if (isLoggable(tag, Log.ERROR)) { - Log.e(tag, message.format(Locale.US, *args), throwable) - } - } - - /** - * Logs a warning message. - * - * @param tag The tag for the log message - * @param message The message to log (can include placeholders) - * @param args The formatting arguments for the log message - * - * @see [isLoggable] - * @see [Log.w] - * - * @author Patches - * @since 3.0 - */ - override fun warn(tag: String, message: String, vararg args: Any) { - if (isLoggable(tag, Log.WARN)) { - Log.w(tag, message.format(Locale.US, *args)) - } - } - - /* - * HELPERS - */ - - /** - * Checks to see given a TAG, log level, and if logging is enabled if logging should occur. - * - * @param tag The tag to be used for the log - * @param level The level of logging (i.error Log.DEBUG, Log.WARN, Log.ERROR, etc) - * - * @return boolean - True if logging should occur based off level and other factors - * - * @see [isLoggingEnabled] - * @see [Log.isLoggable] - * @see [MAX_TAG_LENGTH] - * - * @author Patches - * @since 3.0 - */ - private fun isLoggable(tag: String, level: Int): Boolean { - val loggable: Boolean - var tagToUse = tag - if (isLoggingEnabled) { - loggable = true - } else { - if (tag.length > MAX_TAG_LENGTH) { - tagToUse = tag.substring(0, MAX_TAG_LENGTH - 1) - } - loggable = Log.isLoggable(tagToUse, level) - } - return loggable - } -} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggingStrategy.kt b/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggingStrategy.kt deleted file mode 100644 index 85b14b2d..00000000 --- a/wisefy/src/main/java/com/isupatches/wisefy/logging/WiseFyLoggingStrategy.kt +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2018 Patches Klinefelter - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.isupatches.wisefy.logging - -/** - * An interface for different logging options for implementation. - * - * @author Patches - * @since 3.0 - */ -internal interface WiseFyLoggingStrategy { - - fun debug(tag: String, message: String, vararg args: Any) - - fun warn(tag: String, message: String, vararg args: Any) - - fun error(tag: String, message: String, vararg args: Any) - - fun error(tag: String, throwable: Throwable, message: String, vararg args: Any) -} diff --git a/wisefy/src/main/java/com/isupatches/wisefy/search/AbstractWiseFySearch.kt b/wisefy/src/main/java/com/isupatches/wisefy/search/AbstractWiseFySearch.kt index 0bad8ae2..de06d214 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/search/AbstractWiseFySearch.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/search/AbstractWiseFySearch.kt @@ -31,12 +31,16 @@ import java.util.Locale * @see [WifiManager] * @see [WiseFySearch] * + * Updates + * - 01/07/2020: Added WiseFyLogger + * * @author Patches * @since 4.0 */ @Suppress("LargeClass") internal abstract class AbstractWiseFySearch( - private val wifiManager: WifiManager + private val wifiManager: WifiManager, + private val logger: WiseFyLogger? ) : WiseFySearch { companion object { @@ -183,7 +187,7 @@ internal abstract class AbstractWiseFySearch( val accessPointsTemp = scanResultsProvider() - WiseFyLogger.debug(TAG, "Scanning SSIDs, pass %d", scanPass) + logger?.d(TAG, "Scanning SSIDs, pass %d", scanPass) if (accessPointsTemp != null && accessPointsTemp.isNotEmpty()) { var found = false for (accessPoint in accessPointsTemp) { @@ -209,9 +213,9 @@ internal abstract class AbstractWiseFySearch( break } } else { - WiseFyLogger.warn(TAG, "Empty access point list") + logger?.w(TAG, "Empty access point list") } - WiseFyLogger.debug(TAG, "Current time: %d, end time: %d (findAccessPointByRegex)", currentTime, endTime) + logger?.d(TAG, "Current time: %d, end time: %d (findAccessPointByRegex)", currentTime, endTime) scanPass++ rest() } while (currentTime < endTime) @@ -360,7 +364,7 @@ internal abstract class AbstractWiseFySearch( * @since 3.0 */ private fun accessPointMatchesRegex(accessPoint: ScanResult?, regexForSSID: String): Boolean { - WiseFyLogger.debug( + logger?.d( TAG, "accessPoint. SSID: %s, regex for SSID: %s".format(Locale.US, accessPoint?.SSID, regexForSSID) ) @@ -390,15 +394,15 @@ internal abstract class AbstractWiseFySearch( private fun hasHighestSignalStrength(accessPoints: List, currentAccessPoint: ScanResult): Boolean { for (accessPoint in accessPoints) { if (accessPoint.SSID.equals(currentAccessPoint.SSID, ignoreCase = true)) { - WiseFyLogger.debug(TAG, "RSSI level of current access point: %d", currentAccessPoint.level) - WiseFyLogger.debug(TAG, "RSSI level of access point in list: %d", accessPoint.level) - WiseFyLogger.debug( + logger?.d(TAG, "RSSI level of current access point: %d", currentAccessPoint.level) + logger?.d(TAG, "RSSI level of access point in list: %d", accessPoint.level) + logger?.d( TAG, "comparison result: %d (hasHighestSignalStrength)", WifiManager.compareSignalLevel(accessPoint.level, currentAccessPoint.level) ) if (WifiManager.compareSignalLevel(accessPoint.level, currentAccessPoint.level) > 0) { - WiseFyLogger.debug(TAG, "Stronger signal strength found") + logger?.d(TAG, "Stronger signal strength found") return false } } @@ -433,25 +437,25 @@ internal abstract class AbstractWiseFySearch( var found = false for (i in accessPointsToReturn.indices) { val scanResult = accessPointsToReturn[i] - WiseFyLogger.debug(TAG, "SSID 1: %s, SSID 2: %s", accessPoint.SSID, scanResult.SSID) + logger?.d(TAG, "SSID 1: %s, SSID 2: %s", accessPoint.SSID, scanResult.SSID) if (accessPoint.SSID.equals(scanResult.SSID, ignoreCase = true)) { found = true - WiseFyLogger.debug(TAG, "RSSI level of access point 1: %d", scanResult.level) - WiseFyLogger.debug(TAG, "RSSI level of access point 2: %d", accessPoint.level) - WiseFyLogger.debug( + logger?.d(TAG, "RSSI level of access point 1: %d", scanResult.level) + logger?.d(TAG, "RSSI level of access point 2: %d", accessPoint.level) + logger?.d( TAG, "comparison result: %d (removeEntriesWithLowerSignalStrength)", WifiManager.compareSignalLevel(accessPoint.level, scanResult.level) ) if (WifiManager.compareSignalLevel(accessPoint.level, scanResult.level) > 0) { - WiseFyLogger.debug(TAG, "New result has a higher or same signal strength, swapping") + logger?.d(TAG, "New result has a higher or same signal strength, swapping") accessPointsToReturn[i] = accessPoint } } } if (!found) { - WiseFyLogger.debug(TAG, "Found new wifi network") + logger?.d(TAG, "Found new wifi network") accessPointsToReturn.add(accessPoint) } } diff --git a/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchLegacy.kt b/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchLegacy.kt index 28b82e40..eb37e6f3 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchLegacy.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchLegacy.kt @@ -16,6 +16,7 @@ package com.isupatches.wisefy.search import android.net.wifi.WifiManager +import com.isupatches.wisefy.logging.WiseFyLogger /** * A class used internally for the purposes of shared query logic. This handles saved networks and @@ -27,17 +28,22 @@ import android.net.wifi.WifiManager * * Updates * - 01/04/2020: Formatting update + * - 01/07/2020: Added WiseFyLogger * * @author Patches * @since 3.0 */ @Suppress("deprecation") internal class WiseFySearchLegacy private constructor( - wifiManager: WifiManager -) : AbstractWiseFySearch(wifiManager) { + wifiManager: WifiManager, + logger: WiseFyLogger? +) : AbstractWiseFySearch(wifiManager, logger) { internal companion object { - fun create(wifiManager: WifiManager): WiseFySearch = WiseFySearchLegacy(wifiManager) + fun create( + wifiManager: WifiManager, + logger: WiseFyLogger? = null + ): WiseFySearch = WiseFySearchLegacy(wifiManager, logger) } // For SDK 23 and below, devices are still allowed to trigger a scan for nearby access points, diff --git a/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchSDK23.kt b/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchSDK23.kt index 9e8e9e88..aa093d70 100644 --- a/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchSDK23.kt +++ b/wisefy/src/main/java/com/isupatches/wisefy/search/WiseFySearchSDK23.kt @@ -18,6 +18,7 @@ package com.isupatches.wisefy.search import android.net.wifi.WifiManager import android.os.Build import androidx.annotation.RequiresApi +import com.isupatches.wisefy.logging.WiseFyLogger /** * A class used internally to query and determine different parts of the connection state for a @@ -27,16 +28,23 @@ import androidx.annotation.RequiresApi * @see [AbstractWiseFySearch] * @see [WifiManager] * + * Updates + * - 01/07/2020: Added WiseFyLogger + * * @author Patches * @since 4.0 */ @RequiresApi(Build.VERSION_CODES.M) internal class WiseFySearchSDK23 private constructor( - private val wifiManager: WifiManager -) : AbstractWiseFySearch(wifiManager) { + private val wifiManager: WifiManager, + logger: WiseFyLogger? +) : AbstractWiseFySearch(wifiManager, logger) { internal companion object { - fun create(wifiManager: WifiManager): WiseFySearch = WiseFySearchSDK23(wifiManager) + fun create( + wifiManager: WifiManager, + logger: WiseFyLogger? = null + ): WiseFySearch = WiseFySearchSDK23(wifiManager, logger) } // For SDK 23 and above, devices will be limited on ability to trigger scans and it's been diff --git a/wisefy/src/test/java/com/isupatches/wisefy/main/PublicApiVisibilityTests.kt b/wisefy/src/test/java/com/isupatches/wisefy/main/PublicApiVisibilityTests.kt index aeacc17c..597c2b38 100644 --- a/wisefy/src/test/java/com/isupatches/wisefy/main/PublicApiVisibilityTests.kt +++ b/wisefy/src/test/java/com/isupatches/wisefy/main/PublicApiVisibilityTests.kt @@ -368,12 +368,6 @@ internal class PublicApiVisibilityTests { verify(wisefy).isDeviceRoaming() } - @Test - fun isLoggingEnabled_api() { - wisefy.isLoggingEnabled() - verify(wisefy).isLoggingEnabled() - } - @Test fun isNetwork5gHz_apis() { wisefy.isNetwork5gHz() diff --git a/wisefysample/src/main/java/com/isupatches/wisefysample/internal/logging/WiseFySampleLogger.kt b/wisefysample/src/main/java/com/isupatches/wisefysample/internal/logging/WiseFySampleLogger.kt new file mode 100644 index 00000000..cff12afd --- /dev/null +++ b/wisefysample/src/main/java/com/isupatches/wisefysample/internal/logging/WiseFySampleLogger.kt @@ -0,0 +1,66 @@ +package com.isupatches.wisefysample.internal.logging + +import android.util.Log +import com.isupatches.wisefy.logging.WiseFyLogger +import java.util.Locale + +private const val LOG_TAG = "WiseFySample" + +internal object WiseFySampleLogger : WiseFyLogger { + + override fun i(tag: String, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.INFO)) { + Log.i(LOG_TAG, createMessage(tag, message, args)) + } + } + + override fun v(tag: String, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.VERBOSE)) { + Log.v(LOG_TAG, createMessage(tag, message, args)) + } + } + + override fun d(tag: String, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.DEBUG)) { + Log.d(LOG_TAG, createMessage(tag, message, args)) + } + } + + override fun w(tag: String, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.WARN)) { + Log.w(LOG_TAG, createMessage(tag, message, args)) + } + } + + override fun e(tag: String, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.ERROR)) { + Log.e(LOG_TAG, createMessage(tag, message, args)) + } + } + + override fun e(tag: String, throwable: Throwable, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.ERROR)) { + Log.e(LOG_TAG, createMessage(tag, message, args), throwable) + } + } + + override fun wtf(tag: String, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.ERROR)) { + Log.wtf(LOG_TAG, createMessage(tag, message, args)) + } + } + + override fun wtf(tag: String, throwable: Throwable, message: String, vararg args: Any) { + if (Log.isLoggable(LOG_TAG, Log.ERROR)) { + Log.wtf(LOG_TAG, createMessage(tag, message, args), throwable) + } + } + + /* + * Private Helpers + */ + + private fun createMessage(tag: String, message: String, vararg args: Any): String { + return "$tag - ${message.format(Locale.US, *args)}" + } +} diff --git a/wisefysample/src/main/java/com/isupatches/wisefysample/internal/util/WiseFyFactory.kt b/wisefysample/src/main/java/com/isupatches/wisefysample/internal/util/WiseFyFactory.kt index 869c19e1..f6c55698 100644 --- a/wisefysample/src/main/java/com/isupatches/wisefysample/internal/util/WiseFyFactory.kt +++ b/wisefysample/src/main/java/com/isupatches/wisefysample/internal/util/WiseFyFactory.kt @@ -18,6 +18,7 @@ package com.isupatches.wisefysample.internal.util import androidx.fragment.app.FragmentActivity import com.isupatches.wisefy.WiseFy import com.isupatches.wisefy.WiseFyPublicApi +import com.isupatches.wisefysample.internal.logging.WiseFySampleLogger /** * @param activity The context to create a WiseFy implementation @@ -28,7 +29,5 @@ import com.isupatches.wisefy.WiseFyPublicApi * @see [WiseFyPublicApi] */ fun createWiseFy(activity: FragmentActivity): WiseFyPublicApi { - return WiseFy.Brains(activity) - .logging(true) - .getSmarts() + return WiseFy.Brains(activity, WiseFySampleLogger).getSmarts() }