Skip to content

Commit

Permalink
Major logger interface refactoring
Browse files Browse the repository at this point in the history
1. Use namespace instead of class for Log
2. Remove duplicate code by adding a `logWithSeverity`
3. Rename `global` to `globalLogger` to avoid confusion
4. Change code/tests accordingly
  • Loading branch information
Kagamihara Nadeshiko committed Oct 3, 2023
1 parent 0900b37 commit a1a8377
Show file tree
Hide file tree
Showing 16 changed files with 143 additions and 177 deletions.
9 changes: 4 additions & 5 deletions __tests__/Logger.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import type {Reactor} from "../src/core/internal";
import {
App,
Log,
LogLevel,
loggingCLAType,
booleanCLAType,
stringCLAType,
Expand All @@ -26,9 +25,9 @@ describe("Logger functions", function () {

describe("Test for Logger", () => {
it("DEBUG Log", () => {
Log.global.level = LogLevel.DEBUG;
Log.setLevel(Log.LogLevel.DEBUG);

console.log("Log level is " + Log.global.level);
console.log("Log level is " + Log.globalLogger.level);
Log.getInstance("test module");
Log.debug(null, () => "test");
Log.debug(null, () => "test", "test module");
Expand Down Expand Up @@ -65,7 +64,7 @@ describe("Test for Logger", () => {
expect(Log.log).toHaveBeenCalledTimes(2);
expect(Log.warn).toHaveBeenCalledTimes(2);

console.log(Log.global.level);
console.log(Log.globalLogger.level);
});
});

Expand All @@ -75,7 +74,7 @@ describe("Command Line Arguments Helper Functions Tests", () => {
expect(loggingCLAType("")).toBeNull;
});
test("log level check", () => {
expect(loggingCLAType("ERROR")).toBe(1);
expect(loggingCLAType("ERROR")).toBe(Log.LogLevel.ERROR.valueOf());
});
test("boolean test for command line argument parsing", () => {
expect(booleanCLAType("true")).toBe(true);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/OutputGet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class OutputGetTest extends App {
failure: () => void
) {
super(timeout, true, false, success, failure);
Log.global.debug(">>>>>>>>----" + this.util);
Log.globalLogger.debug(">>>>>>>>----" + this.util);
this.addReaction([this.t], [this.writable(this.o)], function (this, o) {
Log.global.debug(">>>>>>>>>>being triggered>>>>>>>>>>>");
Log.globalLogger.debug(">>>>>>>>>>being triggered>>>>>>>>>>>");
if (o.get() != null) {
throw new Error(
"Calling get on an output before it has been set does not return null"
Expand Down
24 changes: 12 additions & 12 deletions __tests__/dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ describe("Manually constructed precedence graphs", () => {
graph.removeNode(nodes[1]);
expect(graph.size()[0]).toEqual(5); // V
expect(graph.size()[1]).toEqual(3); // E
Log.global.debug(graph.toString());
Log.globalLogger.debug(graph.toString());
expect(graph.toString()).toBe(
dontIndent`graph
0["app.R[R3]"]
Expand All @@ -165,7 +165,7 @@ describe("Manually constructed precedence graphs", () => {
graph.addEdge(nodes[3], nodes[2]);
expect(graph.size()[0]).toEqual(6); // V
expect(graph.size()[1]).toEqual(4); // E
Log.global.debug(graph.toString());
Log.globalLogger.debug(graph.toString());
expect(graph.toString()).toBe(
dontIndent`graph
0["app.R[R3]"]
Expand Down Expand Up @@ -195,7 +195,7 @@ describe("Manually constructed precedence graphs", () => {
it("introduce a cycle", () => {
graph.addEdge(nodes[2], nodes[5]);
expect(graph.updatePriorities(false)).toBeFalsy();
Log.global.debug(graph.toString());
Log.globalLogger.debug(graph.toString());
});
});

Expand All @@ -217,18 +217,18 @@ describe("ReactionQ", () => {
var reactionQ = new PrioritySet<Priority>();

for (let i = 0; i < 6; i++) {
Log.global.debug(
Log.globalLogger.debug(
"Pushing node: " + i + " with prio: " + nodes[i].getPriority()
);
reactionQ.push(nodes[i]);
}

// duplicate insertions
Log.global.debug(
Log.globalLogger.debug(
"Pushing duplicate node with prio: " + nodes[5].getPriority()
);
reactionQ.push(nodes[5]);
Log.global.debug(
Log.globalLogger.debug(
"Pushing duplicate node with prio: " + nodes[1].getPriority()
);
reactionQ.push(nodes[1]);
Expand All @@ -237,7 +237,7 @@ describe("ReactionQ", () => {
const r = reactionQ.pop();
for (let i = 0; i < 6; i++) {
if (Object.is(r, nodes[i])) {
Log.global.debug(
Log.globalLogger.debug(
"Found matching node: " + i + " with prio: " + nodes[i].getPriority()
);
}
Expand All @@ -251,7 +251,7 @@ describe("ReactionQ", () => {

for (let i = 0; i < 6; i++) {
if (Object.is(r, nodes[i])) {
Log.global.debug(
Log.globalLogger.debug(
"Found matching node: " + i + " with prio: " + nodes[i].getPriority()
);
}
Expand All @@ -264,7 +264,7 @@ describe("ReactionQ", () => {
const r = reactionQ.pop();
for (let i = 0; i < 6; i++) {
if (Object.is(r, nodes[i])) {
Log.global.debug(
Log.globalLogger.debug(
"Found matching node: " + i + " with prio: " + nodes[i].getPriority()
);
}
Expand All @@ -277,7 +277,7 @@ describe("ReactionQ", () => {
const r = reactionQ.pop();
for (let i = 0; i < 6; i++) {
if (Object.is(r, nodes[i])) {
Log.global.debug(
Log.globalLogger.debug(
"Found matching node: " + i + " with prio: " + nodes[i].getPriority()
);
}
Expand All @@ -290,7 +290,7 @@ describe("ReactionQ", () => {
const r = reactionQ.pop();
for (let i = 0; i < 6; i++) {
if (Object.is(r, nodes[i])) {
Log.global.debug(
Log.globalLogger.debug(
"Found matching node: " + i + " with prio: " + nodes[i].getPriority()
);
}
Expand All @@ -303,7 +303,7 @@ describe("ReactionQ", () => {
const r = reactionQ.pop();
for (let i = 0; i < 6; i++) {
if (Object.is(r, nodes[i])) {
Log.global.debug(
Log.globalLogger.debug(
"Found matching node: " + i + " with prio: " + nodes[i].getPriority()
);
}
Expand Down
1 change: 0 additions & 1 deletion __tests__/disconnect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
TimeValue,
Origin,
Log,
LogLevel,
Action
} from "../src/core/internal";

Expand Down
4 changes: 2 additions & 2 deletions __tests__/mutations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ describe("Creating reactors at runtime", function () {
jest.setTimeout(5000);

it("Reactor with periodic timer", (done) => {
// Log.global.level = LogLevel.DEBUG
// Log.global.level = Log.LogLevel.DEBUG

const app = new Zeno(TimeValue.secs(4), done, () => {});

Expand All @@ -181,7 +181,7 @@ describe("Creating reactors at runtime", function () {
// jest.setTimeout(5000);

// it("Simple scatter gather", done => {
// Log.global.level = LogLevel.DEBUG
// Log.global.level = Log.LogLevel.DEBUG

// let app = new ScatterGather(TimeValue.secs(5), done, () => {})

Expand Down
5 changes: 2 additions & 3 deletions __tests__/reactors.errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import {
InPort,
TimeUnit,
TimeValue,
Log,
LogLevel
Log
} from "../src/core/internal";

class R extends Reactor {
Expand Down Expand Up @@ -49,7 +48,7 @@ class R extends Reactor {
}

describe("Testing Error Cases", function () {
Log.global.level = LogLevel.DEBUG;
Log.setLevel(Log.LogLevel.DEBUG);

it("Multiple reactions for a callee port", () => {
var parent = new App();
Expand Down
11 changes: 5 additions & 6 deletions __tests__/reactors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
TimeValue,
Origin,
Log,
LogLevel,
Action
} from "../src/core/internal";

Expand Down Expand Up @@ -163,7 +162,7 @@ describe("Testing deadlines", function () {
jest.setTimeout(5000);

it("Missed reaction deadline on InPort", (done) => {
Log.global.level = LogLevel.WARN;
Log.setLevel(Log.LogLevel.WARN);

function fail() {
throw new Error("Test has failed.");
Expand All @@ -187,7 +186,7 @@ describe("Testing deadlines", function () {
it("Missed reaction deadline on the second reaction in the chain", (done) => {
// let consoleOutput: string[] = []

Log.global.level = LogLevel.WARN;
Log.setLevel(Log.LogLevel.WARN);

function fail() {
throw new Error("Test has failed.");
Expand Down Expand Up @@ -219,7 +218,7 @@ describe("Testing deadlines", function () {
});

it("Missed deadline with custom message", (done) => {
Log.global.level = LogLevel.WARN;
Log.setLevel(Log.LogLevel.WARN);

// let deadlineMissed:string = ""

Expand All @@ -233,7 +232,7 @@ describe("Testing deadlines", function () {
done,
fail,
() => {
Log.global.warn("Deadline missed!");
Log.warn(null, () => "Deadline missed!");
},
TimeValue.withUnits(1, TimeUnit.nsec)
);
Expand Down Expand Up @@ -274,7 +273,7 @@ describe("Testing Reactions", function () {

describe("Testing Actions", function () {
it("Mismatched logical time", () => {
Log.global.level = LogLevel.WARN;
Log.setLevel(Log.LogLevel.WARN);

function fail() {
throw new Error("Test has failed.");
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark/FacilityLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
App
} from "../core/internal";

Log.global.level = Log.levels.INFO;
Log.setLevel(Log.LogLevel.INFO);

class Point {
x: number;
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark/PingPong.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
App
} from "../core/internal";

Log.global.level = Log.levels.ERROR;
Log.setLevel(Log.LogLevel.DEBUG);

export class Ping extends Reactor {
count: Parameter<number>;
Expand Down
2 changes: 1 addition & 1 deletion src/benchmark/Sieve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Log
} from "../core/internal";

Log.global.level = Log.levels.INFO;
Log.setLevel(Log.LogLevel.INFO);
class Ramp extends Reactor {
next: Action<number>;

Expand Down
12 changes: 6 additions & 6 deletions src/core/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {TimeUnit, TimeValue, LogLevel} from "./internal";
import {TimeUnit, TimeValue, Log} from "./internal";

// ---------------------------------------------------------------------//
// Command Line Arguments Helper Functions //
Expand All @@ -9,10 +9,10 @@ import {TimeUnit, TimeValue, LogLevel} from "./internal";
* Returns null if the input is malformed.
* @param logging the raw command line argument
*/
export function loggingCLAType(logging: string): LogLevel | null {
if (logging in LogLevel) {
type LevelString = keyof typeof LogLevel;
return LogLevel[logging as LevelString];
export function loggingCLAType(logging: string): Log.LogLevel | null {
if (logging in Log.LogLevel) {
type LevelString = keyof typeof Log.LogLevel;
return Log.LogLevel[logging as LevelString];
} else {
return null;
}
Expand Down Expand Up @@ -94,7 +94,7 @@ export interface ProcessedCommandLineArgs {
keepalive: boolean | undefined;
advanceMessageInterval: TimeValue | undefined;
timeout: TimeValue | null | undefined;
logging: LogLevel | undefined;
logging: Log.LogLevel | undefined;
id: string | undefined;
help: boolean;
}
Expand Down
10 changes: 5 additions & 5 deletions src/core/federation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1318,7 +1318,7 @@ export class FederatedApp extends App {
// Ignore federatate's _shutdown call if stop is requested.
// The final shutdown should be done by calling super._shutdown.
if (this.stopRequestInfo.state !== StopRequestState.NOT_SENT) {
Log.global.debug(
Log.globalLogger.debug(
"Ignoring FederatedApp._shutdown() as stop is already requested to RTI."
);
return;
Expand All @@ -1330,7 +1330,7 @@ export class FederatedApp extends App {
) {
this.sendRTIStopRequest(this.util.getCurrentTag().getMicroStepsLater(1));
} else {
Log.global.debug(
Log.globalLogger.debug(
"Ignoring FederatedApp._shutdown() since EndOfExecution is already set earlier than current tag." +
`currentTag: ${this.util.getCurrentTag()} endTag: ${String(endTag)}`
);
Expand Down Expand Up @@ -1396,7 +1396,7 @@ export class FederatedApp extends App {
"received from the RTI is less than the timestamp of the " +
"next event on the event queue"
);
Log.global.debug("Exiting _next.");
Log.globalLogger.debug("Exiting _next.");
return false;
}
}
Expand Down Expand Up @@ -1426,7 +1426,7 @@ export class FederatedApp extends App {
throw e;
}
} else {
Log.global.debug(
Log.globalLogger.debug(
"Max level allowed to advance is higher than the next reaction's priority."
);
return false;
Expand All @@ -1437,7 +1437,7 @@ export class FederatedApp extends App {
// network inputs.
return false;
}
Log.global.debug("Finished handling all events at current time.");
Log.globalLogger.debug("Finished handling all events at current time.");
return true;
}

Expand Down
10 changes: 5 additions & 5 deletions src/core/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ export class PrecedenceGraph<T> {
// Start a new line when this is not the first match,
// or when the current node is a start node.
chain = [];
Log.global.debug("Starting new chain.");
Log.globalLogger.debug("Starting new chain.");
}

// Mark current node as visited.
Expand All @@ -288,21 +288,21 @@ export class PrecedenceGraph<T> {
chain.push(node);

if (chain.includes(v)) {
Log.global.debug("Cycle detected.");
Log.globalLogger.debug("Cycle detected.");
printChain(v, chain);
} else if (visited.has(v)) {
Log.global.debug("Overlapping chain detected.");
Log.globalLogger.debug("Overlapping chain detected.");
printChain(v, chain);
} else {
Log.global.debug("Adding link to the chain.");
Log.globalLogger.debug("Adding link to the chain.");
buildChain(v, chain);
}
// Indicate that a match has been found.
match = true;
}
}
if (!match) {
Log.global.debug("End of chain.");
Log.globalLogger.debug("End of chain.");
printChain(node, chain);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/reaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export class Reaction<T extends Variable[]>
private readonly react: (...args: ArgList<T>) => void,
private deadline?: TimeValue,
private readonly late: (...args: ArgList<T>) => void = () => {
Log.global.warn("Deadline violation occurred!");
Log.globalLogger.warn("Deadline violation occurred!");
}
) {}

Expand Down
Loading

0 comments on commit a1a8377

Please sign in to comment.