Home | History | Annotate | Download | only in mocking
      1 /*
      2  * Copyright 2010 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  * http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 package com.google.android.testing.mocking;
     17 
     18 import java.io.ByteArrayOutputStream;
     19 import java.io.File;
     20 import java.io.FileNotFoundException;
     21 import java.io.FileOutputStream;
     22 import java.io.IOException;
     23 import java.io.OutputStream;
     24 import java.io.PrintStream;
     25 import java.net.URL;
     26 import java.net.URLClassLoader;
     27 import java.text.SimpleDateFormat;
     28 import java.util.Date;
     29 
     30 import javax.annotation.processing.ProcessingEnvironment;
     31 import javax.tools.Diagnostic.Kind;
     32 
     33 /**
     34  * @author swoodward (at) google.com (Stephen Woodward)
     35  *
     36  */
     37 class ProcessorLogger {
     38   private final OutputStream logFile;
     39   private final ProcessingEnvironment processingEnv;
     40 
     41   ProcessorLogger(OutputStream logFile, ProcessingEnvironment processingEnv) {
     42     this.logFile = logFile;
     43     this.processingEnv = processingEnv;
     44   }
     45 
     46   ProcessorLogger(String logFileName, ProcessingEnvironment processingEnv) {
     47     this.logFile = openLogFile(logFileName);
     48     this.processingEnv = processingEnv;
     49   }
     50 
     51   void reportClasspathError(String clazz, Throwable e) {
     52     printMessage(Kind.ERROR, "Could not find " + clazz);
     53     printMessage(Kind.ERROR, e);
     54     printMessage(Kind.NOTE, "Known Classpath: ");
     55     URL[] allUrls = ((URLClassLoader) this.getClass().getClassLoader()).getURLs();
     56     for (URL url : allUrls) {
     57       printMessage(Kind.NOTE, url.toString());
     58     }
     59   }
     60 
     61   void printMessage(Kind kind, String message) {
     62     processingEnv.getMessager().printMessage(kind, message);
     63     if (logFile != null) {
     64       try {
     65         logFile.write((SimpleDateFormat.getDateTimeInstance().format(new Date()) + " - "
     66             + kind.toString() + " : " + message + "\n").getBytes());
     67       } catch (IOException e) {
     68         // That's unfortunate, but not much to do about it.
     69         processingEnv.getMessager().printMessage(Kind.WARNING,
     70             "IOException logging to file" + e.toString());
     71       }
     72     }
     73   }
     74 
     75   void printMessage(Kind kind, Throwable e) {
     76     ByteArrayOutputStream stackTraceByteStream = new ByteArrayOutputStream();
     77     PrintStream stackTraceStream = new PrintStream(stackTraceByteStream);
     78     e.printStackTrace(stackTraceStream);
     79     printMessage(kind, stackTraceByteStream.toString());
     80   }
     81 
     82   FileOutputStream openLogFile(String logFileName) {
     83     try {
     84       if (logFileName != null) {
     85         File log = new File(logFileName);
     86         if (!log.exists() && log.getParentFile() != null) {
     87           log.getParentFile().mkdirs();
     88         }
     89         return new FileOutputStream(log, true);
     90       }
     91     } catch (FileNotFoundException e) {
     92       printMessage(Kind.WARNING, e);
     93     }
     94     return null;
     95   }
     96 
     97   void close() {
     98     if (logFile != null) {
     99       try {
    100         logFile.close();
    101       } catch (IOException e) {
    102         // That's ok
    103       }
    104     }
    105   }
    106 }
    107