defae214bbf0d4f9700716a3fdaab9d5c78503a0
[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 private static final String SIMPLE_DATE_FORMAT = "HH:mm:ss";
16 private static final long MAX_FILE_SIZE = 10000;
17
18 private static File mLogFile;
19 private static File mFolder;
20 private static BufferedWriter mBuf;
21
22 private static String[] mLogFileNames = {"currentLog.txt", "backupLog.txt"};
23 private static boolean isMaxFileSizeReached = false;
24
25 public static void i(String TAG, String message){
26
27 // Write the log message to the file
28 appendLog(TAG+" : "+ message);
29 }
30
31 public static void d(String TAG, String message){
32 Log.d(TAG, message);
33 appendLog(TAG + " : " + message);
34 }
35 public static void d(String TAG, String message, Exception e) {
36 Log.d(TAG, message, e);
37 appendLog(TAG + " : " + message + " Exception : "+ e.getStackTrace());
38 }
39 public static void e(String TAG, String message){
40 Log.e(TAG, message);
41 appendLog(TAG + " : " + message);
42 }
43
44 public static void e(String TAG, String message, Throwable e) {
45 Log.e(TAG, message, e);
46 appendLog(TAG+" : " + message +" Exception : " + e.getStackTrace());
47 }
48
49 public static void v(String TAG, String message){
50 Log.v(TAG, message);
51 appendLog(TAG+" : "+ message);
52 }
53
54 public static void w(String TAG, String message) {
55 Log.w(TAG,message);
56 appendLog(TAG+" : "+ message);
57 }
58
59 public static void wtf(String TAG, String message) {
60 Log.wtf(TAG,message);
61 appendLog(TAG+" : "+ message);
62 }
63
64 /**
65 * Start doing logging
66 * @param logPath : path of log file
67 */
68 public static void startLogging(String logPath) {
69 mFolder = new File(logPath);
70 mLogFile = new File(mFolder + File.separator + mLogFileNames[0]);
71
72 boolean isFileCreated = false;
73
74 if (!mFolder.exists()) {
75 mFolder.mkdirs();
76 isFileCreated = true;
77 Log.d("LOG_OC", "Log file created");
78 }
79
80 try {
81
82 if (isMaxFileSizeReached) {
83
84 // Move current log file info to another file
85 File secondLogFile = new File(mFolder + File.separator + mLogFileNames[1]);
86 if (mLogFile.exists()) {
87 mLogFile.renameTo(secondLogFile);
88 }
89
90 // Construct a new file for current log info
91 mLogFile = new File(mFolder + File.separator + mLogFileNames[0]);
92 isMaxFileSizeReached = false;
93 }
94
95 // Create the current log file if does not exist
96 mLogFile.createNewFile();
97 mBuf = new BufferedWriter(new FileWriter(mLogFile, true));
98 if (isFileCreated) {
99 appendPhoneInfo();
100 }
101
102 // Check if current log file size is bigger than the max file size defined
103 if (mLogFile.length() > MAX_FILE_SIZE) {
104 isMaxFileSizeReached = true;
105 }
106 } catch (IOException e) {
107 e.printStackTrace();
108 }
109 }
110
111 /**
112 * Stop doing logging
113 */
114 public static void stopLogging() {
115 if (mLogFile != null) {
116 try {
117 mBuf = new BufferedWriter(new FileWriter(mLogFile, false));
118 mBuf.append("");
119 mBuf.close();
120 } catch (IOException e) {
121 e.printStackTrace();
122 }
123 }
124 }
125
126 /**
127 * Append the info of the device
128 */
129 private static void appendPhoneInfo() {
130 appendLog("Model : " + android.os.Build.MODEL);
131 appendLog("Brand : " + android.os.Build.BRAND);
132 appendLog("Product : " + android.os.Build.PRODUCT);
133 appendLog("Device : " + android.os.Build.DEVICE);
134 appendLog("Version-Codename : " + android.os.Build.VERSION.CODENAME);
135 appendLog("Version-Release : " + android.os.Build.VERSION.RELEASE);
136 }
137
138 /**
139 * Append to the log file the info passed
140 * @param text : text for adding to the log file
141 */
142 private static void appendLog(String text) {
143 String logPath = Environment.getExternalStorageDirectory()+File.separator+"owncloud"+File.separator+"log";
144 startLogging(logPath);
145 String timeStamp = new SimpleDateFormat(SIMPLE_DATE_FORMAT).format(Calendar.getInstance().getTime());
146
147 try {
148 mBuf = new BufferedWriter(new FileWriter(mLogFile, true));
149 mBuf.write(timeStamp + " -> " +text);
150 mBuf.newLine();
151 mBuf.close();
152 } catch (IOException e) {
153 e.printStackTrace();
154 }
155 }
156 }