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