
|
October 2003 |
|
|
Watching your application's internals across the Internet, Java programmers are familiar with the standard logging feature System.out.println(text). This is useful in an emulator environment but increasingly applications are becoming Internet aware and need a way to monitor activity on target. One approach is to wrap the standard logging call into a common routine that can be directed to various destinations, all from the device itself. This code is in PersonalJava for UIQ, but a J2ME version is very similar and the changes required for this are indicated. // Example application // Flags to control logging, managed by some means in code ... public void log(String text) { if (logToFile) { // Not possible on J2ME // Utility class to POST data to a url uc.setRequestMethod("POST"); Now, all that needs to be done is to ensure all logging calls go through MyApp.log(). The flags to control logging could be set via e.g. debug menu options. HTTP POST is used rather than a datagram socket (or other means), as it is the only protocol mandated in the MIDP 1.x specification, and will be more widely compatible. The logs made to a local file open and close it each time – this is so there is something to examine in case the program exits unexpectedly. Remote logging will obviously slow down the applications operation, but used sensibly, it can give a valuable "real time" insight into the workings of an application. Any kind of logging calls can be made, for example it is very interesting to see memory usage remotely as a user moves through a menu structure: // Using the logger private final SimpleDateFormat public MyAppSub(MyApp app) { private String getFormattedDateTime() { public void actionPerformed( ActionEvent ae ) { // Code to analyse "ae" and perform various operations... ... // Then, on leaving the event handler report stats long end = System.currentTimeMillis();
public class MonitorServlet extends HttpServlet { public void doPost (HttpServletRequest req, HttpServletResponse res) } So there we have it - just remember to disable it in production code, or your users really will have "Big Brother" watching them!
| |
Copyright © 2001 - 2009 Sony Ericsson Mobile Communications AB. All Rights Reserved.