net.rim.device.api.system
Class EventLogger

java.lang.Object
  |
  +--net.rim.device.api.system.EventLogger

public final class EventLogger
extends Object

Logs events into the persistent store.

The event logger stores a sequence of bytes. What those bytes "mean" is determined by the viewer registered to view them. So, for instance, you can log an integer (four bytes) and register a string viewer: those four bytes will then be interpreted as four ASCII characters and displayed as such.

Events are persistent across reset. The device maintains an event queue; when the log gets too full, new events push old ones out of the front of the queue. Approximately 16 K of data can be logged until the log gets too full.

To use the event logger, you should invoke one of the register(long, java.lang.String, int) methods with your application's GUID to register your application to log events. The GUID can be created in the IDE by right-clicking a String, and selecting "Convert String to Long". You can then use the various logEvent(long, byte[], int) methods to log events in the system's event log. You can use any of the logEvent methods as appropriate to the event data to log; it doesn't need to be the same type as your intended viewer type: the logger stores all data as a simple sequence of bytes in any event.

After registering your application, you can make calls to the logEvent(long, byte[], int) to log events for that application. The logEvent(long, byte[], int) method can get passed an integer or a long integer if you are using a VIEWER_NUMBER viewer type for your application or a byte array if you are using a VIEWER_STRING or VIEWER_EXCEPTION viewer type.

For example, to pass a String to logEvent(long, byte[]), convert it to a byte array using the String class' getBytes() function:

   EventLogger.register(0x4c9d3452d87922f2L, "myApp", EventLogger.VIEWER_STRING);

   String logMessage = "An event has been logged.";

   if ( EventLogger.logEvent( 0x4c9d3452d87922f2L, logMessage.getBytes() ) ) {
       System.out.println("Log Successful!");
   }
 

Any time you catch a Throwable object, a message is automatically logged.

To be written into the log, each posted event must either have an event level equal to or lower then the current cut off point or be logged with the ALWAYS_LOG event level.

The default cut off point is WARNING. That is, by default, only events logged with WARNING, ERROR, SEVERE_ERROR, or ALWAYS_LOG are actually written in the log.

The log itself is 16 KB in size; each log entry uses 15 bytes for overhead, plus whatever space is used by the entry's actual data. Once the log meets or exceeds the 16 KB size, old entries will get erased as required to fit in the new entries.

To view the current event log for the device, hold down the ALT key and type "lglg".

Examples
To log a string, you can use code like this:

     EventLogger.logEvent( GUID, yourString.getBytes(), level );
 
The following example demonstrates how to log a numeric event:
 // Register application for event logging.
 EventLogger.register(0x9c805919833654d6L, SampleApp);
 
 // Set minimum logging level.
 EventLogger.setMinimumLEvel(EventLogger.INFORMATION);
 
 // Log a numeric event.
 EventLogger.logEvent(0x9c805919833654d6L, 12, EventLogger.INFORMATION);
 


Field Summary
static int ALWAYS_LOG
          Always log event, regardless of severity.
static int DEBUG_INFO
          Debug information level event.
static int ERROR
          Error level event.
static int INFORMATION
          Information level event.
static int SEVERE_ERROR
          Severe level event.
static long SYSTEM_LOG_GUID
          System event log GUID.
static int VIEWER_EXCEPTION
          Exception event viewer.
static int VIEWER_NUMBER
          Number event viewer.
static int VIEWER_STRING
          String event viewer.
static int WARNING
          Warning level event.
 
Method Summary
static void clearLog()
          Clears the EventLog of all events.
static int getInt(byte[] data)
          Transmogrify a byte array into an integer.
static int getMinimumLevel()
          Retrieves the current minimum logging level.
static String getRegisteredAppName(long forGUID)
          Retrieves name of application given GUID.
static int getRegisteredViewerType(long forGUID)
          Retrieves the viewer type registered for an application given GUID.
static boolean logEvent(long guid, byte[] data)
          Logs an ALWAYS_LOG level event for an application.
static boolean logEvent(long guid, byte[] data, int level)
          Logs an event for an application.
static boolean logEvent(long guid, int value)
          Logs an ALWAYS_LOG level numeric event for an application.
static boolean logEvent(long guid, int value, int level)
          Logs a numeric event for an application.
static boolean logEvent(long guid, long value, int level)
          Logs a numeric event for an application.
static boolean register(long guid, String name)
          Registers the calling application's name and guid.
static boolean register(long guid, String name, int viewerType)
          Registers the calling application's name and GUID, with a particular viewer type.
static void setMinimumLevel(int level)
          Sets the new minimum level for logging.
static boolean startEventLogViewer()
          Starts the event log viewer.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

ALWAYS_LOG

public static final int ALWAYS_LOG
Always log event, regardless of severity.

DEBUG_INFO

public static final int DEBUG_INFO
Debug information level event.

ERROR

public static final int ERROR
Error level event.

INFORMATION

public static final int INFORMATION
Information level event.

SEVERE_ERROR

public static final int SEVERE_ERROR
Severe level event.

SYSTEM_LOG_GUID

public static final long SYSTEM_LOG_GUID
System event log GUID.

Used for text messages.


VIEWER_EXCEPTION

public static final int VIEWER_EXCEPTION
Exception event viewer. This viewer interprets the logged data as the VMs special traceback data to give you a proper stack dump.

VIEWER_NUMBER

public static final int VIEWER_NUMBER
Number event viewer.

VIEWER_STRING

public static final int VIEWER_STRING
String event viewer.

WARNING

public static final int WARNING
Warning level event.
Method Detail

clearLog

public static final void clearLog()
Clears the EventLog of all events.


