View Javadoc
1   /*******************************************************************************
2    * jArduino: Arduino C++ Code Generation From Java
3    * Copyright 2020 Tony Washer
4    *
5    * Licensed under the Apache License, Version 2.0 (the "License");
6    * you may not use this file except in compliance with the License.
7    * You may obtain a copy of the License at
8    *
9    *   http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   ******************************************************************************/
17  package net.sourceforge.jarduino.util;
18  
19  import java.io.PrintStream;
20  
21  /**
22   * Log Manager.
23   */
24  public final class ArduinoLogManager {
25      /**
26       * The data section length.
27       */
28      private static final int DATA_SECTION = 32;
29  
30      /**
31       * The data advance.
32       */
33      private static final int DATA_ADVANCE = 3;
34  
35      /**
36       * The output stream.
37       */
38      private final PrintStream theOutput;
39  
40      /**
41       * The initial time.
42       */
43      private final long theTimeZero;
44  
45      /**
46       * The constructor.
47       */
48      private ArduinoLogManager() {
49          theOutput = System.out;
50          theTimeZero = System.nanoTime();
51      }
52  
53      /**
54       * Obtain the singleton instance.
55       * @return the instance.
56       */
57      private static ArduinoLogManager getInstance() {
58          return ArduinoLogManagerHelper.INSTANCE;
59      }
60  
61      /**
62       * Obtain a logger.
63       * @param pOwner the owning class
64       * @return the logger
65       */
66      public static ArduinoLogger getLogger(final Class<?> pOwner) {
67          return new ArduinoLogger(getInstance(), pOwner);
68      }
69  
70      /**
71       * Format message.
72       * @param pOwner the owner
73       * @param pLevel the log level
74       * @param pMessage the message to format
75       * @return the formatted string
76       */
77      String formatMessage(final Class<?> pOwner,
78                           final ArduinoLogLevel pLevel,
79                           final String pMessage) {
80          return (System.nanoTime() - theTimeZero)
81                  + " "
82                  + pLevel.name()
83                  + ": "
84                  + pOwner.getSimpleName()
85                  + "- "
86                  + pMessage;
87      }
88  
89      /**
90       * Format data.
91       * @param pData the data to format
92       * @return the formatted data
93       */
94      public static String formatData(final byte[] pData) {
95          return pData == null
96                 ? "\nnull"
97                 : formatData(pData, 0, pData.length);
98      }
99  
100     /**
101      * Format data.
102      * @param pData the data to format
103      * @param pOffset the offset
104      * @param pLength the length of data
105      * @return the formatted data
106      */
107     public static String formatData(final byte[] pData,
108                                     final int pOffset,
109                                     final int pLength) {
110         /* Handle null data */
111         if (pData == null) {
112             return "\nnull";
113         }
114 
115         /* Handle partial buffer */
116         byte[] myData = pData;
117         if (pOffset != 0 || pLength != pData.length) {
118             myData = new byte[pLength];
119             System.arraycopy(pData, pOffset, myData, 0, pLength);
120         }
121 
122         /* Format the data */
123         final String myFormatted = ArduinoHex.bytesToHexString(myData);
124 
125         /* Place it into StringBuilder buffer */
126         final StringBuilder myBuilder = new StringBuilder();
127         myBuilder.append(myFormatted);
128 
129         /* Loop through the data */
130         int myOffSet = 0;
131         for (int i = 0; i < pLength; i++) {
132             /* Insert blank/newLine between each HexPair */
133             final char myChar = (i % DATA_SECTION) == 0 ? '\n' : ' ';
134             myBuilder.insert(myOffSet, myChar);
135             myOffSet += DATA_ADVANCE;
136         }
137 
138         /* Return the data */
139         return myBuilder.toString();
140     }
141 
142     /**
143      * Write log message.
144      * @param pMessage the message to format
145      */
146     void writeLogMessage(final String pMessage) {
147         synchronized (this) {
148             theOutput.println(pMessage);
149         }
150     }
151 
152     /**
153      * Write log message and exception.
154      * @param pMessage the message to format
155      * @param pException the exception
156      */
157     void writeLogMessage(final String pMessage,
158                          final Throwable pException) {
159         synchronized (this) {
160             theOutput.println(pMessage);
161             if (pException != null) {
162                 pException.printStackTrace(theOutput);
163             } else {
164                 theOutput.println("Null Exception");
165             }
166         }
167     }
168 
169     /**
170      * Log Manager Helper.
171      */
172     private static class ArduinoLogManagerHelper  {
173         /**
174          * The Log Manager instance.
175          */
176         private static final ArduinoLogManageranager.html#ArduinoLogManager">ArduinoLogManager INSTANCE = new ArduinoLogManager();
177     }
178 }