Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ZipLogsTask.java « log « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 58c1759d7de454f2fec6c60c6474aae489ac9a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package com.mapswithme.util.log;

import android.app.Application;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;

import com.mapswithme.util.StorageUtils;
import com.mapswithme.util.Utils;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


class ZipLogsTask implements Runnable
{
  private final static String TAG = ZipLogsTask.class.getSimpleName();
  private final static int BUFFER_SIZE = 2048;
  @NonNull
  private final String mSourcePath;
  @NonNull
  private final String mDestPath;
  @Nullable
  private final LoggerFactory.OnZipCompletedListener mOnCompletedListener;
  @NonNull
  private final Application mApplication;

  ZipLogsTask(@NonNull Application application, @NonNull String sourcePath,
              @NonNull String destPath,
              @Nullable LoggerFactory.OnZipCompletedListener onCompletedListener)
  {
    mApplication = application;
    mSourcePath = sourcePath;
    mDestPath = destPath;
    mOnCompletedListener = onCompletedListener;
  }

  @Override
  public void run()
  {
    saveSystemLogcat();
    boolean success = zipFileAtPath(mSourcePath, mDestPath);
    if (mOnCompletedListener != null)
      mOnCompletedListener.onCompleted(success);
  }

  private boolean zipFileAtPath(@NonNull String sourcePath, @NonNull String toLocation)
  {
    File sourceFile = new File(sourcePath);
    try(FileOutputStream dest = new FileOutputStream(toLocation, false);
        ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(dest)))
    {
      if (sourceFile.isDirectory())
      {
        zipSubFolder(out, sourceFile, sourceFile.getParent().length());
      }
      else
      {
        try(FileInputStream fi = new FileInputStream(sourcePath);
            BufferedInputStream origin = new BufferedInputStream(fi, BUFFER_SIZE);)
        {
          ZipEntry entry = new ZipEntry(getLastPathComponent(sourcePath));
          out.putNextEntry(entry);
          byte data[] = new byte[BUFFER_SIZE];
          int count;
          while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1) {
            out.write(data, 0, count);
          }
        } catch (Exception e)
        {
          Log.e(TAG, "Failed to read zip file entry '" + sourcePath +"' to location '"
                  + toLocation + "'", e);
          return false;
        }
      }
    }
    catch (Exception e)
    {
      Log.e(TAG, "Failed to zip file '" + sourcePath +"' to location '" + toLocation + "'", e);
      return false;
    }
    return true;
  }

  private void zipSubFolder(ZipOutputStream out, File folder,
                            int basePathLength) throws IOException
  {
    File[] fileList = folder.listFiles();
    BufferedInputStream origin;
    for (File file : fileList)
    {
      if (file.isDirectory())
      {
        zipSubFolder(out, file, basePathLength);
      }
      else
      {
        byte data[] = new byte[BUFFER_SIZE];
        String unmodifiedFilePath = file.getPath();
        String relativePath = unmodifiedFilePath
            .substring(basePathLength);
        FileInputStream fi = new FileInputStream(unmodifiedFilePath);
        origin = new BufferedInputStream(fi, BUFFER_SIZE);
        ZipEntry entry = new ZipEntry(relativePath);
        out.putNextEntry(entry);
        int count;
        while ((count = origin.read(data, 0, BUFFER_SIZE)) != -1)
        {
          out.write(data, 0, count);
        }
        origin.close();
      }
    }
  }

  private static String getLastPathComponent(String filePath)
  {
    String[] segments = filePath.split(File.separator);
    if (segments.length == 0)
      return "";
    return segments[segments.length - 1];
  }

  private void saveSystemLogcat()
  {
    String fullName = StorageUtils.getLogsFolder(mApplication) + File.separator + "logcat.log";
    final File file = new File(fullName);
    InputStreamReader reader = null;
    FileWriter writer = null;
    try
    {
      writer = new FileWriter(file);
      FileLoggerStrategy.WriteTask.writeSystemInformation(mApplication, writer);
      String cmd = "logcat -d -v time";
      Process process = Runtime.getRuntime().exec(cmd);
      reader = new InputStreamReader(process.getInputStream());
      char[] buffer = new char[10000];
      do
      {
        int n = reader.read(buffer, 0, buffer.length);
        if (n == -1)
          break;
        writer.write(buffer, 0, n);
      } while (true);
    }
    catch (Throwable e)
    {
      Log.e(TAG, "Failed to save system logcat to file: " + file, e);
    }
    finally
    {
      Utils.closeSafely(writer);
      Utils.closeSafely(reader);
    }
  }
}