// Copyright 2014, Kevin Ko <kevin@faveset.com>

package com.faveset.log;

import java.io.OutputStream;
import java.io.PrintWriter;

public class OutputStreamLog implements Log {
    private static final String sDebugLabel = "DEBUG";
    private static final String sErrorLabel = "ERROR";
    private static final String sInfoLabel = "INFO";
    private static final String sVerboseLabel = "VERBOSE";
    private static final String sWarnLabel = "WARN";

    private PrintWriter mWriter;

    public OutputStreamLog(OutputStream os) {
        mWriter = new PrintWriter(os);
    }

    public synchronized int d(String tag, String msg) {
        String s = makeString(sDebugLabel, tag, msg);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int d(String tag, String msg, Throwable tr) {
        String s = makeExceptionString(sDebugLabel, tag, msg, tr);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int e(String tag, String msg) {
        String s = makeString(sErrorLabel, tag, msg);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int e(String tag, String msg, Throwable tr) {
        String s = makeExceptionString(sErrorLabel, tag, msg, tr);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int i(String tag, String msg) {
        String s = makeString(sInfoLabel, tag, msg);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int i(String tag, String msg, Throwable tr) {
        String s = makeExceptionString(sInfoLabel, tag, msg, tr);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int v(String tag, String msg) {
        String s = makeString(sVerboseLabel, tag, msg);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int v(String tag, String msg, Throwable tr) {
        String s = makeExceptionString(sVerboseLabel, tag, msg, tr);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int w(String tag, String msg) {
        String s = makeString(sWarnLabel, tag, msg);
        mWriter.append(s);
        return s.length();
    }

    public synchronized int w(String tag, String msg, Throwable tr) {
        String s = makeExceptionString(sWarnLabel, tag, msg, tr);
        mWriter.append(s);
        return s.length();
    }

    private static String makeString(String label, String tag, String msg) {
        StringBuilder builder = new StringBuilder();
        builder.append(label);
        builder.append(": ");
        builder.append(tag);
        builder.append(": ");
        builder.append(msg);
        return builder.toString();
    }

    private static String makeExceptionString(String label, String tag, String msg, Throwable tr) {
        StringBuilder builder = new StringBuilder();
        builder.append(label);
        builder.append(": ");
        builder.append(tag);
        builder.append(": caught exception ");
        builder.append(tr.toString());
        builder.append(": ");
        builder.append(msg);
        return builder.toString();
    }
}

@@ -566,32 +566,159 @@
 ter(os);%0A    %7D%0A%0A
+    /**%0A     * Closes the stream and releases all resources.%0A     */%0A    public void close() %7B%0A        mWriter.close();%0A    %7D%0A%0A
     public synch

Add close() method.