-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoggingToFile.pde
More file actions
47 lines (42 loc) · 1.39 KB
/
LoggingToFile.pde
File metadata and controls
47 lines (42 loc) · 1.39 KB
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
/** LoggingToFile
* For logging text file
* Appends text to the end of a text file located in the data directory,
* creates the file if it does not exist.
* Can be used for big files with lots of rows,
* existing lines will not be rewritten
*
* Example usage: appendTextToFile(myLogFileName, ("Client disconnect: " + myClient.ip()));
* General usage: appendTextToFile(myLogFileName, String);
*/
// if (Verbose) {appendTextToFile(myLogFileName, "Client connected: " + s_clientAddress );}
/* For logging text file */
import java.io.BufferedWriter;
import java.io.FileWriter;
String myLogFileName = "ServerComeAndGoes.log.txt";
void appendTextToFile(String filename, String text) {
String myTime = str(year())+ String.format("%02d", month())+ String.format("%02d", day())+ "_"+ String.format("%02d", hour())+ String.format("%02d", minute())+ String.format("%02d", second());
text = myTime +" " + text;
File f = new File(dataPath(filename));
if(!f.exists()){
createFile(f);
}
try {
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(f, true)));
out.println(text);
out.close();
}catch (IOException e){
e.printStackTrace();
}
}
/**
* Creates a new file including all subfolders
*/
void createFile(File f){
File parentDir = f.getParentFile();
try{
parentDir.mkdirs();
f.createNewFile();
}catch(Exception e){
e.printStackTrace();
}
}