Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Refactoring
  • Loading branch information
DayS committed Nov 22, 2013
commit 1c9d514424bdb5fc6fa77643515d8e7b05e90d63
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@
import org.androidannotations.helper.ErrorHelper;
import org.androidannotations.helper.Option;
import org.androidannotations.helper.TimeStats;
import org.androidannotations.logger.Level;
import org.androidannotations.logger.Logger;
import org.androidannotations.logger.LoggerContext;
import org.androidannotations.logger.LoggerFactory;
import org.androidannotations.model.AndroidRes;
import org.androidannotations.model.AndroidSystemServices;
Expand Down Expand Up @@ -300,8 +302,9 @@ public class AndroidAnnotationProcessor extends AbstractProcessor {
@Override
public synchronized void init(ProcessingEnvironment processingEnv) {
super.init(processingEnv);
LoggerContext.getInstance().setProcessingEnv(processingEnv);

LOGGER.warn("test: {}", processingEnv);
LOGGER.warn("Initialize AndroidAnnotationProcessor with options {}", processingEnv.getOptions());

Messager messager = processingEnv.getMessager();

Expand Down Expand Up @@ -332,6 +335,13 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
timeStats.clear();
timeStats.start("Whole Processing");

Set<? extends Element> rootElements = roundEnv.getRootElements();
if (LOGGER.isLoggable(Level.TRACE)) {
LOGGER.trace("Start processing for {} annotations {} on {} elements {}", annotations.size(), annotations, rootElements.size(), rootElements);
} else {
LOGGER.info("Start processing for {} annotations on {} elements", annotations.size(), rootElements.size());
}

try {
checkApiAndCoreVersions();
processThrowing(annotations, roundEnv);
Expand All @@ -342,6 +352,10 @@ public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment
}
timeStats.stop("Whole Processing");
timeStats.logStats();

LOGGER.info("Finish processing");

LoggerContext.getInstance().close();
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package org.androidannotations.logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.annotation.processing.Messager;
import javax.annotation.processing.ProcessingEnvironment;
import javax.tools.Diagnostic.Kind;

public class Appender {

private File file;
private FileOutputStream outputStream;
private ProcessingEnvironment processingEnv;

public synchronized void openFile() {
try {
outputStream = new FileOutputStream(file, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}

public synchronized void closeFile() {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
outputStream = null;
}
}

public synchronized void append(String log) {
if (!isFileOpened()) {
openFile();
}

try {
outputStream.write(log.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
}

public void resolveLogFile() {
if (file == null) {
if (processingEnv.getOptions().containsKey(LoggerContext.LOG_FILE_OPTION)) {
file = resolveLogFileInSpecifiedPath();
} else {
file = resolveLogFileInParentsDirectories();
}
}
}

private File resolveLogFileInSpecifiedPath() {
String path = processingEnv.getOptions().get(LoggerContext.LOG_FILE_OPTION);
return new File(path);
}

private File resolveLogFileInParentsDirectories() {
// TODO

if (file == null) {
Messager messager = processingEnv.getMessager();
messager.printMessage(Kind.ERROR, "Can't resolve log file path");
}
return null;
}

public boolean isFileOpened() {
return outputStream != null;
}

public void setFile(File file) {
this.file = file;
}

public void setProcessingEnv(ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public class Formatter {
private static final String ARGS_PATTERN = "{}";
private static final int ARGS_PATTERN_LENGTH = ARGS_PATTERN.length();

public static String buildLog(Level level, String loggerName, String message, Throwable thr, Object... args) {
public String buildLog(Level level, String loggerName, String message, Throwable thr, Object... args) {
String fullMessage = buildFullMessage(message, args);
StringBuilder stringBuilder = new StringBuilder(fullMessage.length());

stringBuilder.append(DATE_FORMAT.format(new Date())) //
.append(" [").append(Thread.currentThread().getName()).append("] ") //
.append(" [").append(Thread.currentThread().getName()).append("]") //
.append(" ").append(level.name) //
.append(" ").append(loggerName) //
.append(" - ").append(fullMessage);
Expand All @@ -33,7 +33,7 @@ public static String buildLog(Level level, String loggerName, String message, Th
return stringBuilder.toString();
}

public static String buildFullMessage(String message, Object... args) {
public String buildFullMessage(String message, Object... args) {
StringBuilder stringBuilder = new StringBuilder(message.length());
int lastIndex = 0;
int argIndex = 0;
Expand All @@ -60,14 +60,14 @@ public static String buildFullMessage(String message, Object... args) {
return stringBuilder.toString();
}

private static String formatArgument(Object arg) {
private String formatArgument(Object arg) {
if (arg != null && arg.getClass().isArray()) {
return Arrays.toString((Object[]) arg);
}
return arg.toString();
}

private static String stackTraceToString(Throwable e) {
private String stackTraceToString(Throwable e) {
StringWriter writer = new StringWriter();
PrintWriter pw = new PrintWriter(writer);
e.printStackTrace(pw);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ private Level(int weight, String name) {
this.name = name;
}

public boolean isGreaterOrEqual(Level l) {
public boolean isGreaterOrEquals(Level l) {
return weight >= l.weight;
}

public boolean isSmaller(Level l) {
return weight < l.weight;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

public class Logger {

private final LoggerContext loggerContext;
private final String name;

public Logger(String name) {
public Logger(LoggerContext loggerContext, String name) {
this.loggerContext = loggerContext;
this.name = name;
}

Expand Down Expand Up @@ -36,12 +38,16 @@ public void error(String message, Throwable thr, Object... args) {
log(Level.ERROR, message, thr, args);
}

public boolean isLoggable(Level level) {
return level.isGreaterOrEquals(loggerContext.getCurrentLevel());
}

private void log(Level level, String message, Throwable thr, Object... args) {
if (LoggerContext.getInstance().getCurrentLevel().isGreaterOrEqual(level)) {
if (!isLoggable(level)) {
return;
}

LoggerContext.getInstance().writeLog(level, name, message, thr, args);
loggerContext.writeLog(level, name, message, thr, args);
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
package org.androidannotations.logger;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.annotation.processing.ProcessingEnvironment;

public class LoggerContext {

public static final String LOG_FILE_OPTION = "logFile";
public static final String LOG_LEVEL_OPTION = "logLevel";
private static LoggerContext INSTANCE = null;
private static final Level DEFAULT_LEVEL = Level.DEBUG;

private Level currentLevel = DEFAULT_LEVEL;
private FileOutputStream outputStream;
private File fileLog;
private Appender appender = new Appender();
private Formatter formatter = new Formatter();

public static LoggerContext getInstance() {
if (INSTANCE == null) {
Expand All @@ -25,24 +26,12 @@ public static LoggerContext getInstance() {
return INSTANCE;
}

private LoggerContext() {
fileLog = new File("androidannotations.log");
try {
outputStream = new FileOutputStream(fileLog, true);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
LoggerContext() {
}

public void writeLog(Level level, String loggerName, String message, Throwable thr, Object... args) {
String log = Formatter.buildLog(level, loggerName, message, thr, args);

try {
outputStream.write(log.getBytes());
outputStream.flush();
} catch (IOException e) {
e.printStackTrace();
}
String log = formatter.buildLog(level, loggerName, message, thr, args);
appender.append(log);
}

public Level getCurrentLevel() {
Expand All @@ -53,4 +42,17 @@ public void setCurrentLevel(Level currentLevel) {
this.currentLevel = currentLevel;
}

public void setFileLog(String path) {
appender.setFile(new File(path));
}

public void setProcessingEnv(ProcessingEnvironment processingEnv) {
appender.setProcessingEnv(processingEnv);
appender.resolveLogFile();
}

public void close() {
appender.closeFile();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ private static Logger getLogger(String name) {

Logger logger = LOGGER_CACHE.get(name);
if (logger == null) {
logger = new Logger(name);
logger = new Logger(LoggerContext.getInstance(), name);
LOGGER_CACHE.put(name, logger);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

public class FormatterTest {

private Formatter formatter = new Formatter();

class SomeObject {
String name;

Expand All @@ -23,28 +25,28 @@ public String toString() {

@Test
public void testBuildFullMessage_string() throws Exception {
Assert.assertEquals("This is a test", Formatter.buildFullMessage("{} is a test", "This"));
Assert.assertEquals("This is a test", Formatter.buildFullMessage("This is a {}", "test"));
Assert.assertEquals("This is a test", Formatter.buildFullMessage("This {} {} test", "is", "a"));
Assert.assertEquals("This is a test", Formatter.buildFullMessage("This {} a test", "is", "a"));
Assert.assertEquals("This is a test", formatter.buildFullMessage("{} is a test", "This"));
Assert.assertEquals("This is a test", formatter.buildFullMessage("This is a {}", "test"));
Assert.assertEquals("This is a test", formatter.buildFullMessage("This {} {} test", "is", "a"));
Assert.assertEquals("This is a test", formatter.buildFullMessage("This {} a test", "is", "a"));
}

@Test
public void testBuildFullMessage_int_array() throws Exception {
Integer[] values = new Integer[] { 1, 2 };
Assert.assertEquals("Values = [1, 2]", Formatter.buildFullMessage("Values = {}", new Object[] { values }));
Assert.assertEquals("Values = [1, 2]", formatter.buildFullMessage("Values = {}", new Object[] { values }));
}

@Test
public void testBuildFullMessage_object_array() throws Exception {
SomeObject[] values = new SomeObject[] { new SomeObject("a"), new SomeObject("b") };
Assert.assertEquals("Objects = [a, b]", Formatter.buildFullMessage("Objects = {}", new Object[] { values }));
Assert.assertEquals("Objects = [a, b]", formatter.buildFullMessage("Objects = {}", new Object[] { values }));
}

@Test
public void testBuildFullMessage_list() throws Exception {
List<SomeObject> values = Arrays.asList(new SomeObject("a"), new SomeObject("b"));
Assert.assertEquals("Objects = [a, b]", Formatter.buildFullMessage("Objects = {}", values));
Assert.assertEquals("Objects = [a, b]", formatter.buildFullMessage("Objects = {}", values));
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.androidannotations.logger;

import junit.framework.Assert;

import org.junit.Test;

public class LoggerTest {

@Test
public void testIsLoggable() throws Exception {
LoggerContext loggerContext = LoggerContext.getInstance();
loggerContext.setCurrentLevel(Level.INFO);
Logger logger = new Logger(loggerContext, getClass().getName());

Assert.assertFalse(logger.isLoggable(Level.DEBUG));
Assert.assertTrue(logger.isLoggable(Level.INFO));
Assert.assertTrue(logger.isLoggable(Level.WARN));
}

}