import java.util.Date;
import java.util.HashMap;
import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
import org.tizen.common.core.application.InstallPathConfig;
import org.tizen.dynamicanalyzer.constant.CommonConstants;
-public class InternalLogger {
+public class InternalLogger implements Runnable {
public enum testState {
START, END
};
private static InternalLogger instance;
+
+ /**
+ * working thread, takes messages from queue and prints them
+ */
+ private static Thread logThread;
+ /**
+ * capacity of the message queue
+ */
+ private static final int MSG_QUEUE_CAPACITY = 256;
+ /**
+ * message queue, blocks caller thread on put() if it is full and on take()
+ * if it is empty
+ */
+ private static final BlockingQueue<Message> msgQueue = new ArrayBlockingQueue<Message>(
+ MSG_QUEUE_CAPACITY);
// Log level
public final static int ERROR = 0;
public final static int CHECK = 3;
public final static int DEBUG = 4;
public final static int PERFORMANCE = 5;
-
- private final static int PERFORMANCE_TEST_COUNT = 20;
+
private final static String PERFORMANCE_TEST_NAME = "DA performance test";
private final static String TABSPACE = " ";
+ CommonConstants.SPACE;
private int logLevel; // INFO(release), DEBUG & CHECK(develop), PERFORMANCE(test)
-// private String userKey = null;
- private SimpleDateFormat dateFormat;
+ private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
private File outputFile = null;
private FileWriter fileWriter = null;
private HashMap<String, Long> totalSpentTimeMap;
private HashMap<String, List<Long>> subThreadStartTimeMap;
private HashMap<String, Integer> subThreadStartCountMap;
-
- public InternalLogger() {
- init();
- }
-
- public synchronized static InternalLogger getInstance() {
- if(instance == null) {
- instance = new InternalLogger();
+
+ /**
+ * Class representing logging message.
+ */
+ private static class Message {
+ /**
+ * logging type
+ */
+ public int type;
+ /**
+ * message object
+ */
+ public Object obj;
+
+ /**
+ * Constructs new instance with given logging type and message object.
+ *
+ * @param type logging type
+ * @param obj message object
+ */
+ public Message(int type, Object obj) {
+ this.type = type;
+ this.obj = obj;
}
- return instance;
}
- private void init() {
- dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
+ static {
+ instance = new InternalLogger();
+ logThread = new Thread(null, instance, "InternalLogger Thread");
+ logThread.start();
+ }
+
+ public static InternalLogger getInstance() {
+ return instance;
}
public boolean checkLevel(int currentLogLevel) {
bufWriter = null;
printWriter = null;
}
-
+
+ /**
+ * Tries to put message into message queue.
+ *
+ * @param type type of log message
+ * @param obj message object to put into queue
+ */
+ private void tryPutMessage(int type, Object obj) {
+ try {
+ msgQueue.put(new Message(type, obj));
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
+
public void printLog() {
- System.out.println();
+ tryPutMessage(INFO, CommonConstants.NEW_LINE);
}
public void printLog(boolean applyNewLine, int logType, Object msg) {
if (applyNewLine == false) {
- System.out.print(msg);
+ tryPutMessage(logType, msg);
} else {
if (msg instanceof Throwable) {
Throwable e = (Throwable)msg;
}
}
- System.err.println(exceptionMsg);
- } else {
- if (logType == PERFORMANCE) {
- if (printWriter == null) {
- return;
- }
-
- printWriter.println(msg);
- printWriter.checkError();
- } else {
- StringBuilder logMsg = new StringBuilder();
- switch (logType) {
- case ERROR:
+ msg = exceptionMsg;
+ logType = ERROR;
+ } else if (logType != PERFORMANCE) {
+ StringBuilder logMsg = new StringBuilder();
+ switch (logType) {
+ case ERROR :
logMsg.append("[ERROR] ");
break;
- case WARNING:
+ case WARNING :
logMsg.append("[WARNING] ");
break;
- case INFO:
+ case INFO :
logMsg.append("[INFO] ");
break;
- case CHECK:
+ case CHECK :
logMsg.append("[CHECK] ");
break;
- case DEBUG:
+ case DEBUG :
logMsg.append("[DEBUG] ");
break;
- default:
+ default :
break;
- }
-
- logMsg.append(dateFormat.format(new Date()));
- logMsg.append(SEPARATOR_PIPE);
- if (logType != ERROR)
- logMsg.append(getCallerInfo());
- logMsg.append(String.valueOf(msg));
-
- if (logType == ERROR) {
- logMsg.append(getCallstackInfo());
- System.err.println(logMsg);
- } else {
- System.out.println(logMsg);
- }
}
+
+ logMsg.append(dateFormat.format(new Date()));
+ logMsg.append(SEPARATOR_PIPE);
+ if (logType != ERROR)
+ logMsg.append(getCallerInfo());
+ logMsg.append(String.valueOf(msg));
+
+ if (logType == ERROR)
+ logMsg.append(getCallstackInfo());
+
+ msg = logMsg;
}
+
+ tryPutMessage(logType, msg + CommonConstants.NEW_LINE);
}
}
-// public void printLog(int logType, String userKey, Object msg) {
-// if (this.userKey.equals(userKey)) {
-// printLog(logType, msg);
-// } else {
-// return;
-// }
-// }
-
private String getCallerInfo() {
String msg = null;
StackTraceElement[] trace = new Throwable().getStackTrace();
}
}
-// public void performance(String key, String folderPath, testState state) {
-// if (checkLevel(PERFORMANCE)) {
-// switch (state) {
-// case START:
-// performanceStart(key, folderPath);
-// break;
-// case END:
-// performanceEnd(key);
-// break;
-// default:
-// break;
-// }
-// } else {
-// return;
-// }
-// }
-
public void performance(String key, String testStep, String description) {
if (checkLevel(PERFORMANCE)) {
String msg = performanceNormal(key, description);
long startTime = startTimes.remove(startTimes.size() - 1);
long spendTime = nanoTime - startTime;
long totalTime = totalSpentTimeMap.get(key) + spendTime;
-// int totalCount = testTotalCountMap.get(key) + 1;
-// testTotalCountMap.put(key, totalCount);
testCountMap.put(key, count -1);
totalSpentTimeMap.put(key, totalTime);
return msg;
}
- private CharSequence makeCommonLog(int logType, String logMessage) {
- StringBuilder msg = new StringBuilder();
- switch (logType) {
- case ERROR:
- msg.append("[ERROR] ");
- break;
- case WARNING:
- msg.append("[WARNING] ");
- break;
- case CHECK:
- msg.append("[CHECK] ");
- break;
- case INFO:
- msg.append("[INFO] ");
- break;
- case DEBUG:
- msg.append("[DEBUG] ");
- break;
- default:
- break;
- }
-
- msg.append(dateFormat.format(new Date()));
- msg.append(SEPARATOR_PIPE);
- msg.append(getCallerInfo());
- msg.append(logMessage);
-
- return msg;
- }
-
public CharSequence makeTestLog(String testStep, String message) {
StringBuilder msg = new StringBuilder();
msg.append("<testcase step=\"");
return msg;
}
-// public void setPerformanceSaveFilename(String filename) {
-// performanceSaveFilename = filename;
-// }
-
public void setLogLevel(int logLevel) {
this.logLevel = logLevel;
}
-// public void setUserKey(String userKey) {
-// this.userKey = userKey;
-// }
+ @Override
+ public void run() {
+ try {
+ while (true) {
+ Message message = msgQueue.take();
+ switch (message.type) {
+ case ERROR :
+ System.err.print(message.obj);
+ break;
+
+ case PERFORMANCE :
+ if (printWriter == null)
+ break;
+
+ printWriter.println(message.obj);
+ printWriter.checkError();
+ break;
+
+ default :
+ System.out.print(message.obj);
+ break;
+ }
+ }
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ }
}