Ray Zhang 11 年之前
父节点
当前提交
0eb9b673d5
共有 2 个文件被更改,包括 229 次插入53 次删除
  1. 51 1
      RedAnt Photo/src/com/usai/util/Network.java
  2. 178 52
      RedAnt Photo/src/com/usai/util/dbgUtil.java

+ 51 - 1
RedAnt Photo/src/com/usai/util/Network.java

@@ -30,6 +30,7 @@ import org.apache.http.client.HttpClient;
 import org.apache.http.client.methods.HttpPost;
 import org.apache.http.conn.ClientConnectionManager;
 import org.apache.http.conn.ConnectTimeoutException;
+import org.apache.http.conn.HttpHostConnectException;
 import org.apache.http.conn.scheme.PlainSocketFactory;
 import org.apache.http.conn.scheme.Scheme;
 import org.apache.http.conn.scheme.SchemeRegistry;
@@ -182,6 +183,7 @@ public class Network
 		}
 		catch (Exception e)
 		{
+			dbgUtil.fileLog(e.toString());
 			return new DefaultHttpClient();
 		}
 	}
@@ -262,6 +264,7 @@ public class Network
 					}
 					catch (Exception e)
 					{
+						dbgUtil.fileLog(e.toString());
 						Log.e(TAG, e.toString());
 						return null;
 						// TODO: handle exception
@@ -314,6 +317,7 @@ public class Network
 					}
 					catch (Exception e)
 					{
+						dbgUtil.fileLog(e.toString());
 						Log.e(TAG, e.toString());
 						return null;
 						// TODO: handle exception
@@ -339,12 +343,32 @@ public class Network
 		catch (ConnectTimeoutException e)
 		{
 			Log.d(TAG, "request time out");
+			dbgUtil.fileLog(e.toString());
 			return null;
 
 		}
+		catch (SocketTimeoutException e)
+		{
+			Log.d(TAG, "socket time out");
+			dbgUtil.fileLog(e.toString());
+			return null;
+		}
+		catch (UnknownHostException e)
+		{
+			Log.d(TAG, e.toString());
+			dbgUtil.fileLog(e.toString());
+			return null;
+		}
+		catch (HttpHostConnectException e)
+		{
+			Log.d(TAG, e.toString());
+			dbgUtil.fileLog(e.toString());
+			return null;
+		}
 		catch (Exception e)
 		{
 			Log.d(TAG, e.toString());
+			dbgUtil.fileLog(e.toString());
 			return null;
 		}
 
@@ -436,6 +460,7 @@ public class Network
 		catch (JSONException e)
 		{
 			// TODO Auto-generated catch block
+			dbgUtil.fileLog(e.toString());
 			e.printStackTrace();
 			return RESULT_NET_ERROR;
 		}
@@ -652,12 +677,14 @@ public class Network
 		{
 			e.printStackTrace();
 
+			dbgUtil.fileLog(e.toString());
 			return RESULT_LOCALFILE_ERROR;
 		}
 		catch (IOException e)
 		{
 			e.printStackTrace();
 
+			dbgUtil.fileLog(e.toString());
 			return RESULT_LOCALFILE_ERROR;
 		}
 		int ret = UploadFile(scaledPath, user, password, pid,server);
@@ -676,12 +703,16 @@ public class Network
 		File file = new File(path);
 
 		if (file.exists() == false)
+		{
+			dbgUtil.fileLog("file "+ path +" not found!");
 			return RESULT_LOCALFILE_ERROR;
+		}
 		String md5 = MD5.md5sum(path);
 		Log.d(TAG, "MD5:" + md5);
 		if (md5 == null)
 		{
 			Log.d(TAG, "md5 sum failed");
+			dbgUtil.fileLog("md5 sum failed");
 			return RESULT_LOCALFILE_ERROR;
 
 		}
@@ -761,6 +792,7 @@ public class Network
 							catch (Exception e)
 							{
 								Log.e(TAG, e.toString()+1);
+								dbgUtil.fileLog(e.toString());
 								return RESULT_FALSE;
 							}
 						}
@@ -768,6 +800,7 @@ public class Network
 					catch (Exception e)
 					{
 						Log.e(TAG, e.toString());
+						dbgUtil.fileLog(e.toString());
 						return RESULT_NET_ERROR;
 						// TODO: handle exception
 					}
@@ -820,7 +853,9 @@ public class Network
 					catch (Exception e)
 					{
 						Log.e(TAG, e.toString());
+						dbgUtil.fileLog(e.toString());
 						return RESULT_NET_ERROR;
+						
 						// TODO: handle exception
 					}
 					finally
