LWL (Light weight logger).

Release Features 

1.       Logger has very less number of classes.

2.       Easily configurable Logging levels.

3.       J2ME LWL view can easily differentiate logs on the bases of Logging levels.

4.       J2me LWL can ignore logs on the bases of logging levels.

5.       J2me LWL can be easily switched off or switched on.

Logging API

Logging Levels Interface

Logging levels is nothing but a log differentiating parameters.

*     INFO       : General Information kind of log.

*     SEVERE     : ERROR or Exception LOG.

*      WARNING   : WARNINGS App Developer can use only when there is a problem which can

                     e.g. for using levels LoggerLevels.INFO 

Logging Class

1.       Static Method for setting J2ME LWL configuration , this method can  be called anytime before calling logging function, configureLog method has four parameters as follows,

LOG_INFO_BOOL_PARAM à   Boolean parameter for configuring informative logs.

 LOG_SEVERE_PARAM à Boolean parameter for configuring SEVERE kind of problems for e.g. Exceptions and ERRORS.

LOG_WARNING_PARAMà Boolean parameter for configuring a warning kind of logs.

NO_OF_LOG_ENTRIES à For configuring upper limit of log.

LOGGING_ON_SYS_OUT à for logging on system output. public static void configureLog(boolean LOG_INFO_BOOL_PARAM ,boolean  LOG_SEVERE_PARAM ,boolean LOG_WARNING_PARAM, int NO_OF_LOG_ENTRIES)

                e.g. of calling the method

                LogUtil.configureLog(true,true,true,10);

2.       j2me LWL Method for logging, This method has three parameters they are as follows,

a.       logEntry à String value of Log.

b.      logType à Logging level accepts integer constants values given in LoggerLevels interface. e.g. LoggerLevels.INFO

c.       clear à for keeping one log at a time.

public static void log (String logentry, int logType, boolean clear); e.g.        LogUtil.log(“FirstLogEntry”,LoggerLevel.INFO,false);3.       J2me LWL method for closing the record store the data base of LWL. This method has no parameters. This should be called for closing the database.Public static void freeLoggerUtils ();LogUtil.freeLoggerUtils(); 

Log Viewing Utility

This utility can be used for viewing logs , to use the logging utility application has to do following tasks, The tasks are as follows.

1.       While building an application integrate J2meLWL.jar in lib directory.

2.       Once application is build put an entry in .jad file of application i.e.

a.       Midlet-n: Logger,, com.Logger.ViewLog

________________________________________________________________________________

Please Click on More to View Code …

 

________________________________________________________________________________

                                       ********       LogUtil.java     *********

________________________________________________________________________________

 

package com.logger;

import javax.microedition.lcdui.Form;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;

public class LogUtil implements LoggerLevels{

 private static RecordStore rs;

/**
* Configuration booleans
* Description:
*
* ON the bases of following booleans app developer can Save Logs.
**/

/** Enable or Disable INFO logs Default value is enabled i.e. true*/
static boolean LOG_INFO_BOOL = true;

/** Enable or Disable SEVERE logs Default value is enabled i.e. true*/
static boolean LOG_SEVERE = true;

/** Enable or Disable WARNING logs Default value is enabled i.e. true*/
static boolean LOG_WARNING = true;

