package org.usfirst.frc.team2503.r2016.debug;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Logger {
	
	public static class LoggerChannelDoesNotExistException extends Exception {
		
		private static final long serialVersionUID = 1L;
		
	}
	
	public static class LoggerPrintStream extends PrintStream {
		
		public LoggerPrintStream(File file) throws FileNotFoundException {
			super(file);
		}
		
		public LoggerPrintStream(File file, String csn) throws FileNotFoundException, UnsupportedEncodingException {
			super(file, csn);
		}
		
		public LoggerPrintStream(OutputStream out) {
			super(out);
		}
		
		public LoggerPrintStream(OutputStream out, boolean autoFlush) {
			super(out, autoFlush);
		}
		
		public LoggerPrintStream(OutputStream out, boolean autoFlush, String encoding) throws UnsupportedEncodingException {
			super(out, autoFlush, encoding);
		}
		
		public LoggerPrintStream(String fileName) throws FileNotFoundException {
			super(fileName);
		}
		
		public LoggerPrintStream(String fileName, String csn) throws FileNotFoundException, UnsupportedEncodingException {
			super(fileName, csn);
		}
		
	}
	
	private static JSONObject printStreams = new JSONObject();
	
	public static void addPrintStream(String channelName, LoggerPrintStream ps) {
		JSONArray channel;
		
		try {
			channel = printStreams.getJSONArray(channelName);
			channel.put(ps);
		} catch(JSONException e) {
			channel = new JSONArray();
			channel.put(ps);
		}
		
		printStreams.put(channelName, channel);
	}
	
	public static void print(String channelName, Object x) {
		JSONArray channel = printStreams.getJSONArray(channelName);
		
		LoggerPrintStream nextPrintStream = null;
		Iterator<Object> iterator = channel.iterator();
		while(iterator.hasNext()) {
			nextPrintStream = (LoggerPrintStream) iterator.next();
			nextPrintStream.print(x);
		}
	}
	
	protected static void _println(String channelName, Object x, boolean prefix) {
		StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
		StackTraceElement caller = stackTraceElements[3];

		String prefixString = (prefix ? "[(" + caller.getFileName() + ":" + caller.getLineNumber() + ") <" + caller.getMethodName() + ">] " : "");
		
		JSONArray channel = printStreams.getJSONArray(channelName);
		
		LoggerPrintStream nextPrintStream = null;
		Iterator<Object> iterator = channel.iterator();
		while(iterator.hasNext()) {
			nextPrintStream = (LoggerPrintStream) iterator.next();
			nextPrintStream.println(prefixString + x);
		}
	}
	
	public static void println(String channelName, Object x) {
		_println(channelName, x, true);
	}
	
	public static void println(String channelName, Object x, boolean prefix) {
		_println(channelName, x, prefix);
	}

}

@@ -338,18 +338,17 @@
 ogger %7B%0A
-%09
 %0A
+
 %09public 
@@ -415,19 +415,17 @@
 ption %7B%0A
-%09%09
 %0A
+
 %09%09privat
@@ -467,24 +467,21 @@
 D = 1L;%0A
-%09%09
 %0A
+
 %09%7D%0A
-%09
 %0A
+
 %09public 
@@ -533,19 +533,17 @@
 tream %7B%0A
-%09%09
 %0A
+
 %09%09public
@@ -615,35 +615,33 @@
 uper(file);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public LoggerP
@@ -756,27 +756,25 @@
 , csn);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public Log
@@ -823,27 +823,25 @@
 r(out);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public Log
@@ -920,27 +920,25 @@
 Flush);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public Log
@@ -1080,27 +1080,25 @@
 oding);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public Log
@@ -1184,19 +1184,17 @@
 e);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09public
@@ -1335,16 +1335,13 @@
 %09%09%7D%0A
-%09%09
 %0A
+
 %09%7D%0A
-%09
 %0A
+
 %09pri
@@ -1396,18 +1396,17 @@
 ject();%0A
-%09
 %0A
+
 %09public 
@@ -1497,19 +1497,17 @@
 hannel;%0A
-%09%09
 %0A
+
 %09%09try %7B%0A
@@ -1662,19 +1662,17 @@
 s);%0A%09%09%7D%0A
-%09%09
 %0A
+
 %09%09printS
@@ -1704,26 +1704,25 @@
 hannel);%0A%09%7D%0A
-%09
 %0A
+
 %09public stat
@@ -1821,35 +1821,33 @@
 y(channelName);%0A
-%09%09
 %0A
+
 %09%09LoggerPrintStr
@@ -1916,32 +1916,33 @@
 nel.iterator();%0A
+%0A
 %09%09while(iterator
@@ -2049,18 +2049,17 @@
 %0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09protect
@@ -2407,19 +2407,17 @@
  : %22%22);%0A
-%09%09
 %0A
+
 %09%09JSONAr
@@ -2474,11 +2474,9 @@
 e);%0A
-%09%09
 %0A
+
 %09%09Lo
@@ -2702,26 +2702,25 @@
  x);%0A%09%09%7D%0A%09%7D%0A
-%09
 %0A
+
 %09public stat
@@ -2804,17 +2804,16 @@
 ue);%0A%09%7D%0A
-%09
 %0A%09public

Delete trailing whitespace