Last active
August 16, 2017 10:15
-
-
Save jorgevila/7d32214bcf3547a3f0c05874bbd554c5 to your computer and use it in GitHub Desktop.
kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@file:JvmName("Log") | |
package .... | |
import java.text.SimpleDateFormat | |
import java.util.* | |
const val TAG = "TAG" | |
const val FORMAT = "yyyy-MM-dd'T'HH:mm:ss.SSSZ" | |
private var enabled = true | |
/** | |
* Enable Log. | |
*/ | |
fun enableLogs() { | |
enabled = true | |
} | |
/** | |
* Disable Log. | |
*/ | |
fun disableLogs() { | |
enabled = false | |
} | |
/** | |
* Get sub tag. | |
* @param tag | |
* * | |
* @return a Lazy<String>Builder | |
*/ | |
private fun getSubTag(tag: String): StringBuilder { | |
val log = StringBuilder() | |
if (tag != "") { | |
log.append("[") | |
log.append(tag) | |
log.append("]") | |
} | |
log.append(" [") | |
log.append(Thread.currentThread().id) | |
log.append("] ") | |
return log | |
} | |
@JvmOverloads | |
fun v(tag: String, value: String, th: Throwable? = null) { | |
v(tag, lazy { value }, th) | |
} | |
@JvmOverloads | |
fun v(tag: String, value: Lazy<String>, th: Throwable? = null) { | |
if (enabled) { | |
val message = getSubTag(tag).append(value.value).toString() | |
if (th != null) { | |
android.util.Log.v(TAG, message, th) | |
} else { | |
android.util.Log.v(TAG, message) | |
} | |
} | |
} | |
@JvmOverloads | |
fun d(tag: String, value: String, th: Throwable? = null) { | |
d(tag, lazy { value }, th) | |
} | |
@JvmOverloads | |
fun d(tag: String, value: Lazy<String>, th: Throwable? = null) { | |
if (enabled) { | |
val message = getSubTag(tag).append(value.value).toString() | |
if (th != null) { | |
android.util.Log.d(TAG, message, th) | |
} else { | |
android.util.Log.d(TAG, message) | |
} | |
} | |
} | |
@JvmOverloads | |
fun e(tag: String, value: String, th: Throwable? = null) { | |
e(tag, lazy { value }, th) | |
} | |
@JvmOverloads | |
fun e(tag: String, value: Lazy<String>, th: Throwable? = null) { | |
if (enabled) { | |
val message = getSubTag(tag).append(value.value).toString() | |
if (th != null) { | |
android.util.Log.e(TAG, message, th) | |
} else { | |
android.util.Log.e(TAG, message) | |
} | |
} | |
} | |
@JvmOverloads | |
fun i(tag: String, value: String, th: Throwable? = null) { | |
i(tag, lazy { value }, th) | |
} | |
@JvmOverloads | |
fun i(tag: String, value: Lazy<String>, th: Throwable? = null) { | |
if (enabled) { | |
val message = getSubTag(tag).append(value.value).toString() | |
if (th != null) { | |
android.util.Log.i(TAG, message, th) | |
} else { | |
android.util.Log.i(TAG, message) | |
} | |
} | |
} | |
@JvmOverloads | |
fun wtf(tag: String, value: String, th: Throwable? = null) { | |
wtf(tag, lazy { value }, th) | |
} | |
@JvmOverloads | |
fun wtf(tag: String, value: Lazy<String>, th: Throwable? = null) { | |
if (enabled) { | |
val message = getSubTag(tag).append(value.value).toString() | |
if (th != null) { | |
android.util.Log.wtf(TAG, message, th) | |
} else { | |
android.util.Log.wtf(TAG, message) | |
} | |
} | |
} | |
@JvmOverloads | |
fun w(tag: String, value: String, th: Throwable? = null) { | |
w(tag, lazy { value }, th) | |
} | |
@JvmOverloads | |
fun w(tag: String, value: Lazy<String>, th: Throwable? = null) { | |
if (enabled) { | |
val message = getSubTag(tag).append(value.value).toString() | |
if (th != null) { | |
android.util.Log.w(TAG, message, th) | |
} else { | |
android.util.Log.w(TAG, message) | |
} | |
} | |
} | |
/** | |
* Returns a debug date Lazy<String> for the given time Lazy<String> (in milliseconds). | |
* @param time | |
* * | |
* @return | |
*/ | |
fun formatDate(time: String?): String { | |
if (time == null) { | |
return "" | |
} | |
return formatDate(java.lang.Long.parseLong(time)) | |
} | |
/** | |
* Returns a debug date Lazy<String> for the given time (in milliseconds). | |
* @param time | |
* * | |
* @return | |
*/ | |
fun formatDate(time: Long): String { | |
if (time < 0) { | |
return "-1" | |
} | |
return SimpleDateFormat(FORMAT).format(Date(time)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Utils | |
*/ | |
fun <T1, T2> ifNotNull(value1: T1?, value2: T2?, bothNotNull: (T1, T2) -> (Unit)) { | |
if (value1 != null && value2 != null) { | |
bothNotNull(value1, value2) | |
} | |
} | |
fun <T1, T2, T3> ifNotNull(value1: T1?, value2: T2?, value3: T3?, bothNotNull: (T1, T2, T3) -> | |
(Unit)) { | |
if (value1 != null && value2 != null && value3 != null) { | |
bothNotNull(value1, value2, value3) | |
} | |
}% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment