2014-11-20 13:21:19 +00:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2011-2014 Laurent CLOUET
|
|
|
|
|
* Author Laurent CLOUET <laurent.clouet@nopnop.net>
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
|
* as published by the Free Software Foundation; version 2
|
|
|
|
|
* of the License.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.sheepit.client;
|
|
|
|
|
|
|
|
|
|
import java.text.DateFormat;
|
|
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
import java.util.HashMap;
|
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
public class Log {
|
|
|
|
|
private static Log instance = null;
|
|
|
|
|
|
2015-01-25 18:26:21 +00:00
|
|
|
private Map<Integer, ArrayList<String>> checkpoints = new HashMap<Integer, ArrayList<String>>();
|
2014-11-20 13:21:19 +00:00
|
|
|
private int lastCheckPoint;
|
|
|
|
|
private DateFormat dateFormat;
|
|
|
|
|
|
|
|
|
|
private boolean printStdOut;
|
|
|
|
|
|
|
|
|
|
private Log(boolean print_) {
|
|
|
|
|
this.printStdOut = print_;
|
|
|
|
|
this.lastCheckPoint = 0;
|
|
|
|
|
this.checkpoints.put(this.lastCheckPoint, new ArrayList<String>());
|
2019-07-18 16:55:22 +02:00
|
|
|
this.dateFormat = new SimpleDateFormat("dd-MM HH:mm:ss");
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void debug(String msg_) {
|
|
|
|
|
this.append("debug", msg_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void info(String msg_) {
|
|
|
|
|
this.append("info", msg_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void error(String msg_) {
|
|
|
|
|
this.append("error", msg_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void append(String level_, String msg_) {
|
|
|
|
|
if (msg_.equals("") == false) {
|
|
|
|
|
String line = this.dateFormat.format(new java.util.Date()) + " (" + level_ + ") " + msg_;
|
|
|
|
|
if (this.checkpoints.containsKey(this.lastCheckPoint)) {
|
|
|
|
|
this.checkpoints.get(this.lastCheckPoint).add(line);
|
|
|
|
|
}
|
|
|
|
|
if (this.printStdOut == true) {
|
|
|
|
|
System.out.println(line);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public int newCheckPoint() {
|
|
|
|
|
int time = (int) (new Date().getTime());
|
|
|
|
|
this.checkpoints.put(time, new ArrayList<String>());
|
|
|
|
|
this.lastCheckPoint = time;
|
|
|
|
|
return this.lastCheckPoint;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public ArrayList<String> getForCheckPoint(int point_) {
|
|
|
|
|
return this.checkpoints.get(point_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeCheckPoint(int point_) {
|
|
|
|
|
try {
|
|
|
|
|
this.checkpoints.remove(point_);
|
|
|
|
|
}
|
|
|
|
|
catch (UnsupportedOperationException e) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-25 18:24:27 +00:00
|
|
|
public static synchronized Log getInstance(Configuration config) {
|
2014-11-20 13:21:19 +00:00
|
|
|
if (instance == null) {
|
|
|
|
|
boolean print = false;
|
|
|
|
|
if (config != null) {
|
2019-08-07 22:17:59 +02:00
|
|
|
print = config.isPrintLog();
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
instance = new Log(print);
|
|
|
|
|
}
|
|
|
|
|
return instance;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-25 18:24:27 +00:00
|
|
|
public static synchronized void printCheckPoint(int point_) {
|
2014-11-20 13:21:19 +00:00
|
|
|
Log log = Log.getInstance(null);
|
|
|
|
|
ArrayList<String> logs = log.getForCheckPoint(point_);
|
2015-01-25 18:39:57 +00:00
|
|
|
for (String alog : logs) {
|
|
|
|
|
System.out.println(alog);
|
2014-11-20 13:21:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|