Annotation for logged name, added time in log.
This commit is contained in:
@@ -4,6 +4,7 @@ package mightypork.rogue;
|
||||
import java.io.File;
|
||||
import java.io.RandomAccessFile;
|
||||
import java.nio.channels.FileLock;
|
||||
import java.util.Locale;
|
||||
import java.util.logging.Level;
|
||||
|
||||
import javax.swing.JOptionPane;
|
||||
@@ -116,6 +117,9 @@ public class App implements AppAccess {
|
||||
|
||||
public void initialize()
|
||||
{
|
||||
// to get dot instead of comma in floats
|
||||
Locale.setDefault(Locale.ENGLISH);
|
||||
|
||||
/*
|
||||
* Lock working directory
|
||||
*/
|
||||
@@ -138,6 +142,7 @@ public class App implements AppAccess {
|
||||
*/
|
||||
Log.f2("Initializing Event Bus...");
|
||||
eventBus = new EventBus();
|
||||
eventBus.logSending = true;
|
||||
initChannels();
|
||||
|
||||
/*
|
||||
|
||||
@@ -9,6 +9,7 @@ import java.io.InputStream;
|
||||
import mightypork.rogue.loading.BaseDeferredResource;
|
||||
import mightypork.rogue.loading.MustLoadInMainThread;
|
||||
import mightypork.utils.files.FileUtils;
|
||||
import mightypork.utils.logging.LoggedName;
|
||||
import mightypork.utils.math.color.RGB;
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
|
||||
@@ -19,6 +20,7 @@ import mightypork.utils.math.coord.Coord;
|
||||
* @author MightyPork
|
||||
*/
|
||||
@MustLoadInMainThread
|
||||
@LoggedName(name="Font")
|
||||
public class DeferredFont extends BaseDeferredResource implements GLFont {
|
||||
|
||||
public static enum FontStyle
|
||||
|
||||
@@ -5,12 +5,15 @@ import java.awt.Font;
|
||||
import java.awt.FontFormatException;
|
||||
import java.io.IOException;
|
||||
|
||||
import mightypork.utils.logging.LoggedName;
|
||||
|
||||
|
||||
/**
|
||||
* Font obtained from the OS
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
@LoggedName(name="FontNative")
|
||||
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
|
||||
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() {
|
||||
|
||||
@Override
|
||||
public void run()
|
||||
{
|
||||
System.out.println("~~");
|
||||
def.load();
|
||||
}
|
||||
}));
|
||||
|
||||
@@ -3,6 +3,7 @@ package mightypork.rogue.loading;
|
||||
|
||||
import mightypork.utils.control.interf.Destroyable;
|
||||
import mightypork.utils.logging.Log;
|
||||
import mightypork.utils.logging.LoggedName;
|
||||
|
||||
|
||||
/**
|
||||
@@ -12,6 +13,7 @@ import mightypork.utils.logging.Log;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
@LoggedName(name = "Resource")
|
||||
public abstract class BaseDeferredResource implements DeferredResource, Destroyable {
|
||||
|
||||
private final String resource;
|
||||
@@ -27,23 +29,28 @@ public abstract class BaseDeferredResource implements DeferredResource, Destroya
|
||||
@Override
|
||||
public synchronized final void load()
|
||||
{
|
||||
if (loadAttempted) return;
|
||||
if (loadAttempted) {
|
||||
Log.w("<RES> Already loaded @ load():\n " + this);
|
||||
return;
|
||||
}
|
||||
|
||||
loadAttempted = true;
|
||||
|
||||
loadFailed = false;
|
||||
|
||||
if (isNull()) return;
|
||||
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);
|
||||
Log.f3("<RES> Loaded: " + this + " loaded.");
|
||||
Log.f3("<RES> Loaded:\n " + this);
|
||||
} catch (final Exception e) {
|
||||
loadFailed = true;
|
||||
Log.e("<RES> Failed to load \"" + resource + "\"", e);
|
||||
Log.e("<RES> Failed to load:\n " + this, e);
|
||||
}
|
||||
|
||||
loadAttempted = true;
|
||||
}
|
||||
|
||||
|
||||
@@ -68,7 +75,7 @@ public abstract class BaseDeferredResource implements DeferredResource, Destroya
|
||||
if (isLoaded()) {
|
||||
return true;
|
||||
} else {
|
||||
Log.w("<RES> First use, not loaded yet - loading directly\n"+this);
|
||||
Log.w("<RES> First use, not loaded yet - loading directly\n " + this);
|
||||
load();
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@ import java.io.IOException;
|
||||
|
||||
import mightypork.rogue.loading.BaseDeferredResource;
|
||||
import mightypork.utils.files.FileUtils;
|
||||
import mightypork.utils.logging.LoggedName;
|
||||
import mightypork.utils.math.coord.Coord;
|
||||
|
||||
import org.newdawn.slick.openal.Audio;
|
||||
@@ -16,6 +17,7 @@ import org.newdawn.slick.openal.SoundStore;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
@LoggedName(name="Audio")
|
||||
public class DeferredAudio extends BaseDeferredResource {
|
||||
|
||||
private enum PlayMode
|
||||
|
||||
@@ -2,6 +2,7 @@ package mightypork.rogue.sound;
|
||||
|
||||
|
||||
import mightypork.rogue.loading.NullResource;
|
||||
import mightypork.utils.logging.LoggedName;
|
||||
|
||||
|
||||
/**
|
||||
@@ -10,6 +11,7 @@ import mightypork.rogue.loading.NullResource;
|
||||
*
|
||||
* @author MightyPork
|
||||
*/
|
||||
@LoggedName(name="NullAudio")
|
||||
public class NullAudio extends DeferredAudio implements NullResource {
|
||||
|
||||
public NullAudio() {
|
||||
|
||||
@@ -4,6 +4,7 @@ package mightypork.rogue.textures;
|
||||
import mightypork.rogue.loading.BaseDeferredResource;
|
||||
import mightypork.rogue.loading.MustLoadInMainThread;
|
||||
import mightypork.rogue.render.Render;
|
||||
import mightypork.utils.logging.LoggedName;
|
||||
import mightypork.utils.math.coord.Rect;
|
||||
|
||||
import org.lwjgl.opengl.GL11;
|
||||
@@ -16,6 +17,7 @@ import org.newdawn.slick.opengl.Texture;
|
||||
* @author MightyPork
|
||||
*/
|
||||
@MustLoadInMainThread
|
||||
@LoggedName(name="Texture")
|
||||
public class DeferredTexture extends BaseDeferredResource implements FilteredTexture {
|
||||
|
||||
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 (logSending && !sent) Log.w("<bus> Not delivered: " + Log.str(event));
|
||||
|
||||
channels.setBuffering(false);
|
||||
clients.setBuffering(false);
|
||||
|
||||
@@ -192,20 +192,21 @@ public class Log {
|
||||
return o.toString();
|
||||
} else {
|
||||
|
||||
final Class<?> cls = o.getClass();
|
||||
|
||||
final Class<?> enclosing = cls.getEnclosingClass();
|
||||
|
||||
return (enclosing == null ? "" : enclosing.getSimpleName() + ".") + cls.getSimpleName();
|
||||
return str(o.getClass());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static String str(Class<?> cls)
|
||||
{
|
||||
LoggedName ln = cls.getAnnotation(LoggedName.class);
|
||||
if (ln != null) {
|
||||
return ln.name();
|
||||
}
|
||||
|
||||
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 mightypork.utils.files.FileUtils;
|
||||
import mightypork.utils.string.StringUtils;
|
||||
|
||||
|
||||
/**
|
||||
@@ -53,6 +54,8 @@ public class LogInstance {
|
||||
|
||||
private LogToSysoutMonitor sysoutMonitor;
|
||||
|
||||
private long started_ms;
|
||||
|
||||
|
||||
/**
|
||||
* Log
|
||||
@@ -66,6 +69,7 @@ public class LogInstance {
|
||||
this.file = new File(dir, name + getSuffix());
|
||||
this.log_dir = dir;
|
||||
this.logs_to_keep = oldLogCount;
|
||||
this.started_ms = System.currentTimeMillis();
|
||||
|
||||
init();
|
||||
}
|
||||
@@ -344,7 +348,7 @@ public class LogInstance {
|
||||
@Override
|
||||
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");
|
||||
@@ -371,6 +375,11 @@ public class LogInstance {
|
||||
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 = "[ ? ]";
|
||||
|
||||
if (level == Level.FINE) {
|
||||
@@ -387,7 +396,7 @@ public class LogInstance {
|
||||
prefix = "[!W!] ";
|
||||
}
|
||||
|
||||
message = prefix + message.replaceAll("\n", nl + prefix) + nl;
|
||||
message = time + prefix + message.replaceAll("\n", nl + time_blank + prefix) + nl;
|
||||
|
||||
if (throwable != null) {
|
||||
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();
|
||||
}
|
||||
Reference in New Issue
Block a user