]> git.somenet.org - pub/jan/lsdc.git/blob - src/at/ac/tuwien/lsdc/util/CSVLogger.java
change CSV logging to own logging
[pub/jan/lsdc.git] / src / at / ac / tuwien / lsdc / util / CSVLogger.java
1 package at.ac.tuwien.lsdc.util;
2
3 import java.io.BufferedWriter;
4 import java.io.File;
5 import java.io.FileWriter;
6 import java.io.IOException;
7 import java.io.PrintWriter;
8
9 import at.ac.tuwien.lsdc.types.ScenarioData;
10 import at.ac.tuwien.lsdc.types.SchedulerData;
11 import au.com.bytecode.opencsv.CSVWriter;
12
13 public class CSVLogger {
14
15         private File file;
16
17         public CSVLogger(File file) throws IOException {
18                 this.file = file;
19         }
20
21         public void logScenarioData(ScenarioData data) throws IOException {
22                 if (file.length() == 0) {
23                         writeScenarioDataHeader();
24                 }
25                 writeCSVLine(data.toStringArray());
26         }
27
28         public void logSchedulerData(SchedulerData data) throws IOException {
29                 if (file.length() == 0) {
30                         writeSchedulerDataHeader();
31                 }
32                 writeCSVLine(data.toStringArray());
33         }
34
35         private void writeScenarioDataHeader() throws IOException {
36                 String[] header = new String[] { "Scheduler", "Scenario", "TotalPMs",
37                                 "TotalVMs", "TotalDuration", "TotalPowerConsumption",
38                                 "TotalInSourced", "TotalOutSourced" };
39                 writeCSVLine(header);
40         }
41
42         private void writeSchedulerDataHeader() throws IOException {
43                 String[] header = new String[] { "Timestamp", "TotalRAM", "TotalCPU",
44                                 "TotalSize", "RunningPMs", "RunningVMs",
45                                 "TotalPowerConsumption", "InSourced", "OutSourced" };
46                 writeCSVLine(header);
47         }
48
49         private void writeCSVLine(String[] arr) throws IOException {
50                 PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(
51                                 file, true)));
52                 StringBuilder csvBuilder = new StringBuilder();
53
54                 for (String s : arr) {
55                         csvBuilder.append(s).append(";");
56                 }
57
58                 csvBuilder.deleteCharAt(csvBuilder.length() - 1);
59                 out.println(csvBuilder.toString());
60                 out.close();
61         }
62
63 }