`
wayfarer
  • 浏览: 296683 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

mobile日志

阅读更多

 

1.项目中的实际应用

这个demo用到了JSR75 - FileConnection Optional Package。直接看代码:


(1)File类

package cn.navi.util;

import java.io.IOException;
import java.io.OutputStream;
import javax.microedition.io.Connector;
import javax.microedition.io.file.FileConnection;

public class File {
    public static void writeFile(String content) {
        if(System.getProperty("microedition.io.file.FileConnection.version") != null ) { // file.separator
        	String url = "file:///root1/data.txt"; // sun DefaultColorPhone
//        	String url = "file://localhost/E:/data.txt"; // nokia phone
        	
            FileConnection fconn = null;
            try {
                fconn = (FileConnection) Connector.open(url, Connector.READ_WRITE);
                if (!fconn.exists()) {
                    fconn.create();
                    if (!fconn.canWrite()) fconn.setWritable(true);
                    OutputStream out = fconn.openOutputStream();
                    out.write(content.trim().getBytes("utf-8"));
                    out.flush();
                    out.close();
                } else {
                	if (!fconn.canWrite()) fconn.setWritable(true);
                    OutputStream out = fconn.openOutputStream();
                    out.write(content.trim().getBytes("utf-8"));
                    out.flush();
                    out.close();
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } finally {
            	if (fconn != null) {
            		try {
    					fconn.close();
    				} catch (IOException e) {
    					e.printStackTrace();
    				}
    				fconn = null;
            	}
            }
        }
    }
}
 

 

(2)LogManager类

package cn.navi.util;

public final class LogManager {
    private static Logger log = new LoggerImpl();

    /**
     * 安装自己实现的日志记录器/显示器
     * @param log 新的日志记录器/显示器
     */
    public static void install(Logger log) {
        LogManager.log = log;
    }

    /**
     * 记录OFF/FATAL/ERROR/WARN/INFO/DEBUG/TRACE/ALL级别的日志
     * @param 日志内容
     */
    public static void off(String off) {
        log.log(Logger.OFF, off);
    }
    public static void fatal(String fatal) {
        log.log(Logger.FATAL, fatal);
    }
    public static void error(String error) {
        log.log(Logger.ERROR, error);
    }
    public static void warn(String warn) {
        log.log(Logger.WARN, warn);
    }
    public static void info(String info) {
        log.log(Logger.INFO, info);
    }
    public static void debug(String debug) {
        log.log(Logger.DEBUG, debug);
    }
    public static void trace(String trace) {
        log.log(Logger.TRACE, trace);
    }
    public static void all(String all) {
        log.log(Logger.ALL, all);
    }

    public static void clearLog() {
        log.clearLog();
    }
    public static String getLogContent() {
    	return log.getLogContent();
    }
    
    static class LoggerImpl implements Logger {
        private StringBuffer sb;
        public LoggerImpl() {
            sb = new StringBuffer();
        }
        public void log(int level, String content) {
            sb.append(getLevelName(level)).append(":").append(content).append("\n");
        }

        private String getLevelName(int level) {
            switch (level) {
                case OFF:
                    return "OFF";
                case FATAL:
                    return "FATAL";
                case ERROR:
                    return "ERROR";
                case WARN:
                    return "WARN";
                case INFO:
                    return "INFO";
                case DEBUG:
                    return "DEBUG";
                case TRACE:
                    return "TRACE";
                case ALL:
                    return "ALL";
                default:
                    return "UNKNOWN";
            }
        }
        public String getLogContent() {
            return sb.toString();
        }

        public void clearLog() {
            sb.delete(0, sb.length());
        }

    }
}

interface Logger {
    public static final int OFF = 70, // 最高等级的,用于关闭所有日志记录
                            FATAL = 60,
                            ERROR = 50,
                            WARN = 40,
                            INFO = 30,
                            DEBUG = 20,
                            TRACE = 10,
                            ALL = 0; // 最低等级的,用于打开所有日志记录

    /**
     * 实现的log方法
     * @param level 级别
     * @param info 内容
     */
    public void log(int level, String content);

    /**
     * 得到所有的日志内容
     * @return 内容
     */
    public String getLogContent();

    /**
     * 清除当前的日志
     */
    public void clearLog();
}
 

(3)使用方法

LogManager.debug("系统出现Debug");
LogManager.error("系统出现Error");
LogManager.fatal("系统出现Fatal");
LogManager.warn("系统出现Warn");
File.writeFile(LogManager.getLogContent());

 

 

2. 参考Demo

package forrest.logger;

/**
 * 一个用于显示日志的接口,此接口用于LogManager来调用
 * 日志显示器,因为日志记录了之后,肯定是要被我们显示出来的,想要如何显示,可以实现此接口.
 * @author Forrest.He
 */
public interface LogShower {

    /**
     * 显示日志,由LogManager调用此方法来显示日志
     * 显示日志可以有多种方法,比如可以用列表来显示
     * 也可以用TextArea来显示,还可以用Canvas来显示
     * @param logContent 日志内容
     * @param action 返回的时候要做的动作
     */
    public void showLog(String logContent, BackAction action);

     /**
     * 内部的一个静态接口,实现此接口以供LogShower在点击了返回之后,要做的事情
     */
    public static interface BackAction {

        /**
         * 点击返回之后要做的事情
         */
        public void back();
    }
}


package forrest.logger;

/**
 * 一个日志生成器要实现的接口, 提供了日志的记录器所要做的一些事情.
 * @author forrest.he
 */
public interface Logger {
    public static final int OFF = 70, // 最高等级的,用于关闭所有日志记录
                            FATAL = 60,
                            ERROR = 50,
                            WARN = 40,
                            INFO = 30,
                            DEBUG = 20,
                            TRACE = 10,
                            ALL = 0; // 最低等级的,用于打开所有日志记录

    /**
     * 实现的log方法
     * @param level 级别
     * @param info 内容
     */
    public void log(int level, String content);

    /**
     * 得到所有的日志内容
     * @return 内容
     */
    public String getLogContent();

    /**
     * 清除当前的日志
     */
    public void clearLog();
}
 

package forrest.logger;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;
import forrest.logger.LogShower.BackAction;

public final class LogManager extends MIDlet {

    private static Logger log = new LoggerImpl();
    private static LogShower shower = new LogShowerImpl();
    protected static Display display;

    //midlet的构造函数要被系统调用的,如果设为private在这一步就出错了。
    //在运行midlet 的时候,实实上运行的是wtk的main函数,然后层层调用,调用了你了midlet的构造函数。以及startApp函数。

    public LogManager() {
        display = Display.getDisplay(this);
    }

    /**
     * 安装自己实现的日志记录器/显示器
     * @param log 新的日志记录器/显示器
     */
    public static void install(Logger log) {
        LogManager.log = log;
    }
    public static void install(LogShower shower) {
        LogManager.shower = shower;
    }

    /**
     * 记录OFF/FATAL/ERROR/WARN/INFO/DEBUG/TRACE/ALL级别的日志
     * @param 日志内容
     */
    public static void off(String off) {
        log.log(Logger.OFF, off);
    }
    public static void fatal(String fatal) {
        log.log(Logger.FATAL, fatal);
    }
    public static void error(String error) {
        log.log(Logger.ERROR, error);
    }
    public static void warn(String warn) {
        log.log(Logger.WARN, warn);
    }
    public static void info(String info) {
        log.log(Logger.INFO, info);
    }
    public static void debug(String debug) {
        log.log(Logger.DEBUG, debug);
    }
    public static void trace(String trace) {
        log.log(Logger.TRACE, trace);
    }
    public static void all(String all) {
        log.log(Logger.ALL, all);
    }

    /**
     * 显示当前日志管理器的日志
     * @param back 要返回的时候,做的动作
     */
    public static void showLog(BackAction back) {
        shower.showLog(log.getLogContent(), back);
    }

    /**
     * 清除当前日志管理器的日志
     */
    public static void clearLog() {
        log.clearLog();
    }

    protected void startApp() throws MIDletStateChangeException {
        off("off");
        fatal("fatal致命错误");
        error("error错误");
        warn("warn警告");
        info("info信息");
        debug("debug");
        trace("trace");
        all("all");
        LogManager.showLog(new BackAction() {
            public void back() {
                clearLog();
                try {
                    destroyApp(true);
                    notifyDestroyed();
                } catch (MIDletStateChangeException ex) {
                    ex.printStackTrace();
                }
            }
            
        });
    }

    protected void pauseApp() {
    }
    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
    }

    static class LogShowerImpl implements LogShower, CommandListener {
        private BackAction action;
        private Command backCommand, clearCommand;
        private TextBox ta;
        public void showLog(String logContent, final BackAction action) {
            this.action = action;
            backCommand = new Command("Back", Command.BACK, 1);
            clearCommand = new Command("Clear", Command.CANCEL, 1);
            ta = new TextBox("", logContent, 100, TextField.UNEDITABLE);
            ta.addCommand(backCommand);
            ta.addCommand(clearCommand);
            ta.setCommandListener(this);
            display.setCurrent(ta);
        }

        public void commandAction(Command c, Displayable d) {
            if (c == backCommand) {
                action.back();
            } else if (c == clearCommand) {
                clearLog();
                ta.setString(null);
            }
        }
    }
    static class LoggerImpl implements Logger {
        private StringBuffer sb;
        public LoggerImpl() {
            sb = new StringBuffer(1024);
        }
        public void log(int level, String content) {
//            sb.append(getPrefix()).append("\n").append(getLevelName(level)).append(":").append(content).append("\n");
            sb.append(getLevelName(level)).append(":").append(content).append("\n");
        }

        private String getPrefix() {
            return "[" + Thread.currentThread() + "-" + System.currentTimeMillis() + "]";
        }
        private String getLevelName(int level) {
            switch (level) {
                case OFF:
                    return "OFF";
                case FATAL:
                    return "FATAL";
                case ERROR:
                    return "ERROR";
                case WARN:
                    return "WARN";
                case INFO:
                    return "INFO";
                case DEBUG:
                    return "DEBUG";
                case TRACE:
                    return "TRACE";
                case ALL:
                    return "ALL";
                default:
                    return "UNKNOWN";
            }
        }
        public String getLogContent() {
            return sb.toString();
        }

        public void clearLog() {
            sb.delete(0, sb.length());
        }
    }
}
 
分享到:
评论

相关推荐

    启动日志WINDOWS MOBILE

    启动日志WINDOWS MOBILE,可以记录手机开机的时间还有关机的时间

    Kubernetes Ingress日志分析最佳实践.pptx

    同时,Mobile & Web也可以提供实时的日志分析和监控,以便快速发现和解决问题。 Camera Camera是Kubernetes Ingress日志分析的重要组件之一。Camera可以提供详细的日志信息,以便更好地了解应用程序的运行情况。...

    ios wifi 调试日志抓取

    ### iOS WiFi调试日志抓取知识点详解 #### 一、问题背景及应用场景 在iOS设备上遇到Wi-Fi连接问题时,通常需要收集一系列的日志文件以便进行深入分析和故障排除。这些日志包括但不限于Wi-Fi日志、设备控制台日志...

    Mobile Atlas Creator 2.1.1a.zip

    9. **CHANGELOG.txt**:变更日志文件,记录了软件从一个版本到另一个版本的改动和更新,帮助用户了解新功能和修复的问题。 10. **gpl.txt**:这是 GNU 通用公共许可证的文本,表明 Mobile Atlas Creator 是遵循 GPL...

    SQL Server Mobile数据库开发

    6. **错误处理和日志记录**:为了确保系统的稳定性和可维护性,开发者应实现适当的错误处理机制,并记录数据库操作日志。 总的来说,“SQL Server Mobile数据库开发”涉及从数据库设计、编程、调试到性能优化的整个...

    mobile-angular-ui-1.3.3

    "mobile-angular-ui-1.3.3" 是一个基于AngularJS和Bootstrap的...需要注意的是,对于具体版本的1.3.3,开发者应查阅当时官方的更新日志,了解该版本相较于之前版本的改进和修复,以充分利用其新特性并避免已知问题。

    windows mobile控制台插件

    4. **系统调试工具**:对于开发者而言,控制台插件是调试移动应用的重要工具,因为它允许他们查看系统日志、跟踪内存使用情况、检查网络连接等。 5. **第三方工具集成**:许多控制台插件支持安装和使用第三方命令行...

    PockePc QQ例子 windows mobile

    《Windows Mobile上的Pocket PC QQ应用探索》 在移动通信领域,Windows Mobile操作系统曾一度占据了重要的地位,尤其是在智能手机初期。为了满足用户在移动设备上进行即时通讯的需求,开发人员设计出了适用于...

    WindowsMobile5.0三十几个经典手机软件开发源码WindowsMobile5.0

    7. 错误处理和调试:学习源码中的错误处理机制,如异常处理和日志记录,可以帮助开发者更好地调试和优化程序。 通过深入研究这些源码,开发者不仅可以掌握Windows Mobile 5.0的编程技巧,还能了解到软件设计的最佳...

    苹果手机ios抓取蓝牙HCI日志

    在探讨如何在iOS设备上抓取蓝牙HCI(Host Controller Interface)日志时,我们会涉及一系列重要的知识点,这些知识点主要包括了操作系统对蓝牙设备的日志抓取机制、使用Wireshark分析数据的技巧,以及在不同操作系统...

    java 日志的数据脱敏的实现方法

    Java日志数据脱敏是为了确保在记录日志时,敏感信息不会被泄露,从而保护用户隐私和企业数据安全。在本文中,我们将探讨如何在Java应用程序中实现这一目标。 首先,理解数据脱敏的重要性至关重要。在处理包含敏感...

    ios获取崩溃日志方法

    iOS 崩溃日志获取方法 iOS 崩溃日志获取是 iOS 开发和测试中非常重要的一步,能够帮助开发者和测试人员快速定位崩溃原因,提高应用程序的稳定性和可靠性。下面将详细介绍获取 iOS 崩溃日志的方法。 什么是崩溃日志...

    Windows Mobile 设备管理器

    ADB支持通过USB连接进行设备控制、文件传输和日志记录,而且在Android Studio等开发环境中集成得更加紧密,提供了更全面的开发者支持。 **使用步骤** 1. **连接设备**:使用USB线将Windows Mobile设备连接到电脑。 ...

    NLog-Mobile 简单例子

    NLog-Mobile 是一个专门为移动平台设计的日志记录框架,它基于流行的 .NET 框架 NLog 进行了优化,适用于移动设备如智能手机或平板电脑。在这个“NLog-Mobile 简单例子”中,我们将探讨如何在 C# 语言环境下,针对 ...

    《windows mobile程序应用开发》源码

    对于移动应用的调试,源码可能包含了一些断点、日志记录和错误处理机制,这些都是调试和问题排查的关键。同时,源码可能还包括了针对不同设备分辨率和屏幕尺寸的适配策略,以及如何使用资源文件进行国际化和本地化。...

    windows mobile驱动开发

    此外,还可以使用Event Tracing for Windows (ETW)进行日志记录,帮助分析驱动问题。 7. **兼容性问题**:由于Windows Mobile有多个版本,如Windows CE、Windows Mobile 5.0、6.0、6.1和6.5等,驱动开发需考虑不同...

    windows mobile模拟器

    在开发过程中,模拟器的调试功能至关重要,它允许开发者查看日志、追踪错误并进行性能分析。 通过WM模拟器,开发者可以测试各种应用程序的兼容性和稳定性,而无需拥有每种型号的Windows Mobile设备。这大大降低了...

    Windows Mobile 播放器

    Windows Mobile 播放器是针对微软在移动设备上操作系统的一种多媒体播放解决方案。这款播放器是在Visual Studio 2005环境下,结合MFC(Microsoft Foundation Classes)库和Windows Mobile 6.0 SDK进行开发的。MFC是...

    windows mobile开发常用工具介绍

    9. **Remote File Viewer**:提供对设备文件系统的远程访问,能查看系统关键目录下的文件,包括隐藏文件,便于调试和日志分析。 以上工具为Windows Mobile开发者提供了全面的开发、测试和调试环境,大大简化了移动...

    Mobile Atlas Creator 1.9.8 无限版

    - `CHANGELOG.txt`:记录软件更新和改进的日志。 - `gpl.txt`:软件遵循的 GNU General Public License 文件。 - `ReleaseNotes.txt`:发布说明,详述每个版本的主要更新和修复。 6. **数据格式**:创建的离线...

Global site tag (gtag.js) - Google Analytics