1 package cz.cuni.amis.pogamut.base.utils.logging;
2
3 import java.io.File;
4 import java.io.FileNotFoundException;
5 import java.io.FileOutputStream;
6 import java.io.IOException;
7 import java.io.PrintWriter;
8 import java.util.logging.Formatter;
9 import java.util.logging.LogRecord;
10
11 import cz.cuni.amis.pogamut.base.agent.IAgentId;
12 import cz.cuni.amis.utils.FilePath;
13 import cz.cuni.amis.utils.NullCheck;
14 import cz.cuni.amis.utils.exception.PogamutIOException;
15
16
17
18
19
20
21
22
23
24
25 public abstract class LogPublisher implements ILogPublisher {
26
27 protected Formatter formatter = null;
28
29 public LogPublisher() {
30 formatter = new LogFormatter();
31 }
32
33 public LogPublisher(IAgentId name) {
34 formatter = new LogFormatter(name);
35 }
36
37 public LogPublisher(Formatter formatter) {
38 this.formatter = formatter;
39 if (this.formatter == null) {
40 formatter = new LogFormatter();
41 }
42 }
43
44 public Formatter getFormatter() {
45 return formatter;
46 }
47
48 public void setFormatter(Formatter formatter) {
49 this.formatter = formatter;
50 }
51
52 @Override
53 public abstract void close() throws SecurityException;
54
55 @Override
56 public abstract void flush();
57
58 public abstract void publish(LogRecord record, String formattedMsg);
59
60 @Override
61 public synchronized void publish(LogRecord record) {
62 Formatter actualFormatter = formatter;
63 if (actualFormatter != null) publish(record, actualFormatter.format(record));
64 }
65
66
67
68
69
70 public static class ConsolePublisher extends LogPublisher {
71
72 public ConsolePublisher() {
73 super();
74 }
75
76 public ConsolePublisher(IAgentId name) {
77 super(name);
78 }
79
80 @Override
81 public void close() throws SecurityException {
82 }
83
84 @Override
85 public void flush() {
86 }
87
88 @Override
89 public synchronized void publish(LogRecord record, String formattedMsg) {
90 System.out.println(formattedMsg);
91 }
92
93 }
94
95 public static class FilePublisher extends LogPublisher {
96
97 protected File file;
98 protected FileOutputStream fileOut;
99 protected PrintWriter fileWriter;
100
101 public FilePublisher(File file) throws PogamutIOException {
102 this(file, null);
103 }
104
105 public FilePublisher(File file, Formatter formatter) throws PogamutIOException {
106 super(formatter);
107 NullCheck.check(file, "file");
108 this.file = file;
109 FilePath.makeDirsToFile(file);
110 try {
111 fileOut = new FileOutputStream(file);
112 } catch (FileNotFoundException e) {
113 throw new PogamutIOException("Can't open file '" + file.getAbsolutePath() + "' for logging.", e);
114 }
115 fileWriter = new PrintWriter(fileOut);
116 }
117
118 public File getFile() {
119 return file;
120 }
121
122 @Override
123 public void close() throws SecurityException {
124 try {
125 fileWriter.close();
126 } catch (Exception e) {
127 }
128 try {
129 fileOut.close();
130 } catch (IOException e) {
131 e.printStackTrace();
132 }
133 }
134
135 @Override
136 public void flush() {
137 fileWriter.flush();
138 try {
139 fileOut.flush();
140 } catch (IOException e) {
141 e.printStackTrace();
142 }
143 }
144
145 @Override
146 public synchronized void publish(LogRecord record, String formattedMsg) {
147 fileWriter.println(formattedMsg);
148 }
149
150 }
151
152 }