1 package org.apache.maven.surefire.util.internal;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import org.apache.maven.surefire.report.ReporterConfiguration;
23
24 import java.io.File;
25 import java.io.FileOutputStream;
26 import java.io.IOException;
27 import java.io.OutputStreamWriter;
28 import java.io.PrintWriter;
29 import java.io.Writer;
30 import java.text.SimpleDateFormat;
31 import java.util.Date;
32
33 import static org.apache.maven.surefire.util.internal.StringUtils.UTF_8;
34
35
36
37
38
39
40
41
42 public final class DumpFileUtils
43 {
44 private DumpFileUtils()
45 {
46 throw new IllegalStateException( "no instantiable constructor" );
47 }
48
49
50
51
52
53
54
55 public static synchronized File newDumpFile( String dumpFileName, ReporterConfiguration configuration )
56 {
57 return new File( configuration.getReportsDirectory(), dumpFileName );
58 }
59
60 public static void dumpException( Throwable t, File dumpFile )
61 {
62 dumpException( t, null, dumpFile );
63 }
64
65 public static void dumpException( Throwable t, String msg, File dumpFile )
66 {
67 try
68 {
69 if ( t != null && dumpFile != null
70 && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
71 {
72 Writer fw = createWriter( dumpFile );
73 if ( msg != null )
74 {
75 fw.append( msg )
76 .append( StringUtils.NL );
77 }
78 PrintWriter pw = new PrintWriter( fw );
79 t.printStackTrace( pw );
80 pw.flush();
81 fw.append( StringUtils.NL )
82 .append( StringUtils.NL )
83 .close();
84 }
85 }
86 catch ( Exception e )
87 {
88
89 }
90 }
91
92 public static void dumpText( String msg, File dumpFile )
93 {
94 try
95 {
96 if ( msg != null && dumpFile != null
97 && ( dumpFile.exists() || mkdirs( dumpFile ) && dumpFile.createNewFile() ) )
98 {
99 createWriter( dumpFile )
100 .append( msg )
101 .append( StringUtils.NL )
102 .append( StringUtils.NL )
103 .close();
104 }
105 }
106 catch ( Exception e )
107 {
108
109 }
110 }
111
112 public static String newFormattedDateFileName()
113 {
114 return new SimpleDateFormat( "yyyy-MM-dd'T'HH-mm-ss_SSS" ).format( new Date() );
115 }
116
117 private static Writer createWriter( File dumpFile ) throws IOException
118 {
119 return new OutputStreamWriter( new FileOutputStream( dumpFile, true ), UTF_8 )
120 .append( "# Created at " )
121 .append( new SimpleDateFormat( "yyyy-MM-dd'T'HH:mm:ss.SSS" ).format( new Date() ) )
122 .append( StringUtils.NL );
123 }
124
125 private static boolean mkdirs( File dumpFile )
126 {
127 File dir = dumpFile.getParentFile();
128 return dir.exists() || dir.mkdirs();
129 }
130 }