Skip to content

Commit

Permalink
Allow top-level error reporter to be customized.
Browse files Browse the repository at this point in the history
  • Loading branch information
samskivert committed Feb 17, 2014
1 parent cf73da8 commit 37ffeb5
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
6 changes: 3 additions & 3 deletions android/src/playn/android/AndroidSurfaceGL.java
Expand Up @@ -73,7 +73,7 @@ public void onSurfaceCreated() {
cachedPixels.delete();
cachedPixels = null;
} catch (IOException e) {
PlayN.platform().reportError("Error reading cached surface pixels from file.", e);
PlayN.reportError("Error reading cached surface pixels from file.", e);
}
}
}
Expand All @@ -93,12 +93,12 @@ public void onSurfaceLost() {
out.write(pixelBuffer.array());
out.close();
} catch (IOException e) {
PlayN.platform().reportError("IOException writing cached Surface to file.", e);
PlayN.reportError("IOException writing cached Surface to file.", e);
cachedPixels = null;
}
pixelBuffer = null;
} catch (OutOfMemoryError e) {
PlayN.platform().reportError("OutOfMemoryError reading cached Surface to buffer.", e);
PlayN.reportError("OutOfMemoryError reading cached Surface to buffer.", e);
cachedPixels = null;
}
clearTexture();
Expand Down
14 changes: 13 additions & 1 deletion core/src/playn/core/AbstractPlatform.java
Expand Up @@ -23,14 +23,21 @@
*/
public abstract class AbstractPlatform implements Platform {

protected final PlayN.ErrorReporter DEFAULT_REPORTER = new PlayN.ErrorReporter() {
public void reportError(String message, Throwable err) {
log.warn(message, err);
}
};

protected final RunQueue runQueue;
protected final Log log;

private PlayN.LifecycleListener lifecycleListener;
private PlayN.ErrorReporter errorReporter;

@Override
public void reportError(String message, Throwable err) {
log.warn(message, err);
errorReporter.reportError(message, err);
}

@Override
Expand All @@ -43,6 +50,11 @@ public void setLifecycleListener(PlayN.LifecycleListener listener) {
lifecycleListener = listener;
}

@Override
public void setErrorReporter(PlayN.ErrorReporter reporter) {
errorReporter = (reporter == null) ? DEFAULT_REPORTER : reporter;
}

@Override
public Log log() {
return log;
Expand Down
4 changes: 2 additions & 2 deletions core/src/playn/core/Dispatcher.java
Expand Up @@ -162,8 +162,8 @@ static <L, E extends Input.Impl> void tryInteract (AbstractLayer layer,
try {
layer.interact(listenerType, interaction, event);
} catch (Throwable t) {
PlayN.platform().reportError("Interaction failure [layer=" + layer + ", iact=" + interaction +
", event=" + event + "]", t);
PlayN.reportError("Interaction failure [layer=" + layer + ", iact=" + interaction +
", event=" + event + "]", t);
}
}

Expand Down
11 changes: 4 additions & 7 deletions core/src/playn/core/Platform.java
Expand Up @@ -22,16 +22,11 @@ public interface Platform {

enum Type { JAVA, HTML, ANDROID, IOS, FLASH, STUB }

/**
* Called when a backend (or other framework code) encounters an exception that it can recover
* from, but which it would like to report in some orderly fashion. <em>NOTE:</em> this method
* may be called from threads other than the main PlayN thread.
*/
void reportError(String message, Throwable cause);
Platform.Type type();

void run(Game game);

Platform.Type type();
void reportError(String message, Throwable cause);

double time();

Expand All @@ -45,6 +40,8 @@ enum Type { JAVA, HTML, ANDROID, IOS, FLASH, STUB }

void setLifecycleListener(PlayN.LifecycleListener listener);

void setErrorReporter(PlayN.ErrorReporter reporter);

void setPropagateEvents(boolean propagate);

Audio audio();
Expand Down
45 changes: 38 additions & 7 deletions core/src/playn/core/PlayN.java
Expand Up @@ -38,13 +38,15 @@ public static interface LifecycleListener {
void onExit();
}

/**
* Call this method to start your {@link Game}. It must be called only once,
* and all work after this call is made will be performed in {@link Game}'s
* callback methods.
*/
public static void run(Game game) {
platform.run(game);
/** Used to customize top-level error reporting. See {@link #setErrorReporter}. */
public static interface ErrorReporter {
/** Called to report a top-level error. This is called by internal PlayN backend code when it
* encounters an error from which it can recover, but which it would like to report in an
* orderly fashion. <em>NOTE:</em> this method may be called from any thread, not just the main
* PlayN thread. So an implementation must take care to get back onto the PlayN thread if it's
* going to do anything other than turn around and log the message.
*/
void reportError(String message, Throwable err);
}

/**
Expand All @@ -63,6 +65,24 @@ public static Platform.Type platformType() {
return platform.type();
}

/**
* Call this method to start your {@link Game}. It must be called only once,
* and all work after this call is made will be performed in {@link Game}'s
* callback methods.
*/
public static void run(Game game) {
platform.run(game);
}

/**
* Called when a backend (or other framework code) encounters an exception that it can recover
* from, but which it would like to report in some orderly fashion. <em>NOTE:</em> this method
* may be called from threads other than the main PlayN thread.
*/
public static void reportError(String message, Throwable err) {
platform.reportError(message, err);
}

/**
* Returns the current time, as a double value in millis since January 1, 1970, 00:00:00 GMT.
*
Expand Down Expand Up @@ -111,6 +131,17 @@ public static void setLifecycleListener(LifecycleListener listener) {
platform.setLifecycleListener(listener);
}

/**
* Configures the top-level error reporter. Any previous reporter will be overwritten. Supply
* null to revert to the default error reporter. <em>NOTE:</em> the error reporter may be called
* from any thread, not just the main PlayN thread. So an implementation must take care to get
* back onto the PlayN thread if it's going to do anything other than turn around and log the
* message.
*/
public static void setErrorReporter(ErrorReporter reporter) {
platform.setErrorReporter(reporter);
}

/**
* Configures whether events on a layer (pointer, touch and mouse) are automatically dispatched
* to the layer's ancestors. This is a global option that may affect performance, so applications
Expand Down

0 comments on commit 37ffeb5

Please sign in to comment.