source: src/main/java/genius/core/logging/FileLogger.java

Last change on this file was 127, checked in by Wouter Pasman, 6 years ago

#41 ROLL BACK of rev.126 . So this version is equal to rev. 125

File size: 1.9 KB
Line 
1package genius.core.logging;
2
3import java.io.Closeable;
4import java.io.File;
5import java.io.FileNotFoundException;
6import java.io.IOException;
7import java.io.PrintStream;
8
9import genius.core.Bid;
10import genius.core.actions.Action;
11import genius.core.events.MultipartyNegoActionEvent;
12import genius.core.events.NegotiationEvent;
13import genius.core.events.SessionEndedNormallyEvent;
14import genius.core.listener.Listener;
15import genius.core.parties.NegotiationPartyInternal;
16
17/**
18 * Creates a file logger which will log the nego events to a csv file
19 */
20public class FileLogger implements Listener<NegotiationEvent>, Closeable {
21 // The internal print stream used for file writing
22 PrintStream ps;
23
24 /**
25 *
26 * @param fileName
27 * the log file without the .csv extension. If parent
28 * directory(s) of file don't exist, they are created
29 * @throws FileNotFoundException
30 */
31 public FileLogger(String fileName) throws FileNotFoundException {
32 new File(fileName).getParentFile().mkdirs();
33 ps = new PrintStream(fileName + ".csv");
34 }
35
36 @Override
37 public void close() throws IOException {
38 ps.close();
39 }
40
41 @Override
42 public void notifyChange(NegotiationEvent e) {
43 if (e instanceof MultipartyNegoActionEvent) {
44 MultipartyNegoActionEvent ea = (MultipartyNegoActionEvent) e;
45 Action action = ea.getAction();
46 if (action == null)
47 return; // error?
48 ps.println(
49 ea.getRound() + "," + ea.getTurn() + "," + ea.getTime() + "," + action.getAgent() + "," + action);
50 } else if (e instanceof SessionEndedNormallyEvent) {
51 SessionEndedNormallyEvent ee = (SessionEndedNormallyEvent) e;
52 Bid bid = ee.getAgreement();
53 if (bid == null) {
54 ps.print("ended-no-agreement");
55 } else {
56 ps.print("agreement," + bid);
57 for (NegotiationPartyInternal party : ((SessionEndedNormallyEvent) e).getParties()) {
58 ps.print("," + party.getUtility(bid));
59 }
60 }
61 ps.println();
62 }
63
64 }
65}
Note: See TracBrowser for help on using the repository browser.