Annotation for logged name, added time in log.

v5stable
Ondřej Hruška 11 years ago
parent 6d7ee87ae6
commit 99bb04ae43
  1. 5
      src/mightypork/rogue/App.java
  2. 2
      src/mightypork/rogue/fonts/DeferredFont.java
  3. 3
      src/mightypork/rogue/fonts/DeferredFontNative.java
  4. 3
      src/mightypork/rogue/loading/AsyncResourceLoader.java
  5. 21
      src/mightypork/rogue/loading/BaseDeferredResource.java
  6. 2
      src/mightypork/rogue/sound/DeferredAudio.java
  7. 2
      src/mightypork/rogue/sound/NullAudio.java
  8. 2
      src/mightypork/rogue/textures/DeferredTexture.java
  9. 1
      src/mightypork/utils/control/bus/EventBus.java
  10. 13
      src/mightypork/utils/logging/Log.java
  11. 15
      src/mightypork/utils/logging/LogInstance.java
  12. 21
      src/mightypork/utils/logging/LoggedName.java

@ -4,6 +4,7 @@ package mightypork.rogue;
import java.io.File; import java.io.File;
import java.io.RandomAccessFile; import java.io.RandomAccessFile;
import java.nio.channels.FileLock; import java.nio.channels.FileLock;
import java.util.Locale;
import java.util.logging.Level; import java.util.logging.Level;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
@ -116,6 +117,9 @@ public class App implements AppAccess {
public void initialize() public void initialize()
{ {
// to get dot instead of comma in floats
Locale.setDefault(Locale.ENGLISH);
/* /*
* Lock working directory * Lock working directory
*/ */
@ -138,6 +142,7 @@ public class App implements AppAccess {
*/ */
Log.f2("Initializing Event Bus..."); Log.f2("Initializing Event Bus...");
eventBus = new EventBus(); eventBus = new EventBus();
eventBus.logSending = true;
initChannels(); initChannels();
/* /*

@ -9,6 +9,7 @@ import java.io.InputStream;
import mightypork.rogue.loading.BaseDeferredResource; import mightypork.rogue.loading.BaseDeferredResource;
import mightypork.rogue.loading.MustLoadInMainThread; import mightypork.rogue.loading.MustLoadInMainThread;
import mightypork.utils.files.FileUtils; import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.color.RGB; import mightypork.utils.math.color.RGB;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
@ -19,6 +20,7 @@ import mightypork.utils.math.coord.Coord;
* @author MightyPork * @author MightyPork
*/ */
@MustLoadInMainThread @MustLoadInMainThread
@LoggedName(name="Font")
public class DeferredFont extends BaseDeferredResource implements GLFont { public class DeferredFont extends BaseDeferredResource implements GLFont {
public static enum FontStyle public static enum FontStyle

@ -5,12 +5,15 @@ import java.awt.Font;
import java.awt.FontFormatException; import java.awt.FontFormatException;
import java.io.IOException; import java.io.IOException;
import mightypork.utils.logging.LoggedName;
/** /**
* Font obtained from the OS * Font obtained from the OS
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name="FontNative")
public class DeferredFontNative extends DeferredFont { public class DeferredFontNative extends DeferredFont {
/** /**

@ -64,13 +64,14 @@ public class AsyncResourceLoader extends Thread implements ResourceLoadRequest.L
// textures & fonts needs to be loaded in main thread // textures & fonts needs to be loaded in main thread
if (def.getClass().isAnnotationPresent(MustLoadInMainThread.class)) { if (def.getClass().isAnnotationPresent(MustLoadInMainThread.class)) {
Log.f3("<LOADER> Loading in main thread:\n "+Log.str(def)); Log.f3("<LOADER> Delegating to main thread:\n "+Log.str(def));
app.bus().send(new MainLoopTaskRequest(new Runnable() { app.bus().send(new MainLoopTaskRequest(new Runnable() {
@Override @Override
public void run() public void run()
{ {
System.out.println("~~");
def.load(); def.load();
} }
})); }));

@ -3,6 +3,7 @@ package mightypork.rogue.loading;
import mightypork.utils.control.interf.Destroyable; import mightypork.utils.control.interf.Destroyable;
import mightypork.utils.logging.Log; import mightypork.utils.logging.Log;
import mightypork.utils.logging.LoggedName;
/** /**
@ -12,6 +13,7 @@ import mightypork.utils.logging.Log;
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name = "Resource")
public abstract class BaseDeferredResource implements DeferredResource, Destroyable { public abstract class BaseDeferredResource implements DeferredResource, Destroyable {
private final String resource; private final String resource;
@ -27,23 +29,28 @@ public abstract class BaseDeferredResource implements DeferredResource, Destroya
@Override @Override
public synchronized final void load() public synchronized final void load()
{ {
if (loadAttempted) return; if (loadAttempted) {
Log.w("<RES> Already loaded @ load():\n " + this);
return;
}
loadAttempted = true;
loadFailed = false; loadFailed = false;
if (isNull()) return; if (isNull()) return;
try { try {
if (resource == null) throw new NullPointerException("Resource string cannot be null for non-null resource."); if (resource == null) {
throw new NullPointerException("Resource string cannot be null for non-null resource.");
}
Log.f3("<RES> Loading: " + this); Log.f3("<RES> Loading:\n " + this);
loadResource(resource); loadResource(resource);
Log.f3("<RES> Loaded: " + this + " loaded."); Log.f3("<RES> Loaded:\n " + this);
} catch (final Exception e) { } catch (final Exception e) {
loadFailed = true; loadFailed = true;
Log.e("<RES> Failed to load \"" + resource + "\"", e); Log.e("<RES> Failed to load:\n " + this, e);
} }
loadAttempted = true;
} }

@ -5,6 +5,7 @@ import java.io.IOException;
import mightypork.rogue.loading.BaseDeferredResource; import mightypork.rogue.loading.BaseDeferredResource;
import mightypork.utils.files.FileUtils; import mightypork.utils.files.FileUtils;
import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.coord.Coord; import mightypork.utils.math.coord.Coord;
import org.newdawn.slick.openal.Audio; import org.newdawn.slick.openal.Audio;
@ -16,6 +17,7 @@ import org.newdawn.slick.openal.SoundStore;
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name="Audio")
public class DeferredAudio extends BaseDeferredResource { public class DeferredAudio extends BaseDeferredResource {
private enum PlayMode private enum PlayMode

@ -2,6 +2,7 @@ package mightypork.rogue.sound;
import mightypork.rogue.loading.NullResource; import mightypork.rogue.loading.NullResource;
import mightypork.utils.logging.LoggedName;
/** /**
@ -10,6 +11,7 @@ import mightypork.rogue.loading.NullResource;
* *
* @author MightyPork * @author MightyPork
*/ */
@LoggedName(name="NullAudio")
public class NullAudio extends DeferredAudio implements NullResource { public class NullAudio extends DeferredAudio implements NullResource {
public NullAudio() { public NullAudio() {

@ -4,6 +4,7 @@ package mightypork.rogue.textures;
import mightypork.rogue.loading.BaseDeferredResource; import mightypork.rogue.loading.BaseDeferredResource;
import mightypork.rogue.loading.MustLoadInMainThread; import mightypork.rogue.loading.MustLoadInMainThread;
import mightypork.rogue.render.Render; import mightypork.rogue.render.Render;
import mightypork.utils.logging.LoggedName;
import mightypork.utils.math.coord.Rect; import mightypork.utils.math.coord.Rect;
import org.lwjgl.opengl.GL11; import org.lwjgl.opengl.GL11;
@ -16,6 +17,7 @@ import org.newdawn.slick.opengl.Texture;
* @author MightyPork * @author MightyPork
*/ */
@MustLoadInMainThread @MustLoadInMainThread
@LoggedName(name="Texture")
public class DeferredTexture extends BaseDeferredResource implements FilteredTexture { public class DeferredTexture extends BaseDeferredResource implements FilteredTexture {
private Texture backingTexture; private Texture backingTexture;

@ -203,6 +203,7 @@ final public class EventBus implements Destroyable {
} }
if (!accepted) Log.e("<bus> Not accepted by any channel: " + Log.str(event)); if (!accepted) Log.e("<bus> Not accepted by any channel: " + Log.str(event));
if (logSending && !sent) Log.w("<bus> Not delivered: " + Log.str(event));
channels.setBuffering(false); channels.setBuffering(false);
clients.setBuffering(false); clients.setBuffering(false);

@ -192,20 +192,21 @@ public class Log {
return o.toString(); return o.toString();
} else { } else {
final Class<?> cls = o.getClass(); return str(o.getClass());
final Class<?> enclosing = cls.getEnclosingClass();
return (enclosing == null ? "" : enclosing.getSimpleName() + ".") + cls.getSimpleName();
} }
} }
public static String str(Class<?> cls) public static String str(Class<?> cls)
{ {
LoggedName ln = cls.getAnnotation(LoggedName.class);
if (ln != null) {
return ln.name();
}
final Class<?> enclosing = cls.getEnclosingClass(); final Class<?> enclosing = cls.getEnclosingClass();
return (enclosing == null ? "" : enclosing.getSimpleName() + ".") + cls.getSimpleName(); return (enclosing == null ? "" : str(enclosing) + ".") + cls.getSimpleName();
} }

@ -18,6 +18,7 @@ import java.util.logging.LogRecord;
import java.util.logging.Logger; import java.util.logging.Logger;
import mightypork.utils.files.FileUtils; import mightypork.utils.files.FileUtils;
import mightypork.utils.string.StringUtils;
/** /**
@ -53,6 +54,8 @@ public class LogInstance {
private LogToSysoutMonitor sysoutMonitor; private LogToSysoutMonitor sysoutMonitor;
private long started_ms;
/** /**
* Log * Log
@ -66,6 +69,7 @@ public class LogInstance {
this.file = new File(dir, name + getSuffix()); this.file = new File(dir, name + getSuffix());
this.log_dir = dir; this.log_dir = dir;
this.logs_to_keep = oldLogCount; this.logs_to_keep = oldLogCount;
this.started_ms = System.currentTimeMillis();
init(); init();
} }
@ -344,7 +348,7 @@ public class LogInstance {
@Override @Override
public String format(LogRecord record) public String format(LogRecord record)
{ {
return LogInstance.formatMessage(record.getLevel(), record.getMessage(), record.getThrown()); return LogInstance.this.formatMessage(record.getLevel(), record.getMessage(), record.getThrown());
} }
} }
@ -358,7 +362,7 @@ public class LogInstance {
} }
private static String formatMessage(Level level, String message, Throwable throwable) private String formatMessage(Level level, String message, Throwable throwable)
{ {
final String nl = System.getProperty("line.separator"); final String nl = System.getProperty("line.separator");
@ -371,6 +375,11 @@ public class LogInstance {
message = nl + message.substring(1); message = nl + message.substring(1);
} }
long time_ms = (System.currentTimeMillis()-started_ms);
double time_s = time_ms / 1000D;
String time = String.format("%6.2f ", time_s);
String time_blank = StringUtils.repeat(" ", time.length());
String prefix = "[ ? ]"; String prefix = "[ ? ]";
if (level == Level.FINE) { if (level == Level.FINE) {
@ -387,7 +396,7 @@ public class LogInstance {
prefix = "[!W!] "; prefix = "[!W!] ";
} }
message = prefix + message.replaceAll("\n", nl + prefix) + nl; message = time + prefix + message.replaceAll("\n", nl + time_blank + prefix) + nl;
if (throwable != null) { if (throwable != null) {
message += getStackTrace(throwable); message += getStackTrace(throwable);

@ -0,0 +1,21 @@
package mightypork.utils.logging;
import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
/**
* Specify pretty name to be used when logging (<code>Log.str()</code>)
*
* @author MightyPork
*/
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface LoggedName {
String name();
}
Loading…
Cancel
Save