@@ -844,17 +879,32 @@ public class Network
 		catch (ConnectTimeoutException e)
 		{
 			Log.d(TAG, "request time out");
+			dbgUtil.fileLog(e.toString());
 			return RESULT_NET_ERROR;
 
 		}
 		catch (SocketTimeoutException e)
 		{
 			Log.d(TAG, "socket time out");
+			dbgUtil.fileLog(e.toString());
+			return RESULT_NET_ERROR;
+		}
+		catch (UnknownHostException e)
+		{
+			Log.d(TAG, e.toString());
+			dbgUtil.fileLog(e.toString());
+			return RESULT_NET_ERROR;
+		}
+		catch (HttpHostConnectException e)
+		{
+			Log.d(TAG, e.toString());
+			dbgUtil.fileLog(e.toString());
 			return RESULT_NET_ERROR;
 		}
 		catch (Exception e)
 		{
-			Log.d(TAG, e.toString()+2);
+			Log.d(TAG, e.toString()+" RESULT_ERROR!!");
+			dbgUtil.fileLog(e.toString());
 			return RESULT_ERROR;
 		}
 

+ 178 - 52
RedAnt Photo/src/com/usai/util/dbgUtil.java

@@ -1,15 +1,32 @@
 package com.usai.util;
 
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileFilter;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Calendar;
+import java.util.Date;
+
+import com.usai.redant.photo.RedAntApplication;
+import com.usai.redant.photo.R.string;
+
+import android.app.Activity;
+import android.app.Application;
 import android.content.Context;
 import android.database.Cursor;
