This commit is contained in:
Olivier Duval 2021-02-11 02:21:53 +01:00
parent 63290cd3a2
commit 52a93601dd
7 changed files with 148 additions and 12 deletions

View File

@ -3,8 +3,8 @@ plugins {
id 'maven-publish'
}
group 'org.example'
version '1.0-SNAPSHOT'
group 'fr.doap.llog'
version '0.1'
repositories {
mavenCentral()
@ -32,16 +32,20 @@ def getDate() {
publishing {
publications {
mavenJava(MavenPublication) {
groupId = 'fr.doap.dlog'
artifactId = 'dlog'
version = "0.9.4-SNAPSHOT"
groupId = "${group}"
artifactId = "llog"
version = "${version}"
from components.java
}
}
repositories {
maven {
// url = "https://maven.doap.fr/repository/maven-releases/"
url = "https://maven.doap.fr/repository/maven-snapshots/"
if (project.version.endsWith('-SNAPSHOT')) {
url "https://maven.doap.fr/repository/maven-snapshots/"
} else {
url "https://maven.doap.fr/repository/maven-releases/"
}
credentials {
username privateProperties["mavenUsername"]
password privateProperties["mavenPassword"]

View File

@ -0,0 +1,55 @@
package fr.doap.llog;
import java.util.*;
public class Dict<T> {
public void put(String aKey, T aValue) {
if (aKey == null || aKey.length() == 0) mValue = aValue;
else {
String[] lS = aKey.split("[.]", 2);
Dict<T> lD = mDict.get(lS[0]);
if (lD == null) {
lD = new Dict<>();
mDict.put(lS[0], lD);
}
if (lS.length > 1) lD.put(lS[1], aValue);
else lD.put("", aValue);
}
}
public T get(String aKey) {
if (aKey == null || aKey.length() == 0) return mValue;
else {
String[] lS = aKey.split("[.]", 2);
Dict<T> lD = mDict.get(lS[0]);
if (lD == null) return mValue;
else {
T lT = lD.mValue;
if (lS.length > 1) lT = lD.get(lS[1]);
if (lT == null) return mValue;
else return lT;
}
}
}
@Override
public String toString() {
return "\n{\n"+toString(0)+"}";
}
private String toString(int aI) {
String lR = ": "+ mValue+"\n";
for (Map.Entry<String, Dict<T>> me: mDict.entrySet()) lR += spc(aI+1)+me.getKey()+me.getValue().toString(aI+1);
return lR;
}
private String spc(int aI) {
String l = "";
for (int i=0; i<aI; i++) l += " ";
return l;
}
private Map<String, Dict<T>> mDict = new HashMap<>();
private T mValue;
}

View File

@ -0,0 +1,6 @@
package fr.doap.llog;
public class Filter {
public int mLogLevel;
public int mSTLevel;
}

View File

@ -11,8 +11,10 @@ public class L {
mL.mLooper.post(lLogMsg);
return lLogMsg;
}
static public void filter(String aKey, Filter aFilter) {
mL.mLooper.put(aKey,aFilter);
}
private LogLooper mLooper = new LogLooper();
private ILogger mLogger;
}

View File

@ -1,22 +1,52 @@
package fr.doap.llog;
import java.util.Set;
public class LogLooper extends ThreadLooper {
public void post(LogMsg aLogMsg) {
add(new LLMsg(aLogMsg));
}
public void put(String aKey, Filter aF) {
mFilters.put(aKey, aF);
}
public class LLMsg implements WorkItem {
public LLMsg(LogMsg aLogMsg) { mLogMsg = aLogMsg; }
@Override
public void run() {
int lI = mLogMsg.getCallerIndex();
StackTraceElement lS = mLogMsg.mSTE[lI];
String lClass = lS.getClassName();
lClass = lClass.substring(lClass.lastIndexOf('.'));
if (mLogMsg.mTag == null || mLogMsg.mTag.isEmpty()) mLogMsg.mTag = lClass;
String lC = lS.getClassName() + "." + lS.getMethodName();
Filter lF = mFilters.get(lC);
if (lF != null) {
if (mLogMsg.mSeverity >= lF.mLogLevel) {
System.out.println(mLogMsg.mSeverity+ "-" + lC + ": " + mLogMsg.mMessage + " (" + lS.getFileName() + ":" + lS.getLineNumber() + ")");
if (mLogMsg.mSeverity <= lF.mSTLevel) {
for (int i=lI+1; i<mLogMsg.mSTE.length; i++) {
StackTraceElement l = mLogMsg.mSTE[i];
System.out.println(" "+l.getClassName() + "." + l.getMethodName() + " (" + l.getFileName() + ":" + l.getLineNumber() + ")");
}
}
}
} else {
System.out.println(lS.getClassName() + "." + lS.getMethodName() + ": " + mLogMsg.mMessage + " (" + lS.getFileName() + ":" + lS.getLineNumber() + ")");
}
/*
for (int i=mLogMsg.getCallerIndex(); i<mLogMsg.mSTE.length; i++) {
StackTraceElement lS = mLogMsg.mSTE[i];
System.out.println(lS.getClassName() + "." + lS.getMethodName() + ": " + mLogMsg.mMessage + " (" + lS.getFileName() + ":" + lS.getLineNumber() + ")");
}
*/
}
private LogMsg mLogMsg;
}
private Dict<Filter> mFilters = new Dict<>();
}

View File

@ -59,8 +59,8 @@ public class ThreadLooper {
onStartWaiting();
ThreadLooper.this.wait();
} catch(InterruptedException e) {
System.out.println("Interrupted");
System.out.println("Caller threads: "+ mCallerThreads);
// System.out.println("Interrupted");
// System.out.println("Caller threads: "+ mCallerThreads);
if (mCallerThreads.isEmpty()) Thread.currentThread().interrupt();
}
}

View File

@ -1,15 +1,54 @@
package fr.doap.test;
import fr.doap.llog.L;
import fr.doap.llog.*;
public class Test {
public static void main(String[] args) {
L.l(1, null, "test", "Message sans format");
test1();
testDict();
testFilter();
}
protected static void test1() {
L.l(1, null, "test", "Message avec format: %s", "toto");
}
protected static void testDict() {
Dict<String> lD = new Dict<>();
L.l(1, null, "Dict", "Empty: %s", lD.toString());
lD.put("fr.doap", "fr_doap");
L.l(1, null, "Dict", "1: %s", lD.toString());
lD.put("com.doap.fr", "com_doap_fr");
L.l(1, null, "Dict", "2: %s", lD.toString());
lD.put("com", "com");
L.l(1, null, "Dict", "3: %s", lD.toString());
lD.put("fr.doap.llog", "fr_doap_llog");
L.l(1, null, "Dict", "4: %s", lD.toString());
lD.put("", "ROOT");
L.l(1, null, "Dict", "5: %s", lD.toString());
lD.put("com.doap.fr", "com_doap_fr+++");
L.l(1, null, "Dict", "6: %s", lD.toString());
L.l(1, null, "Dict", "fr.doap.llog: %s", lD.get("fr.doap.llog"));
L.l(1, null, "Dict", "fr: %s", lD.get("fr"));
L.l(1, null, "Dict", "com.doap.fr.toto: %s", lD.get("com.doap.fr.toto"));
L.l(1, null, "Dict", "com.bibi: %s", lD.get("com.bibi"));
L.l(1, null, "Dict", "org.tata: %s", lD.get("org.tata"));
L.l(1, null, "Dict", "com.doap: %s", lD.get("com.doap"));
L.l(1, null, "Dict", "com.doap.bibi: %s", lD.get("com.doap.bibi"));
}
protected static void testFilter() {
Filter lF = new Filter();
lF.mLogLevel = 3;
lF.mSTLevel = 5;
L.filter("", lF);
L.l(10, null, "Dict", "Always show");
L.l(6, null, "Dict", "Always show 6");
L.l(5, null, "Dict", "Always with ST 5");
L.l(3, null, "Dict", "Always with ST 3");
L.l(2, null, "Dict", "Never show");
L.l(100, null, "Dict", "Finished");
}
}