Child: [r17] (diff)

Download this file

LogEntry.java    124 lines (103 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
* A log entry for keeping the data from each event that we want to track.
*/
package script;
import definitions.is;
/**
*
* @author Nuno Brito, 15th of December 2012 in Darmstadt, Germany.
*/
public class LogEntry {
private int
result; // what is the generic error/event code that we talk about?
private String
message;
private String[]
args; // arguments used for translating the message text
private Boolean hasArgs = false;
private final long
time = System.currentTimeMillis();
/** Set the content for this message */
public LogEntry(final int code, final String message,
final String... args){
this.message = message;
this.result = code;
this.args = args;
this.hasArgs = true;
}
/** Option without arguments */
public LogEntry(final int code, final String message){
this.message = message;
this.result = code;
this.args = null;
}
/**
* Just prints a message, nothing attached
* @param message
*/
public LogEntry (String message){
this.message = message;
this.result = is.EXTRA; // code word of things to ignore internally
this.args = null;
}
/** Convert all arguments onto the nicely formatted text */
private String convert(){
String output = message.replace("%", "%!%");
int count = 1;
try{
while(output.contains("%!%")){
output = output.replace("%!%"+count, args[count-1]);
count++;
}
}catch(Exception e){
output = output.replace("%!%", "%");
} finally{
// all done
return output;
}
}
/** Gets our message. If it has arguments, they are converted.*/
public String getMessage() {
String output = message;
// messages with no header, just to be printed
if(this.result == is.EXTRA){
output = convert();
return output;
}
// convert arguments to a proper text
if(hasArgs){
output = "["+is.translated(this.result)
+"] " +convert();
}
return output;
}
/** Gets our message without describing the type of command.*/
public String getMessageSimple() {
String output = message;
// messages with no header, just to be printed
if(this.result == is.EXTRA){
output = convert();
return output;
}
// convert arguments to a proper text
if(hasArgs){
output = convert();
}
return output;
}
/**
* Get the time when the entry was made
* @return a date string
*/
public String getTimeReadable(){
String output;
output = utils.time.getTimeFromLong(time);
return output;
}
public int getResult() {
return result;
}
public long getTime() {
return time;
}
}