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