⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 logfile.java

📁 MoMELog是J2ME的日志框架。它是非常简单的
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      {        closeConnection();      }  }  /*   * (non-Javadoc)   *    * @see javax.microedition.io.file.FileSystemListener#rootChanged(int,   *      java.lang.String)   */  public void rootChanged(int eventType, String root)  {    if (eventType == FileSystemListener.ROOT_REMOVED && this.canLog)      this.canLog = !root.equals(this.root);    else if (!this.canLog)      this.canLog = root.equals(this.root);  }  /**   * <p>   * Returns {@code footer} line to be written at the end of the destination   * file or {@code null}, if no line should be written.   * </p>   * <p>   * <strong>Note:</strong> {@code footer} line is written only when   * {@link #close()} method is called at the end of application's run (e.g.   * from {@code MIDlet.destroyApp(boolean unconditional)} method).   * </p>   *    * @return {@code footer} line to be written at the end of the destination   *         file or {@code null}, if no line should be written.   */  public String getFooter()  {    return this.footer;  }  /**   * <p>   * Sets {@code footer} line to be written at the end of the destination file.   * <p>   * <strong>Note:</strong> {@code footer} line is written only when   * {@link #close()} method is called at the end of application's run (e.g.   * from {@code MIDlet.destroyApp(boolean unconditional)} method).   * </p>   *    * @param footer   *          {@code footer} line to be written at the end of the destination   *          file or {@code null}, if no line should be written.   */  public synchronized void setFooter(String footer)  {    this.footer = footer;  }  /**   * Returns {@code header} line to be written at the start of the destination   * file or {@code null}, if no line should be written.   * </p>   * <p>   * <strong>Note:</strong> {@code header} line is written only, if   * {@link LogFile} creates or rewrites the destination file.   * </p>   *    * @return {@code header} line to be written at the start of the destination   *         file or {@code null}, if no line should be written.   */  public String getHeader()  {    return this.header;  }  /**   * Sets {@code header} line to be written at the start of the destination   * file.   * <p>   * <strong>Note:</strong> {@code header} line is written only, if   * {@link LogFile} creates or rewrites the destination file.   * </p>   *    * @param header   *          line to be written at the start of the destination file or   *          {@code null}, if no line should be written.   */  public synchronized void setHeader(String header)  {    this.header = header;  }  /**   * Returns string to be written at the start of each logging line or   * {@code null}, if no string should be written.   *    * @return string to be written at the start of each logging line or   *         {@code null}, if no string should be written.   */  public String getPrefix()  {    return this.prefix;  }  /**   * Sets string to be written at the start of each logging line.   *    * @param prefix   *          string to be written at the start of each logging line or   *          {@code null}, if no string should be written.   */  public synchronized void setPrefix(String prefix)  {    this.prefix = prefix;  }  /**   * Returns string to be written at the end of each logging line or   * {@code null}, if no string should be written.   *    * @return string to be written at the end of each logging line or   *         {@code null}, if no string should be written.   */  public String getSuffix()  {    return this.suffix;  }  /**   * Sets string to be written at the end of each logging line.   *    * @param suffix   *          string to be written at the end of each logging line or   *          {@code null}, if no string should be written.   */  public synchronized void setSuffix(String suffix)  {    this.suffix = suffix;  }  /**   * <p>   * Configures {@link LogFile} instance from the given character sequence.   * Character sequence should consist of lines separated by {@code new-line}   * character ({@code '\n'}). Each line should contain the name-value pair   * separated by equal sign ({@code =}). Spaces around properties names are   * allowed and ignored. Spaces around properties values are allowed.   * </p>   * <p>   * {@link LogFile} supports following properties. All properties names are in   * lower-case. If some property is specified more than one time, the last   * occurrence prevails. <table width="100%" border="1" cellpadding="2"   * cellspacing="0"> <col width="15%"> <col width="*"> <col width="15%">   * <tr valign="top">   * <td>   * <p>   * <strong>Property Name</strong>   * </p>   * </td>   * <td>   * <p>   * <strong>Description</strong>   * </p>   * </td>   * <td>   * <p>   * <strong>Default Value</strong>   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;outfile&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * Specifies the location of the destination file. It is specified as fully   * qualified path-name, that includes starting slash ({@code /}) and root's   * name. In other words, it is a file's url without protocol and host   * specifiers (i.e. {@code file://}). For example file identified by url   * {@code file:///SDCard/logs/myapp/log.txt} can be specified by path-name   * {@code /SDCard/logs/myapp/log.txt}.   * </p>   * <p>   * The default value is set to the {@code "momelog.log"} file in first root   * returned from {@link FileSystemRegistry#listRoots()} method.   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are ignored.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code "/<1st-root>/momelog.log"}   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;append&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * If {@code false}, destination file is rewritten on open, if it already   * exists. If {@code true} and destination file exists, no   * {@code header} is written and logging lines are appended to the end of the   * destination file. The default value is {@code false}.   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are ignored.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code false}   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;encoding&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * Specifies character encoding to be used for writing the destination file.   * The default value is {@code UTF-8}.   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are ignored.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code UTF-8}   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;footer&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * Specifies line to be written at the end of the destination file. The   * default value is {@code null} (i.e. no line is written).   * </p>   * <p>   * <strong>Note:</strong> {@code footer} line is only written, if method   * {@link #close()} is called at the end of the application's run (e.g. from   * {@code MIDlet.destroyApp(boolean unconditional)} method).   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are   * <strong>significant</strong>.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code null} (no line is written).   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;header&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * Specifies line to be written at the start of the destination file. The   * default value is {@code null} (i.e. no line is written).   * </p>   * <p>   * <strong>Note:</strong> {@code header} line is only written, if    * {@link LogFile} creates or rewrites the destination file.   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are   * <strong>significant</strong>.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code null} (no line is written).   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;prefix&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * Specifies string to be written at the start of each logging line. The   * default value is {@code null} (i.e. no string is written).   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are   * <strong>significant</strong>.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code null} (no string is written).   * </p>   * </td>   * </tr>   * <tr>   * <td>   * <p align="left">   * <strong>&quot;suffix&quot;</strong>   * </p>   * </td>   * <td>   * <p align="left">   * Specifies string to be written at the end of each logging line. The default   * value is {@code null} (i.e. no string is written).   * </p>   * <p>   * <strong>Note:</strong> Spaces around value of this property are   * <strong>significant</strong>.   * </p>   * <p>   * <em>Since version 1.0</em>   * </p>   * </td>   * <td>   * <p align="center">   * {@code null} (no string is written).   * </p>   * </td>   * </tr>   * </table>   *    * <p>   * For example: following configuration file snippet configures   * {@link LogFile} to append all logging to the existent file   * {@code "/root2/logs/my-app.log} using {@code UTF-16} encoding, sets   * {@code header} and {@code footer} to   * {@code '   ***  Logging from MyApp   ***   '}, prefix to {@code '['} and   * suffix to {@code ']'}.   * </p>   *    * <pre>   *       *  #setting LogListener   *  listener = momelog.listener.LogFile   *       *  #setting log file.   *  listener.outfile = /root2/logs/my-app.log   *       *  #don't rewrite   *  listener.append = true   *       *  #encoding setting.   *  listener.encoding = UTF-16   *       *  #header setting. spaces are significant.   *  listener.header =   ***  Logging from MyApp   ***   *  #footer setting. spaces are significant.   *  listener.footer =   ***  Logging from MyApp   ***   *  #prefix setting. spaces are significant.   *  listener.prefix =[   *  #suffix setting. spaces are significant.   *  listener.suffix =]   * </pre>   *    * @see momelog.Configurable#configure(char[], int, int)   */  public void configure(char[] lines, int offset, int length)  {    if (length < 0)      throw new IllegalArgumentException("length: " + length + " < 0");    if (offset < 0 || offset > lines.length)      throw new ArrayIndexOutOfBoundsException("offset: "          + Integer.toString(offset));    if ((length += offset) > lines.length)      length = lines.length;    if (length > offset)    {      for (int lineEnd = offset; lineEnd < length; offset = ++lineEnd)      {        for (; offset < length            && (lines[offset] == ' ' || lines[offset] == '\t'); offset++);        for (lineEnd = offset; lineEnd < length && lines[lineEnd] != '\n'; lineEnd++);        int nameEnd = offset;        boolean notValid = true;        for (; nameEnd < lineEnd && (notValid = (lines[nameEnd] != '=')); nameEnd++);        if (!notValid)        {          int vStart = (nameEnd--) + 1;          int vEnd = lineEnd;          for (; nameEnd >= offset              && (lines[nameEnd] == ' ' || lines[nameEnd] == '\t'); nameEnd--);          String name = String.valueOf(lines, offset, ++nameEnd - offset);          nameEnd = vStart;          for (; vStart < length              && (lines[vStart] == ' ' || lines[vStart] == '\t'); vStart++);          for (int pos = vEnd - 1; vStart <= pos              && (lines[pos] == ' ' || lines[pos] == '\t'); vEnd = pos--);          try          {            if (name.equals("outfile"))              setOutfile(String.valueOf(lines, vStart, vEnd - vStart));            else if (name.equals("encoding"))              setEncoding(String.valueOf(lines, vStart, vEnd - vStart));            else if (name.equals("header"))              setHeader(String.valueOf(lines, nameEnd, lineEnd - nameEnd));            else if (name.equals("fotter"))              setFooter(String.valueOf(lines, nameEnd, lineEnd - nameEnd));            else if (name.equals("prefix"))              setPrefix(String.valueOf(lines, nameEnd, lineEnd - nameEnd));            else if (name.equals("suffix"))              setSuffix(String.valueOf(lines, nameEnd, lineEnd - nameEnd));            else if (name.equals("append"))            {              String value = String.valueOf(lines, vStart, vEnd - vStart);              setAppend(value.equalsIgnoreCase("true")                  || value.equalsIgnoreCase("yes")                  || value.equalsIgnoreCase("on"));            } else System.err.println("ERROR: Unknown property " + name                + " of " + LogFile.class.getName() + ". Ignored.");          } catch (Throwable e)          {            String message = e.getMessage();            if (message != null && message.length() > 0)              message = ": " + message;            System.err.println("ERROR: Invalid value \""                + String.valueOf(lines, vStart, vEnd - vStart)                + "\" of property " + name + " of " + LogFile.class.getName()                + message + ". Ignored.");          }        } else System.err.println("ERROR: Invalid properties line "            + "\"listener." + String.valueOf(lines, offset, lineEnd - offset)            + "\" of " + LogFile.class.getName() + ". Ignored.");      }    }  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -