Commit 7ca54706 authored by Chengzhao Li's avatar Chengzhao Li
Browse files

- add logger

parent e487910e
......@@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">
......
......@@ -2,6 +2,9 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nclab.ncmultipeerchat">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
......
package com.nclab.ncmultipeerchat;
import android.content.Context;
import android.os.Environment;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
/**
* log to file
*/
public class NetworkLogger {
private BufferedWriter m_bufferedWriter;
public NetworkLogger(Context context, String fileName) {
String SUFFIX = ".txt";
Calendar calendar = Calendar.getInstance();
String year = String.valueOf(calendar.get(Calendar.YEAR));
String month = String.valueOf(calendar.get(Calendar.MONTH) + 1);
String date = String.valueOf(calendar.get(Calendar.DATE));
String hour = String.valueOf(calendar.get(Calendar.HOUR_OF_DAY));
String minute = String.valueOf(calendar.get(Calendar.MINUTE));
String second = String.valueOf(calendar.get(Calendar.SECOND));
String time = year+"-"+month+"-"+date+"-"+hour+"-"+minute+"-"+second;
String targetPath;
if (fileName.isEmpty()) {
targetPath = Environment.getExternalStorageDirectory().getPath() + File.separator + time + SUFFIX;
} else {
targetPath = Environment.getExternalStorageDirectory().getPath() + File.separator + fileName + "-" + time + SUFFIX;
}
File targetFile = new File(targetPath);
if (targetFile != null) {
if (!targetFile.exists()) {
try {
if (!targetFile.createNewFile())
{
Toast.makeText(context, "Can not create log file!", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
} else {
if (!targetFile.delete()) {
Toast.makeText(context, "Can not delete old log file!", Toast.LENGTH_SHORT).show();
} else {
try {
if (!targetFile.createNewFile())
{
Toast.makeText(context, "Can not create log file!", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
try {
m_bufferedWriter = new BufferedWriter(new FileWriter(targetPath, true));
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void writeHeaders(String str) {
try {
m_bufferedWriter.write(str);
m_bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
try {
m_bufferedWriter.flush();
m_bufferedWriter.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
public void write(String str, boolean isFlush) {
try {
m_bufferedWriter.write(System.getProperty("line.separator"));
m_bufferedWriter.write(str);
if (isFlush) {
m_bufferedWriter.flush();
}
} catch (IOException e) {
e.printStackTrace();
try {
m_bufferedWriter.flush();
m_bufferedWriter.close();
} catch (IOException e2) {
e2.printStackTrace();
}
}
}
public void flush() {
try {
m_bufferedWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
public void close(){
try {
m_bufferedWriter.flush();
m_bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment