Skip to content

Commit

Permalink
Create a Logger to log all the debugging info centralized
Browse files Browse the repository at this point in the history
  • Loading branch information
Morteza-Rastgoo committed Sep 3, 2021
1 parent df07eaa commit 2be50eb
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package se.kantarsifo.mobileanalytics.framework

import android.util.Log

object Logger {
const val TAG = "MobileAppTagging"
fun log(message: String) {
if (TSMobileAnalytics.logPrintsActivated) {
Log.d(TAG, "***********************************")
Log.d(TAG, message)
Log.d(TAG, "***********************************")
}
}

fun error(message: String) {
if (TSMobileAnalytics.logPrintsActivated) {
Log.e(TAG, "***********************************")
Log.e(TAG, message)
Log.e(TAG, "***********************************")
}
}

fun fatalError(message: String) {
Log.e(TAG, "***********************************")
Log.e(TAG, message)
Log.e(TAG, "***********************************")
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import android.app.Activity
import android.content.Context
import android.content.Intent
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.net.Uri
import androidx.activity.ComponentActivity
import org.json.JSONArray
import org.json.JSONException
import se.kantarsifo.mobileanalytics.framework.Logger.error
import se.kantarsifo.mobileanalytics.framework.Utils.isPackageInstalled
import java.io.BufferedReader
import java.io.FileInputStream
Expand Down Expand Up @@ -92,7 +92,7 @@ internal object PanelistHandler {
context.createPackageContext(packageName, 0).openFileInput(filename)
} catch (e: Exception) {

TSMobileAnalyticsBackend.logError("Error Getting InputStream")
error("Error Getting InputStream")
null
}
}
Expand All @@ -106,7 +106,7 @@ internal object PanelistHandler {
cookieList.add(CookieHandler.getCookieFromJson(entry))
}
} catch (e: JSONException) {
TSMobileAnalyticsBackend.logError("Error parsing TNS Panelist JSON data")
error("Error parsing TNS Panelist JSON data")
}
return cookieList
}
Expand All @@ -121,7 +121,7 @@ internal object PanelistHandler {
cookieList.add(CookieHandler.getCookieFromJson(entry))
}
} catch (e: JSONException) {
TSMobileAnalyticsBackend.logError("Error parsing TNS Panelist JSON data")
error("Error parsing TNS Panelist JSON data")
}
return cookieList
}
Expand All @@ -148,12 +148,12 @@ internal object PanelistHandler {
}
return buffer.toString()
} catch (e: Exception) {
TSMobileAnalyticsBackend.logError(errorString)
error(errorString)
} finally {
try {
input?.close()
} catch (e: IOException) { // Should never happen
TSMobileAnalyticsBackend.logError("Error Closing InputStream")
error("Error Closing InputStream")
}
}
return ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import androidx.activity.ComponentActivity
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import se.kantarsifo.mobileanalytics.framework.Logger.fatalError
import se.kantarsifo.mobileanalytics.framework.Logger.log
import se.kantarsifo.mobileanalytics.framework.Utils.isPackageInstalled
import java.net.HttpCookie

