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