Back to project page AndroidMemoryLogger.
The source code is released under:
MIT License
If you think the Android project AndroidMemoryLogger listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.otiasj.memoryLogger.loggers; //w ww . j av a 2 s. co m import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import android.content.Context; import android.util.Log; import com.otiasj.memoryLogger.utils.MemoryUtils; /** * Write a log to a sdcard file */ public class FileLogger implements OnMemoryLog { public static final String TAG = FileLogger.class.getCanonicalName(); private final String mLogPath; private PrintWriter mLogFile = null; /** * @param context * @param filePath the path of the file where to write the log data */ public FileLogger(final Context context, final String filePath) { mLogPath = filePath; Log.v(FileLogger.TAG, "Logging memory usage to file " + mLogPath); } @Override public void onLog(final String tag, final double allocated, final double heapSize, final double percent, final double nativeUsed, final double nativeHeapSize) { if (mLogPath != null) { try { // Append data to the file final File file = new File(mLogPath); if (mLogFile == null) { mLogFile = new PrintWriter(new BufferedWriter(new FileWriter(file, true))); } mLogFile.println("" + MemoryUtils.decimalFormat.format(allocated) + "," + MemoryUtils.decimalFormat.format(heapSize) + "," + MemoryUtils.decimalPercentFormat.format(percent) + "," + MemoryUtils.decimalPercentFormat.format(nativeUsed) + "," + MemoryUtils.decimalPercentFormat.format(nativeHeapSize)); mLogFile.flush(); } catch (final IOException e) { e.printStackTrace(); } } } }