1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sourceforge.jarduino.util;
18
19 import java.io.PrintStream;
20
21
22
23
24 public final class ArduinoLogManager {
25
26
27
28 private static final int DATA_SECTION = 32;
29
30
31
32
33 private static final int DATA_ADVANCE = 3;
34
35
36
37
38 private final PrintStream theOutput;
39
40
41
42
43 private final long theTimeZero;
44
45
46
47
48 private ArduinoLogManager() {
49 theOutput = System.out;
50 theTimeZero = System.nanoTime();
51 }
52
53
54
55
56
57 private static ArduinoLogManager getInstance() {
58 return ArduinoLogManagerHelper.INSTANCE;
59 }
60
61
62
63
64
65
66 public static ArduinoLogger getLogger(final Class<?> pOwner) {
67 return new ArduinoLogger(getInstance(), pOwner);
68 }
69
70
71
72
73
74
75
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
91
92
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
102
103
104
105
106
107 public static String formatData(final byte[] pData,
108 final int pOffset,
109 final int pLength) {
110
111 if (pData == null) {
112 return "\nnull";
113 }
114
115
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
123 final String myFormatted = ArduinoHex.bytesToHexString(myData);
124
125
126 final StringBuilder myBuilder = new StringBuilder();
127 myBuilder.append(myFormatted);
128
129
130 int myOffSet = 0;
131 for (int i = 0; i < pLength; i++) {
132
133 final char myChar = (i % DATA_SECTION) == 0 ? '\n' : ' ';
134 myBuilder.insert(myOffSet, myChar);
135 myOffSet += DATA_ADVANCE;
136 }
137
138
139 return myBuilder.toString();
140 }
141
142
143
144
145
146 void writeLogMessage(final String pMessage) {
147 synchronized (this) {
148 theOutput.println(pMessage);
149 }
150 }
151
152
153
154
155
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
171
172 private static class ArduinoLogManagerHelper {
173
174
175
176 private static final ArduinoLogManageranager.html#ArduinoLogManager">ArduinoLogManager INSTANCE = new ArduinoLogManager();
177 }
178 }