aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/fil/libre/repwifiapp/helpers/Logger.java
blob: 2641571da470f9751fee5722cdc0c9439b21d660 (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
//
// Copyright 2017, 2018 Filippo "Fil" Bergamo <fil.bergamo@riseup.net>
// 
// This file is part of RepWifiApp.
//
// RepWifiApp is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// RepWifiApp is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with RepWifiApp.  If not, see <http://www.gnu.org/licenses/>.
// 
// ********************************************************************

package fil.libre.repwifiapp.helpers;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import fil.libre.repwifiapp.Utils;
import fil.libre.repwifiapp.service.ConnectionManagementService;
import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;

public abstract class Logger {
    
    public static String APP_NAME = "RepWifi";
        
    private static int logPriority = 3;
    
    public static void logError(String msg, Exception e) {
        Log.e(APP_NAME, msg, e);
    }

    public static void logError(String msg) {
        Log.e(APP_NAME, msg);
    }

    public static void logDebug(String msg) {
        logDebug(msg, 0);
    }

    public static void logDebug(String msg, int level) {

        if (level < logPriority) {
            return;
        }

        Log.d(APP_NAME, msg);
    }
    

    @SuppressLint("SimpleDateFormat")
    public static String getLogDumpFile() {

        File f = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        if (f == null || !f.exists()) {
            return null;
        }

                
        String basefolder;
        try {
            basefolder = f.getCanonicalPath();
        } catch (Exception e) {
            logError("Exception while resolving canonical path for log dump file.", e);
            return null;
        }

        DateFormat dateFormat = new SimpleDateFormat("yyyyMMddhhmmss", Locale.getDefault());
        String ts = dateFormat.format(Calendar.getInstance().getTime());
        return basefolder + "/repwifi_log_dump." + ts + ".log";
    }
    
    public static boolean dumpLogcatToFile(Context appcontext){
        return dumpLogcatToFile(getLogDumpFile(),appcontext);
    }
    
    public static void setLogPriority(int priority){
                
        if (priority < 0){
            priority = 0;
        }
        
        logPriority = priority;
        
    }
            
    public static boolean dumpLogcatToFile(String filePath, Context appcontext) {

        if (filePath == null) {
            return false;
        }

        try {

            String cmd1 = "logcat -d | grep " + APP_NAME + ">" + filePath;
            String cmd2 = "logcat -d | grep -A10 -B10 " + appcontext.getPackageName()
                            + ">>" + filePath;
            String cmd3 = "logcat -d | grep " + ConnectionManagementService.LOG_TAG_NETWORKAGENT
                            + ">>" + filePath;
            String cmd4 = "logcat -d | grep " + ConnectionManagementService.LOG_TAG_SERVICE + ">>"
                            + filePath;

            String SEP_LOG = "\n\n---------- [REPWIFI_LOG_SEPARATOR] ----------\n\n";

            RootCommand c1 = new RootCommand(cmd1);
            RootCommand c2 = new RootCommand(cmd2);
            RootCommand c3 = new RootCommand(cmd3);
            RootCommand c4 = new RootCommand(cmd4);

            if (c1.execute() != 0) {
                return false;
            }

            if (!Utils.writeFile(filePath, SEP_LOG, false)) {
                return false;
            }

            if (c2.execute() != 0) {
                return false;
            }

            if (!Utils.writeFile(filePath, SEP_LOG, false)) {
                return false;
            }

            if (c3.execute() != 0) {
                return false;
            }

            if (c4.execute() != 0) {
                return false;
            }
            
            RootCommand.executeRootCmd("chmod 666 " + filePath);

            return true;

        } catch (Exception e) {
            logError("Exception during log dump.", e);
            return false;
        }

    }


    
    
}