Skip to content

Commit

Permalink
better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Orbiter committed May 30, 2012
1 parent ae173f4 commit 43c2c6e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 37 deletions.
10 changes: 4 additions & 6 deletions source/de/anomic/http/server/HTTPDFileHandler.java
Expand Up @@ -579,6 +579,7 @@ public static void doResponse(final HashMap<String, Object> conProp, final Reque
e.getTargetException().getMessage() +
"; java.awt.graphicsenv='" + System.getProperty("java.awt.graphicsenv","") + "'");
Log.logException(e);
Log.logException(e.getCause());
Log.logException(e.getTargetException());
targetClass = null;
}
Expand Down Expand Up @@ -978,12 +979,9 @@ && matchesSuffix(path, switchboard.getConfig("cgi.suffixes", null)) // "right" f
if (e.getCause() instanceof InterruptedException) {
throw new InterruptedException(e.getCause().getMessage());
}

theLogger.logSevere("INTERNAL ERROR: " + e.toString() + ":" +
e.getMessage() +
" target exception at " + targetClass + ": " +
e.getTargetException().toString() + ":" +
e.getTargetException().getMessage(),e);
Log.logException(e);
Log.logException(e.getCause());
Log.logException(e.getTargetException());
targetClass = null;
throw e;
}
Expand Down
8 changes: 8 additions & 0 deletions source/de/anomic/server/serverCore.java
Expand Up @@ -298,15 +298,18 @@ public InetSocketAddress generateSocketAddress(String extendedPortString) throws
: new InetSocketAddress(bindIP, bindPort);
}

@Override
public void open() {
this.log.logConfig("* server started on " + Domains.myPublicLocalIP() + ":" + this.extendedPort);
}

@Override
public void freemem() {
// FIXME: can we something here to flush memory? Idea: Reduce the size of some of our various caches.
}

// class body
@Override
public boolean job() throws Exception {
try {
// prepare for new connection
Expand Down Expand Up @@ -407,6 +410,7 @@ public boolean job() throws Exception {
}
}

@Override
public synchronized void close() {
// consuming the isInterrupted Flag. Otherwise we could not properly close the session pool
Thread.interrupted();
Expand Down Expand Up @@ -456,6 +460,7 @@ public List<Session> getJobList() {
return l;
}

@Override
public int getJobCount() {
final Thread[] threadList = new Thread[sessionThreadGroup.activeCount()];
serverCore.sessionThreadGroup.enumerate(threadList, false);
Expand Down Expand Up @@ -527,6 +532,7 @@ public Session(final ThreadGroup theThreadGroup, final Socket controlSocket, fin
sessionCounter++;
}

@Override
public int hashCode() {
// return a hash code so it is possible to store objects of httpc objects in a HashSet
return this.hashIndex;
Expand Down Expand Up @@ -619,6 +625,7 @@ public boolean isRunning() {
*
* @see java.lang.Thread#run()
*/
@Override
public void run() {
this.runningsession = true;

Expand Down Expand Up @@ -804,6 +811,7 @@ private void listen() {
} catch (final InvocationTargetException e) {
serverCore.this.log.logSevere("command execution, target exception " + e.getMessage() + " for client " + this.userAddress.getHostAddress(), e);
// we extract a target exception
writeLine(this.commandObj.error(e.getCause()));
writeLine(this.commandObj.error(e.getTargetException()));
break;
} catch (final NoSuchMethodException e) {
Expand Down
18 changes: 18 additions & 0 deletions source/net/yacy/kelondro/logging/Log.java
Expand Up @@ -76,6 +76,7 @@ public final void logSevere(final String message) {
}

public final void logSevere(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.SEVERE, message, thrown);
}

Expand All @@ -88,6 +89,7 @@ public final void logWarning(final String message) {
}

public final void logWarning(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.WARNING, message, thrown);
}

Expand All @@ -100,6 +102,7 @@ public final void logConfig(final String message) {
}

public final void logConfig(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.CONFIG, message, thrown);
}

Expand All @@ -112,6 +115,7 @@ public final void logInfo(final String message) {
}

public final void logInfo(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.INFO, message, thrown);
}

Expand All @@ -124,6 +128,7 @@ public final void logFine(final String message) {
}

public final void logFine(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.FINE, message, thrown);
}

Expand All @@ -136,6 +141,7 @@ public final void logFiner(final String message) {
}

public final void logFiner(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.FINER, message, thrown);
}

Expand All @@ -148,6 +154,7 @@ public final void logFinest(final String message) {
}

public final void logFinest(final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(this.theLogger, Level.FINEST, message, thrown);
}

Expand All @@ -165,37 +172,43 @@ public final static void logSevere(final String appName, final String message) {
enQueueLog(appName, Level.SEVERE, message);
}
public final static void logSevere(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.SEVERE, message, thrown);
}

