Home | History | Annotate | Download | only in utility
      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.content.Context;
     20 import android.os.Environment;
     21 
     22 import java.io.FileWriter;
     23 import java.io.IOException;
     24 import java.io.PrintWriter;
     25 import java.util.Date;
     26 
     27 public class FileLogger {
     28     private static FileLogger LOGGER = null;
     29     private static FileWriter sLogWriter = null;
     30     public static String LOG_FILE_NAME =
     31         Environment.getExternalStorageDirectory() + "/emaillog.txt";
     32 
     33     public synchronized static FileLogger getLogger (Context c) {
     34         LOGGER = new FileLogger();
     35         return LOGGER;
     36     }
     37 
     38     private FileLogger() {
     39         try {
     40             sLogWriter = new FileWriter(LOG_FILE_NAME, true);
     41         } catch (IOException e) {
     42             // Doesn't matter
     43         }
     44     }
     45 
     46     static public synchronized void close() {
     47         if (sLogWriter != null) {
     48             try {
     49                 sLogWriter.close();
     50             } catch (IOException e) {
     51                 // Doesn't matter
     52             }
     53             sLogWriter = null;
     54         }
     55     }
     56 
     57     static public synchronized void log(Exception e) {
     58         if (sLogWriter != null) {
     59             log("Exception", "Stack trace follows...");
     60             PrintWriter pw = new PrintWriter(sLogWriter);
     61             e.printStackTrace(pw);
     62             pw.flush();
     63         }
     64     }
     65 
     66     @SuppressWarnings("deprecation")
     67     static public synchronized void log(String prefix, String str) {
     68         if (LOGGER == null) {
     69             LOGGER = new FileLogger();
     70             log("Logger", "\r\n\r\n --- New Log ---");
     71         }
     72         Date d = new Date();
     73         int hr = d.getHours();
     74         int min = d.getMinutes();
     75         int sec = d.getSeconds();
     76 
     77         // I don't use DateFormat here because (in my experience), it's much slower
     78         StringBuffer sb = new StringBuffer(256);
     79         sb.append('[');
     80         sb.append(hr);
     81         sb.append(':');
     82         if (min < 10)
     83             sb.append('0');
     84         sb.append(min);
     85         sb.append(':');
     86         if (sec < 10) {
     87             sb.append('0');
     88         }
     89         sb.append(sec);
     90         sb.append("] ");
     91         if (prefix != null) {
     92             sb.append(prefix);
     93             sb.append("| ");
     94         }
     95         sb.append(str);
     96         sb.append("\r\n");
     97         String s = sb.toString();
     98 
     99         if (sLogWriter != null) {
    100             try {
    101                 sLogWriter.write(s);
    102                 sLogWriter.flush();
    103             } catch (IOException e) {
    104                 // Something might have happened to the sdcard
    105                 if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
    106                     // If the card is mounted and we can create the writer, retry
    107                     LOGGER = new FileLogger();
    108                     if (sLogWriter != null) {
    109                         try {
    110                             log("FileLogger", "Exception writing log; recreating...");
    111                             log(prefix, str);
    112                         } catch (Exception e1) {
    113                             // Nothing to do at this point
    114                         }
    115                     }
    116                 }
    117             }
    118         }
    119     }
    120 }
    121