+import android.os.Environment;
 import android.util.Log;
 
 public class dbgUtil
 {
-//	static boolean ENABLE_DEBUG = true;
-//	static boolean ENABLE_SQL_DEBUG = true;
-//	final int DEBUG_LEVEL_NONE = 8;
-//	static int DEBUG_LEVEL = Log.VERBOSE;
+	// static boolean ENABLE_DEBUG = true;
+	// static boolean ENABLE_SQL_DEBUG = true;
+	// final int DEBUG_LEVEL_NONE = 8;
+	// static int DEBUG_LEVEL = Log.VERBOSE;
 
 	// private static boolean m_dbgFlag = true;
 	/*
@@ -23,53 +40,160 @@ public class dbgUtil
 	 * Boolean.parseBoolean(context.getString(R.string.flag_debug)); return
 	 * this; }
 	 */
-	public static void Logd(String tag,String msg)
+	public static void fileLog(String content)
+	{
+		
+		File dir = new File(Environment.getExternalStorageDirectory().getPath()
+				+ "/redant/pop");
+		if (!dir.exists())
+			dir.mkdirs();
+
+		Calendar c = Calendar.getInstance();
+		c.add(Calendar.MONDAY, -1);
+
+		String newtime = new SimpleDateFormat("yyyy-MM-dd").format(c
+				.getTimeInMillis());
+
+		File[] f = dir.listFiles();
+		for (int i = 0; i < f.length; i++)
+		{
+			if(!f[i].isFile())
+				continue;
+			String n = f[i].getName();
+			if ((n != null) && (n.length() > 0))
+			{
+				int dot = n.lastIndexOf('.');
+				if ((dot > -1) && (dot < (n.length())))
+				{
+					n = n.substring(0, dot);
+				}
+				if(newtime.compareTo(n)>0)
+					f[i].delete();
+				
+
+
+			}
+		}
+
+		// dir.listfiles
+
+		String file = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
+		String timeStamp = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
+				.format(new Date());
+
+		FileOutputStream outputStream = null;
+		// try
+		// {
+
+		// dir.createNewFile()
+		File logfile = new File(Environment.getExternalStorageDirectory()
+				.getPath() + "/redant/pop/" + file + ".log");
+
+		// int mode = Application.MODE_APPEND;
+		if (!logfile.exists())
+		{
+			// mode = Application.MODE_PRIVATE;
+			try
+			{
+				logfile.createNewFile();
+			}
+			catch (IOException e)
+			{
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+		}
+		// outputStream
+		// =RedAntApplication.getInstance().openFileOutput("pop.log", mode);
+
+		String logtextString = timeStamp + "   " + content;
+
+		FileWriter fw = null;
+		BufferedWriter bw = null;
+		String datetime = "";
+		try
+		{
+			RedAntApplication.loglock.writeLock().lock();
+
+			fw = new FileWriter(Environment.getExternalStorageDirectory()
+					.getPath() + "/redant/pop/" + file + ".log", true);//
+			// 创建FileWriter对象,用来写入字符流
+			bw = new BufferedWriter(fw); // 将缓冲对文件的输出
+			// String myreadline = datetime + "[]" + str;
+
+			bw.write(logtextString + "\n"); // 写入文件
+			bw.newLine();
+			bw.flush(); // 刷新该流的缓冲
+			bw.close();
+			fw.close();
+			RedAntApplication.loglock.writeLock().unlock();
+		}
+		catch (IOException e)
+		{
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+			try
+			{
+				bw.close();
+				fw.close();
+			}
+			catch (IOException e1)
+			{
+				// TODO Auto-generated catch block
+			}
+		}
+
+	}
+
+	public static void Logd(String tag, String msg)
 	{
 		String pos = getTracePos();
-		Log.d(tag,msg+pos);
+		Log.d(tag, msg + pos);
 	}
-//	public static void Log(int type, String tag, String msg)
-//	{
-////		if (ENABLE_DEBUG == false)
-////			return;
-////		if (type < DEBUG_LEVEL)
-////			return;
-//		String pos= getTracePos();
-//		switch (type)
-//		{
-//			case Log.ASSERT:
-//				Log.wtf(tag, msg+pos);
-//				break;
-//			case Log.DEBUG:
-//				Log.d(tag, msg+pos);
-//				break;
-//			case Log.ERROR:
-//				Log.e(tag, msg+pos);
-//				break;
-//			case Log.INFO:
-//				Log.i(tag, msg+pos);
-//				break;
-//			case Log.VERBOSE:
-//				Log.v(tag, msg+pos);
-//				break;
-//			case Log.WARN:
-//				Log.w(tag, msg+pos);
-//				break;
-//			default:
-//				Log.wtf("dbgUtil", "LOG TYPE ERROR!"+pos);
-//				break;
-//		}
-//
-//	}
+
+	// public static void Log(int type, String tag, String msg)
+	// {
+	// // if (ENABLE_DEBUG == false)
+	// // return;
+	// // if (type < DEBUG_LEVEL)
+	// // return;
+	// String pos= getTracePos();
+	// switch (type)
+	// {
+	// case Log.ASSERT:
+	// Log.wtf(tag, msg+pos);
+	// break;
+	// case Log.DEBUG:
+	// Log.d(tag, msg+pos);
+	// break;
+	// case Log.ERROR:
+	// Log.e(tag, msg+pos);
+	// break;
+	// case Log.INFO:
+	// Log.i(tag, msg+pos);
+	// break;
+	// case Log.VERBOSE:
+	// Log.v(tag, msg+pos);
+	// break;
+	// case Log.WARN:
+	// Log.w(tag, msg+pos);
+	// break;
+	// default:
+	// Log.wtf("dbgUtil", "LOG TYPE ERROR!"+pos);
+	// break;
+	// }
+	//
+	// }
 
 	private static String getTracePos()
 	{
 		// StackTraceElement[] elements =
 		// Thread.currentThread().getStackTrace();
 		StackTraceElement element = Thread.currentThread().getStackTrace()[4];
-		
+
 		return " at " + element.getClassName() + "." + element.getMethodName()
-				+ "("+element.getFileName()+":" + element.getLineNumber()+")" ;
+				+ "(" + element.getFileName() + ":" + element.getLineNumber()
+				+ ")";
 	}
 
 	public static void dbgCursorInfo(Cursor cursor, Context context/*
@@ -77,21 +201,23 @@ public class dbgUtil
 																	 * TracePos
 																	 */)
 	{
-//		if (ENABLE_SQL_DEBUG == false)
-//			return;
+		// if (ENABLE_SQL_DEBUG == false)
+		// return;
 
 		// ----------------------debug cursor-----------------------------------
-		Log.d("sql_dbg@dbgCursorInfo", getTracePos() + "Cursor size:	" + cursor.getCount()
-				+ ";	columns count:	" + cursor.getColumnCount() + ";");
-//		dbgUtil.Log(Log.DEBUG,"sql_dbg", getTracePos() + "Cursor size:	" + cursor.getCount()
-//				+ ";	columns count:	" + cursor.getColumnCount() + ";");
+		Log.d("sql_dbg@dbgCursorInfo",
+				getTracePos() + "Cursor size:	" + cursor.getCount()
+						+ ";	columns count:	" + cursor.getColumnCount() + ";");
+		// dbgUtil.Log(Log.DEBUG,"sql_dbg", getTracePos() + "Cursor size:	" +
+		// cursor.getCount()
+		// + ";	columns count:	" + cursor.getColumnCount() + ";");
 		for (int i = 0; i < cursor.getColumnCount(); i++)
 		{
-			Log.d("sql_dbg@dbgCursorInfo","column idx:	" + i + ";		column name:	"
-					+ cursor.getColumnName(i) + ";");
-//			dbgUtil.Log(Log.DEBUG,"sql_dbg",
-//					"column idx:	" + i + ";		column name:	"
-//							+ cursor.getColumnName(i) + ";");
+			Log.d("sql_dbg@dbgCursorInfo", "column idx:	" + i
+					+ ";		column name:	" + cursor.getColumnName(i) + ";");
+			// dbgUtil.Log(Log.DEBUG,"sql_dbg",
+			// "column idx:	" + i + ";		column name:	"
+			// + cursor.getColumnName(i) + ";");
 		}
 		// ----------------------debug cursor-----------------------------------