1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.exchange.utility; 18 19 import android.os.Environment; 20 21 import java.io.FileWriter; 22 import java.io.IOException; 23 import java.io.PrintWriter; 24 import java.util.Date; 25 26 public class FileLogger { 27 private static FileLogger LOGGER = null; 28 private static FileWriter sLogWriter = null; 29 public static String LOG_FILE_NAME = 30 Environment.getExternalStorageDirectory() + "/emaillog.txt"; 31 32 public synchronized static FileLogger getLogger() { 33 LOGGER = new FileLogger(); 34 return LOGGER; 35 } 36 37 private FileLogger() { 38 try { 39 sLogWriter = new FileWriter(LOG_FILE_NAME, true); 40 } catch (IOException e) { 41 // Doesn't matter 42 } 43 } 44 45 static public synchronized void close() { 46 if (sLogWriter != null) { 47 try { 48 sLogWriter.close(); 49 } catch (IOException e) { 50 // Doesn't matter 51 } 52 sLogWriter = null; 53 } 54 } 55 56 static public synchronized void log(Exception e) { 57 if (sLogWriter != null) { 58 log("Exception", "Stack trace follows..."); 59 PrintWriter pw = new PrintWriter(sLogWriter); 60 e.printStackTrace(pw); 61 pw.flush(); 62 } 63 } 64 65 @SuppressWarnings("deprecation") 66 static public synchronized void log(String prefix, String str) { 67 if (LOGGER == null) { 68 LOGGER = new FileLogger(); 69 log("Logger", "\r\n\r\n --- New Log ---"); 70 } 71 Date d = new Date(); 72 int hr = d.getHours(); 73 int min = d.getMinutes(); 74 int sec = d.getSeconds(); 75 76 // I don't use DateFormat here because (in my experience), it's much slower 77 StringBuffer sb = new StringBuffer(256); 78 sb.append('['); 79 sb.append(hr); 80 sb.append(':'); 81 if (min < 10) 82 sb.append('0'); 83 sb.append(min); 84 sb.append(':'); 85 if (sec < 10) { 86 sb.append('0'); 87 } 88 sb.append(sec); 89 sb.append("] "); 90 if (prefix != null) { 91 sb.append(prefix); 92 sb.append("| "); 93 } 94 sb.append(str); 95 sb.append("\r\n"); 96 String s = sb.toString(); 97 98 if (sLogWriter != null) { 99 try { 100 sLogWriter.write(s); 101 sLogWriter.flush(); 102 } catch (IOException e) { 103 // Something might have happened to the sdcard 104 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) { 105 // If the card is mounted and we can create the writer, retry 106 LOGGER = new FileLogger(); 107 if (sLogWriter != null) { 108 try { 109 log("FileLogger", "Exception writing log; recreating..."); 110 log(prefix, str); 111 } catch (Exception e1) { 112 // Nothing to do at this point 113 } 114 } 115 } 116 } 117 } 118 } 119 } 120