Skip to content

Commit

Permalink
added small additional log infos for timer functions
Browse files Browse the repository at this point in the history
  • Loading branch information
MFlisar committed Feb 11, 2019
1 parent 242528f commit dcd3570
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
25 changes: 20 additions & 5 deletions library/src/main/java/com/michaelflisar/lumberjack/T.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ object T {
* and will reset any already existing data bound to that key
*
* @param key the key that this timer should be bound to
* @return true, if another timer has been replaced, false if not
*/
fun start(key: Any) {
clear(key)
fun start(key: Any): Boolean {
val timerData = clear(key)
mTimers[key] = TimerData().start()
return timerData != null
}

/**
Expand Down Expand Up @@ -52,9 +54,10 @@ object T {
* Clears all data that exists for a given key
*
* @param key the key that the desired timer is bound to
* @return the removed data or null, if key did not exist
*/
fun clear(key: Any) {
mTimers.remove(key)
fun clear(key: Any): TimerData? {
return mTimers.remove(key)
}

/**
Expand All @@ -70,6 +73,19 @@ object T {
// Convenient action functions with pretty result print as result
// ------------------

/**
* start a time log accessable (and bound to) the provided key
* and will reset any already existing data bound to that key
*
* @param key the key that this timer should be bound to
* @return a logable message that the timer has been started
*/
fun printAndStart(key: Any): String {
val replaced = start(key)
val data = getTimer(key)
return "New timer started at ${data.getStartTime()}${if (replaced) " [old timer has been replaced!]" else ""}"
}

/**
* adds a lap to the timer bound to the provider key
*
Expand Down Expand Up @@ -99,7 +115,6 @@ object T {
* @return the last laps duration and the total duration as a readable string or null, if key does not exist or timer was stopped already
*/
fun printAndLapTotal(key: Any): String {

val lap = lap(key) ?: return "NULL"
val data = getTimer(key)
val total = data.getTotal() ?: "NULL"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.michaelflisar.lumberjack.data

import java.text.SimpleDateFormat
import java.util.*

/**
Expand All @@ -8,6 +9,10 @@ import java.util.*

class TimerData {

companion object {
val TIME_FORMATTER = SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
}

private var mStart: Long = 0
private var mEnd: Long = 0
private var mLaps: MutableList<Long>? = null
Expand All @@ -26,6 +31,8 @@ class TimerData {

fun getTotal(): Long? = if (wasEnded()) mEnd - mStart else (if (wasStarted()) System.currentTimeMillis() - mStart else null)

fun getStartTime() : String = if (wasStarted()) TIME_FORMATTER.format(mStart) else "NOT STARTED"

// -------------
// helper functions
// -------------
Expand Down

0 comments on commit dcd3570

Please sign in to comment.