OSDN Git Service

Merge branch 'master' of https://scm.sourceforge.jp/gitroot/chemicraft/chemicraft
[chemicraft/chemicraft.git] / common / pcc / chemicraft / ChemiCraftLogging.java
1 package pcc.chemicraft;
2
3 import java.io.BufferedOutputStream;
4 import java.io.BufferedWriter;
5 import java.io.File;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.FileWriter;
9 import java.io.IOException;
10
11 public class ChemiCraftLogging {
12
13         private FileWriter outStream;
14         private String directoryPath;
15         private File file;
16
17         public ChemiCraftLogging(String directoryPath) {
18                 this.directoryPath = directoryPath;
19         }
20
21         public void startLogging() {
22                 this.file = new File(this.directoryPath + "/ChemiCraft.log");
23                 if (!this.file.exists()) {
24                         try {
25                                 this.file.createNewFile();
26                         } catch (IOException e) {
27                                 e.printStackTrace();
28                         }
29                 }
30
31                 try {
32                         this.outStream = new FileWriter(this.file);
33                 } catch (FileNotFoundException e) {
34                         e.printStackTrace();
35                 } catch (IOException e) {
36                         e.printStackTrace();
37                 }
38         }
39
40         public void write(String writeStr) {
41                 String s = writeStr;
42                 try {
43                         this.outStream.write(writeStr);
44                 } catch (IOException e) {
45                         e.printStackTrace();
46                 }
47         }
48
49         public void write(String writeStr, EnumLoggingType type) {
50                 String s = writeStr;
51                 switch (type) {
52                 case NORMAL:
53                         break;
54                 case ERROR:
55                         s = "[Error]" + s;
56                         break;
57                 case WARNING:
58                         s = "[Warning]" + s;
59                         break;
60                 case INFO:
61                         s = "[Info]" + s;
62                         break;
63                 default:
64                         throw new IllegalStateException();
65                 }
66                 try {
67                         this.outStream.write(s + "\n");
68                         this.outStream.flush();
69                 } catch (IOException e) {
70                         e.printStackTrace();
71                 }
72         }
73
74 }