com.google.jstestdriver.ui.LogPanelLog.java Source code

Java tutorial

Introduction

Here is the source code for com.google.jstestdriver.ui.LogPanelLog.java

Source

/*
 * Copyright 2009 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.jstestdriver.ui;

import org.apache.commons.logging.impl.SimpleLog;

/**
 * Overrides the write() method of SimpleLog to print to the delegated LogPrinter instead of
 * StdErr.
 * @author alexeagle@google.com (Alex Eagle)
 */
public class LogPanelLog extends SimpleLog {

    public LogPanelLog(String s) {
        super(s);
    }

    @Override
    protected void write(StringBuffer stringBuffer) {
        LogPanelHolder.logPanel.logLine(stringBuffer.toString());
    }

    /**
     * Static injection of the LogPrinter. This is needed because the logging framework creates
     * instances of this Log, without an opportunity to inject fields or access to the object
     * after it's created.
     */
    static class LogPanelHolder {
        protected static LogPrinter logPanel = new LogPrinter() {
            public void logLine(String logRecord) {
                System.err.println("Log printed with no panel to display in: " + logRecord);
            }
        };

        public static void setLogPanel(LogPrinter logPanel) {
            LogPanelHolder.logPanel = logPanel;
        }
    }
}