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