public final static void logWarning(final String appName, final String message) {
enQueueLog(appName, Level.WARNING, message);
}
public final static void logException(final Throwable thrown) {
if (thrown == null) return;
enQueueLog("StackTrace", Level.WARNING, thrown.getMessage(), thrown);
}
public final static void logWarning(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.WARNING, message, thrown);
}

public final static void logConfig(final String appName, final String message) {
enQueueLog(appName, Level.CONFIG, message);
}
public final static void logConfig(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.CONFIG, message, thrown);
}

public final static void logInfo(final String appName, final String message) {
enQueueLog(appName, Level.INFO, message);
}
public final static void logInfo(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.INFO, message, thrown);
}

public final static void logFine(final String appName, final String message) {
enQueueLog(appName, Level.FINE, message);
}
public final static void logFine(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.FINE, message, thrown);
}
public final static boolean isFine(final String appName) {
Expand All @@ -206,20 +219,23 @@ public final static void logFiner(final String appName, final String message) {
enQueueLog(appName, Level.FINER, message);
}
public final static void logFiner(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.FINER, message, thrown);
}

public final static void logFinest(final String appName, final String message) {
enQueueLog(appName, Level.FINEST, message);
}
public final static void logFinest(final String appName, final String message, final Throwable thrown) {
if (thrown == null) return;
enQueueLog(appName, Level.FINEST, message, thrown);
}
public final static boolean isFinest(final String appName) {
return Logger.getLogger(appName).isLoggable(Level.FINEST);
}

