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