Android Open Source - Rejsekort-Reminder Logger






From Project

Back to project page Rejsekort-Reminder.

License

The source code is released under:

GNU General Public License

If you think the Android project Rejsekort-Reminder 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.example.publictransportation.service;
/*from   ww  w . j av  a2  s.c  om*/
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

import android.os.Environment;
import android.util.Log;

public class Logger {

  String filename;
  ArrayList<LogItem> items;

  int BUFFER_LIMIT = 250;


  public Logger() {
    Log.i("Logger", "constructor");
    filename = Environment.getExternalStorageDirectory() + File.separator + "rejsereminder.log";
    Log.i("logger", "writing to: " + filename);
    items = new ArrayList<LogItem>();
  }

  public void log(LogTypes type, String data) {
    Log.i("Logger", "log()");
    LogItem item = new LogItem(type, data);
    items.add(item);

    if (items.size() == BUFFER_LIMIT) {
      writeToFile();
    }
  }

  private void writeToFile() {
    Log.i("Logger", "writeToFile()");
    File logFile = new File(filename);
    
    if (!logFile.exists()) {
      try {
        logFile.createNewFile();
      }
      catch (IOException e) {
        e.printStackTrace();
      }
    }
    
    try {
      // BufferedWriter for performance, true to set append to file flag
      BufferedWriter buf = new BufferedWriter(new FileWriter(logFile, true));
      for (LogItem item : items) {
        
        // so that it's easier to notice non-sensor output
        if (item.getType() != LogTypes.SENSOR) {
          buf.newLine();
        }
        
        buf.append(item.toString());
        buf.newLine();
      }
      buf.close();
      Log.i("logger", "successfully wrote to log file");
      items.clear();
      Log.i("logger", "cleared buffer");

    }
    catch (IOException e) {
      e.printStackTrace();
      Log.e("logger", "couldn't write to log file!!");
    }
  }

  public void kill() {
    Log.i("Logger", "Kill()");
    writeToFile();
  }
}




Java Source Code List

com.example.publictransportation.MainActivity.java
com.example.publictransportation.WidgetProvider.java
com.example.publictransportation.modes.AbstractMode.java
com.example.publictransportation.modes.ActivityResults.java
com.example.publictransportation.modes.BusMode.java
com.example.publictransportation.modes.DefaultMode.java
com.example.publictransportation.modes.ForcedMode.java
com.example.publictransportation.modes.MetroMode.java
com.example.publictransportation.modes.ModeTypes.java
com.example.publictransportation.modes.MovingMode.java
com.example.publictransportation.modes.STrainMode.java
com.example.publictransportation.modes.WaitingMode.java
com.example.publictransportation.profiles.AbstractProfile.java
com.example.publictransportation.profiles.DefaultProfile.java
com.example.publictransportation.sensors.AbstractSensor.java
com.example.publictransportation.sensors.ActivitySensorIntentService.java
com.example.publictransportation.sensors.ActivitySensor.java
com.example.publictransportation.sensors.CellSensor.java
com.example.publictransportation.sensors.SensorTypes.java
com.example.publictransportation.sensors.TimeSensor.java
com.example.publictransportation.sensors.WifiGroup.java
com.example.publictransportation.sensors.WifiSensor.java
com.example.publictransportation.service.IModeManager.java
com.example.publictransportation.service.LogItem.java
com.example.publictransportation.service.LogTypes.java
com.example.publictransportation.service.Logger.java
com.example.publictransportation.service.TrackerService.java