Skip to content

Commit

Permalink
Add LogUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
gurleensethi committed Sep 17, 2017
1 parent fbaeaaa commit c74fddd
Show file tree
Hide file tree
Showing 2 changed files with 201 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,36 @@ class MainActivity : AppCompatActivity() {
.maximumLength(20)
.minimumLength(5)
.noNumbers()
.addSuccessCallback {
//Proceed
}
.addErrorCallback { errorType ->
when (errorType) {
ValidationError.AT_LEAST_ONE_LOWER_CASE -> {
editText.error = "Please provide at-least one lower case letter"
}
ValidationError.AT_LEAST_ONE_UPPER_CASE -> {
editText.error = "Please provide at-least one upper case letter"
}
else -> {
editText.error = "Not Enough"
}
}
}
.validate()
}

LogUtils.addLevel(LogLevel.ALL)
debug("This is a debug message")
error("Some error occurred")
warn("This is a warning")
info("Some information")
verbose("VERBOSE!")
wtf("Ignore this")
json("{message:'This is a message', version: {num: 10}}")
shout("Shout this message loud!\nThank YOU")
exception(Exception("ERROR"))

/*val validator = Validator(passwordEditText.text.toString())
validator.atLeastOneNumber()
.atLeastOneUpperCase()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,174 @@
package com.thetechnocafe.gurleensethi.liteutils

import android.util.Log
import org.json.JSONObject

/**
* Created by gurleensethi on 17/09/17.
*/

/**
* Created by gurleensethi on 17/09/17.
* Motivation : Simplify android logging using Kotlin
*/

class LogUtils {
/*
* Maintain which log level the user has allowed to logged by
* adding all the allowed log levels to a set.
* */
companion object {
private val logLevels = mutableSetOf<LogLevel>()

fun addLevel(logLevel: LogLevel) {
if (logLevel == LogLevel.ALL) {
logLevels.add(LogLevel.DEBUG)
logLevels.add(LogLevel.INFO)
logLevels.add(LogLevel.WARN)
logLevels.add(LogLevel.VERBOSE)
logLevels.add(LogLevel.ERROR)
logLevels.add(LogLevel.WTF)
} else {
logLevels.add(logLevel)
}
}

fun addLevel(vararg logLevel: LogLevel) {
if (logLevel.contains(LogLevel.ALL)) {
logLevels.add(LogLevel.DEBUG)
logLevels.add(LogLevel.INFO)
logLevels.add(LogLevel.WARN)
logLevels.add(LogLevel.VERBOSE)
logLevels.add(LogLevel.ERROR)
logLevels.add(LogLevel.WTF)
} else {
logLevels.addAll(logLevel)
}
}

fun removeLevel(logLevel: LogLevel) {
logLevels.remove(logLevel)
}

fun containsLevel(logLevel: LogLevel): Boolean = logLevel in logLevels
}
}

//Different log levels that user can select
enum class LogLevel {
ALL,
INFO,
DEBUG,
WARN,
VERBOSE,
WTF,
ERROR,
}

fun Any.info(message: String) {
if (LogUtils.containsLevel(LogLevel.INFO)) {
Log.i(javaClass.simpleName, message)
}
}

fun Any.debug(message: String) {
if (LogUtils.containsLevel(LogLevel.DEBUG)) {
Log.d(javaClass.simpleName, message)
}
}

fun Any.error(message: String) {
if (LogUtils.containsLevel(LogLevel.ERROR)) {
Log.e(javaClass.simpleName, message)
}
}

fun Any.verbose(message: String) {
if (LogUtils.containsLevel(LogLevel.VERBOSE)) {
Log.v(javaClass.simpleName, message)
}
}

fun Any.warn(message: String) {
if (LogUtils.containsLevel(LogLevel.WARN)) {
Log.w(javaClass.simpleName, message)
}
}

fun Any.wtf(message: String) {
if (LogUtils.containsLevel(LogLevel.WTF)) {
Log.wtf(javaClass.simpleName, message)
}
}

/**
* Pretty Print the message inside a box of characters.
* The default boundary character is '*', but a different one can be provided by
* the user
* @param message to be logged
* @param boundaryCharacter of the box
* */
fun Any.shout(message: String, boundaryCharacter: Char = '*') {
val listOfStrings = message.split("\n")
val largestLength = listOfStrings.maxBy { it.length }?.length ?: message.length

val logWidth = largestLength + 11
var lineWithCharsAtEnd = ""
var lineTop = ""
var lineBelow = ""
for (count in 0..logWidth) {
lineTop += boundaryCharacter
lineBelow += boundaryCharacter

lineWithCharsAtEnd += if (count == 0 || count == logWidth) {
boundaryCharacter
} else {
" "
}
}

var finalMessage = "$lineTop\n$lineWithCharsAtEnd\n"
for (string in listOfStrings) {
var leftSpace = "$boundaryCharacter "
var rightSpace = " "
val differenceFromLargestString = largestLength - string.length
val rightAddedSpace = differenceFromLargestString / 2
val leftAddedSpace = differenceFromLargestString - rightAddedSpace

for (count in 0..leftAddedSpace) {
leftSpace += " "
}

for (count in 0..rightAddedSpace) {
rightSpace += " "
}
rightSpace += "$boundaryCharacter"
finalMessage += "$leftSpace$string$rightSpace\n"
}
debug("$finalMessage$lineWithCharsAtEnd\n$lineBelow")
}

/*
* Pretty print string that contains data in json format.
* Use debug log level tp print data and error to print error.
* */
fun Any.json(message: String) {
try {
val jsonString = JSONObject(message).toString(4)
debug(jsonString)
} catch (e: Exception) {
if (LogUtils.containsLevel(LogLevel.ERROR)) {
Log.e(javaClass.simpleName, "Wrong JSON format. Please check the structure.", e)
}
}
}

/*
* Print the stack trace for an exception.
* Use Error log level to print.
* */
fun Any.exception(e: Exception) {
if (LogUtils.containsLevel(LogLevel.ERROR)) {
Log.e(javaClass.simpleName, "", e)
}
}

0 comments on commit c74fddd

Please sign in to comment.