View Javadoc

1   /*
2    * #%L
3    * xcode-maven-plugin
4    * %%
5    * Copyright (C) 2012 SAP AG
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * 
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   * 
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
19   */
20  package com.sap.prd.mobile.ios.mios;
21  
22  import java.util.Arrays;
23  import java.util.Collection;
24  import java.util.Collections;
25  import java.util.logging.ErrorManager;
26  import java.util.logging.Handler;
27  import java.util.logging.Level;
28  import java.util.logging.LogRecord;
29  import java.util.logging.Logger;
30  
31  import org.apache.maven.plugin.logging.Log;
32  import org.apache.maven.plugin.logging.SystemStreamLog;
33  
34  
35  public class XCodePluginLogger extends Logger
36  {
37    private Log log = new SystemStreamLog();
38    
39    private final static Collection<Level> DEBUG_LEVELS = Collections.unmodifiableCollection(Arrays.asList(Level.ALL,
40          Level.FINEST, Level.FINER, Level.FINE, Level.CONFIG));
41  
42    public XCodePluginLogger()
43    {
44      super(getLoggerName(), null);
45      setUseParentHandlers(false);
46  
47      for(Handler h : getHandlers()) {
48        super.removeHandler(h);
49      }
50  
51      addHandler(new Handler() {
52  
53        @Override
54        public void close() throws SecurityException
55        {
56        }
57  
58        @Override
59        public void flush()
60        {
61        }
62  
63        @Override
64        public boolean isLoggable(LogRecord record)
65        {
66          if(DEBUG_LEVELS.contains(record.getLevel()))
67          {
68            return log.isDebugEnabled();
69          }
70  
71          return super.isLoggable(record);
72        }
73  
74        @Override
75        public void publish(LogRecord record)
76        {
77          final Level level = record.getLevel();
78  
79          if(DEBUG_LEVELS.contains(level))
80          {
81            if(record.getThrown() == null)
82            {
83              log.debug(record.getMessage());
84            } 
85            else
86            { 
87              log.debug(record.getMessage(), record.getThrown());
88            }
89          } 
90          else if(level == Level.INFO)
91          {
92            if(record.getThrown() == null)
93            {
94              log.info(record.getMessage());
95            }
96            else 
97            {
98              log.info(record.getMessage(), record.getThrown());
99            }
100         }
101         else if(level == Level.WARNING) 
102         {
103           if(record.getThrown() == null)
104           {
105             log.warn(record.getMessage());
106           }
107           else 
108           {
109             log.warn(record.getMessage(), record.getThrown());
110           }
111         }
112         else if(level == Level.SEVERE) 
113         {
114           if(record.getThrown() == null)
115           {
116             log.error(record.getMessage());
117           }
118           else 
119           {
120             log.error(record.getMessage(), record.getThrown());
121           }
122         }
123         else
124         {
125           getErrorManager().error("Cannot handle log message with level '" + record.getLevel() + "'.", null, ErrorManager.GENERIC_FAILURE);
126         }
127       }
128     });
129   }
130 
131   public void setLog(Log log) {
132 
133     if(log == null)
134       throw new NullPointerException();
135 
136     this.log = log;
137   }
138   
139   public final static String getLoggerName() {
140     return XCodePluginLogger.class.getPackage().getName();
141   }
142 }