Created
August 17, 2012 14:02
-
-
Save hippiefahrzeug/3378935 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.mobino.util; | |
import java.io.BufferedWriter; | |
import java.io.ByteArrayOutputStream; | |
import java.io.File; | |
import java.io.FileWriter; | |
import java.io.PrintStream; | |
import java.text.DateFormat; | |
import java.util.Locale; | |
import android.content.Context; | |
import android.util.Log; | |
public class Ln { | |
private static boolean androidlog = true; | |
private static boolean filelog = false; | |
public static void d(String msg) { | |
if (androidlog) { | |
String tag = getTag(); | |
d(tag, msg); | |
} | |
} | |
public static void i(String msg) { | |
if (androidlog) { | |
String tag = getTag(); | |
i(tag, msg); | |
} | |
} | |
public static void e(String msg) { | |
String tag = getTag(); | |
e(tag, msg); | |
} | |
public static void e(Throwable e) { | |
String tag = getTag(); | |
e(tag, e); | |
} | |
public static void d(String tag, String msg) { | |
if (androidlog) { | |
Log.d(tag, msg); | |
} | |
writeLog(tag, msg, "d"); | |
} | |
public static void i(String tag, String msg) { | |
if (androidlog) { | |
Log.i(tag, msg); | |
} | |
writeLog(tag, msg, "i"); | |
} | |
public static void e(String tag, Throwable e) { | |
//if (androidlog) { | |
Log.e(tag, "exception caught: ", e); | |
//} | |
writeLog(tag, getStackTrace(e), "e"); | |
} | |
public static void e(String tag, String msg) { | |
//if (androidlog) { | |
Log.e(tag, msg); | |
//} | |
writeLog(tag, msg, "e"); | |
} | |
private static String getTag() { | |
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace(); | |
StackTraceElement element = stackTrace[4]; | |
String TAG = element.getClassName(); | |
return TAG; | |
} | |
public static String getThreadSignature() { | |
Thread t = Thread.currentThread(); | |
long l = t.getId(); | |
String name = t.getName(); | |
long p = t.getPriority(); | |
String gname = t.getThreadGroup().getName(); | |
return (name | |
+ ":(id)" + l | |
+ ":(priority)" + p | |
+ ":(group)" + gname); | |
} | |
private static void writeLog(String tag, String msg, String level) { | |
if (!filelog) { | |
return; | |
} | |
try { | |
File dest = new File("/sdcard/mobino.log"); | |
FileWriter fstream = new FileWriter(dest, true); | |
BufferedWriter out = new BufferedWriter(fstream); | |
String t = time2DateTime(null, System.currentTimeMillis()); | |
out.write(t + " " + tag + " " + level + " " + msg + "\n"); | |
out.close(); | |
} | |
catch (Exception e) { | |
Log.e(Ln.class.getName(), "exception caught", e); | |
} | |
} | |
public static String getStackTrace(Throwable ex) { | |
ByteArrayOutputStream buf = new ByteArrayOutputStream(); | |
ex.printStackTrace(new PrintStream(buf)); | |
return buf.toString(); | |
} | |
public static String time2DateTime(Context ctx, long d) { | |
DateFormat df = DateFormat.getDateTimeInstance( | |
DateFormat.SHORT, | |
DateFormat.SHORT, | |
Locale.US | |
); | |
return df.format(d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment