Android Open Source - shareplay File Logger






From Project

Back to project page shareplay.

License

The source code is released under:

Copyright (c) 2014, Benjamin Damer All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: ...

If you think the Android project shareplay 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 com.afqa123.log;
/*from   w ww. j a  va2 s  . c  o  m*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;

import android.os.Environment;

class FileLogger implements Logger {

  private static final String LEVEL_ERROR = "ERROR";
  private static final String LEVEL_DEBUG = "DEBUG";
  private static final String LEVEL_WARNING = "WARN";
  private static final String LEVEL_INFO = "INFO";

  private final String className;
  
  public FileLogger(String forClass) {
    className = forClass;
  }
    
  private void log(String level, String msg, Throwable t) {
        File SDCardRoot = Environment.getExternalStorageDirectory();
        // @TODO this implementation should keep a file open and flush each time it logs
        File logFile = new File(SDCardRoot, "shareplay.log");    
    
    if (!logFile.exists()) {
      try {
        logFile.createNewFile();
        } catch (IOException e) {
          // tough luck...
        }
    }
    
    try {
      //BufferedWriter for performance, true to set append to file flag
        BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true)); 
        buf.append(new Date().toGMTString());
        buf.append(',');
        buf.append(level);
        buf.append(',');
        buf.append(className);
        buf.append(',');
        buf.append(msg);
        buf.newLine();

        if (t != null) {
          buf.append(t.getClass().getName());
          buf.append(t.getMessage());
          buf.newLine();
          
        StackTraceElement elements[] = t.getStackTrace();
        for (StackTraceElement el : elements) {
          buf.append("at " + el.getClassName() + "." + el.getMethodName() + "(" + el.getFileName() + ":" + el.getLineNumber() + ")");
          buf.newLine();
        }
        }
        buf.close();
    } catch (IOException e) {
    
    }
  }
  
  @Override
  public void debug(String msg) {
    log(LEVEL_DEBUG, msg, null);
  }

  @Override
  public void debug(String msg, Throwable t) {
    log(LEVEL_DEBUG, msg, t);
  }

  @Override
  public void warn(String msg) {
    log(LEVEL_WARNING, msg, null);
  }

  @Override
  public void warn(String msg, Throwable t) {
    log(LEVEL_WARNING, msg, t);        
  }

  @Override
  public void info(String msg) {
    log(LEVEL_INFO, msg, null);
  }

  @Override
  public void info(String msg, Throwable t) {
    log(LEVEL_INFO, msg, t);
  }

  @Override
  public void error(String msg) {
    log(LEVEL_ERROR, msg, null);
  }

  @Override
  public void error(String msg, Throwable t) {
    log(LEVEL_ERROR, msg, t);  
  }
}




Java Source Code List

com.afqa123.log.DefaultLogger.java
com.afqa123.log.FileLogger.java
com.afqa123.log.LoggerFactory.java
com.afqa123.log.Logger.java
com.afqa123.log.NullLogger.java
com.afqa123.shareplay.DialogFactory.java
com.afqa123.shareplay.MediaPlayerService.java
com.afqa123.shareplay.SelectionActivity.java
com.afqa123.shareplay.SharePlayActivity.java
com.afqa123.shareplay.common.AuthorizationException.java
com.afqa123.shareplay.common.Base64.java
com.afqa123.shareplay.common.Constants.java
com.afqa123.shareplay.common.CustomFeedback.java
com.afqa123.shareplay.common.DAAPException.java
com.afqa123.shareplay.common.DBHelper.java
com.afqa123.shareplay.common.Feedback.java
com.afqa123.shareplay.common.Filename.java
com.afqa123.shareplay.common.ListWrapper.java
com.afqa123.shareplay.common.StoppableThread.java
com.afqa123.shareplay.common.StreamProxy.java
com.afqa123.shareplay.data.ContentCode.java
com.afqa123.shareplay.data.Item.java
com.afqa123.shareplay.data.Playlist.java
com.afqa123.shareplay.impl.Client.java
com.afqa123.shareplay.impl.DatabaseCatalog.java
com.afqa123.shareplay.impl.ServerProvider.java
com.afqa123.shareplay.impl.Server.java
com.afqa123.shareplay.interfaces.Catalog.java
com.afqa123.shareplay.interfaces.IClient.java
com.afqa123.shareplay.interfaces.IServerProvider.java