/**
Expand Down Expand Up @@ -39,7 +42,7 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {

fun createInstance(activity: ComponentActivity, cpID: String?, applicationName: String?, onlyPanelist: Boolean, isWebBased: Boolean): TSMobileAnalyticsBackend? {
if (activity == null) {
logFatalError("Mobile Application Tagging Framework Failed to initiate - context must not be null")
fatalError("Mobile Application Tagging Framework Failed to initiate - context must not be null")
return frameworkInstance
}
val version = BuildConfig.VERSION_NAME
Expand Down Expand Up @@ -112,20 +115,20 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {
private fun paramsAreValid(cpID: String?, applicationName: String?): Boolean {
return when {
(cpID.isNullOrEmpty()) -> {
logFatalError("Mobile Application Tagging Framework Failed to initiate - CPID must not be null or empty")
fatalError("Mobile Application Tagging Framework Failed to initiate - CPID must not be null or empty")
false
}
(cpID.length != TagStringsAndValues.CPID_LENGTH_CODIGO) -> {
logFatalError("Mobile Application Tagging Framework Failed to initiate - CPID must be " +
fatalError("Mobile Application Tagging Framework Failed to initiate - CPID must be " +
"${TagStringsAndValues.CPID_LENGTH_CODIGO} characters")
false
}
(applicationName.isNullOrEmpty()) -> {
logFatalError("Mobile Application Tagging Framework Failed to initiate - Application Name must not be null or empty")
fatalError("Mobile Application Tagging Framework Failed to initiate - Application Name must not be null or empty")
false
}
(applicationName.length > TagStringsAndValues.MAX_LENGTH_APP_NAME) -> {
logFatalError("Mobile Application Tagging Framework Failed to initiate - Application Name must not have more than "
fatalError("Mobile Application Tagging Framework Failed to initiate - Application Name must not have more than "
+ TagStringsAndValues.MAX_LENGTH_APP_NAME + " characters")
false
}
Expand All @@ -149,15 +152,15 @@ internal class TSMobileAnalyticsBackend : TSMobileAnalytics {
private fun initLegacyTags(activity: ComponentActivity, cpID: String, applicationName: String, onlyPanelist: Boolean) {
val panelistKey = PanelistHandler.getPanelistKey(activity)
if (cpID.length != TagStringsAndValues.CPID_LENGTH_CODIGO) {
logFatalError("Mobile Application Tagging Framework Failed to initiate - " +
fatalError("Mobile Application Tagging Framework Failed to initiate - " +
"CPID must either be exactly " + TagStringsAndValues.CPID_LENGTH_CODIGO)
} else if (onlyPanelist && panelistKey == TagStringsAndValues.NO_PANELIST_ID) {
logFatalError("Mobile Application Tagging Framework Failed to initiate - " +
fatalError("Mobile Application Tagging Framework Failed to initiate - " +
"Panelist Id was not found, it must exist if only panelist tracking is active")
} else {
// TODO print panelist setting
frameworkInstance = TSMobileAnalyticsBackend(activity, activity, cpID, applicationName, panelistKey, onlyPanelist)
logMessage("Mobile Application Tagging Framework initiated with the following values " +
log("Mobile Application Tagging Framework initiated with the following values " +
"\nCPID: $cpID\nApplication name: $applicationName\nOnly panelist tracking : $onlyPanelist")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
package se.kantarsifo.mobileanalytics.framework

import se.kantarsifo.mobileanalytics.framework.Logger.error
import se.kantarsifo.mobileanalytics.framework.Logger.log
import java.io.IOException
import java.net.URL
import java.util.UUID
Expand Down Expand Up @@ -110,7 +112,7 @@ internal constructor(
* Print request information to LogCat.
*/
private fun logRequestInfo() {
TSMobileAnalyticsBackend.logMessage(
log(
"Tag request sent: " +
"\nRequestID: " + requestID +
"\nCat encoded value:" + TagHandler.urlEncode(cat) +
Expand All @@ -123,7 +125,7 @@ internal constructor(
* Handle a successful request.
*/
private fun dataRequestComplete() {
TSMobileAnalyticsBackend.logMessage("Tag request completed with success: \nRequestID: $requestID")
log("Tag request completed with success: \nRequestID: $requestID")
notifySuccess()
}

Expand All @@ -132,7 +134,7 @@ internal constructor(
* @param e The exception if one was thrown.
*/
private fun dataRequestFail(e: Exception) {
TSMobileAnalyticsBackend.logError("Tag request failed with exception:\n$e\nRequestID: $requestID")
error("Tag request failed with exception:\n$e\nRequestID: $requestID")
notifyFailure()
}

Expand All @@ -142,7 +144,7 @@ internal constructor(
* @param message HTTP response message.
*/
private fun dataRequestFailWithResponseCode(statusCode: Int, message: String) {
TSMobileAnalyticsBackend.logError("Tag request failed with http status code:$statusCode\nmessage:$message\nRequestID: $requestID")
error("Tag request failed with http status code:$statusCode\nmessage:$message\nRequestID: $requestID")
notifyFailure()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ package se.kantarsifo.mobileanalytics.framework

import android.content.Context
import androidx.activity.ComponentActivity
import se.kantarsifo.mobileanalytics.framework.Logger.error
import se.kantarsifo.mobileanalytics.framework.Logger.fatalError
import se.kantarsifo.mobileanalytics.framework.Logger.log
import se.kantarsifo.mobileanalytics.framework.TagDataRequestHandler.State.*
import java.net.HttpCookie
import java.util.ArrayList
import java.util.concurrent.ExecutorService
Expand Down Expand Up @@ -146,7 +150,7 @@ internal class TagDataRequestHandler : TagDataRequestCallbackListener {
* This method is called when a data request has been completed successfully.
*/
override fun onDataRequestComplete(request: TagDataRequest) {
synchronized(this) { dataRequestQueue.remove(request) }
log( "RequestCompleted: " + request.cat)
nbrOfSuccessfulRequests++
}

Expand All @@ -155,7 +159,7 @@ internal class TagDataRequestHandler : TagDataRequestCallbackListener {
* This method is called when a data request has been failed.
*/
override fun onDataRequestFailed(request: TagDataRequest) {
synchronized(this) { dataRequestQueue.remove(request) }
error( "RequestFailed: " + request.cat)
nbrOfFailedRequests++
}

Expand All @@ -167,29 +171,25 @@ internal class TagDataRequestHandler : TagDataRequestCallbackListener {
private fun checkRequestParams(category: String?, contentID: String?): Int {
return when {
(category == null) -> {
logFatalError("category may not be null")
fatalError("category may not be null")
TagStringsAndValues.ERROR_CATEGORY_NULL
}
(category.length > TagStringsAndValues.MAX_LENGTH_CATEGORY) -> {
logFatalError("category may not have more than ${TagStringsAndValues.MAX_LENGTH_CATEGORY} characters")
fatalError("category may not have more than ${TagStringsAndValues.MAX_LENGTH_CATEGORY} characters")
TagStringsAndValues.ERROR_CATEGORY_TOO_LONG
}
(contentID == null) -> {
logFatalError("contentID may not be null")
fatalError("contentID may not be null")
TagStringsAndValues.ERROR_CONTENT_ID_NULL
}
(contentID.length > TagStringsAndValues.MAX_LENGTH_CONTENT_ID) -> {
logFatalError("contentID may not have more than ${TagStringsAndValues.MAX_LENGTH_CONTENT_ID} characters")
fatalError("contentID may not have more than ${TagStringsAndValues.MAX_LENGTH_CONTENT_ID} characters")
TagStringsAndValues.ERROR_CONTENT_ID_TOO_LONG
}
else -> TagStringsAndValues.RESULT_SUCCESS
}
}

private fun logFatalError(message: String) {
TSMobileAnalyticsBackend.logFatalError("Failed to send tag - $message")
}

/**
* Append a list of category names to a string to use in the request.
* Example {News, Sports, Football} will generate News/Sports/Football.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import android.os.Build.VERSION_CODES
import android.provider.Settings.Secure
import androidx.activity.ComponentActivity
import org.json.JSONObject
import se.kantarsifo.mobileanalytics.framework.Logger.error
import se.kantarsifo.mobileanalytics.framework.Logger.fatalError
import se.kantarsifo.mobileanalytics.framework.Logger.log
import java.io.UnsupportedEncodingException
import java.net.CookieStore
import java.net.HttpCookie
Expand Down Expand Up @@ -126,7 +129,7 @@ internal class TagHandler(
try {
applicationVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionName
} catch (e: Exception) {
TSMobileAnalyticsBackend.logFatalError("Failed to retrieve application version, will not set be set in request header")
error("Failed to retrieve application version, will not set be set in request header")
}
}

Expand Down Expand Up @@ -158,7 +161,7 @@ internal class TagHandler(
}

private fun logFatalError(message: String) {
TSMobileAnalyticsBackend.logFatalError("Failed to create URL - $message")
fatalError("Failed to create URL - $message")
}

companion object {
Expand All @@ -173,7 +176,7 @@ internal class TagHandler(
URLEncoder.encode(input, TagStringsAndValues.URL_ENCODING)
} catch (e: UnsupportedEncodingException) {
// Since encoding UTF-8 is supported by android, this should not happen
TSMobileAnalyticsBackend.logMessage("URL-Encoding not supported")
log("URL-Encoding not supported")
input
}
}
Expand Down Expand Up @@ -201,9 +204,7 @@ internal class TagHandler(
}
cookies = CookieHandler.setupPanelistCookies(cookieList)
} catch (e: PackageManager.NameNotFoundException) {
TSMobileAnalyticsBackend.logError(
"Failed to setup panel list cookies - Retry counter = $retryCounter"
)
error("Failed to setup panel list cookies - Retry counter = $retryCounter")
}
}
}
Expand Down

0 comments on commit 2be50eb

Please sign in to comment.