-
Notifications
You must be signed in to change notification settings - Fork 6
/
FileLog.java
263 lines (213 loc) · 6.42 KB
/
FileLog.java
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
package com.example.filelogsampleapp.logging;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
/**
* Sends Log output to a file
* Created by volker on 06.02.15.
*/
public class FileLog
{
private static final String TIMESTAMP_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";
private static final String MSG_FORMAT = "%s: %s - %s"; // timestamp, tag, message
private static String sLogFilePath;
private static File sTheLogFile;
private static BufferedWriter sBufferedWriter;
private static int sCurrentPriority;
private static int sFileSizeLimit; // bytes
public static void open( String logFilePath, int currentPriority, int fileSizeLimit )
{
sLogFilePath = logFilePath;
sCurrentPriority = currentPriority;
sFileSizeLimit = fileSizeLimit;
sTheLogFile = new File( sLogFilePath );
if ( !sTheLogFile.exists( ) )
{
try
{
sTheLogFile.createNewFile( );
}
catch ( IOException e )
{
Log.e( "FileLog", Log.getStackTraceString( e ) );
}
}
checkFileSize( );
try
{
sBufferedWriter = new BufferedWriter( new FileWriter( sTheLogFile, true ) );
}
catch ( IOException e )
{
Log.e( "FileLog", Log.getStackTraceString( e ) );
}
}
public static void setCurrentPriority( int currentPriority )
{
sCurrentPriority = currentPriority;
}
public static void close( )
{
try
{
if ( sBufferedWriter != null )
{
sBufferedWriter.newLine( );
sBufferedWriter.flush( );
sBufferedWriter.close( );
}
}
catch ( IOException e )
{
Log.e( "FileLog", Log.getStackTraceString( e ) );
}
}
public static void delete( )
{
close( );
if ( sTheLogFile != null )
{
sTheLogFile.delete( );
}
}
public static int v( String tag, String msg )
{
writeToFile( Log.VERBOSE, tag, msg );
return Log.v( tag, msg );
}
public static int v( String tag, String msg, Throwable tr )
{
writeToFile( Log.VERBOSE, tag, msg, tr );
return Log.v( tag, msg, tr );
}
public static int d( String tag, String msg )
{
writeToFile( Log.DEBUG, tag, msg );
return Log.d( tag, msg );
}
public static int d( String tag, String msg, Throwable tr )
{
writeToFile( Log.DEBUG, tag, msg, tr );
return Log.d( tag, msg, tr );
}
public static int i( String tag, String msg )
{
writeToFile( Log.INFO, tag, msg );
return Log.i( tag, msg );
}
public static int i( String tag, String msg, Throwable tr )
{
writeToFile( Log.INFO, tag, msg, tr );
return Log.i( tag, msg, tr );
}
public static int w( String tag, String msg )
{
writeToFile( Log.WARN, tag, msg );
return Log.w( tag, msg );
}
public static int w( String tag, String msg, Throwable tr )
{
writeToFile( Log.WARN, tag, msg, tr );
return Log.w( tag, msg, tr );
}
public static int w( String tag, Throwable tr )
{
writeToFile( Log.WARN, tag, "", tr );
return Log.w( tag, tr );
}
public static int e( String tag, String msg )
{
writeToFile( Log.ERROR, tag, msg );
return Log.e( tag, msg );
}
public static int e( String tag, String msg, Throwable tr )
{
writeToFile( Log.ERROR, tag, msg, tr );
return Log.e( tag, msg, tr );
}
public static String getStackTraceString( Throwable tr )
{
return Log.getStackTraceString( tr );
}
private static void writeToFile( int priority, String tag, String msg )
{
writeToFile( priority, tag, msg, null );
}
private static void writeToFile( int priority, String tag, String msg, Throwable tr )
{
if ( ( priority >= sCurrentPriority ) &&
( sBufferedWriter != null ) )
{
try
{
if (checkFileSize( ))
{
sBufferedWriter = new BufferedWriter( new FileWriter( sTheLogFile, true ) );
}
sBufferedWriter.write( formatMsg( tag, msg ) );
sBufferedWriter.newLine( );
if ( tr != null )
{
sBufferedWriter.write( Log.getStackTraceString( tr ) );
sBufferedWriter.newLine( );
}
sBufferedWriter.flush( );
}
catch ( IOException e )
{
Log.e( "FileLog", Log.getStackTraceString( e ) );
}
}
if ( sBufferedWriter == null )
{
Log.e( "FileLog", "You have to call FileLog.open(...) before starting to log" );
}
}
private static String formatMsg( String tag, String msg )
{
return String.format( MSG_FORMAT, getCurrentTimeStamp( ), tag, msg );
}
private static String getCurrentTimeStamp( )
{
String currentTimeStamp = null;
try
{
SimpleDateFormat dateFormat = new SimpleDateFormat( TIMESTAMP_FORMAT,
java.util.Locale.getDefault( ) );
currentTimeStamp = dateFormat.format( new Date( ) );
}
catch ( Exception e )
{
Log.e( "FileLog", Log.getStackTraceString( e ) );
}
return currentTimeStamp;
}
private static boolean checkFileSize( )
{
boolean createdNewLogFile = false;
try
{
if ( sTheLogFile.length() > sFileSizeLimit )
{
File to = new File( sLogFilePath + ".old" );
if ( to.exists() )
{
to.delete();
}
sTheLogFile.renameTo( to );
sTheLogFile = new File( sLogFilePath );
sTheLogFile.createNewFile();
createdNewLogFile = true;
}
}
catch ( IOException e )
{
Log.e( "FileLog", Log.getStackTraceString( e ) );
}
return createdNewLogFile;
}
}