Skip to content

Commit 23557b8

Browse files
committed
Logging system rewritten + export to file
1 parent 78518fa commit 23557b8

File tree

4 files changed

+85
-45
lines changed

4 files changed

+85
-45
lines changed

src/labs/sem1/lab10/DebugStack.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,43 +2,49 @@
22

33
public class DebugStack extends Stack {
44
protected boolean debug = false;
5+
Logger logger;
56

6-
public void setDebug(boolean debug) {
7-
this.debug = debug;
7+
public void setLogger(Object logger) {
8+
if (logger instanceof Logger) {
9+
this.logger = (Logger) logger;
10+
debug = true;
11+
} else {
12+
debug = false;
13+
}
814
}
915

1016
public void push(int i) {
1117
if (debug)
12-
Utils.printRow("Push START");
18+
logger.printRow("Push START");
1319
super.push(i);
1420
if (debug)
15-
Utils.printRow("Push END");
21+
logger.printRow("Push END");
1622
}
1723

18-
public int pop() throws RuntimeException {
24+
public int pop() {
1925
if (debug)
20-
Utils.printRow("Pop START");
26+
logger.printRow("Pop START");
2127
int i = super.pop();
2228
if (debug)
23-
Utils.printRow("Pop END");
29+
logger.printRow("Pop END");
2430
return i;
2531
}
2632

2733
public boolean equals(Object o) {
2834
if (debug)
29-
Utils.printRow("Equals START");
35+
logger.printRow("Equals START");
3036
boolean b = super.equals(o);
3137
if (debug)
32-
Utils.printRow("Equals END");
38+
logger.printRow("Equals END");
3339
return b;
3440
}
3541

3642
public String toString() {
3743
if (debug)
38-
Utils.printRow("toString START");
44+
logger.printRow("toString START");
3945
String s = super.toString();
4046
if (debug)
41-
Utils.printRow("toString END");
47+
logger.printRow("toString END");
4248
return s;
4349
}
4450
}

src/labs/sem1/lab10/Logger.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package labs.sem1.lab10;
2+
3+
import java.nio.file.Files;
4+
import java.nio.file.Paths;
5+
import java.io.IOException;
6+
7+
public class Logger {
8+
StringBuilder sb;
9+
String[] row;
10+
11+
public Logger(int threadsCount) {
12+
sb = new StringBuilder();
13+
// header of the CSV file
14+
row = new String[threadsCount + 1];
15+
row[0] = "main";
16+
for (int i = 1; i < row.length; i++) {
17+
row[i] = "Thread " + (i - 1);
18+
}
19+
// append header to the string
20+
sb.append(String.join(",", row) + "\n");
21+
// empty row for next lines
22+
for (int i = 0; i < row.length; i++) {
23+
row[i] = "\"\"";
24+
}
25+
}
26+
27+
public void printRow(String s) {
28+
// print s at the column of the current thread (other columns should be empty)
29+
// get current thread id from "Thread 0" -> 1 (main -> 0)
30+
String threadName = Thread.currentThread().getName();
31+
int threadId = (threadName.equals("main")) ? 0 : Integer.parseInt(threadName.split(" ")[1]) + 1;
32+
// add s to the its column and append to the string
33+
row[threadId] = s;
34+
sb.append(String.join(",", row) + "\n");
35+
// empty row for next lines
36+
row[threadId] = "\"\"";
37+
}
38+
39+
public void writeFile(String filename) {
40+
// if file or folder doesn't exist, create it
41+
try {
42+
if (!Files.exists(Paths.get(filename))) {
43+
Files.createDirectories(Paths.get(filename).getParent());
44+
Files.createFile(Paths.get(filename));
45+
}
46+
// write the string to the file
47+
Files.write(Paths.get(filename), sb.toString().getBytes());
48+
} catch (IOException e) {
49+
e.printStackTrace();
50+
}
51+
}
52+
}

src/labs/sem1/lab10/Main.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ public static void main(String[] args) {
55
// classes to benchmark
66
Class<?>[] classes = {
77
Stack.class,
8+
DebugStack.class,
89
SynchroStack.class,
910
SynchroStackFast.class
1011
};
@@ -59,12 +60,20 @@ public static void main(String[] args) {
5960
for (int j = 0; j < threadsPerMethod; j++) {
6061
int index = i * threadsPerMethod + j;
6162
threads[index] = new Thread(methods[i]);
62-
threads[index].setName("Thread-" + index);
63+
threads[index].setName("Thread " + index);
6364
}
6465
}
6566

67+
Logger log = null;
68+
// set logger if stack is subclass of DebugStack and not DebugStack itself
69+
// because DebugStack is not synchronized (and therefore not thread-safe)
70+
if (stack instanceof DebugStack && !stack.getClass().getSimpleName().equals("DebugStack")) {
71+
log = new Logger(threads.length);
72+
((DebugStack) stack).setLogger(log);
73+
}
74+
6675
// print stack type
67-
System.out.print(String.format("Benchmarking: %s ", stack.getClass().getSimpleName()));
76+
System.out.print("Benchmarking: " + stack.getClass().getSimpleName() + " ");
6877

6978
// start measuring time
7079
long start = System.currentTimeMillis();
@@ -86,6 +95,11 @@ public static void main(String[] args) {
8695
// stop measuring time
8796
long end = System.currentTimeMillis();
8897
System.out.println("- took " + (end - start) + " ms");
98+
99+
// write to file
100+
if (log != null) {
101+
log.writeFile("results/" + stack.getClass().getSimpleName() + ".csv");
102+
}
89103
}
90104
}
91105
}

src/labs/sem1/lab10/Utils.java

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)