Child: [r25] (diff)

Download this file

log.java    253 lines (213 with data), 8.9 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
/*
* A mix of log messages and output of results for methods that is all-in-one.
*
* Some people would argue it is better to use something like log4J and I agree
* with their perspective. However, I don't like to add overkills to something
* that should be simple from the start: I want an output as integer, I want
* to log a message and all of this to be logged somewhere to be output on the
* console or web panels when needed.
*
* Static version of the Log class
*/
package script;
import definitions.is;
import java.io.File;
import java.util.ArrayList;
import utils.Settings;
/**
* Modified in 2013-09-19, log.write now outputs the formatted string as result
*
*
* @author Nuno Brito, 15th of December 2012 in Darmstadt, Germany.
*/
public class log {
private static final Boolean
outputToScreen = true; // should the log message be printed?
long
TimeStart = System.currentTimeMillis(); // when this log started
static Settings translation; // support for dynamic translations
static Boolean doTranslation = false; // should we support this or not?
//HashMap<Long, LogEntry> list = new HashMap(); // where keep log messages
static ArrayList<LogEntry> list = new ArrayList(); // where keep log messages
public static Hooks hooks = new Hooks();
static LogEntry latest = null; // the most recent message
public log(){
}
/**
* Add a new message to our log history. If enabled, outputs a message to
* the text console.
*
* @param code The error code associated to this message (404, 500, ...)
* @param message The text of the message, using %1, %2 for variables
* @param args The variables, they will replace the %1, %2 on the message
*/
private static String write1(final int code, final String message,
final String[] args){
if(message == null){
return "";
}
// ignore empty messages
if(message.isEmpty()){
return "";
}
// create a new log entry with our data
LogEntry newEntry = new LogEntry(code, message, args);
// increase the counter
// add this new entry on our list of log messages
list.add(newEntry);
// add this to our translator?
if(doTranslation && (code != is.COMMAND)){
addTranslation(message);
}
// shall we print this out to the screen? (and avoid too much info)
if(outputToScreen){
System.out.println(
utils.time.getTimeFromLong(newEntry.getTime())
+ " " + newEntry.getMessage());
}
// process any hooks we might have
hooks.process(message);
latest = newEntry;
return newEntry.getMessageSimple();
}
/**
* Prints a message without the typical message code and time stamps
* @param message what we want to output
* @param writeTranslation Should this be written on the translation XML?
*/
public static void print(final String message, Boolean writeTranslation){
// create a new log entry with our data
LogEntry newEntry = new LogEntry(message);
// add this new entry on our list of log messages
list.add(newEntry);
// add this to our translator?
if(writeTranslation){
addTranslation(message);
}
latest = newEntry;
}
/**
* Prints a message without the typical message code and time stamps
* @param message
*/
public static void print(final String message){
print(message, false);
}
/**
* Gets the current number of indexed log messages
* @return number of messages indexed
*/
public static long getCounter() {
return list.size();
}
public static ArrayList<LogEntry> getList() {
return list;
}
public static LogEntry getLatest() {
return latest;
}
/**
* Get all the messages since a given index number on our counter
* @param lastCounter Last index that was retrieved
* @return The list with relevant messages or an empty string if no
* messages were found
*/
public static String getMessagesSince(long lastCounter){
String output = "";
int counter = -1;
for(LogEntry newEntry : list){
counter++;
// ignore older entries
if(counter < lastCounter){
continue;
}
if(newEntry.getResult() == is.EXTRA){
output = output.concat(
newEntry.getMessage()
+ "\n");
}else{
output = output.concat(
// utils.time.getTimeFromLong(newEntry.getTime())
utils.time.getTimeFromLongNoDate(newEntry.getTime())
+ " " + newEntry.getMessage()
+ "\n");
}
}
return output;
}
/**
* Decides if wether or not we should output all the logged messages to the
* console screen by default.
* @param outputToScreenText Decide if the message should be readable or not
*/
public void setOutputToScreen(Boolean outputToScreenText) {
outputToScreenText = outputToScreen;
}
/**
* Adds support for dynamic translation of values from English to any
* other given language. The idea is to log all messages in English, write
* them to a file on disk where volunteers can submit the translation and
* then use these values when available.
*
* Not sure about performance issues, but might be worth to try and see how
* this code behaves.
* @param languageID Language identifier based on two letters ISO standard
* @param description Long text description of the language
*/
public static void EnableTranslator(String languageID, String description){
// where are we running from?
String folderBase = System.getProperties().getProperty("user.dir");
// getMessage the folder where translated files are available
File folderTranslation = new File(folderBase, "translation");
// create the translations folder if necessary
utils.files.mkdirs(folderTranslation);
// now create the translation file
File fileTranslation = new File(folderTranslation, "lang-"+languageID
+ ".xml"); // add the XML extension to ease editing
// create the new file or reopen if necessary
translation = new Settings(fileTranslation, description);
// user.dir and user.language from JVM can be used in the future
doTranslation = true;
}
/**
* Adds a new key available for translation if the key does not exist yet.
* This way translators will only need to replace the corresponding message
* value with the term in the language they select.
*
* @param message
*/
static void addTranslation(String message){
if(translation.hasKey(message)==false){
translation.write(message, message);
}
}
public static String write(int code, String message){
String[] text = new String[]{};
return write1(code, message, text);
}
public static String write(int code, String message, String arg1){
String[] text = new String[]{arg1};
return write1(code, message, text);
}
public static String write(final int code, final String message,
final String arg1, String arg2){
String[] text = new String[]{arg1, arg2};
return write1(code, message, text);
}
public static String write(final int code, final String message,
final String arg1, String arg2, String arg3){
String[] text = new String[]{arg1, arg2, arg3};
return write1(code, message, text);
}
public static String write(final int code, final String message,
final String arg1, String arg2, String arg3, String arg4){
String[] text = new String[]{arg1, arg2, arg3, arg4};
return write1(code, message, text);
}
public static String write(final int code, final String message,
final String arg1, String arg2, String arg3, String arg4,
String arg5){
String[] text = new String[]{arg1, arg2, arg3, arg4, arg5};
return write1(code, message, text);
}
}