theshell.ch/bonus2/src/ch/usi/inf/atelier/group1/util/Log.kt
bevilj 5270dfef3a bonus2: add support for more special chars and fix syntax of parsed files
git-svn-id: svn+ssh://atelier.inf.usi.ch/home/bevilj/group-1@250 a672b425-5310-4d7a-af5c-997e18724b81
2018-11-18 17:39:51 +00:00

59 lines
1.4 KiB
Kotlin

/*
* Copyright (c) 2018 Bevilacqua Joey.
*/
package ch.usi.inf.atelier.group1.util
import java.text.SimpleDateFormat
import java.util.*
object Log {
/**
* Log an exception as an error
*
* @param exception The error exception
* @param shouldThrow Whether the exception should be thrown
*/
fun <T : Exception> e(exception: T, shouldThrow: Boolean) {
e(exception.message ?: "Unknown error")
if (shouldThrow) {
throw exception
}
}
/**
* Log an error
*
* @param message The log message
*/
fun e(message: String) {
print('E', message , true)
}
/**
* Log an object using its toString() method.
*
* @param obj The object which content will be printed.
*/
fun i(obj: Any) {
print('I', obj.toString(), false)
}
/**
* Print a log with a date and prefix
*
* @param prefix The prefix of the log. Helps differentiating the various types of logs
* @param message The message that will be displayed in the log
* @param isErr Whether the log should be printed as an error
*/
private fun print(prefix: Char, message: String, isErr: Boolean) {
val time = SimpleDateFormat("yyyy-MM-dd hh:mm").format(Date())
if (isErr) {
System.err.println("$prefix $time\t$message")
} else {
System.out.println("$prefix $time\t$message")
}
}
}