* Log: Fix level stuck at DEBUG when called from constructor

This commit is contained in:
zzz
2011-08-28 18:47:13 +00:00
parent d4bf3403e1
commit 8b05b16c8a
2 changed files with 29 additions and 10 deletions

View File

@ -23,12 +23,12 @@ import net.i2p.I2PAppContext;
* @author jrandom * @author jrandom
*/ */
public class Log { public class Log {
private Class _class; private final Class _class;
private String _className; private final String _className;
private String _name; private final String _name;
private int _minPriority; private int _minPriority;
private LogScope _scope; private final LogScope _scope;
private LogManager _manager; private final LogManager _manager;
public final static int DEBUG = 10; public final static int DEBUG = 10;
public final static int INFO = 20; public final static int INFO = 20;
@ -69,11 +69,19 @@ public class Log {
return (level > CRIT ? STR_CRIT : STR_DEBUG); return (level > CRIT ? STR_CRIT : STR_DEBUG);
} }
/**
* Warning - not recommended.
* Use I2PAppContext.getGlobalContext().logManager().getLog(cls)
*/
public Log(Class cls) { public Log(Class cls) {
this(I2PAppContext.getGlobalContext().logManager(), cls, null); this(I2PAppContext.getGlobalContext().logManager(), cls, null);
_manager.addLog(this); _manager.addLog(this);
} }
/**
* Warning - not recommended.
* Use I2PAppContext.getGlobalContext().logManager().getLog(name)
*/
public Log(String name) { public Log(String name) {
this(I2PAppContext.getGlobalContext().logManager(), null, name); this(I2PAppContext.getGlobalContext().logManager(), null, name);
_manager.addLog(this); _manager.addLog(this);
@ -190,19 +198,23 @@ public class Log {
if (name == null) return cls.getName(); if (name == null) return cls.getName();
return name + "" + cls.getName(); return name + "" + cls.getName();
} }
private static final class LogScope { private static final class LogScope {
private String _scopeName; private final String _scopeName;
private Class _scopeClass; private final Class _scopeClass;
private String _scopeCache; private final String _scopeCache;
public LogScope(String name, Class cls) { public LogScope(String name, Class cls) {
_scopeName = name; _scopeName = name;
_scopeClass = cls; _scopeClass = cls;
_scopeCache = getScope(name, cls); _scopeCache = getScope(name, cls);
} }
@Override @Override
public int hashCode() { public int hashCode() {
return _scopeCache.hashCode(); return _scopeCache.hashCode();
} }
@Override @Override
public boolean equals(Object obj) { public boolean equals(Object obj) {
if (obj == null) if (obj == null)

View File

@ -179,10 +179,17 @@ public class LogManager {
return new ArrayList(_logs.values()); return new ArrayList(_logs.values());
} }
/**
* If the log already exists, its priority is set here but cannot
* be changed later, as it becomes an "orphan" not tracked by the manager.
*/
void addLog(Log log) { void addLog(Log log) {
Log old = _logs.putIfAbsent(log.getScope(), log); Log old = _logs.putIfAbsent(log.getScope(), log);
if (old == null) updateLimit(log);
updateLimit(log); if (old != null) {
if (_log.shouldLog(Log.INFO))
_log.info("Duplicate log for " + log.getName());
}
} }
public LogConsoleBuffer getBuffer() { return _consoleBuffer; } public LogConsoleBuffer getBuffer() { return _consoleBuffer; }