private final static void enQueueLog(final Logger logger, final Level level, final String message, final Throwable thrown) {
if (thrown == null) return;
if (logRunnerThread == null || !logRunnerThread.isAlive()) {
logger.log(level, message, thrown);
} else {
Expand All @@ -244,6 +260,7 @@ private final static void enQueueLog(final Logger logger, final Level level, fin
}

private final static void enQueueLog(final String loggername, final Level level, final String message, final Throwable thrown) {
if (thrown == null) return;
if (logRunnerThread == null || !logRunnerThread.isAlive()) {
Logger.getLogger(loggername).log(level, message, thrown);
} else {
Expand Down Expand Up @@ -385,6 +402,7 @@ public final static void configureLogging(final File dataPath, final File appPat
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler(){
@Override
public void uncaughtException(final Thread t, final Throwable e) {
if (e == null) return;
final String msg = String.format("Thread %s: %s",t.getName(), e.getMessage());
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final PrintStream ps = new PrintStream(baos);
Expand Down
65 changes: 35 additions & 30 deletions source/net/yacy/kelondro/order/MergeIterator.java
Expand Up @@ -35,14 +35,14 @@


public class MergeIterator<E> implements CloneableIterator<E> {

private final Comparator<E> comp;
private final CloneableIterator<E> a;
private final CloneableIterator<E> b;
private E na, nb;
private final Method merger;
private final boolean up;

public MergeIterator(
final CloneableIterator<E> a,
final CloneableIterator<E> b,
Expand All @@ -61,51 +61,54 @@ public MergeIterator(
nextb();
}

@Override
public MergeIterator<E> clone(final Object modifier) {
assert a != null;
assert b != null;
assert merger != null;
return new MergeIterator<E>(a.clone(modifier), b.clone(modifier), comp, merger, up);
assert this.a != null;
assert this.b != null;
assert this.merger != null;
return new MergeIterator<E>(this.a.clone(modifier), this.b.clone(modifier), this.comp, this.merger, this.up);
}

private void nexta() {
try {
if (a != null && a.hasNext()) na = a.next(); else na = null;
if (this.a != null && this.a.hasNext()) this.na = this.a.next(); else this.na = null;
} catch (final ConcurrentModificationException e) {
na = null;
this.na = null;
}
}
private void nextb() {
try {
if (b != null && b.hasNext()) nb = b.next(); else nb = null;
if (this.b != null && this.b.hasNext()) this.nb = this.b.next(); else this.nb = null;
} catch (final ConcurrentModificationException e) {
nb = null;
this.nb = null;
}
}


@Override
public boolean hasNext() {
return (na != null) || (nb != null);
return (this.na != null) || (this.nb != null);
}

@SuppressWarnings("unchecked")

@Override
@SuppressWarnings("unchecked")
public E next() {
E s;
if (na == null) {
s = nb;
if (this.na == null) {
s = this.nb;
nextb();
return s;
}
if (nb == null) {
s = na;
if (this.nb == null) {
s = this.na;
nexta();
return s;
}
// compare the Objects
final int c = comp.compare(na, nb);
final int c = this.comp.compare(this.na, this.nb);
if (c == 0) {
try {
//System.out.print("MERGE OF " + na.toString() + " AND " + nb.toString() + ": ");
s = (E) this.merger.invoke(null, new Object[]{na, nb});
s = (E) this.merger.invoke(null, new Object[]{this.na, this.nb});
//System.out.println("RESULT IS " + s.toString());
} catch (final IllegalArgumentException e) {
Log.logException(e);
Expand All @@ -115,34 +118,36 @@ public E next() {
s = null;
} catch (final InvocationTargetException e) {
Log.logException(e);
Log.logException(e.getCause());
s = null;
}
nexta();
nextb();
return s;
} else if ((up && c < 0) || (!up && c > 0)) {
s = na;
} else if ((this.up && c < 0) || (!this.up && c > 0)) {
s = this.na;
nexta();
return s;
} else {
s = nb;
s = this.nb;
nextb();
return s;
}
}


@Override
public void remove() {
throw new java.lang.UnsupportedOperationException("merge does not support remove");
}

public static <A> CloneableIterator<A> cascade(final Collection<CloneableIterator<A>> iterators, final Order<A> c, final Method merger, final boolean up) {
// this extends the ability to combine two iterators
// to the ability of combining a set of iterators
if (iterators == null) return null;
if (iterators.isEmpty()) return null;
return cascade(iterators.iterator(), c, merger, up);
}

private static <A> CloneableIterator<A> cascade(final Iterator<CloneableIterator<A>> iiterators, final Order<A> c, final Method merger, final boolean up) {
if (iiterators == null) return null;
if (!(iiterators.hasNext())) return null;
Expand All @@ -151,7 +156,7 @@ private static <A> CloneableIterator<A> cascade(final Iterator<CloneableIterator
assert merger != null;
return new MergeIterator<A>(one, cascade(iiterators, c, merger, up), c, merger, up);
}

public static final Method simpleMerge;
static {
Method meth = null;
Expand All @@ -166,9 +171,9 @@ private static <A> CloneableIterator<A> cascade(final Iterator<CloneableIterator
meth = null;
}
assert meth != null;
simpleMerge = meth;
simpleMerge = meth;
}

// do not remove the following method, it is not reference anywhere directly but indirectly using reflection
// please see initialization of simpleMerge above
public static Object mergeEqualByReplace(final Object a, final Object b) {
Expand Down

0 comments on commit 43c2c6e

Please sign in to comment.