Skip to content

Commit

Permalink
Fixed exception in 'DataLogger' class.
Browse files Browse the repository at this point in the history
Signed-off-by: Umair Adil <[email protected]>
  • Loading branch information
Umair Adil committed Nov 27, 2018
1 parent 6537b06 commit d2b0a13
Show file tree
Hide file tree
Showing 23 changed files with 106 additions and 131 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ allprojects {

```groovy
dependencies {
implementation 'com.github.umair13adil:RxLogs:v0.15'
implementation 'com.github.umair13adil:RxLogs:v0.16'
}
```

Expand Down
15 changes: 3 additions & 12 deletions app/src/main/java/com/blackbox/library/plog/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.blackbox.library.plog
import android.Manifest
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.CountDownTimer
import android.support.v4.app.ActivityCompat
import android.support.v4.content.ContextCompat
import android.support.v7.app.AppCompatActivity
Expand Down Expand Up @@ -40,6 +39,7 @@ class MainActivity : AppCompatActivity() {
//This will get 'DataLogger' object for predefined type in ConfigFile.
val locationsLog = PLog.getLoggerFor(LogType.Location.type)
val notificationsLog = PLog.getLoggerFor(LogType.Notification.type)
val deliveriesLog = PLog.getLoggerFor("Deliveries")

//Will log to PLogs
log_plog_event.setOnClickListener {
Expand All @@ -61,11 +61,13 @@ class MainActivity : AppCompatActivity() {

locationsLog?.appendToFile(dataToLog)
notificationsLog?.appendToFile(dataToLog)
deliveriesLog?.appendToFile("Deliveries: $dataToLog")
} else {
dataToLog = editText.text.toString() + "\n"

locationsLog?.appendToFile(dataToLog)
notificationsLog?.appendToFile(dataToLog)
deliveriesLog?.appendToFile("Deliveries: $dataToLog")
}
}

Expand Down Expand Up @@ -157,17 +159,6 @@ class MainActivity : AppCompatActivity() {
switch1.setOnCheckedChangeListener { compoundButton, b ->
encryptLogs = b
}

object : CountDownTimer(2000, 1000) {
override fun onFinish() {
PLog.logThis(TAG, "Error", info = "CountDownTimer", throwable = Throwable("This is an Error"))
}

override fun onTick(p0: Long) {

}

}.start()
}

override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
Expand Down
15 changes: 7 additions & 8 deletions plog/src/main/java/com/blackbox/plog/dataLogs/DataLogger.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import java.io.File
class DataLogger(var logFileName: String = "log") {

val TAG = "DataLogger"
val autoExportTypes = PLogImpl.getLogsConfig(PLog)?.autoExportLogTypes!!
val autoExportTypes = PLogImpl.logsConfig?.autoExportLogTypes!!

/**
* Overwrite to file.
Expand Down Expand Up @@ -78,18 +78,17 @@ class DataLogger(var logFileName: String = "log") {
val logFilePath = setupFilePaths(logFileName, isPLog = false)
dataLoggerCalledBeforePLoggerException()

val shouldLog: Pair<Boolean, String>
val f = checkFileExists(logFilePath, isPLog = false)

if (!PART_FILE_CREATED_DATALOG) {
shouldLog = LogWriter.shouldWriteLog(f, isPLog = false, logFileName = logFileName)
val shouldLog = if (!PART_FILE_CREATED_DATALOG) {
LogWriter.shouldWriteLog(f, isPLog = false, logFileName = logFileName)
} else {
shouldLog = LogWriter.shouldWriteLog(File(CURRENT_PART_FILE_PATH_DATALOG), isPLog = false, logFileName = logFileName)
LogWriter.shouldWriteLog(File(CURRENT_PART_FILE_PATH_DATALOG), isPLog = false, logFileName = logFileName)
}

if (PLogImpl.getLogsConfig(PLog)?.encryptionEnabled!!) {
if (PLogImpl.logsConfig?.encryptionEnabled!!) {

val secretKey = PLogImpl.getLogsConfig(PLog)?.secretKey!!
val secretKey = PLogImpl.logsConfig?.secretKey!!

if (shouldLog.first) {
if (overwrite)
Expand All @@ -108,7 +107,7 @@ class DataLogger(var logFileName: String = "log") {
}
}

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, it)

//Check if auto Export is enabled, and then export it
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ object DataLogsExporter {

private val TAG = DataLogsExporter::class.java.simpleName

private var exportFileName = PLogImpl.getLogsConfig(PLog)?.zipFileName!!
private var exportFileName = PLogImpl.logsConfig?.zipFileName!!
private var exportPath = ""

/*
Expand Down Expand Up @@ -59,13 +59,13 @@ object DataLogsExporter {
emitter.onError(Throwable("No Files to zip!"))
}

if (PLogImpl.getLogsConfig(PLog)?.encryptionEnabled!! && exportDecrypted) {
if (PLogImpl.logsConfig?.encryptionEnabled!! && exportDecrypted) {
decryptSaveFiles(filesToSend, exportPath, this.exportFileName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = {
if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Output Zip: ${exportFileName}")

emitter.onNext(it)
Expand All @@ -84,7 +84,7 @@ object DataLogsExporter {
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = {
if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Output Zip: $exportPath${exportFileName}")

emitter.onNext(exportPath + exportFileName)
Expand Down Expand Up @@ -129,7 +129,7 @@ object DataLogsExporter {
emitter.onNext("Start...................................................\n")
emitter.onNext("File: ${f.name} Start..\n")

if (PLogImpl.getLogsConfig(PLog)?.encryptionEnabled!! && printDecrypted) {
if (PLogImpl.logsConfig?.encryptionEnabled!! && printDecrypted) {
emitter.onNext(readFileDecrypted(f.absolutePath))
} else {
f.forEachLine {
Expand Down Expand Up @@ -186,14 +186,14 @@ object DataLogsExporter {
var timeStamp = ""
var noOfFiles = ""

if (PLogImpl.getLogsConfig(PLog)?.attachTimeStamp!!)
if (PLogImpl.logsConfig?.attachTimeStamp!!)
timeStamp = PLog.getFormattedTimeStamp() + "_" + ExportType.TODAY.type

if (PLogImpl.getLogsConfig(PLog)?.attachNoOfFiles!!)
if (PLogImpl.logsConfig?.attachNoOfFiles!!)
noOfFiles = "_[${files.size}]"

val preName = PLogImpl.getLogsConfig(PLog)?.exportFileNamePreFix!!
val postName = PLogImpl.getLogsConfig(PLog)?.exportFileNamePostFix!!
val preName = PLogImpl.logsConfig?.exportFileNamePreFix!!
val postName = PLogImpl.logsConfig?.exportFileNamePostFix!!

return "$preName$exportFileName$timeStamp$noOfFiles$postName.zip"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ object DataLogsFilter {
val enabledTypes: List<String>

init {
enabledTypes = PLogImpl.getLogsConfig(PLog)?.logTypesEnabled!!
enabledTypes = PLogImpl.logsConfig?.logTypesEnabled!!
}

fun getFilesForLogName(logsPath: String, logFileName: String): List<File> {

val listOfFiles = arrayListOf<File>()
val files = FilterUtils.listFiles(logsPath, arrayListOf())

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Found files: ${files.size}")

if (files.isNotEmpty()) {
Expand Down Expand Up @@ -51,7 +51,7 @@ object DataLogsFilter {
val listOfFiles = arrayListOf<File>()
val files = FilterUtils.listFiles(logsPath, arrayListOf())

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Found files: ${files.size}")

if (files.isNotEmpty()) {
Expand Down
8 changes: 4 additions & 4 deletions plog/src/main/java/com/blackbox/plog/pLogs/PLog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ object PLog : PLogImpl() {
*/
fun exportDataLogsForName(name: String, exportDecrypted: Boolean = false): Observable<String> {

getLogsConfig(this)?.let {
logsConfig?.let {

val path = getLogsSavedPaths(it.nameForEventDirectory)
return DataLogsExporter.getDataLogs(name, path, outputPath, exportDecrypted)
Expand All @@ -116,7 +116,7 @@ object PLog : PLogImpl() {
*/
fun exportAllDataLogs(exportDecrypted: Boolean = false): Observable<String> {

getLogsConfig(this)?.let {
logsConfig?.let {

val path = getLogsSavedPaths(it.nameForEventDirectory, isForAll = true)
return DataLogsExporter.getDataLogs("", path, outputPath, exportDecrypted)
Expand All @@ -134,7 +134,7 @@ object PLog : PLogImpl() {
*/
fun printDataLogsForName(name: String, printDecrypted: Boolean = false): Observable<String> {

getLogsConfig(this)?.let {
logsConfig?.let {

val path = getLogsSavedPaths(it.nameForEventDirectory)
return DataLogsExporter.printLogsForName(name, path, printDecrypted)
Expand All @@ -161,7 +161,7 @@ object PLog : PLogImpl() {
if (PLog.logTypes.containsKey(type))
return PLog.logTypes.get(type)

if (getLogsConfig(PLog)?.isDebuggable!!)
if (logsConfig?.isDebuggable!!)
Log.e(TAG, "No log type defined for provided type '$type'")

return null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ fun isLogLevelEnabled(logLevel: LogLevel): Boolean {

return if (!PLog.isLogsConfigSet())
true
else if (PLog.isLogsConfigSet() && PLogImpl.getLogsConfig(PLog)?.logLevelsEnabled?.isEmpty()!!)
else if (PLog.isLogsConfigSet() && PLogImpl.logsConfig?.logLevelsEnabled?.isEmpty()!!)
true
else PLogImpl.getLogsConfig(PLog)?.logLevelsEnabled?.contains(logLevel)!!
else PLogImpl.logsConfig?.logLevelsEnabled?.contains(logLevel)!!

}
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ internal fun updateValue(value: String, tag: String) {

// Change the content of node
val nodes = doc.getElementsByTagName(tag).item(0)
nodes.setTextContent(value)
nodes.textContent = value

val transformer = TransformerFactory.newInstance().newTransformer()
transformer.setOutputProperty(OutputKeys.INDENT, "yes")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,14 @@ private fun getLogsForAllInRoot(): Triple<String, List<File>, String> {

private fun composeZipName(files: Pair<List<File>, String>, exportType: ExportType): String {

if (PLogImpl.getLogsConfig(PLog)?.attachTimeStamp!!)
if (PLogImpl.logsConfig?.attachTimeStamp!!)
timeStamp = PLog.getFormattedTimeStamp() + "_" + exportType.type

if (PLogImpl.getLogsConfig(PLog)?.attachNoOfFiles!!)
if (PLogImpl.logsConfig?.attachNoOfFiles!!)
noOfFiles = "_[${files.first.size}]"

val preName = PLogImpl.getLogsConfig(PLog)?.exportFileNamePreFix!!
val postName = PLogImpl.getLogsConfig(PLog)?.exportFileNamePostFix!!
val preName = PLogImpl.logsConfig?.exportFileNamePreFix!!
val postName = PLogImpl.logsConfig?.exportFileNamePostFix!!

return "$preName$timeStamp$noOfFiles$postName.zip"
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ object LogExporter {

private lateinit var files: Triple<String, List<File>, String>
private val exportPath = PLog.outputPath
private var zipName = PLogImpl.getLogsConfig(PLog)?.zipFileName
private var zipName = PLogImpl.logsConfig?.zipFileName

/*
* Will filter & export log files to zip package.
Expand Down Expand Up @@ -76,7 +76,7 @@ object LogExporter {
emitter.onNext("Start<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n")
emitter.onNext("File: ${f.name} Start..\n")

if (PLogImpl.getLogsConfig(PLog)?.encryptionEnabled!! && printDecrypted) {
if (PLogImpl.logsConfig?.encryptionEnabled!! && printDecrypted) {
emitter.onNext(readFileDecrypted(f.absolutePath))
} else {
f.forEachLine {
Expand All @@ -101,7 +101,7 @@ object LogExporter {
//First entry is Zip Name
this.zipName = files.first

if (PLogImpl.getLogsConfig(PLog)?.zipFilesOnly!!) {
if (PLogImpl.logsConfig?.zipFilesOnly!!) {

val filesToSend = files.second //List of filtered files

Expand All @@ -110,15 +110,15 @@ object LogExporter {
emitter.onError(Throwable("No Files to zip!"))
}

if (PLogImpl.getLogsConfig(PLog)?.encryptionEnabled!! && exportDecrypted) {
if (PLogImpl.logsConfig?.encryptionEnabled!! && exportDecrypted) {
decryptFirstThenZip(emitter, filesToSend = filesToSend)
} else {
zipFilesOnly(emitter, filesToSend)
}

} else {

if (PLogImpl.getLogsConfig(PLog)?.encryptionEnabled!! && exportDecrypted) {
if (PLogImpl.logsConfig?.encryptionEnabled!! && exportDecrypted) {
decryptFirstThenZip(emitter, exportedPath = "")
} else {
zipFilesAndFolder(emitter, this.files.third)
Expand All @@ -132,7 +132,7 @@ object LogExporter {
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = {
if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Output Zip: $zipName")

emitter.onNext(it)
Expand All @@ -153,7 +153,7 @@ object LogExporter {
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = {
if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Output Zip: $zipName")

emitter.onNext(exportPath + zipName)
Expand All @@ -174,7 +174,7 @@ object LogExporter {
.observeOn(AndroidSchedulers.mainThread())
.subscribeBy(
onNext = {
if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(PLog.TAG, "Output Zip: $zipName")

emitter.onNext(exportPath + zipName)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ internal object FileFilter {
if (file.isDirectory) {
val day = FilterUtils.extractDay(file.name)

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(FileFilter.TAG, "Files between dates: $lastDay & $today,Date File Present: $day")

if (lastDay < today) {
Expand Down Expand Up @@ -77,7 +77,7 @@ internal object FileFilter {
val lisOfFiles = arrayListOf<File>()
val listOfDates = DateTimeUtils.getDatesBetween()

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(FileFilter.TAG, "Files between dates: ${listOfDates.first()} & ${listOfDates.last()}")

for (date in listOfDates) {
Expand Down Expand Up @@ -112,7 +112,7 @@ internal object FileFilter {
for (i in files.indices) {
val fileHour = FilterUtils.extractHour(files[i].name)

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(FileFilter.TAG, "Last Hour: " + lastHour + " Check File Hour: " + fileHour + " " + files[i].name)

if (fileHour == lastHour) {
Expand All @@ -135,7 +135,7 @@ internal object FileFilter {
//Copy Files to temp folder
File(folderPath).copyRecursively(File(tempOutputPath), true)

return if (PLogImpl.getLogsConfig(PLog)?.zipFilesOnly!!) {
return if (PLogImpl.logsConfig?.zipFilesOnly!!) {
Pair(lisOfFiles, tempOutputPath)
} else {
Pair(lisOfFiles, tempOutputPath)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ object FilterUtils {

fun prepareOutputFile(outputPath: String) {

if (PLogImpl.getLogsConfig(PLog)?.autoClearLogsOnExport!!)
if (PLogImpl.logsConfig?.autoClearLogsOnExport!!)
File(outputPath).deleteRecursively() //Delete all previous Exports

//Create export directory if it doesn't already exists
Expand All @@ -36,7 +36,7 @@ object FilterUtils {
for (i in files.indices) {
val fileHour = extractHour(files[i].name)

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(FileFilter.TAG, "Last Hour: " + lastHour + " Check File Hour: " + fileHour + " " + files[i].name)

if (fileHour == lastHour) {
Expand Down Expand Up @@ -94,7 +94,7 @@ object FilterUtils {
while (entries.hasMoreElements()) {
val entry = entries.nextElement()

if (PLogImpl.getLogsConfig(PLog)?.isDebuggable!!)
if (PLogImpl.logsConfig?.isDebuggable!!)
Log.i(FileFilter.TAG, entry.name)
}
} catch (e: ZipException) { }
Expand Down
Loading

0 comments on commit d2b0a13

Please sign in to comment.