// 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) {
        mWriter.append(makeString(sDebugLabel, tag, msg));
    }

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

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

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

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

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

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

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

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

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

    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();
    }
}

@@ -634,39 +634,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeString(s
@@ -674,32 +674,85 @@
 Label, tag, msg)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -819,39 +819,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeExceptio
@@ -872,32 +872,85 @@
 l, tag, msg, tr)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -1003,39 +1003,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeString(s
@@ -1043,32 +1043,85 @@
 Label, tag, msg)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -1188,39 +1188,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeExceptio
@@ -1241,32 +1241,85 @@
 l, tag, msg, tr)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -1372,39 +1372,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeString(s
@@ -1411,32 +1411,85 @@
 Label, tag, msg)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -1556,39 +1556,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeExceptio
@@ -1608,32 +1608,85 @@
 l, tag, msg, tr)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -1739,39 +1739,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeString(s
@@ -1781,32 +1781,85 @@
 Label, tag, msg)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -1926,39 +1926,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeExceptio
@@ -1981,32 +1981,85 @@
 l, tag, msg, tr)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D%0A%0A    pu
@@ -2112,39 +2112,35 @@
 ) %7B%0A        
-mWriter.append(
+String s = 
 makeString(s
@@ -2159,16 +2159,69 @@
 ag, msg)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D
@@ -2300,31 +2300,27 @@
         
-mWriter.append(
+String s = 
 makeExce
@@ -2356,16 +2356,69 @@
 msg, tr)
+;%0A        mWriter.append(s);%0A        return s.length(
 );%0A    %7D

return values.