Android Open Source - MCCDash Logger






From Project

Back to project page MCCDash.

License

The source code is released under:

Apache License

If you think the Android project MCCDash listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package edu.bupt.mccdash.io;
/* w  ww  .j ava 2 s.  c  o m*/
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

import edu.bupt.mccdash.BuildConfig;
import android.content.Context;
import android.os.Environment;
import android.util.Log;

public class Logger {
  /**
   * log TAG
   */
  private static String TAG = "Logger";
  private static final String ROOT = Environment.getExternalStorageDirectory().getAbsolutePath()+"/";
  private static String FILE_NAME = "logger.log";
  
  private static String logFile = ROOT + FILE_NAME;
  
  /**
   * debug or not
   */
  private static boolean debug = BuildConfig.DEBUG;
  
  private static boolean write2Sdcard = true;

  private static Logger instance = new Logger();

  private Logger() {

  }

  public static Logger getLogger() {
    return instance;
  }
  public static Logger getLogger(Context context,String fileName){
    TAG = context.getPackageName();
    FILE_NAME = fileName;
    return instance;
  }
  /**
   * ??????????
   */
  private String getFunctionName() {
    StackTraceElement[] sts = Thread.currentThread().getStackTrace();

    if (sts == null) {
      return null;
    }

    for (StackTraceElement st : sts) {
      if (st.isNativeMethod()) {
        continue;
      }

      if (st.getClassName().equals(Thread.class.getName())) {
        continue;
      }

      if (st.getClassName().equals(this.getClass().getName())) {
        continue;
      }

      return "[" + Thread.currentThread().getName() + "(" + Thread.currentThread().getId()
          + "): " + st.getFileName() + ":" + st.getLineNumber() + "]";
    }

    return null;
  }

  private String createMessage(String msg) {
    String functionName = getFunctionName();
    String message = (functionName == null ? msg : (functionName + " - " + msg));
    return message;
  }

  /**
   * log.i
   */
  public void info(String msg) {
    String message = createMessage(msg);
    if (debug) {
      Log.i(TAG, message);
    }
    if(write2Sdcard){
      instance.writeLog(message);
    }
  }
  public static void i(String msg){
    instance.info(msg);
  }
  public static void i(Exception e){
    instance.info(e!=null?e.toString():"null");
  }
  /**
   * log.v
   */
  public void verbose(String msg) {
    String message = createMessage(msg);
    if (debug) {
      Log.v(TAG, message);
    }
    if(write2Sdcard){
      instance.writeLog(message);
    }
  }
  public void v(String msg){
    if(debug){
      instance.verbose(msg);
    }
    if(write2Sdcard){
      instance.writeLog(msg);
    }
  }
  public void v(Exception e){
    if(debug){
      instance.verbose(e!=null?e.toString():"null");
    }
    if(write2Sdcard){
      instance.writeLog(e.toString());
    }
  }
  /**
   * log.d
   */
  public void debug(String msg) {
    if (debug) {
      String message = createMessage(msg);
      Log.d(TAG, message);
    }
    if(write2Sdcard){
      instance.writeLog(msg);
    }
  }
  /**
   * log.e
   */
  public void error(String msg) {
    String message = createMessage(msg);
    if (debug) {
      Log.e(TAG, message);
    }
    if(write2Sdcard){
      instance.writeLog(message);
    }
  }
  /**
   * log.error 
   */
  public void error(Exception e){
    StringBuffer sb = new StringBuffer();
    String name = getFunctionName();
    StackTraceElement[] sts = e.getStackTrace();
    
    if (name != null) {
      sb.append(name+" - "+e+"\r\n");
    } else {
      sb.append(e+"\r\n");
    }
    if (sts != null && sts.length > 0) {
      for (StackTraceElement st:sts) {
        if (st != null) {
          sb.append("[ "+st.getFileName()+":"+st.getLineNumber()+" ]\r\n");
        }
      }
    }
    if(debug){
          Log.e(TAG,sb.toString());
    }
    if(write2Sdcard){
      instance.writeLog(sb.toString());
    }
  }
  /**
   * log.warn
   */
  public void warn(String msg) {
    String message = createMessage(msg);
    if (debug) {
      Log.w(TAG, message);
    }
    if(write2Sdcard){
      instance.writeLog(message);
    }
  }
  public static void d(String msg){
    instance.debug(msg);
    
  }
  public static void d(Exception e){
    instance.debug(e!=null?e.toString():"null");
  }
  /**
   * log.e
   * @description   
   * @param msg
   * hylin  2012-9-20????2:05:37
   */
  public static void e(String msg){
    instance.error(msg);
  }
  
  public static void e(Exception e){
    instance.error(e);
  }
  /**
   * log.w
   */
  public static void w(String msg){
    instance.warn(msg);
  }
  public static void w(Exception e){
    instance.warn(e!=null?e.toString():"null");
  }
  public static void resetLogFile(){
    File file = new File(logFile);
    file.delete();
    try {
      file.createNewFile();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
  private void writeLog(String content){
    try {
      File file = new File(logFile);
      if (!file.exists()) {
        file.createNewFile();
      }
//      DateFormat formate = SimpleDateFormat.getDateTimeInstance();
      SimpleDateFormat formate = new SimpleDateFormat(
          "yyyy-MM-dd HH:mm:ss");
      FileWriter write = new FileWriter(file, true);
      write.write(formate.format(new Date()) + "   "+
          content+"\n");
      write.flush();
      write.close();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}




Java Source Code List

edu.bupt.mccdash.HttpDownloader.java
edu.bupt.mccdash.MainActivity.java
edu.bupt.mccdash.MediaPlayerDemo_Video.java
edu.bupt.mccdash.OnBluetoothRecvCompleteListener.java
edu.bupt.mccdash.OnHttpDownloadCompleteListener.java
edu.bupt.mccdash.bluetooth.BluetoothConnectionService.java
edu.bupt.mccdash.bluetooth.DeviceListActivity.java
edu.bupt.mccdash.bluetooth.FileInfo.java
edu.bupt.mccdash.bluetooth.WirelessConnectionService.java
edu.bupt.mccdash.cpuutils.CpuManager.java
edu.bupt.mccdash.io.FileFactory.java
edu.bupt.mccdash.io.Logger.java
edu.bupt.mccdash.m3u8.FakeStreamPlayer.java
edu.bupt.mccdash.m3u8.M3u8Resolver.java