Added Log4J for logging to a file
This commit is contained in:
parent
e5ca4aea34
commit
fa69c367e5
18 changed files with 127 additions and 26 deletions
|
|
@ -1,12 +1,16 @@
|
|||
package com.ericsson.uecontrol.gui;
|
||||
|
||||
import android.app.ActionBar;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.SharedPreferences;
|
||||
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
|
||||
import android.os.Bundle;
|
||||
import android.os.Environment;
|
||||
import android.os.Handler;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.support.v4.app.FragmentActivity;
|
||||
import android.util.Log;
|
||||
import android.view.Menu;
|
||||
import android.view.MenuItem;
|
||||
import android.widget.Toast;
|
||||
|
|
@ -20,8 +24,16 @@ import com.ericsson.uecontrol.gui.fragments.NavigationDrawerFragment;
|
|||
import com.ericsson.uecontrol.gui.fragments.StatusFragment;
|
||||
import com.ericsson.uecontrol.gui.util.CSVWriter;
|
||||
|
||||
import org.apache.log4j.Level;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
public class MainActivity extends FragmentActivity {
|
||||
import java.io.File;
|
||||
|
||||
import de.mindpipe.android.logging.log4j.LogConfigurator;
|
||||
|
||||
|
||||
public class MainActivity extends FragmentActivity implements OnSharedPreferenceChangeListener{
|
||||
private static final Logger log = Logger.getLogger(MainActivity.class);
|
||||
|
||||
/**
|
||||
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
|
||||
|
|
@ -31,14 +43,18 @@ public class MainActivity extends FragmentActivity {
|
|||
private boolean backButtonPressed = false;
|
||||
|
||||
private static UeControlExecutor currentExecutor;
|
||||
private static CSVWriter logger;
|
||||
private static CSVWriter csvLogger;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
|
||||
// Setup Debugging
|
||||
setupDebugLogging();
|
||||
|
||||
// Setup Main GUI
|
||||
setContentView(R.layout.activity_main);
|
||||
//navigationDrawerFragment = (NavigationDrawerFragment)
|
||||
// getFragmentManager().findFragmentById(R.id.navigation_drawer);
|
||||
statusFragment = (StatusFragment)
|
||||
|
|
@ -58,6 +74,37 @@ public class MainActivity extends FragmentActivity {
|
|||
currentExecutor.setThroughputListener(statusFragment.getThroughputListener());
|
||||
}
|
||||
|
||||
public void setupDebugLogging(){
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
String path = prefs.getString("logging_path", "/sdcard/uecontrol/");
|
||||
if(!path.endsWith("/")) path += File.separator;
|
||||
|
||||
LogConfigurator logConfigurator = new LogConfigurator();
|
||||
logConfigurator.setFileName(path + "uecontrol.log");
|
||||
if(prefs.getBoolean("debug", false))
|
||||
logConfigurator.setRootLevel(Level.DEBUG);
|
||||
else
|
||||
logConfigurator.setRootLevel(Level.WARN);
|
||||
// Set log level of a specific logger
|
||||
logConfigurator.setLevel("org.apache", Level.ERROR);
|
||||
logConfigurator.configure();
|
||||
|
||||
prefs.registerOnSharedPreferenceChangeListener(this);
|
||||
}
|
||||
|
||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
||||
log.debug("Preference changed: "+key);
|
||||
if(key.equals("debug")) {
|
||||
if (sharedPreferences.getBoolean("debug", false)) {
|
||||
Logger.getRootLogger().setLevel(Level.DEBUG);
|
||||
log.info("Enabling debug logging.");
|
||||
} else {
|
||||
log.info("Disabling debug logging.");
|
||||
Logger.getRootLogger().setLevel(Level.WARN);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public boolean onCreateOptionsMenu(Menu menu) {
|
||||
|
|
@ -87,14 +134,14 @@ public class MainActivity extends FragmentActivity {
|
|||
if(currentExecutor.isRunning()){
|
||||
currentExecutor.terminate();
|
||||
item.setTitle(R.string.action_run);
|
||||
logger = null;
|
||||
csvLogger = null;
|
||||
}
|
||||
else {
|
||||
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
|
||||
if(prefs.getBoolean("logging", false))
|
||||
logger = new CSVWriter(this);
|
||||
csvLogger = new CSVWriter(this);
|
||||
else
|
||||
logger = null;
|
||||
csvLogger = null;
|
||||
currentExecutor.execute();
|
||||
item.setTitle(R.string.action_stop);
|
||||
}
|
||||
|
|
@ -117,12 +164,13 @@ public class MainActivity extends FragmentActivity {
|
|||
}
|
||||
|
||||
public static void logThroughput(double downThroughput, double upThroughput) {
|
||||
if(logger == null || currentExecutor == null || currentExecutor.getRunningBehaviour() == null)
|
||||
if(csvLogger == null || currentExecutor == null || currentExecutor.getRunningBehaviour() == null)
|
||||
return;
|
||||
|
||||
logger.write(currentExecutor.getRunningBehaviour().getName(), downThroughput, upThroughput);
|
||||
csvLogger.write(currentExecutor.getRunningBehaviour().getName(), downThroughput, upThroughput);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onBackPressed() {
|
||||
if (backButtonPressed) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue