2018-11-12 18:16:45 +00:00
|
|
|
/*
|
2018-11-13 22:16:49 +00:00
|
|
|
* Copyright (c) 2018 Bevilacqua Joey.
|
2018-11-12 18:16:45 +00:00
|
|
|
*/
|
|
|
|
package ch.usi.inf.atelier.group1.util
|
|
|
|
|
2018-11-13 22:16:49 +00:00
|
|
|
import java.text.SimpleDateFormat
|
|
|
|
import java.util.*
|
2018-11-12 18:16:45 +00:00
|
|
|
|
|
|
|
object Log {
|
|
|
|
|
2018-11-15 15:11:32 +00:00
|
|
|
/**
|
|
|
|
* Log an exception as an error
|
|
|
|
*
|
2018-11-18 17:39:51 +00:00
|
|
|
* @param exception The error exception
|
2018-11-15 15:11:32 +00:00
|
|
|
* @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)
|
2018-11-12 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:11:32 +00:00
|
|
|
/**
|
|
|
|
* Log an object using its toString() method.
|
|
|
|
*
|
|
|
|
* @param obj The object which content will be printed.
|
|
|
|
*/
|
2018-11-13 22:16:49 +00:00
|
|
|
fun i(obj: Any) {
|
|
|
|
print('I', obj.toString(), false)
|
2018-11-12 18:16:45 +00:00
|
|
|
}
|
|
|
|
|
2018-11-15 15:11:32 +00:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2018-11-13 22:16:49 +00:00
|
|
|
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")
|
|
|
|
}
|
2018-11-12 18:16:45 +00:00
|
|
|
}
|
|
|
|
}
|