[DEV] add init dynamic parameters
This commit is contained in:
parent
348bf2ed80
commit
de619e6d5d
@ -9,6 +9,9 @@
|
||||
package io.scenarium.logger;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
import io.scenarium.logger.internal.Log;
|
||||
|
||||
@SuppressWarnings("unused")
|
||||
public class Logger {
|
||||
@ -45,12 +48,83 @@ public class Logger {
|
||||
private static final String BASH_GO_TOP = "\033[0;0f";
|
||||
|
||||
private static HashMap<String, LogLevel> logLevels = new HashMap<>();
|
||||
|
||||
private static LogLevel defaultLevel = LogLevel.CRITICAL;
|
||||
// to enable color, you need to install in eclipse the plug-in "ANSI escape in console"
|
||||
private static boolean haveColor = true;
|
||||
private static boolean haveColor = false;
|
||||
private static boolean isInit = false;
|
||||
|
||||
private Logger() {}
|
||||
|
||||
public static void usage(String applicationName) {
|
||||
Log.print("logger - help : ");
|
||||
Log.print(" " + applicationName + " [options]");
|
||||
Log.print(" --logger-level= Change the default log level (set all Log level):");
|
||||
Log.print(" -3: debug None");
|
||||
Log.print(" -2: debug Print");
|
||||
Log.print(" -1: debug Todo");
|
||||
Log.print(" 0: debug Critical (default)");
|
||||
Log.print(" 1: debug Error");
|
||||
Log.print(" 2: debug Warning");
|
||||
Log.print(" 3: debug Info (default in debug)");
|
||||
Log.print(" 4: debug Debug");
|
||||
Log.print(" 5: debug Verbose");
|
||||
Log.print(" --logger-lib=name:X Set a library specific level:");
|
||||
Log.print(" name Name of the library");
|
||||
Log.print(" X Log level to set [0..6]");
|
||||
Log.print(" note: ':' can be replace with '/' or '+'");
|
||||
Log.print(" --logger-color Enable color in log (default in Linux/debug)");
|
||||
Log.print(" --logger-no-color Disable color in log (default in Linux/release and Other)");
|
||||
Log.print(" -h/--help: Display this help");
|
||||
Log.print(" example:");
|
||||
Log.print(" " + applicationName + " --logger-color --logger-level=2 --logger-lib=etk:5 --logger-lib=appl:5");
|
||||
}
|
||||
|
||||
public static void init(String applicationName, List<String> args) {
|
||||
if (isInit)
|
||||
return;
|
||||
isInit = true;
|
||||
for (int iii = 0; iii < args.size(); ++iii) {
|
||||
String data = args.get(iii);
|
||||
if (data.startsWith("--logger-level=")) {
|
||||
String value = data.substring(15);
|
||||
LogLevel level = LogLevel.fromString(value);
|
||||
System.out.println("Change global level at " + value + " ==> " + level);
|
||||
Logger.defaultLevel = level;
|
||||
} else if (data.contentEquals("--logger-color"))
|
||||
Logger.haveColor = true;
|
||||
else if (data.contentEquals("--logger-no-color"))
|
||||
Logger.haveColor = false;
|
||||
else if (data.startsWith("--logger-lib=")) {
|
||||
String value = data.substring(13);
|
||||
String[] values = value.split("/");
|
||||
if (values.length != 2) {
|
||||
values = value.split(":");
|
||||
if (values.length != 2) {
|
||||
values = value.split("+");
|
||||
if (values.length != 2) {
|
||||
System.err.println("Can not set the --logger-lib= with value='" + value + "' not formated name:X or name/X or name+X");
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
System.out.println("Change level of '" + values[0] + "' at " + LogLevel.fromString(values[1]));
|
||||
logLevels.put(values[0], LogLevel.fromString(values[1]));
|
||||
} else if (data.contentEquals("-h") || data.contentEquals("--help"))
|
||||
usage(applicationName);
|
||||
else if (data.startsWith("--logger"))
|
||||
Log.error("Can not parse the argument : '" + data + "'");
|
||||
}
|
||||
// Clear all logger elements.
|
||||
int iii = 0;
|
||||
while (iii < args.size()) {
|
||||
String data = args.get(iii);
|
||||
if (data.startsWith("--logger"))
|
||||
args.remove(iii);
|
||||
else
|
||||
iii++;
|
||||
}
|
||||
}
|
||||
|
||||
/** This function permit to get the printable string to print in the log element (select here the number of char to print)
|
||||
* @param libName Name of the library
|
||||
* @return string to set in the logger information */
|
||||
@ -61,7 +135,7 @@ public class Logger {
|
||||
public static boolean getNeedPrint(String libName, LogLevel level) {
|
||||
LogLevel reference = logLevels.get(libName);
|
||||
if (reference == null)
|
||||
return level.isLessEqual(LogLevel.INFO);
|
||||
return level.isLessEqual(Logger.defaultLevel);
|
||||
return level.isLessEqual(reference);
|
||||
}
|
||||
|
||||
@ -79,6 +153,15 @@ public class Logger {
|
||||
System.out.println("[C] " + libName + " | " + data);
|
||||
if (haveColor)
|
||||
System.out.print(BASH_COLOR_NORMAL);
|
||||
System.out.flush();
|
||||
if (haveColor)
|
||||
System.out.print(BASH_COLOR_YELLOW);
|
||||
for (StackTraceElement ste : Thread.currentThread().getStackTrace())
|
||||
System.out.println(ste);
|
||||
System.out.print(BASH_COLOR_NORMAL);
|
||||
System.out.flush();
|
||||
System.out.flush();
|
||||
System.exit(-50);
|
||||
}
|
||||
|
||||
public static void error(String libName, String data) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user