Reduce logs in release build

This commit is contained in:
Ming Ming 2022-05-15 16:45:18 +08:00
parent f2b17ccc6b
commit c784cf63ff
17 changed files with 132 additions and 56 deletions

View file

@ -9,6 +9,7 @@
tools:ignore="ScopedStorage" />
<application
android:name=".App"
android:label="@string/app_name"
android:icon="@mipmap/ic_launcher"
android:networkSecurityConfig="@xml/network_security_config"

View file

@ -0,0 +1,14 @@
package com.nkming.nc_photos
import com.nkming.nc_photos.plugin.LogConfig
import io.flutter.BuildConfig
import io.flutter.app.FlutterApplication
class App : FlutterApplication() {
override fun onCreate() {
super.onCreate()
LogConfig.isShowInfo = BuildConfig.DEBUG
LogConfig.isShowDebug = BuildConfig.DEBUG
LogConfig.isShowVerbose = BuildConfig.DEBUG
}
}

View file

@ -1,6 +1,7 @@
package com.nkming.nc_photos
import android.content.Context
import com.nkming.nc_photos.plugin.logI
import io.flutter.Log
import java.util.*
import javax.net.ssl.HostnameVerifier
@ -15,7 +16,7 @@ class CustomHostnameVerifier(context: Context) : HostnameVerifier {
override fun verify(hostname: String, session: SSLSession): Boolean {
return if (allowedHosts.contains(hostname.toLowerCase())) {
// good
Log.i("CustomHostnameVerifier::verify",
logI("CustomHostnameVerifier::verify",
"Allowing registered host: $hostname")
true
} else {

View file

@ -7,6 +7,8 @@ import android.util.Log
import androidx.annotation.NonNull
import com.nkming.nc_photos.plugin.NcPhotosPlugin
import com.nkming.nc_photos.plugin.UriUtil
import com.nkming.nc_photos.plugin.logE
import com.nkming.nc_photos.plugin.logI
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugin.common.EventChannel
@ -25,7 +27,7 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler {
super.onCreate(savedInstanceState)
if (intent.action == NcPhotosPlugin.ACTION_SHOW_IMAGE_PROCESSOR_RESULT) {
val route = getRouteFromImageProcessorResult(intent) ?: return
Log.i(TAG, "Initial route: $route")
logI(TAG, "Initial route: $route")
_initialRoute = route
}
}
@ -59,7 +61,7 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler {
override fun onNewIntent(intent: Intent) {
if (intent.action == NcPhotosPlugin.ACTION_SHOW_IMAGE_PROCESSOR_RESULT) {
val route = getRouteFromImageProcessorResult(intent) ?: return
Log.i(TAG, "Navigate to route: $route")
logI(TAG, "Navigate to route: $route")
flutterEngine?.navigationChannel?.pushRoute(route)
} else {
super.onNewIntent(intent)
@ -83,7 +85,7 @@ class MainActivity : FlutterActivity(), MethodChannel.MethodCallHandler {
NcPhotosPlugin.EXTRA_IMAGE_RESULT_URI
)
if (resultUri == null) {
Log.e(TAG, "Image result uri == null")
logE(TAG, "Image result uri == null")
return null
}
val filename = UriUtil.resolveFilename(this, resultUri)?.let {

View file

@ -1,6 +1,7 @@
package com.nkming.nc_photos
import android.app.Activity
import com.nkming.nc_photos.plugin.logE
import io.flutter.Log
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
@ -49,7 +50,7 @@ class SelfSignedCertChannelHandler(activity: Activity) :
HttpsURLConnection.setDefaultSSLSocketFactory(_sslSocketFactory)
HttpsURLConnection.setDefaultHostnameVerifier(_hostNameVerifier)
} catch (e: Exception) {
Log.e("SelfSignedCertChannelHandler::init",
logE("SelfSignedCertChannelHandler::init",
"Failed while setting custom SSL handler, self-signed cert will not work",
e)
}

View file

@ -2,6 +2,8 @@ package com.nkming.nc_photos
import android.content.Context
import android.util.Pair
import com.nkming.nc_photos.plugin.logE
import com.nkming.nc_photos.plugin.logI
import io.flutter.Log
import org.json.JSONObject
import java.io.File
@ -47,11 +49,11 @@ class SelfSignedCertManager {
val jsonFile = File(certDir, f.name + ".json")
val jsonStr = jsonFile.bufferedReader().use { it.readText() }
val info = CertInfo.fromJson(JSONObject(jsonStr))
Log.i("SelfSignedCertManager::readAllCerts",
logI("SelfSignedCertManager::readAllCerts",
"Found certificate: ${f.name} for host: ${info.host}")
products.add(Pair(info, c))
} catch (e: Exception) {
Log.e("SelfSignedCertManager::readAllCerts",
logE("SelfSignedCertManager::readAllCerts",
"Failed to read certificate file: ${f.name}", e)
}
}
@ -87,7 +89,7 @@ class SelfSignedCertManager {
certDir.mkdir()
certDir
} else if (!certDir.isDirectory) {
Log.e("SelfSignedCertManager::openCertsDir",
logE("SelfSignedCertManager::openCertsDir",
"Removing certs file to make way for the directory")
certDir.delete()
certDir.mkdir()

View file

@ -4,7 +4,6 @@ import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.net.Uri
import android.util.Log
fun Bitmap.aspectRatio() = width / height.toFloat()
@ -24,7 +23,7 @@ interface BitmapUtil {
BitmapResizeMethod.FILL
)
if (subsample > 1) {
Log.d(
logD(
TAG,
"Subsample image to fixed: $subsample ${opt.outWidth}x${opt.outHeight} -> ${targetW}x$targetH"
)
@ -34,9 +33,7 @@ interface BitmapUtil {
}
val bitmap = loadImage(context, uri, outOpt)
if (subsample > 1) {
Log.d(
TAG, "Bitmap subsampled: ${bitmap.width}x${bitmap.height}"
)
logD(TAG, "Bitmap subsampled: ${bitmap.width}x${bitmap.height}")
}
return Bitmap.createScaledBitmap(bitmap, targetW, targetH, true)
}
@ -83,7 +80,7 @@ interface BitmapUtil {
opt.outWidth, opt.outHeight, dstW, dstH, resizeMethod
)
if (subsample > 1) {
Log.d(
logD(
TAG,
"Subsample image to ${resizeMethod.name}: $subsample ${opt.outWidth}x${opt.outHeight} -> ${dstW}x$dstH" +
(if (shouldSwapSide) " (swapped)" else "")
@ -94,9 +91,7 @@ interface BitmapUtil {
}
val bitmap = loadImage(context, uri, outOpt)
if (subsample > 1) {
Log.d(
TAG, "Bitmap subsampled: ${bitmap.width}x${bitmap.height}"
)
logD(TAG, "Bitmap subsampled: ${bitmap.width}x${bitmap.height}")
}
if (bitmap.width < dstW && bitmap.height < dstH && !shouldUpscale) {
return bitmap

View file

@ -13,7 +13,6 @@ import android.os.AsyncTask
import android.os.Bundle
import android.os.IBinder
import android.os.PowerManager
import android.util.Log
import androidx.core.app.NotificationChannelCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
@ -52,7 +51,7 @@ class ImageProcessorService : Service() {
@SuppressLint("WakelockTimeout")
override fun onCreate() {
Log.i(TAG, "[onCreate] Service created")
logI(TAG, "[onCreate] Service created")
super.onCreate()
wakeLock.acquire()
createNotificationChannel()
@ -60,7 +59,7 @@ class ImageProcessorService : Service() {
}
override fun onDestroy() {
Log.i(TAG, "[onDestroy] Service destroyed")
logI(TAG, "[onDestroy] Service destroyed")
wakeLock.release()
super.onDestroy()
}
@ -74,7 +73,7 @@ class ImageProcessorService : Service() {
}
private fun onCancel(startId: Int) {
Log.i(TAG, "[onCancel] Cancel requested")
logI(TAG, "[onCancel] Cancel requested")
cmdTask?.cancel(false)
stopSelf(startId)
}
@ -88,7 +87,7 @@ class ImageProcessorService : Service() {
isForeground = true
} catch (e: Throwable) {
// ???
Log.e(TAG, "[onStartCommand] Failed while startForeground", e)
logE(TAG, "[onStartCommand] Failed while startForeground", e)
}
}
@ -99,7 +98,7 @@ class ImageProcessorService : Service() {
startId, intent.extras!!
)
else -> {
Log.e(TAG, "Unknown method: $method")
logE(TAG, "Unknown method: $method")
// we can't call stopSelf here as it'll stop the service even if
// there are commands running in the bg
addCommand(
@ -253,7 +252,7 @@ class ImageProcessorService : Service() {
try {
getTempDir(this).deleteRecursively()
} catch (e: Throwable) {
Log.e(TAG, "[cleanUp] Failed while cleanUp", e)
logE(TAG, "[cleanUp] Failed while cleanUp", e)
}
}
@ -409,7 +408,7 @@ private open class ImageProcessorCommandTask(context: Context) :
System.gc()
ImageProcessorCompletedEvent(outUri)
} catch (e: Throwable) {
Log.e(TAG, "[doInBackground] Failed while handleCommand", e)
logE(TAG, "[doInBackground] Failed while handleCommand", e)
ImageProcessorFailedEvent(e)
}
}
@ -445,7 +444,7 @@ private open class ImageProcessorCommandTask(context: Context) :
private fun downloadFile(
fileUrl: String, headers: Map<String, String>?
): File {
Log.i(TAG, "[downloadFile] $fileUrl")
logI(TAG, "[downloadFile] $fileUrl")
return (URL(fileUrl).openConnection() as HttpURLConnection).apply {
requestMethod = "GET"
instanceFollowRedirects = true
@ -464,7 +463,7 @@ private open class ImageProcessorCommandTask(context: Context) :
}
file
} else {
Log.e(
logE(
TAG,
"[downloadFile] Failed downloading file: HTTP$responseCode"
)
@ -478,7 +477,7 @@ private open class ImageProcessorCommandTask(context: Context) :
private fun saveBitmap(
bitmap: Bitmap, filename: String, srcFile: File
): Uri {
Log.i(TAG, "[saveBitmap] $filename")
logI(TAG, "[saveBitmap] $filename")
val outFile = File.createTempFile("out", null, getTempDir(context))
outFile.outputStream().use {
bitmap.compress(Bitmap.CompressFormat.JPEG, 85, it)
@ -491,7 +490,7 @@ private open class ImageProcessorCommandTask(context: Context) :
copyExif(iExif, oExif)
oExif.saveAttributes()
} catch (e: Throwable) {
Log.e(TAG, "[copyExif] Failed while saving EXIF", e)
logE(TAG, "[copyExif] Failed while saving EXIF", e)
}
// move file to user accessible storage
@ -509,14 +508,14 @@ private open class ImageProcessorCommandTask(context: Context) :
try {
from.getAttribute(t)?.let { to.setAttribute(t, it) }
} catch (e: Throwable) {
Log.e(TAG, "[copyExif] Failed while copying tag: $t", e)
logE(TAG, "[copyExif] Failed while copying tag: $t", e)
}
}
}
private fun handleCancel() {
if (isCancelled) {
Log.i(TAG, "[handleCancel] Canceled")
logI(TAG, "[handleCancel] Canceled")
throw InterruptedException()
}
}

View file

@ -1,6 +1,5 @@
package com.nkming.nc_photos.plugin
import android.util.Log
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
@ -35,7 +34,7 @@ class LockChannelHandler : MethodChannel.MethodCallHandler {
fun dismiss() {
for (id in _lockedIds) {
if (locks[id] == true) {
Log.w(TAG, "[dismiss] Automatically unlocking id: $id")
logW(TAG, "[dismiss] Automatically unlocking id: $id")
locks[id] = false
}
}

View file

@ -0,0 +1,69 @@
package com.nkming.nc_photos.plugin
class LogConfig {
companion object {
var isShowInfo = true
var isShowDebug = false
var isShowVerbose = false
}
}
fun logWtf(tag: String, msg: String) = android.util.Log.wtf(tag, msg)
fun logWtf(tag: String, msg: String, tr: Throwable) =
android.util.Log.wtf(tag, msg, tr)
fun logE(tag: String, msg: String) = android.util.Log.e(tag, msg)
fun logE(tag: String, msg: String, tr: Throwable) =
android.util.Log.e(tag, msg, tr)
fun logW(tag: String, msg: String) = android.util.Log.w(tag, msg)
fun logW(tag: String, msg: String, tr: Throwable) =
android.util.Log.w(tag, msg, tr)
fun logI(tag: String, msg: String): Int {
return if (LogConfig.isShowInfo) {
android.util.Log.i(tag, msg)
} else {
-1
}
}
fun logI(tag: String, msg: String, tr: Throwable): Int {
return if (LogConfig.isShowInfo) {
android.util.Log.i(tag, msg, tr)
} else {
-1
}
}
fun logD(tag: String, msg: String): Int {
return if (LogConfig.isShowDebug) {
android.util.Log.d(tag, msg)
} else {
-1
}
}
fun logD(tag: String, msg: String, tr: Throwable): Int {
return if (LogConfig.isShowDebug) {
android.util.Log.d(tag, msg, tr)
} else {
-1
}
}
fun logV(tag: String, msg: String): Int {
return if (LogConfig.isShowVerbose) {
android.util.Log.v(tag, msg)
} else {
-1
}
}
fun logV(tag: String, msg: String, tr: Throwable): Int {
return if (LogConfig.isShowVerbose) {
android.util.Log.v(tag, msg, tr)
} else {
-1
}
}

View file

@ -7,7 +7,6 @@ import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.MediaStore
import android.util.Log
import io.flutter.embedding.engine.plugins.activity.ActivityAware
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
import io.flutter.plugin.common.EventChannel
@ -220,14 +219,14 @@ class MediaStoreChannelHandler(context: Context) :
put("mimeType", mimeType)
if (dateTaken != 0L) put("dateTaken", dateTaken)
})
Log.d(
logD(
TAG,
"[queryEnhancedPhotos] Found $displayName, path=$path, uri=$contentUri"
)
}
products
}
Log.i(TAG, "[queryEnhancedPhotos] Found ${files.size} files")
logI(TAG, "[queryEnhancedPhotos] Found ${files.size} files")
result.success(files)
}
@ -254,7 +253,7 @@ class MediaStoreChannelHandler(context: Context) :
try {
context.contentResolver.delete(uri, null, null)
} catch (e: Throwable) {
Log.e(TAG, "[deleteFiles] Failed while delete", e)
logE(TAG, "[deleteFiles] Failed while delete", e)
failed += uri.toString()
}
}

View file

@ -1,7 +1,6 @@
package com.nkming.nc_photos.plugin
import android.content.Intent
import android.util.Log
import androidx.annotation.NonNull
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
@ -157,9 +156,7 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
else -> false
}
} catch (e: Throwable) {
Log.e(
TAG, "Failed while onActivityResult, requestCode=$requestCode"
)
logE(TAG, "Failed while onActivityResult, requestCode=$requestCode")
false
}
}
@ -178,7 +175,7 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
else -> false
}
} catch (e: Throwable) {
Log.e(
logE(
TAG, "Failed while onActivityResult, requestCode=$requestCode"
)
false

View file

@ -10,7 +10,6 @@ import android.graphics.BitmapFactory
import android.media.RingtoneManager
import android.net.Uri
import android.os.Build
import android.util.Log
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import io.flutter.plugin.common.MethodCall
@ -307,7 +306,7 @@ class NotificationChannelHandler(context: Context) :
)
}
} catch (e: Throwable) {
Log.e(
logE(
"NotificationChannelHandler::loadNotificationImage",
"Failed generating preview image",
e

View file

@ -3,7 +3,6 @@ package com.nkming.nc_photos.plugin
import android.content.Context
import android.net.Uri
import android.provider.MediaStore
import android.util.Log
interface UriUtil {
companion object {
@ -16,7 +15,7 @@ interface UriUtil {
null, null
).use {
if (it == null || !it.moveToFirst()) {
Log.i(TAG, "Uri not found: $uri")
logI(TAG, "Uri not found: $uri")
null
} else {
it.getString(

View file

@ -2,7 +2,6 @@ package com.nkming.nc_photos.plugin
import android.app.PendingIntent
import android.os.Build
import android.util.Log
import java.net.HttpURLConnection
fun getPendingIntentFlagImmutable(): Int {
@ -35,6 +34,6 @@ inline fun <T> measureTime(tag: String, message: String, block: () -> T): T {
} finally {
val end = System.currentTimeMillis()
val elapsed = end - begin
Log.i(tag, "$message: ${elapsed}ms")
logI(tag, "$message: ${elapsed}ms")
}
}

View file

@ -3,10 +3,10 @@ package com.nkming.nc_photos.plugin.image_processor
import android.content.Context
import android.graphics.*
import android.net.Uri
import android.util.Log
import com.google.android.renderscript.Toolkit
import com.nkming.nc_photos.plugin.BitmapResizeMethod
import com.nkming.nc_photos.plugin.BitmapUtil
import com.nkming.nc_photos.plugin.logI
import com.nkming.nc_photos.plugin.transform
import org.tensorflow.lite.Interpreter
import java.io.File
@ -58,12 +58,12 @@ private class DeepLab3(context: Context) {
Interpreter(TfLiteHelper.loadModelFromAsset(context, MODEL))
interpreter.allocateTensors()
Log.i(TAG, "Converting bitmap to input")
logI(TAG, "Converting bitmap to input")
val inputBitmap =
BitmapUtil.loadImageFixed(context, imageUri, WIDTH, HEIGHT)
val input = TfLiteHelper.bitmapToRgbFloatArray(inputBitmap)
val output = FloatBuffer.allocate(WIDTH * HEIGHT * Label.values().size)
Log.i(TAG, "Inferring")
logI(TAG, "Inferring")
interpreter.run(input, output)
return TfLiteHelper.argmax(output, WIDTH, HEIGHT, Label.values().size)
}
@ -110,7 +110,7 @@ class DeepLab3Portrait(context: Context, maxWidth: Int, maxHeight: Int) {
private fun enhance(
imageUri: Uri, segmentMap: ByteBuffer, radius: Int
): Bitmap {
Log.i(TAG, "[enhance] Enhancing image")
logI(TAG, "[enhance] Enhancing image")
// downscale original to prevent OOM
val orig = BitmapUtil.loadImage(
context, imageUri, maxWidth, maxHeight, BitmapResizeMethod.FIT,

View file

@ -3,9 +3,9 @@ package com.nkming.nc_photos.plugin.image_processor
import android.content.Context
import android.graphics.Bitmap
import android.net.Uri
import android.util.Log
import com.nkming.nc_photos.plugin.BitmapResizeMethod
import com.nkming.nc_photos.plugin.BitmapUtil
import com.nkming.nc_photos.plugin.logI
import org.tensorflow.lite.Interpreter
import java.nio.FloatBuffer
import java.nio.IntBuffer
@ -30,7 +30,7 @@ class ZeroDce(context: Context, maxWidth: Int, maxHeight: Int) {
Interpreter(TfLiteHelper.loadModelFromAsset(context, MODEL))
interpreter.allocateTensors()
Log.i(TAG, "Converting bitmap to input")
logI(TAG, "Converting bitmap to input")
val inputBitmap =
BitmapUtil.loadImageFixed(context, imageUri, WIDTH, HEIGHT)
val inputs = arrayOf(TfLiteHelper.bitmapToRgbFloatArray(inputBitmap))
@ -38,7 +38,7 @@ class ZeroDce(context: Context, maxWidth: Int, maxHeight: Int) {
0 to FloatBuffer.allocate(inputs[0].capacity()),
1 to FloatBuffer.allocate(inputs[0].capacity())
)
Log.i(TAG, "Inferring")
logI(TAG, "Inferring")
interpreter.runForMultipleInputsOutputs(inputs, outputs)
return TfLiteHelper.rgbFloatArrayToBitmap(
@ -49,7 +49,7 @@ class ZeroDce(context: Context, maxWidth: Int, maxHeight: Int) {
private fun enhance(
imageUri: Uri, alphaMaps: Bitmap, iteration: Int
): Bitmap {
Log.i(TAG, "Enhancing image, iteration: $iteration")
logI(TAG, "Enhancing image, iteration: $iteration")
// we can't work with FloatBuffer directly here as a FloatBuffer is way
// too large to fit in Android's heap limit
// downscale original to prevent OOM