From 73be1d4f8f7650864c828ecf01d35a36433719e1 Mon Sep 17 00:00:00 2001 From: Jeasonfire Date: Sun, 10 May 2015 22:31:18 +0300 Subject: [PATCH] Added a logging system & gitignored the log files. --- .gitignore | 5 +- .../src/com/saltosion/gladiator/util/Log.java | 76 +++++++++++++++++++ 2 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 core/src/com/saltosion/gladiator/util/Log.java diff --git a/.gitignore b/.gitignore index 716567b..a69bfd2 100644 --- a/.gitignore +++ b/.gitignore @@ -63,4 +63,7 @@ build/ ## OS Specific .DS_Store -/.nb-gradle/private/ \ No newline at end of file +/.nb-gradle/private/ + +## Logging +*.log \ No newline at end of file diff --git a/core/src/com/saltosion/gladiator/util/Log.java b/core/src/com/saltosion/gladiator/util/Log.java new file mode 100644 index 0000000..f5c32da --- /dev/null +++ b/core/src/com/saltosion/gladiator/util/Log.java @@ -0,0 +1,76 @@ +package com.saltosion.gladiator.util; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.util.Calendar; + +/** + * + * @author Jens "Jeasonfire" Pitkänen + */ +public class Log { + + public static final File infoLogFile = new File("info.log"); + public static final File errorLogFile = new File("error.log"); + + private static String getTime() { + String result = ""; + Calendar calendar = Calendar.getInstance(); + int hour = calendar.get(Calendar.HOUR_OF_DAY); + int minute = calendar.get(Calendar.MINUTE); + int second = calendar.get(Calendar.SECOND); + result += (hour < 10 ? "0" + hour : hour) + ":"; + result += (minute < 10 ? "0" + minute : minute) + ":"; + result += second < 10 ? "0" + second : second; + return result; + } + + public static void info(String s) { + info(s, false); + } + + public static void info(String s, boolean saveToFile) { + String message = "[" + getTime() + "] : " + s; + System.out.println(message); + if (saveToFile) { + try { + if (!infoLogFile.exists()) { + infoLogFile.createNewFile(); + } + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(infoLogFile, true), "utf-8")); + writer.append(message); + writer.newLine(); + writer.close(); + } catch (IOException ex) { + System.err.print(ex.getMessage()); + } + } + } + + public static void error(String s) { + error(s, false); + } + + public static void error(String s, boolean saveToFile) { + String message = "[" + getTime() + "] : " + s; + System.err.println(message); + if (saveToFile) { + try { + if (!errorLogFile.exists()) { + errorLogFile.createNewFile(); + } + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(errorLogFile, true), "utf-8")); + writer.append(message); + writer.newLine(); + writer.close(); + } catch (IOException ex) { + System.err.print(ex.getMessage()); + } + } + } + +}