aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/plugin/loggingutils/LogsCollector.java
blob: df855862096d22067d2db8ae18d17bd6f8891753 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license. See terms of license at gnu.org.
 */
package net.java.sip.communicator.plugin.loggingutils;

import java.io.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
import java.util.zip.*;

import net.java.sip.communicator.util.Logger;

import org.jitsi.service.fileaccess.*;
import org.jitsi.util.*;

/**
 * Collects logs and save them in compressed zip file.
 * @author Damian Minkov
 */
public class LogsCollector
{
    /**
     * Our Logger.
     */
    private static final Logger logger
            = Logger.getLogger(LogsCollector.class);

    /**
     * The name of the log dir.
     */
    final static String LOGGING_DIR_NAME = "log";

    /**
     * The prefix name of standard java crash log file.
     */
    private static final String JAVA_ERROR_LOG_PREFIX = "hs_err_pid";

    /**
     * The date format used in file names.
     */
    private static final SimpleDateFormat FORMAT
        = new SimpleDateFormat("yyyy-MM-dd@HH.mm.ss");

    /**
     * The pattern we use to match crash logs.
     */
    private static Pattern JAVA_ERROR_LOG_PATTERN =
            Pattern.compile(
                    Pattern.quote("sip.communicator"),
                    Pattern.CASE_INSENSITIVE);

    /**
     * Save the log files in archived file. If destination is a folder
     * we generate filename with current date and time. If the destination
     * is null we do nothing and if its a file we use at, as we check
     * does it end with zip extension, is missing we add it.
     * @param destination the possible destination archived file
     * @param optional an optional file to be added to the archive.
     * @return the resulting file in zip format
     */
    public static File collectLogs(File destination, File optional)
    {
        if(destination == null)
            return null;

        if(!destination.isDirectory())
        {
            if(!destination.getName().endsWith("zip"))
                destination = new File(destination.getParentFile(),
                        destination.getName() + ".zip");
        }
        else
        {
            destination = new File(destination, getDefaultFileName());
        }

        try
        {
            ZipOutputStream out = new ZipOutputStream(
                new FileOutputStream(destination));

            collectHomeFolderLogs(out);
            collectJavaCrashLogs(out);

            if(optional != null)
            {
                addFileToZip(optional, out);
            }

            out.close();

            return destination;
        }
        catch(FileNotFoundException ex)
        {
            logger.error("Error creating logs file archive", ex);
        }
        catch(IOException ex)
        {
            logger.error("Error closing archive file", ex);
        }

        return null;
    }

    /**
     * The default filename to use.
     * @return the default filename to use.
     */
    public static String getDefaultFileName()
    {
        return FORMAT.format(new Date()) + "-logs.zip";
    }

    /**
     * Collects all files from log folder except the lock file.
     * And put them in the zip file as zip entries.
     * @param out the output zip file.
     */
    private static void collectHomeFolderLogs(ZipOutputStream out)
    {
        try
        {
            File[] fs = LoggingUtilsActivator.getFileAccessService()
                .getPrivatePersistentDirectory(LOGGING_DIR_NAME,
                    FileCategory.LOG).listFiles();

            for(File f : fs)
            {
                if(f.getName().endsWith(".lck"))
                    continue;

                addFileToZip(f, out);
            }
        }
        catch(Exception e)
        {
            logger.error("Error obtaining logs folder", e);
        }
    }

    /**
     * Copies a file to the given archive.
     * @param file the file to copy.
     * @param out the output archive stream.
     */
    private static void addFileToZip(File file, ZipOutputStream out)
    {
        byte[] buf = new byte[1024];

        try
        {
            FileInputStream in = new FileInputStream(file);

            // new ZIP entry
            out.putNextEntry(new ZipEntry(
                LOGGING_DIR_NAME + File.separator + file.getName()));

            // transfer bytes
            int len;
            while ((len = in.read(buf)) > 0)
            {
                out.write(buf, 0, len);
            }

            out.closeEntry();
            in.close();
        }
        catch(FileNotFoundException ex)
        {
            logger.error("Error obtaining file to archive", ex);
        }
        catch(IOException ex)
        {
            logger.error("Error saving file to archive", ex);
        }
    }

    /**
     * Searches for java crash logs belonging to us and add them to
     * the log archive.
     *
     * @param out the output archive stream.
     */
    private static void collectJavaCrashLogs(ZipOutputStream out)
    {
        // First check in working dir
        addCrashFilesToArchive(
            new File(".").listFiles(),
            JAVA_ERROR_LOG_PREFIX,
            out);

        String homeDir = System.getProperty("user.home");

        // If we don't have permissions to write to working directory
        // the crash logs maybe in the temp directory,
        // or on the Desktop if its windows
        if(OSUtils.IS_WINDOWS)
        {
            // check Desktop
            File[] desktopFiles =
                new File(homeDir + File.separator + "Desktop").listFiles();
            addCrashFilesToArchive(desktopFiles, JAVA_ERROR_LOG_PREFIX, out);
        }

        if(OSUtils.IS_MAC)
        {
            String logDir = "/Library/Logs/";

            // Look in the following directories:
            // /Library/Logs/CrashReporter (OSX 10.4, 10.5)
            // ~/Library/Logs/CrashReporter (OSX 10.4, 10.5)
            // ~/Library/Logs/DiagnosticReports (OSX 10.6, 10.7, 10.8)
            //
            // Note that for 10.6, there are aliases in
            // ~/Library/Logs/CrashReporter for the crash files in
            // ~/Library/Logs/DiagnosticReports, but the code won't load the
            // aliases so we shouldn't get duplicates.
            String[] locations = {logDir + "CrashReporter",
                                  homeDir + logDir + "CrashReporter",
                                  homeDir + logDir + "DiagnosticReports"};

            for (String location : locations)
            {
                File[] crashLogs = new File(location).listFiles();
                addCrashFilesToArchive(crashLogs, null, out);
            }
        }
        else
        {
            // search in /tmp folder
            // Solaris OS and Linux the temporary directory is /tmp
            // windows TMP or TEMP environment variable is the temporary folder

            //java.io.tmpdir
            File[] tempFiles =
                new File(System.getProperty("java.io.tmpdir")).listFiles();
            addCrashFilesToArchive(tempFiles, JAVA_ERROR_LOG_PREFIX, out);
        }
    }

    /**
     * Checks if file is a crash log file and does it belongs to us.
     * @param files files to check.
     * @param filterStartsWith a prefix for the files, can be null if no
     * prefix check should be made.
     * @param out the output archive stream.
     */
    private static void addCrashFilesToArchive(
            File files[], String filterStartsWith, ZipOutputStream out)
    {
        // no files to add
        if(files == null)
            return;

        // First check in working dir
        for(File f: files)
        {
            if(filterStartsWith != null
                && !f.getName().startsWith(filterStartsWith))
            {
                continue;
            }

            if(isOurCrashLog(f))
            {
                addFileToZip(f, out);
            }
        }
    }

    /**
     * Checks whether the crash log file is for our application.
     * @param file the crash log file.
     * @return <tt>true</tt> if error log is ours.
     */
    private static boolean isOurCrashLog(File file)
    {
        try
        {
            BufferedReader reader = new BufferedReader(new FileReader(file));

            try
            {
                String line;

                while((line = reader.readLine()) != null)
                    if(JAVA_ERROR_LOG_PATTERN.matcher(line).find())
                        return true;
            }
            finally
            {
                reader.close();
            }
        }
        catch(Throwable t)
        {}

        return false;
    }
}