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

FileLoggerStrategy.java « log « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cf2610fbe237066cb1deb4bd15b282ff1021a208 (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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.mapswithme.util.log;

import android.app.Application;
import android.content.Context;
import android.location.LocationManager;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import androidx.annotation.NonNull;
import android.util.Log;

import com.mapswithme.maps.BuildConfig;
import com.mapswithme.util.StorageUtils;
import com.mapswithme.util.Utils;
import net.jcip.annotations.Immutable;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.Executor;

@Immutable
class FileLoggerStrategy implements LoggerStrategy
{
  private static final String TAG = FileLoggerStrategy.class.getSimpleName();
  @NonNull
  private final String mFilePath;
  @NonNull
  private final Executor mExecutor;
  @NonNull
  private final Application mApplication;

  FileLoggerStrategy(@NonNull Application application, @NonNull String filePath,
                     @NonNull Executor executor)
  {
    mApplication = application;
    mFilePath = filePath;
    mExecutor = executor;
  }

  @Override
  public void v(String tag, String msg)
  {
    write("V/" + tag + ": " + msg);
  }

  @Override
  public void v(String tag, String msg, Throwable tr)
  {
    write("V/" + tag + ": " + msg + "\n" + Log.getStackTraceString(tr));
  }

  @Override
  public void d(String tag, String msg)
  {
    write("D/" + tag + ": " + msg);
  }

  @Override
  public void d(String tag, String msg, Throwable tr)
  {
    write("D/" + tag + ": " + msg + "\n" + Log.getStackTraceString(tr));
  }

  @Override
  public void i(String tag, String msg)
  {
    write("I/" + tag + ": " + msg);
  }

  @Override
  public void i(String tag, String msg, Throwable tr)
  {

    write("I/" + tag + ": " + msg + "\n" + Log.getStackTraceString(tr));
  }

  @Override
  public void w(String tag, String msg)
  {
    write("W/" + tag + ": " + msg);
  }

  @Override
  public void w(String tag, String msg, Throwable tr)
  {
    write("W/" + tag + ": " + msg + "\n" + Log.getStackTraceString(tr));
  }

  @Override
  public void w(String tag, Throwable tr)
  {
    write("D/" + tag + ": " + Log.getStackTraceString(tr));
  }

  @Override
  public void e(String tag, String msg)
  {
    write("E/" + tag + ": " + msg);
  }

  @Override
  public void e(String tag, String msg, Throwable tr)
  {
    write("E/" + tag + ": " + msg + "\n" + Log.getStackTraceString(tr));
  }

  private void write(@NonNull final String data)
  {
    mExecutor.execute(new WriteTask(mApplication, mFilePath, data, Thread.currentThread().getName()));
  }

  static class WriteTask implements Runnable
  {
    private static final int MAX_SIZE = 3000000;
    @NonNull
    private final String mFilePath;
    @NonNull
    private final String mData;
    @NonNull
    private final String mCallingThread;
    @NonNull
    private final Application mApplication;

    private WriteTask(@NonNull Application application, @NonNull String filePath,
                      @NonNull String data, @NonNull String callingThread)
    {
      mApplication = application;
      mFilePath = filePath;
      mData = data;
      mCallingThread = callingThread;
    }

    @Override
    public void run()
    {
      FileWriter fw = null;
      try
      {
        File file = new File(mFilePath);
        if (!file.exists() || file.length() > MAX_SIZE)
        {
          fw = new FileWriter(file, false);
          writeSystemInformation(mApplication, fw);
        }
        else
        {
          fw = new FileWriter(mFilePath, true);
        }
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.US);
        fw.write(formatter.format(new Date()) + " " + mCallingThread + ": " + mData + "\n");
      }
      catch (IOException e)
      {
        Log.e(TAG, "Failed to write the string: " + mData, e);
        Log.i(TAG, "Logs folder exists: " + StorageUtils.ensureLogsFolderExistence(mApplication));
      }
      finally
      {
        if (fw != null)
          try
          {
            fw.close();
          }
          catch (IOException e)
          {
            Log.e(TAG, "Failed to close file: " + mData, e);
          }
      }
    }

    static void writeSystemInformation(@NonNull Application application, @NonNull FileWriter fw)
        throws IOException
    {
      fw.write("Android version: " + Build.VERSION.SDK_INT + "\n");
      fw.write("Device: " + Utils.getFullDeviceModel() + "\n");
      fw.write("App version: " + BuildConfig.APPLICATION_ID + " " + BuildConfig.VERSION_NAME + "\n");
      fw.write("Installation ID: " + Utils.getInstallationId() + "\n");
      fw.write("Locale : " + Locale.getDefault());
      fw.write("\nNetworks : ");
      final ConnectivityManager manager = (ConnectivityManager) application.getSystemService(Context.CONNECTIVITY_SERVICE);
      for (NetworkInfo info : manager.getAllNetworkInfo())
        fw.write(info.toString());
      fw.write("\nLocation providers: ");
      final LocationManager locMngr = (android.location.LocationManager) application.getSystemService(Context.LOCATION_SERVICE);
      for (String provider: locMngr.getProviders(true))
        fw.write(provider + " ");
      fw.write("\n\n");
    }
  }
}