getInt

public static int getInt(byte[] data)
Transmogrify a byte array into an integer.

Parameters:
data - Byte array to transmogrify.
Returns:
Integer built from specified byte array.

getMinimumLevel

public static final int getMinimumLevel()
Retrieves the current minimum logging level.

Returns:
Current minimum logging level.

getRegisteredAppName

public static final String getRegisteredAppName(long forGUID)
Retrieves name of application given GUID.
Parameters:
forGUID - GUID of the registered application.
Returns:
Name for the application with GUID.

getRegisteredViewerType

public static final int getRegisteredViewerType(long forGUID)
Retrieves the viewer type registered for an application given GUID.

Parameters:
forGUID - GUID of registered application.
Returns:
Viewer type for the registered applicaiton: one of VIEWER_NUMBER, VIEWER_STRING, VIEWER_EXCEPTION.

logEvent

public static final boolean logEvent(long guid,
                                     byte[] data)
Logs an ALWAYS_LOG level event for an application.

The invoker must provide an already-registered GUID (with one of the register(long, java.lang.String, int) methods).

This method is recommended for use with GUIDs registered as viewer types VIEWER_STRING and VIEWER_EXCEPTION. The data to be logged must be a byte[].

Parameters:
guid - GUID of the application logging the event.
data - Data to log.
Returns:
True if event was successfully logged; otherwise, false (for example, if the application GUID isn't registered, or the data can't fit in the logger, or the data pointer is null).

logEvent

public static final boolean logEvent(long guid,
                                     byte[] data,
                                     int level)
Logs an event for an application.

The invoker must provide an already-registered GUID (with one of the register(long, java.lang.String, int) methods).

This method is recommended for use with GUIDs registered as viewer types VIEWER_STRING and VIEWER_EXCEPTION. The data to be logged must be a byte[].

Parameters:
guid - GUID of the application logging the event.
data - Data to log.
level - Level for this event.
Returns:
True if event was successfully logged, or if the event does not reach the minimum level for logging; otherwise, false (for example, if the application GUID isn't registered, or the data can't fit in the logger, or the data pointer is null).

logEvent

public static final boolean logEvent(long guid,
                                     int value)
Logs an ALWAYS_LOG level numeric event for an application.

The event in this case is always some integer value. The invoker must provide an already-registered GUID (with one of the register(long, java.lang.String, int) methods).

This method is recommended for use with GUIDs registered as viewer type VIEWER_NUMBER.

Parameters:
guid - GUID of the application logging the event.
value - Numeric value of the event.
Returns:
True if event was successfully logged; otherwise, false (for example, if the application GUID isn't registered, or the data can't fit in the logger).

logEvent

public static final boolean logEvent(long guid,
                                     int value,
                                     int level)
Logs a numeric event for an application.

The event in this case is always some integer value. The invoker must provide an already-registered GUID (with one of the register(long, java.lang.String, int) methods).

This method is recommended for use with GUIDs registered as viewer type VIEWER_NUMBER.

Parameters:
guid - GUID of the application logging the event.
value - Numeric value of the event.
level - Level for this event.
Returns:
True if event was logged, or if the event does not reach the minimum level; otherwise, false (if app isn't registered or data can't fit in logger).

logEvent

public static final boolean logEvent(long guid,
                                     long value,
                                     int level)
Logs a numeric event for an application.

The event in this case is always some long value. The invoker must provide an already-registered GUID (with one of the register(long, java.lang.String, int) methods).

This method is recommended for use with GUIDs registered as viewer type VIEWER_NUMBER.

Parameters:
guid - GUID of the application logging the event.
value - Numeric value of the event.
level - Level for this event.
Returns:
True if event was logged, or if the event does not reach the minimum level; otherwise, false (if app isn't registered or data can't fit in logger).
Since:
JDE 4.0.0

register

public static final boolean register(long guid,
                                     String name)
Registers the calling application's name and guid.

You must invoke this method before the logger can log events with the given GUID. This method uses the default VIEWER_NUMBER type for displaying events.

If a name/GUID has already been registered with a viewer type other than VIEWER_NUMBER, calling this method replaces it with the VIEWER_NUMBER type..

Parameters:
guid - GUID of the registering application.
name - Name of the registering application.
Returns:
True if the registration was successful. False if another name has already been registered with the specified guid.

register

public static final boolean register(long guid,
                                     String name,
                                     int viewerType)
Registers the calling application's name and GUID, with a particular viewer type.

You must invoke this method before the logger can log events with the given GUID.

If a name/GUID has already been registered with a particular viewer type, calling this method with a new viewer type replaces the previous one.

Parameters:
guid - GUID of the registering application.
name - Name of the registering application.
viewerType - Type of viewer to be used when displaying the event (one of VIEWER_NUMBER, VIEWER_STRING, VIEWER_EXCEPTION).
Returns:
True if the registration was successful. False if another name has already been registered with the specified guid.

setMinimumLevel

public static final void setMinimumLevel(int level)
Sets the new minimum level for logging.

Parameters:
level - New minimum logging level.

startEventLogViewer

public static boolean startEventLogViewer()
Starts the event log viewer.
Returns:
True If the viewer was successfully started; otherwise, false.
Since:
JDE 4.0.0



Copyright 1999-2009 Research In Motion Limited. 295 Phillip Street, Waterloo, Ontario, Canada, N2L 3W8. All Rights Reserved.
Copyright 1993-2003 Sun Microsystems, Inc. 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
Copyright 2002-2003 Nokia Corporation All Rights Reserved.
Java is a trademark or registered trademark of Sun Microsystems, Inc. in the US and other countries.