从源码与官方文档看之Handle篇(四)

简介: 今天的工作挺忙的,又要接接口,又要修Bug,还要完成需求。晚上家里又有一些事情忙活,所以我们废话不多说,直接进正题。

正篇

首先我们接着看下一个方法setFormatter:

/**
 * Set a <tt>Formatter</tt>.  This <tt>Formatter</tt> will be used
 * to format <tt>LogRecords</tt> for this <tt>Handler</tt>.
 * <p>
 * Some <tt>Handlers</tt> may not use <tt>Formatters</tt>, in
 * which case the <tt>Formatter</tt> will be remembered, but not used.
 * <p>
 * @param newFormatter the <tt>Formatter</tt> to use (may not be null)
 * @exception  SecurityException  if a security manager exists and if
 *             the caller does not have <tt>LoggingPermission("control")</tt>.
 */
public synchronized void setFormatter(Formatter newFormatter) throws SecurityException {
    checkPermission();
    // Check for a null pointer:
    newFormatter.getClass();
    formatter = newFormatter;
}

从翻译看,大致是说这个方法用来设置格式化,支持格式化日志数据。 此 Formatter 将用于为此 Handler 格式化 LogRecords对象。而一些Handle可能不使用Formatters,在这种情况下会被Formatter记住,但不会被使用。通常每个Handler中都会有一个Formatter引用,Formatter可以将LogRecord对象转换为一个string字符串。

/**
 * Return the <tt>Formatter</tt> for this <tt>Handler</tt>.
 * @return the <tt>Formatter</tt> (may be null).
 */
public Formatter getFormatter() {
    return formatter;
}

这个是接受formatter的方法,说到这,不得不提一下,Handle和Formatter类是两个抽象类,它们可以分别独立的变化(有不同的子类);而Handle类中包含对Formatter类的引用。Formatter支持格式化日志数据;通常每个Handler中都会有一个Formatter引用,Formatter可以将LogRecord对象转换为一个string字符串。

/**
 * Set the character encoding used by this <tt>Handler</tt>.
 * <p>
 * The encoding should be set before any <tt>LogRecords</tt> are written
 * to the <tt>Handler</tt>.
 *
 * @param encoding  The name of a supported character encoding.
 *        May be null, to indicate the default platform encoding.
 * @exception  SecurityException  if a security manager exists and if
 *             the caller does not have <tt>LoggingPermission("control")</tt>.
 * @exception  UnsupportedEncodingException if the named encoding is
 *          not supported.
 */
public synchronized void setEncoding(String encoding)
                    throws SecurityException, java.io.UnsupportedEncodingException {
    checkPermission();
    if (encoding != null) {
        try {
            if(!java.nio.charset.Charset.isSupported(encoding)) {
                throw new UnsupportedEncodingException(encoding);
            }
        } catch (java.nio.charset.IllegalCharsetNameException e) {
            throw new UnsupportedEncodingException(encoding);
        }
    }
    this.encoding = encoding;
}
/**
 * Return the character encoding for this <tt>Handler</tt>.
 *
 * @return  The encoding name.  May be null, which indicates the
 *          default encoding should be used.
 */
public String getEncoding() {
    return encoding;
}

setEncoding方法的大意是:

本方法可以设置 Handler使用的字符编码。 但应该在将任何 LogRecord 对象写入 Handler 之前设置编码。

方法的参数:

encoding – 支持的字符编码的名称。 可以为 null,表示默认的平台编码。

错误的抛出:

SecurityException – 如果存在安全管理器且没调用LoggingPermission("control")。

UnsupportedEncodingException – 如果不支持命名编码。

而getEncoding()方法是返回Handle的字符编码。 (未完待续)

总结

这些在安卓开发日常中还是不大容易接触到的,我们会继续往下看,然后去看Handle的实现。

相关文章
|
2天前
|
JSON 安全 数据安全/隐私保护
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
​iOS Class Guard github用法、工作原理和安装详解及使用经验总结
20 0
|
2天前
|
JSON 安全 数据安全/隐私保护
iOS Class Guard github用法、工作原理和安装详解及使用经验总结
iOS Class Guard github用法、工作原理和安装详解及使用经验总结
60 0
|
安全
从源码与官方文档看之Handle篇(二)
上一篇我们根据官方注释解读Handle的一些基本属性,这一篇我们接着慢慢阐述。
|
监控
从源码与官方文档看之Handle篇(一)
之前的文章实在太肤浅,写出来其实很多都没有太大帮助,所以我们另起一个系列“从源码与官方文档看”。
|
安全
从源码与官方文档看之Handle篇(三)
我们继续看看源码,来一步步去深入Handle,去熟悉那些我们平时用得到和用不到的属性与特点。结合到我们开发工作中,看看我们取用了它哪些好用的地方。
从源码与官方文档看之Handler篇(三)
前面我们说到dispatchMessage方法,今天我们继续往下看源码。
107 0
|
消息中间件 Java 调度
从源码与官方文档看之Handler篇(一)
好家伙,写了四篇文章才发现自己看源码的Handle所属的包不一样
从源码与官方文档看之Handler篇(五)
尽管前路坎坷,可我们还是继续走了下去,一转眼已经又到了第五篇了,每篇内容较少,所以后面整理完时,会汇总成一篇文章,相信这样做会更具有可读性。
从源码与官方文档看之Handler篇(七)
接下来就是我们可以正常调用的Handler类方法了,希望早日完成Handler类的源码阅读。
|
安全 API 调度
从源码与官方文档看之Handler篇(十)
每次阅读源码,我都想着许多过往云烟,可以说,一切不在一样,不过,想再多又如何,倒不如按下心思,在手中记下这一切更为妥当。还是看看这些充满智慧结晶的源码吧!