Add git hash and version name
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / LogUtil.java
1 package ai.suanzi.rtmpclient;
2
3 import android.content.Intent;
4
5 import org.apache.log4j.Level;
6 import org.apache.log4j.Logger;
7 import de.mindpipe.android.logging.log4j.LogConfigurator;
8 import java.util.Properties;
9 import javax.mail.Transport;
10 import javax.mail.Session;
11 import javax.mail.internet.MimeMessage;
12 import javax.mail.internet.InternetAddress;
13 import javax.activation.DataHandler;
14 import javax.activation.DataSource;
15 import javax.activation.FileDataSource;
16 import javax.mail.BodyPart;
17 import javax.mail.Multipart;
18 import javax.mail.internet.MimeBodyPart;
19 import javax.mail.internet.MimeMultipart;
20
21 import java.util.Date;
22
23 import android.os.AsyncTask;
24 import android.os.StrictMode;
25
26 import java.io.File;
27 import java.util.zip.ZipEntry;
28 import java.util.zip.ZipOutputStream;
29 import java.io.FileOutputStream;
30 import java.io.FileInputStream;
31 import java.io.BufferedInputStream;
32 import java.io.BufferedOutputStream;
33 import java.io.IOException;
34
35 public class LogUtil {
36
37     private static Logger gLogger;
38     private static final String FILE_NAME = "log.txt";
39     private static final long MAX_SIZE = 1024 * 1024 * 10; // 10M
40     private static String logFile = "";
41     private static File DIR;
42
43
44     public static void config(File dir) {
45         try {
46             final LogConfigurator logConfigurator = new LogConfigurator();
47             DIR = dir;
48             logFile = dir + File.separator + FILE_NAME;
49             logConfigurator.setFileName(logFile);
50             logConfigurator.setRootLevel(Level.DEBUG);
51             logConfigurator.setLevel("org.apache", Level.ERROR);
52             logConfigurator.setMaxFileSize(MAX_SIZE);
53             logConfigurator.configure();
54             gLogger = Logger.getLogger("LogUtil");
55             gLogger.error("#######################################");
56             gLogger.error("RtmpClient for Android by suanzi.ai  ");
57             gLogger.error("Git Revision: " + BuildConfig.GIT_REVISION);
58             gLogger.error("Version : " + BuildConfig.VERSION_NAME);
59             gLogger.debug("Log file is located at: " + logFile);
60
61         } catch (Exception e){
62             gLogger.error("LogUtil.config error: " + e.getMessage());
63             e.printStackTrace();
64         }
65     }
66
67
68     public static void sendLogs (){
69
70         AsyncTask<Void, Integer, Boolean> mailTask = new AsyncTask<Void, Integer, Boolean>() {
71
72             private String zfile = DIR + File.separator + "log.zip";
73
74
75             @Override
76             protected Boolean doInBackground(Void... voids) {
77
78                 String[] s = new String[1];
79                 s[0] = logFile;
80                 gLogger.debug("zipLog - " + zfile);
81                 try {
82                     zip(s, zfile);
83                 }catch (Exception e) {
84                     gLogger.error("zipLog, error: " + e.getMessage());
85                     e.printStackTrace();
86                     return false;
87                 }
88
89                 sendMail(zfile);
90                 return true;
91             }
92             @Override
93             protected void onPostExecute(Boolean result){
94                 if(result) {
95                     gLogger.debug("Zip file completed");
96                     File zz = new File(zfile);
97                     zz.delete();
98                 } else {
99                     gLogger.error("zip file error");
100                 }
101             }
102         };
103         mailTask.execute();
104
105     }
106
107     private static final String SMTP_SERVER = "smtp.exmail.qq.com";
108     private static final String USER = "support@suanzi.ai";
109     private static final String SENT_MAIL = "support@suanzi.ai";
110     private static final String PASSWORD = "oqiDX8fcWa58CmNf";
111     private static final String RECV_MAIL = "support@suanzi.ai";
112
113     private static boolean sendMail(String attachment){
114
115         String account = UserInfo.user;
116         String subject = UserInfo.macAddr + " - " + BuildConfig.VERSION_NAME + " - " + BuildConfig.GIT_REVISION;
117         StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
118         StrictMode.setThreadPolicy(policy);
119
120         Properties props = new Properties();
121         props.setProperty("mail.transport.protocol", "smtp");
122         props.setProperty("mail.smtp.host", SMTP_SERVER);
123         props.setProperty("mail.smtp.auth", "true");
124         /*final String smtpPort = "465";
125         props.setProperty("mail.smtp.port", smtpPort);
126         props.setProperty("mail.smtp.socketFactory.class", "javax.NET.ssl.SSLSocketFactory");
127         props.setProperty("mail.smtp.socketFactory.fallback", "false");
128         props.setProperty("mail.smtp.socketFactory.port", smtpPort);*/
129         Session session = Session.getDefaultInstance(props);
130         session.setDebug(true);
131
132         MimeMessage message = new MimeMessage(session);
133         try {
134             message.setFrom(new InternetAddress(SENT_MAIL, account, "UTF-8"));
135             message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(RECV_MAIL, "support", "UTF-8"));
136             message.setSubject(subject, "UTF-8");
137             //message.setContent("hahaha", "text/html;charset=UTF-8");
138             message.setSentDate(new Date());
139
140
141             BodyPart messageBodyPart = new MimeBodyPart();
142             messageBodyPart.setText("This is message body");
143             Multipart multipart = new MimeMultipart();
144             multipart.addBodyPart(messageBodyPart);
145             messageBodyPart = new MimeBodyPart();
146             DataSource source = new FileDataSource(attachment);
147             messageBodyPart.setDataHandler(new DataHandler(source));
148
149             String fname = attachment.substring(attachment.lastIndexOf("/") + 1);
150             messageBodyPart.setFileName(fname);
151             multipart.addBodyPart(messageBodyPart);
152
153             // Send the complete message parts
154             message.setContent(multipart);
155             message.saveChanges();
156
157             Transport transport = session.getTransport();
158             transport.connect(USER, PASSWORD);
159             transport.sendMessage(message, message.getAllRecipients());
160             transport.close();
161         } catch (Exception e){
162             gLogger.error("send mail, error: " + e.getMessage());
163             e.printStackTrace();
164             return false;
165         }
166         return true;
167     }
168
169     private static final int BUFFER_SIZE = 8192;
170     private static void zip(String[] files, String zipFile) throws IOException {
171         BufferedInputStream origin = null;
172         ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(zipFile)));
173
174         try {
175             byte data[] = new byte[BUFFER_SIZE];
176
177             for (int i = 0; i < files.length; i++) {
178                 FileInputStream fi = new FileInputStream(files[i]);
179                 origin = new BufferedInputStream(fi, BUFFER_SIZE);
180                 try {
181                     ZipEntry entry = new ZipEntry(files[i].substring(files[i].lastIndexOf("/") + 1));
182                     out.putNextEntry(entry);
183                     int count;
184                     while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
185                         out.write(data, 0, count);
186                     }
187                 }
188                 finally {
189                     origin.close();
190                 }
191             }
192         }catch (Exception e){
193             e.printStackTrace();
194         } finally {
195             out.close();
196         }
197     }
198 }