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 /**
20 * Logger instance.
21 */
22 public class ArduinoLogger {
23 /**
24 * The log manager instance.
25 */
26 private final ArduinoLogManager theManager;
27
28 /**
29 * The class for which this is the logger.
30 */
31 private final Class<?> theOwner;
32
33 /**
34 * Constructor.
35 * @param pManager the manager
36 * @param pOwner the owning class
37 */
38 public ArduinoLogger(final ArduinoLogManager pManager,
39 final Class<?> pOwner) {
40 theManager = pManager;
41 theOwner = pOwner;
42 }
43
44 /**
45 * Write a debug message with parameters.
46 * @param pFormat the format
47 * @param pArgs the arguments
48 */
49 public void debug(final String pFormat,
50 final Object... pArgs) {
51 final String myMessage = String.format(pFormat, pArgs);
52 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.DEBUG, myMessage);
53 theManager.writeLogMessage(myLogMessage);
54 }
55
56 /**
57 * Write a debug message with hex data.
58 * @param pMessage the message
59 * @param pData the data
60 */
61 public void debug(final String pMessage,
62 final byte[] pData) {
63 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.DEBUG, pMessage);
64 final String myLogData = ArduinoLogManager.formatData(pData);
65 theManager.writeLogMessage(myLogMessage + myLogData);
66 }
67
68 /**
69 * Write a debug message with hex data.
70 * @param pMessage the message
71 * @param pData the data
72 * @param pOffset the offset
73 * @param pLength the length of data
74 */
75 public void debug(final String pMessage,
76 final byte[] pData,
77 final int pOffset,
78 final int pLength) {
79 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.DEBUG, pMessage);
80 final String myLogData = ArduinoLogManager.formatData(pData, pOffset, pLength);
81 theManager.writeLogMessage(myLogMessage + myLogData);
82 }
83 /**
84 * Write an information message with parameters.
85 * @param pFormat the format
86 * @param pArgs the arguments
87 */
88 public void info(final String pFormat,
89 final Object... pArgs) {
90 final String myMessage = String.format(pFormat, pArgs);
91 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.INFO, myMessage);
92 theManager.writeLogMessage(myLogMessage);
93 }
94
95 /**
96 * Write an error message with parameters.
97 * @param pFormat the format
98 * @param pArgs the arguments
99 */
100 public void error(final String pFormat,
101 final Object... pArgs) {
102 final String myMessage = String.format(pFormat, pArgs);
103 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.ERROR, myMessage);
104 theManager.writeLogMessage(myLogMessage);
105 }
106
107 /**
108 * Write an error message with exception.
109 * @param pMessage the message
110 * @param pException the exception
111 */
112 public void error(final String pMessage,
113 final Throwable pException) {
114 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.ERROR, pMessage);
115 theManager.writeLogMessage(myLogMessage, pException);
116 }
117
118 /**
119 * Write a fatal error message with parameters.
120 * @param pFormat the format
121 * @param pArgs the arguments
122 */
123 public void fatal(final String pFormat,
124 final Object... pArgs) {
125 final String myMessage = String.format(pFormat, pArgs);
126 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.FATAL, myMessage);
127 theManager.writeLogMessage(myLogMessage);
128 }
129
130 /**
131 * Write a fatal error message with exception.
132 * @param pMessage the message
133 * @param pException the exception
134 */
135 public void fatal(final String pMessage,
136 final Throwable pException) {
137 final String myLogMessage = theManager.formatMessage(theOwner, ArduinoLogLevel.FATAL, pMessage);
138 theManager.writeLogMessage(myLogMessage, pException);
139 }
140 }