View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.surefire.api.util.internal;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.PrintWriter;
26  import java.io.Writer;
27  import java.text.SimpleDateFormat;
28  import java.util.Date;
29  
30  import static java.nio.charset.StandardCharsets.UTF_8;
31  
32  /**
33   * Dumps a text or exception in dump file.
34   * Each call logs a date when it was written to the dump file.
35   *
36   * @author <a href="mailto:tibordigana@apache.org">Tibor Digana (tibor17)</a>
37   * @since 2.20
38   */
39  public final class DumpFileUtils {
40      private DumpFileUtils() {
41          throw new IllegalStateException("no instantiable constructor");
42      }
43  
44      /**
45       * New dump file. Synchronized object appears in main memory and perfectly visible in other threads.
46       *
47       * @param reportsDir    only report directory
48       * @param dumpFileName    dump file name
49       */
50      public static synchronized File newDumpFile(File reportsDir, String dumpFileName) {
51          reportsDir.mkdirs();
52          return new File(reportsDir, dumpFileName);
53      }
54  
55      public static void dumpException(Throwable t, File dumpFile) {
56          dumpException(t, null, dumpFile);
57      }
58  
59      public static void dumpException(Throwable t, String msg, File dumpFile) {
60          try {
61              if (t != null && dumpFile != null && (dumpFile.exists() || mkdirs(dumpFile) && dumpFile.createNewFile())) {
62                  try (PrintWriter pw = new PrintWriter(createWriter(dumpFile))) {
63                      if (msg != null) {
64                          pw.append(msg).append(StringUtils.NL);
65                      }
66                      t.printStackTrace(pw);
67                      pw.append(StringUtils.NL).append(StringUtils.NL);
68                  }
69              }
70          } catch (Exception e) {
71              // do nothing
72          }
73      }
74  
75      public static void dumpText(String msg, File dumpFile) {
76          try {
77              if (msg != null
78                      && dumpFile != null
79                      && (dumpFile.exists() || mkdirs(dumpFile) && dumpFile.createNewFile())) {
80                  try (Writer writer = createWriter(dumpFile)) {
81                      writer.append(msg).append(StringUtils.NL).append(StringUtils.NL);
82                  }
83              }
84          } catch (Exception e) {
85              // do nothing
86          }
87      }
88  
89      public static String newFormattedDateFileName() {
90          return new SimpleDateFormat("yyyy-MM-dd'T'HH-mm-ss_SSS").format(new Date());
91      }
92  
93      private static Writer createWriter(File dumpFile) throws IOException {
94          return new OutputStreamWriter(new FileOutputStream(dumpFile, true), UTF_8)
95                  .append("# Created at ")
96                  .append(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS").format(new Date()))
97                  .append(StringUtils.NL);
98      }
99  
100     private static boolean mkdirs(File dumpFile) {
101         File dir = dumpFile.getParentFile();
102         return dir.exists() || dir.mkdirs();
103     }
104 }