Done some changes for storing the log messages on log file and grant that logs are...
[pub/Android/ownCloud.git] / src / com / owncloud / android / utils / Log_OC.java
1 package com.owncloud.android.utils;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.text.SimpleDateFormat;
8 import java.util.Calendar;
9
10 import android.os.Environment;
11 import android.util.Log;
12
13
14 public class Log_OC {
15
16
17 private static boolean isEnabled = false;
18 private static File logFile;
19 private static File folder;
20 private static BufferedWriter buf;
21
22 public static void i(String TAG, String message){
23 // Printing the message to LogCat console
24 int a = Log.i(TAG, message);
25 // Write the log message to the file
26 appendLog(TAG+" : "+message);
27 }
28
29 public static void d(String TAG, String message){
30 Log.d(TAG, message);
31 appendLog(TAG + " : " + message);
32 }
33 public static void d(String TAG, String message, Exception e) {
34 Log.d(TAG, message, e);
35 appendLog(TAG + " : " + message + " Exception : "+ e.getStackTrace());
36 }
37 public static void e(String TAG, String message){
38 Log.e(TAG, message);
39 appendLog(TAG + " : " + message);
40 }
41
42 public static void e(String TAG, String message, Throwable e) {
43 Log.e(TAG, message, e);
44 appendLog(TAG+" : " + message +" Exception : " + e.getStackTrace());
45 }
46
47 public static void v(String TAG, String message){
48 Log.v(TAG, message);
49 appendLog(TAG+" : "+ message);
50 }
51
52 public static void w(String TAG, String message) {
53 Log.w(TAG,message);
54 appendLog(TAG+" : "+ message);
55 }
56
57 public static void wtf(String TAG, String message) {
58 Log.wtf(TAG,message);
59 appendLog(TAG+" : "+ message);
60 }
61
62 public static void startLogging(String logPath) {
63 folder = new File(logPath);
64 logFile = new File(folder + File.separator + "log.txt");
65
66 boolean isFileCreated = false;
67
68 if (!folder.exists()) {
69 folder.mkdirs();
70 isFileCreated = true;
71 Log.d("LOG_OC", "Log file created");
72 }
73
74 try {
75 logFile.createNewFile();
76 buf = new BufferedWriter(new FileWriter(logFile, true));
77 isEnabled = true;
78 if (isFileCreated) {
79 appendPhoneInfo();
80 }
81 }catch (IOException e){
82 e.printStackTrace();
83 }
84 }
85
86 public static void stopLogging() {
87 if (logFile != null) {
88 isEnabled = false;
89 try {
90 buf = new BufferedWriter(new FileWriter(logFile, false));
91 buf.append("");
92 buf.close();
93 } catch (IOException e) {
94 e.printStackTrace();
95 }
96 }
97 }
98
99 private static void appendPhoneInfo() {
100 appendLog("Model : " + android.os.Build.MODEL);
101 appendLog("Brand : " + android.os.Build.BRAND);
102 appendLog("Product : " + android.os.Build.PRODUCT);
103 appendLog("Device : " + android.os.Build.DEVICE);
104 appendLog("Version-Codename : " + android.os.Build.VERSION.CODENAME);
105 appendLog("Version-Release : " + android.os.Build.VERSION.RELEASE);
106 }
107
108 private static void appendLog(String text) {
109 if (isEnabled) {
110 String logPath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
111 startLogging(logPath);
112 String timeStamp = new SimpleDateFormat("HH:mm:ss").format(Calendar.getInstance().getTime());
113
114 try {
115 buf = new BufferedWriter(new FileWriter(logFile, true));
116 buf.write(timeStamp + " -> " +text);
117 buf.newLine();
118 buf.close();
119 } catch (IOException e) {
120 e.printStackTrace();
121 }
122 }
123 }
124
125
126
127
128
129
130
131
132 }