From e7dd395d29a5adf494261def413c4df058d2ecd1 Mon Sep 17 00:00:00 2001 From: Arnaud Vergnet Date: Wed, 16 Dec 2020 09:56:22 +0100 Subject: [PATCH] improve log class --- .../java/fr/insa/clavardator/util/Log.java | 106 ++++++++---------- 1 file changed, 49 insertions(+), 57 deletions(-) diff --git a/src/main/java/fr/insa/clavardator/util/Log.java b/src/main/java/fr/insa/clavardator/util/Log.java index 45712ed..e83cdc5 100755 --- a/src/main/java/fr/insa/clavardator/util/Log.java +++ b/src/main/java/fr/insa/clavardator/util/Log.java @@ -1,67 +1,59 @@ package fr.insa.clavardator.util; +import org.jetbrains.annotations.Nullable; + import java.text.SimpleDateFormat; import java.util.Date; public class Log { - /** - * Verbose level: 0 = nothing, - * 1: errors, - * 2: errors & warnings, - * 3: errors & warnings & debug, - * 4: all - */ - public static int verboseLevel = 0; - - public static void v(String prefix, String message) { - v(prefix, message, null); - } - public static void v(String prefix, String message, Exception e) { - if (verboseLevel >= 4) { - Date date = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); - System.out.println(sdf.format(date) + " | V | "+ prefix + ": " + message); - if (e != null) - e.printStackTrace(); - } - } + /** + * Verbose level: 0 = nothing, + * 1: errors, + * 2: errors & warnings, + * 3: errors & warnings & debug, + * 4: all + */ + public static int verboseLevel = 0; - public static void d(String prefix, String message) { - d(prefix, message, null); - } - public static void d(String prefix, String message, Exception e) { - if (verboseLevel >= 3) { - Date date = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); - System.out.println(sdf.format(date) + " | D | "+ prefix + ": " + message); - if (e != null) - e.printStackTrace(); - } - } + private static void print(String prefix, String message, String mode, int requiredLevel, @Nullable Exception e) { + if (verboseLevel >= requiredLevel) { + Date date = new Date(); + SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); + System.out.println(sdf.format(date) + " | " + mode + " | " + prefix + ": " + message); + if (e != null) + e.printStackTrace(); + } + } - public static void w(String prefix, String message) { - w(prefix, message, null); - } - public static void w(String prefix, String message, Exception e) { - if (verboseLevel >= 2) { - Date date = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); - System.out.println(sdf.format(date) + " | W | "+ prefix + ": " + message); - if (e != null) - e.printStackTrace(); - } - } + public static void v(String prefix, String message) { + v(prefix, message, null); + } - public static void e(String prefix, String message) { - e(prefix, message, null); - } - public static void e(String prefix, String message, Exception e) { - if (verboseLevel >= 1) { - Date date = new Date(); - SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss.SSS"); - System.out.println(sdf.format(date) + " | E | "+ prefix + " " + message); - if (e != null) - e.printStackTrace(); - } - } + public static void v(String prefix, String message, Exception e) { + print(prefix, message, "V", 4, e); + } + + public static void d(String prefix, String message) { + d(prefix, message, null); + } + + public static void d(String prefix, String message, Exception e) { + print(prefix, message, "D", 3, e); + } + + public static void w(String prefix, String message) { + w(prefix, message, null); + } + + public static void w(String prefix, String message, Exception e) { + print(prefix, message, "W", 2, e); + } + + public static void e(String prefix, String message) { + e(prefix, message, null); + } + + public static void e(String prefix, String message, Exception e) { + print(prefix, message, "E", 1, e); + } }