 /**Default Log value is 10*/
private static int NO_OF_LOGS = 10;

/** Tracing on a System*/
static boolean isTracing = false;

/**
* Configure the type of logging.
**/
public static void configureLog(boolean LOG_INFO_BOOL_PARAM, boolean LOG_SEVERE_PARAM, boolean LOG_WARNING_PARAM, int NO_OF_LOGS_ENTRIES, boolean loggingOnSysOut){

LOG_INFO_BOOL = LOG_INFO_BOOL_PARAM;
LOG_SEVERE = LOG_SEVERE_PARAM;
LOG_WARNING = LOG_WARNING_PARAM;
NO_OF_LOGS = NO_OF_LOGS_ENTRIES;
isTracing = loggingOnSysOut;

}

static boolean doLogging(int logType){
// comment this line to have logging enabled
switch(logType){
case LoggerLevels.INFO:
if(LOG_INFO_BOOL == false){
return false;
}
break;
case LoggerLevels.SEVERE:
if(LOG_SEVERE == false){
return false;
}
break;
case LoggerLevels.WARNING:
if(LOG_WARNING == false){
return false;
}
break;
}
return true;
}

public static void log(String str, int logType,boolean clear)
{
int id = 0;
int totalRecs = 0;
RecordEnumeration en = null;

// Tracing on a System out.
if(isTracing)
System.out.println(str);

if(!doLogging(logType))
return;

// check if RMS “LOG Storage” is open or not
if(!openRMS())
return;

try
{
en = rs.enumerateRecords(null, null, false);
totalRecs = en.numRecords();
int smallestRecord = 0;

if(clear)
{
for(int i = 0; i < totalRecs; i++)
{
id = en.nextRecordId();
rs.deleteRecord(id);
}
en = null;
}else if(totalRecs == NO_OF_LOGS){
smallestRecord =  en.nextRecordId();
while(en.hasNextElement()){
id = en.nextRecordId();
//  System.out.println(” traversing record >> “+id);
if(smallestRecord > id)
smallestRecord =  id;
}
rs.deleteRecord(smallestRecord);
//  System.out.println(”Deleted Record id is >> “+smallestRecord);
id = rs.addRecord(str.getBytes(), 0, str.length());
// System.out.println(”Added Record id is >> “+id);
en = null;
rs = null;
}else{
id = rs.addRecord(str.getBytes(), 0, str.length());
// System.out.println(” Record ID is max < 5 –> –> “+id);
}
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}

/*just open the Logger if RMS is not opened.*/
static boolean openRMS()
{
if( rs == null)
{
try
{
rs = RecordStore.openRecordStore(”UtilTraceDB”, true);
return true;
}
catch(Exception E)
{
System.out.println(E);
E.printStackTrace();
return false;
}
}
return true;
}

public static void freeLoggerUtil()
{
if(rs != null)
{
try
{
rs.closeRecordStore();
rs = null;
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}
}
}

________________________________________________________________________________

                                       ********       LoggerLevels.java     *********

________________________________________________________________________________

package com.logger;

/**
* Logger Level
*/

/**
* @author Amit.Chabra
*/
public interface LoggerLevels {
/**  Loging Levels
*
*   Description:
*
*   While Logging Application programer needs to specify which type of log
*   Is that. Log is basically devided in following categoreies.
*
*  INFO –> Gernal Information kind of log.
*  SEVERE –> ERROR or Exception LOG.
*  WARNING –> WARNINGS App Developer can use only when there is a problem which can
*  harm the running application, but there is not current thought
**/
public static final int INFO  = 0;
public  static final int SEVERE = 1;
public static final int WARNING = 2;
}

 

________________________________________________________________________________

                                       ********       ViewLog.java     *********

________________________________________________________________________________

package com.logger;

/*
* UtilTraceMIDlet.java
* Created on May 2, 2007, 3:10 PM
*/

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;

/**
* Helper MIDlet which displays the logs.
* @author  AmitChabra
* @version 1.0
*/
public class ViewLog extends javax.microedition.midlet.MIDlet implements javax.microedition.lcdui.CommandListener {

private Command exitCommand,eraseCommand; // The exit command
private Display display;    // The display for this MIDlet
private RecordStore rs;
private Form form;

public ViewLog() {
try
{
String str = new String(”This Record is Empty. Please use this as debugger in J2ME applications. use method UtilTrace.Trace(String, boolean) for logging of statements.”);
display = Display.getDisplay(this);
rs = RecordStore.openRecordStore(”UtilTraceDB”, true);
exitCommand = new Command(”Exit”, Command.SCREEN, 2);
eraseCommand = new Command(”Erase”, Command.OK, 1);
}
catch(Exception e)
{
e.printStackTrace();
}
}

/**
* Start up the Hello MIDlet by creating the TextBox and associating
* the exit command and listener.
*/
public void startApp() {
byte [] buf = null;
int id = 1;
String str = new String();
form = new Form(”Util Trace”);
try
{
RecordEnumeration en = rs.enumerateRecords(null, null, false);
int max = en.numRecords();
while(en.hasNextElement())
{
id = en.nextRecordId();
}
buf = rs.getRecord(id);
str += (new String(buf) + ‘\n’);
while(en.hasPreviousElement())
{
buf = en.previousRecord();
str += (new String(buf) + ‘\n’);
}
en.destroy();
}
catch(Exception e)
{
}
form.append(str);
form.addCommand(exitCommand);
form.addCommand(eraseCommand);
form.setCommandListener(this);

display.setCurrent(form);
}

/**
* Pause is a no-op since there are no background activities or
* record stores that need to be closed.
*/
public void pauseApp() {
}

/**
* Destroy must cleanup everything not handled by the garbage collector.
* In this case there is nothing to cleanup.
*/
public void destroyApp(boolean unconditional) {
LogUtil.freeLoggerUtil();
}

/*
* Respond to commands, including exit
* On the exit command, cleanup and notify that the MIDlet has been destroyed.
*/
public void commandAction(Command c, Displayable s) {
if (c == exitCommand) {
destroyApp(false);
notifyDestroyed();
}
else
{
EraseRecStore();
destroyApp(false);
notifyDestroyed();
}
}

 public void EraseRecStore()
{
int id;
if(!LogUtil.openRMS())
return;
try
{
{
RecordEnumeration en = rs.enumerateRecords(null, null, false);
int max = en.numRecords();
for(int i = 0; i < max; i++)
{
id = en.nextRecordId();
rs.deleteRecord(id);
}
}
}
catch(Exception e)
{
System.out.println(e);
e.printStackTrace();
}
}

}