| summaryrefslogtreecommitdiff |
diff options
Diffstat (limited to 'src/Error.java')
| -rw-r--r-- | src/Error.java | 196 |
1 files changed, 196 insertions, 0 deletions
diff --git a/src/Error.java b/src/Error.java new file mode 100644 index 0000000..2f40e4b --- /dev/null +++ b/src/Error.java @@ -0,0 +1,196 @@ +import java.util.concurrent.Semaphore; + +import java.io.PrintWriter; +import java.io.FileWriter; + +public enum Error +{ + WARNING("[W]"), + ERROR("[E]"), + FATAL("[F]"), + PROGRAM("[P]"); /* Errors that should not if the code's logic was correct. */ + + private static final Semaphore MUTEX; + private static PrintWriter OUTPUT; + private final String emblem; + private int count; + + static + { + MUTEX = new Semaphore(1); + } + + private Error (final String emblem) + { + this.emblem = emblem; + this.count = 0; + } + + public static int init_handler () + { + try + { + OUTPUT = + new PrintWriter + ( + new FileWriter(Parameters.get_log_filename()) + ); + } + catch (final Exception e) + { + System.err.println("[F] Could not init the Error Handler:"); + e.printStackTrace(); + + return -1; + } + + return 0; + } + + public static void finalize_handler () + { + OUTPUT.flush(); + OUTPUT.close(); + + summary(); + } + + public void from_thread + ( + final String thread_id, + final String error + ) + { + try + { + MUTEX.acquire(); + + OUTPUT.print(emblem); + OUTPUT.print("[Thread: "); + OUTPUT.print(thread_id); + OUTPUT.print("] "); + OUTPUT.println(error); + + ++count; + + MUTEX.release(); + } + catch (final InterruptedException ie) + { + System.err.println + ( + "[W] An thread was interrupted while trying to report an error." + ); + } + } + + public void from_thread + ( + final String thread_id, + final String error, + final String report + ) + { + try + { + MUTEX.acquire(); + + OUTPUT.print(emblem); + OUTPUT.print("[Thread: "); + OUTPUT.print(thread_id); + OUTPUT.print("] "); + OUTPUT.println(error); + OUTPUT.println(report); + + ++count; + + MUTEX.release(); + } + catch (final InterruptedException ie) + { + System.err.println + ( + "[W] An thread was interrupted while trying to report an error." + ); + } + } + + public void from_file + ( + final String filename, + final String error + ) + { + try + { + MUTEX.acquire(); + + OUTPUT.print(emblem); + OUTPUT.print("[File: "); + OUTPUT.print(filename); + OUTPUT.print("] "); + OUTPUT.println(error); + + ++count; + + MUTEX.release(); + } + catch (final InterruptedException ie) + { + System.err.println + ( + "[W] An thread was interrupted while trying to report an error." + ); + } + } + + public void from_file + ( + final String filename, + final String error, + final String report + ) + { + try + { + MUTEX.acquire(); + + OUTPUT.print(emblem); + OUTPUT.print("[File: "); + OUTPUT.print(filename); + OUTPUT.print("] "); + OUTPUT.println(error); + OUTPUT.println(report); + + ++count; + + MUTEX.release(); + } + catch (final InterruptedException ie) + { + System.err.println + ( + "[W] An thread was interrupted while trying to report an error." + ); + } + } + + private static void summary () + { + final StringBuilder sb; + + sb = new StringBuilder(); + + sb.append("Log report:\n"); + + for (final Error e: Error.values()) + { + sb.append(e.emblem); + sb.append(" "); + sb.append(e.count); + sb.append("\n"); + } + + System.out.println(sb.toString()); + } +} |


