Skip to content

Commit

Permalink
fix: formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
elblasco committed Aug 7, 2024
1 parent 0286b02 commit fceb63f
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 166 deletions.
298 changes: 154 additions & 144 deletions src/main/java/it/unitn/disi/ds1/qtop/Logger.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,148 +18,158 @@
*/
public class Logger {

private static Logger instance = null;
// Map to store PrintWriters for each entity
private final Map<String, PrintWriter> entityLogs = new HashMap<>();
private LogLevel logLevel = LogLevel.INFO;
private PrintWriter info;
private PrintWriter debug;

/**
* Private constructor to prevent instantiation from outside
*/
private Logger() {
ensureDirectoryExists("logs"); // Ensure logs directory exists
try {
info = new PrintWriter(
"logs" + File.separator + "simulation.log",
StandardCharsets.UTF_8
);
debug = new PrintWriter(
"logs" + File.separator + "debug.log",
StandardCharsets.UTF_8
);
} catch (IOException e)
{
e.printStackTrace();
}
}

/**
* Singleton pattern to get the single instance of Logger.
*
* @return the Logger instance
*/
public static Logger getInstance() {
if (instance == null) {
synchronized (Logger.class) {
if (instance == null) {
instance = new Logger();
}
}
}
return instance;
}

/**
* Set the log level.
*
* @param level level to set
*/
public void setLogLevel(LogLevel level) {
logLevel = level;
}

/**
* Ensure the logs directory exists, create if it does not.
*
* @param path directory path
*/
private void ensureDirectoryExists(String path) {
File directory = new File(path);
if (!directory.exists()) {
directory.mkdirs();
}
}

/**
* Get the PrintWriter for the given entity, create if it does not exist.
*
* @param entity entity to get the log for
*
* @return PrintWriter for the entity
*/
private @Nullable PrintWriter getEntityLog(String entity) {
if (!entityLogs.containsKey(entity)) {
ensureDirectoryExists("logs");
try {
PrintWriter writer = new PrintWriter(
"logs" + File.separator + entity + ".log",
StandardCharsets.UTF_8
);
entityLogs.put(entity, writer);
return writer;
} catch (IOException e)
{
e.printStackTrace();
return null;
}
}
return entityLogs.get(entity);
}

// Parse the entity (NODE or CLIENT) from the log message

/**
* Parse the entity (NODE or CLIENT) from the log message.
*
* @param message log message
*
* @return the entity "NODE", "CLIENT" or "general"
*/
private String parseEntity(String message) {
// Regular expression to match NODE-<number> or CLIENT-<number>
Pattern pattern = Pattern.compile("(NODE-\\d+)|(CLIENT-\\d+)");
Matcher matcher = pattern.matcher(message);

if (matcher.find()) {
// Return the matched group (NODE or CLIENT)
return matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
} else {
return "general"; // Default entity if none is found
}
}

/**
* Log the message at the specified log level.
*
* @param level log level
* @param message log message
*/
public void log(LogLevel level, String message) {
String log = String.format(
"[%s] [%s] %s",
LocalTime.now(),
level,
message
);

// Log to global info logs if the level is appropriate
if (level.ordinal() >= logLevel.ordinal()) {
info.println(log);
info.flush();
}

// Log to debug logs
debug.println(log);
debug.flush();

// Log to entity-specific logs
String entity = parseEntity(message);
PrintWriter entityLog = getEntityLog(entity);
if (entityLog != null) {
entityLog.println(log);
entityLog.flush();
}
}
private static Logger instance = null;
// Map to store PrintWriters for each entity
private final Map<String, PrintWriter> entityLogs = new HashMap<>();
private LogLevel logLevel = LogLevel.INFO;
private PrintWriter info;
private PrintWriter debug;

/**
* Private constructor to prevent instantiation from outside
*/
private Logger() {
ensureDirectoryExists("logs"); // Ensure logs directory exists
try
{
info = new PrintWriter(
"logs" + File.separator + "simulation.log",
StandardCharsets.UTF_8
);
debug = new PrintWriter(
"logs" + File.separator + "debug.log",
StandardCharsets.UTF_8
);
} catch (IOException e)
{
e.printStackTrace();
}
}

/**
* Singleton pattern to get the single instance of Logger.
*
* @return the Logger instance
*/
public static Logger getInstance() {
if (instance == null)
{
synchronized (Logger.class)
{
if (instance == null)
{
instance = new Logger();
}
}
}
return instance;
}

/**
* Set the log level.
*
* @param level level to set
*/
public void setLogLevel(LogLevel level) {
logLevel = level;
}

/**
* Ensure the logs directory exists, create if it does not.
*
* @param path directory path
*/
private void ensureDirectoryExists(String path) {
File directory = new File(path);
if (! directory.exists())
{
directory.mkdirs();
}
}

/**
* Get the PrintWriter for the given entity, create if it does not exist.
*
* @param entity entity to get the log for
*
* @return PrintWriter for the entity
*/
private @Nullable PrintWriter getEntityLog(String entity) {
if (! entityLogs.containsKey(entity))
{
ensureDirectoryExists("logs");
try
{
PrintWriter writer = new PrintWriter(
"logs" + File.separator + entity + ".log",
StandardCharsets.UTF_8
);
entityLogs.put(
entity,
writer
);
return writer;
} catch (IOException e)
{
e.printStackTrace();
return null;
}
}
return entityLogs.get(entity);
}
// Parse the entity (NODE or CLIENT) from the log message

/**
* Parse the entity (NODE or CLIENT) from the log message.
*
* @param message log message
*
* @return the entity "NODE", "CLIENT" or "general"
*/
private String parseEntity(String message) {
// Regular expression to match NODE-<number> or CLIENT-<number>
Pattern pattern = Pattern.compile("(NODE-\\d+)|(CLIENT-\\d+)");
Matcher matcher = pattern.matcher(message);
if (matcher.find())
{
// Return the matched group (NODE or CLIENT)
return matcher.group(1) != null ? matcher.group(1) : matcher.group(2);
}
else
{
return "general"; // Default entity if none is found
}
}

/**
* Log the message at the specified log level.
*
* @param level log level
* @param message log message
*/
public void log(LogLevel level, String message) {
String log = String.format(
"[%s] [%s] %s",
LocalTime.now(),
level,
message
);
// Log to global info logs if the level is appropriate
if (level.ordinal() >= logLevel.ordinal())
{
info.println(log);
info.flush();
}
// Log to debug logs
debug.println(log);
debug.flush();
// Log to entity-specific logs
String entity = parseEntity(message);
PrintWriter entityLog = getEntityLog(entity);
if (entityLog != null)
{
entityLog.println(log);
entityLog.flush();
}
}
}
39 changes: 26 additions & 13 deletions src/main/java/it/unitn/disi/ds1/qtop/PairsHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@ public PairsHistory(@NotNull PairsHistory sourceObject) {
ArrayList<Pair<Integer, Utils.Decision>> newEpoch = new ArrayList<>();
for (Pair<Integer, Utils.Decision> iteration : epoch)
{
newEpoch.add(
new Pair<>(
iteration.first(),
iteration.second()
)
);
newEpoch.add(new Pair<>(
iteration.first(),
iteration.second()
));
}
this.add(newEpoch);
}
Expand All @@ -47,7 +45,8 @@ public PairsHistory(@NotNull PairsHistory sourceObject) {
* @param finalState final state to set
*/
public void setState(int e, int i, Utils.Decision finalState) {
this.get(e).set(i,
this.get(e).set(
i,
new Pair<>(
this.get(e).get(i).first(),
finalState
Expand Down Expand Up @@ -127,7 +126,10 @@ public Utils.EpochPair getLatest() {
latestIteration
);
}
return new Utils.EpochPair(-1, -1);
return new Utils.EpochPair(
- 1,
- 1
);
}

/**
Expand All @@ -136,7 +138,7 @@ public Utils.EpochPair getLatest() {
*
* @return the latest epoch and iteration committed
*/
public Utils.EpochPair getLatestCommitted(){
public Utils.EpochPair getLatestCommitted() {
if (! this.isEmpty())
{
for (int i = this.size() - 1; i >= 0; i--)
Expand All @@ -157,9 +159,12 @@ public Utils.EpochPair getLatestCommitted(){
}
}
}
return new Utils.EpochPair(-1, -1);
return new Utils.EpochPair(
- 1,
- 1
);
}

@Override
public String toString() {
StringBuilder sb = new StringBuilder();
Expand All @@ -170,10 +175,18 @@ public String toString() {
{
sb.append(iteration.first()).append(" ").append(iteration.second()).append(", ");
}
sb.replace(sb.length() - 2, sb.length(), "");
sb.replace(
sb.length() - 2,
sb.length(),
""
);
sb.append(" ]\n");
}
sb.replace(sb.length() - 1, sb.length(), "");
sb.replace(
sb.length() - 1,
sb.length(),
""
);
return sb.toString();
}
}
Loading

0 comments on commit fceb63f

Please sign in to comment.