Skip to content

Commit 89932d5

Browse files
Minor changes to debug logging for finer grained control.
1 parent 88c17c2 commit 89932d5

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/N2kStream.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,16 @@ size_t N2kStream::print(int val, uint8_t radix) {
7272
return print(ptr);
7373
}
7474

75+
size_t N2kStream::println(void) {
76+
return print("\r\n");
77+
}
78+
7579
size_t N2kStream::println(const char *str) {
76-
return print(str) + print("\r\n");
80+
return print(str) + println();
7781
}
7882

7983
size_t N2kStream::println(int val, uint8_t radix) {
80-
return print(val, radix) + print("\r\n");
84+
return print(val, radix) + println();
8185
}
8286

8387
#endif

src/N2kStream.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,14 @@ class N2kStream {
105105
*/
106106
size_t print(int val, uint8_t radix = 10);
107107

108+
/***********************************************************************//**
109+
* \brief Print newline to stream.
110+
*
111+
* \param str String data for the stream
112+
* \return size_t
113+
*/
114+
size_t println(void);
115+
108116
/***********************************************************************//**
109117
* \brief Print string and newline to stream.
110118
*

src/NMEA2000.cpp

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2929
#include <string.h>
3030
#include <stdlib.h>
3131

32-
#define DebugStream Serial
32+
// #define DebugStream Serial // outputs debug messages to the serial console (only for Arduino)
33+
#define DebugStream (*ForwardStream) // outputs debug messages to same destination as ForwardStream
3334

3435
// #define NMEA2000_FRAME_ERROR_DEBUG
3536
// #define NMEA2000_FRAME_IN_DEBUG
3637
// #define NMEA2000_FRAME_OUT_DEBUG
37-
// #define NMEA2000_MSG_DEBUG
38+
// #define NMEA2000_MSG_TX_DEBUG
39+
// #define NMEA2000_MSG_RX_DEBUG // This one spams the console with every parsed message
3840
// #define NMEA2000_BUF_DEBUG
3941
// #define NMEA2000_DEBUG
4042

@@ -68,7 +70,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
6870
# define N2kFrameOutDbgln(fmt, args...)
6971
#endif
7072

71-
#if defined(NMEA2000_MSG_DEBUG)
73+
#if defined(NMEA2000_MSG_TX_DEBUG)
7274
# define N2kMsgDbgStart(fmt, args...) DebugStream.print(N2kMillis()); DebugStream.print(": "); DebugStream.print (fmt , ## args)
7375
# define N2kMsgDbg(fmt, args...) DebugStream.print (fmt , ## args)
7476
# define N2kMsgDbgln(fmt, args...) DebugStream.println (fmt , ## args)
@@ -78,6 +80,16 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
7880
# define N2kMsgDbgln(fmt, args...)
7981
#endif
8082

83+
#if defined(NMEA2000_MSG_RX_DEBUG)
84+
# define N2kMsgRxDbgStart(fmt, args...) DebugStream.print(N2kMillis()); DebugStream.print(": "); DebugStream.print (fmt , ## args)
85+
# define N2kMsgRxDbg(fmt, args...) DebugStream.print (fmt , ## args)
86+
# define N2kMsgRxDbgln(fmt, args...) DebugStream.println (fmt , ## args)
87+
#else
88+
# define N2kMsgRxDbgStart(fmt, args...)
89+
# define N2kMsgRxDbg(fmt, args...)
90+
# define N2kMsgRxDbgln(fmt, args...)
91+
#endif
92+
8193
#if defined(NMEA2000_BUF_DEBUG)
8294
# define DbgPrintBuf(len, buf, addln) PrintBuf(&DebugStream, len, buf, addln)
8395
#else
@@ -2032,7 +2044,7 @@ uint8_t tNMEA2000::SetN2kCANBufMsg(unsigned long canId, unsigned char len, unsig
20322044
);
20332045
MsgIndex++);
20342046
if (MsgIndex<MaxN2kCANMsgs) { // we found start for this message, so add data to it.
2035-
N2kMsgDbgStart("Use msg slot: "); N2kMsgDbgln(MsgIndex);
2047+
N2kMsgRxDbgStart("Use msg slot: "); N2kMsgRxDbgln(MsgIndex);
20362048
if (N2kCANMsgBuf[MsgIndex].LastFrame+1 == buf[0]) { // Right frame is coming
20372049
N2kCANMsgBuf[MsgIndex].LastFrame=buf[0];
20382050
CopyBufToCANMsg(N2kCANMsgBuf[MsgIndex],1,len,buf);
@@ -2053,7 +2065,7 @@ uint8_t tNMEA2000::SetN2kCANBufMsg(unsigned long canId, unsigned char len, unsig
20532065
FindFreeCANMsgIndex(PGN,Source,Destination,MsgIndex);
20542066
#endif
20552067
if ( MsgIndex<MaxN2kCANMsgs ) { // we found free place, so handle frame
2056-
N2kMsgDbgStart("Use msg slot: "); N2kMsgDbgln(MsgIndex);
2068+
N2kMsgRxDbgStart("Use msg slot: "); N2kMsgRxDbgln(MsgIndex);
20572069
N2kCANMsgBuf[MsgIndex].FreeMsg=false;
20582070
N2kCANMsgBuf[MsgIndex].KnownMessage=KnownMessage;
20592071
N2kCANMsgBuf[MsgIndex].SystemMessage=SystemMessage;
@@ -2631,17 +2643,17 @@ void tNMEA2000::ParseMessages() {
26312643

26322644
while (FramesRead<MaxReadFramesOnParse && CANGetFrame(canId,len,buf) ) { // check if data coming
26332645
FramesRead++;
2634-
N2kMsgDbgStart("Received frame, can ID:"); N2kMsgDbg(canId); N2kMsgDbg(" len:"); N2kMsgDbg(len); N2kMsgDbg(" data:"); DbgPrintBuf(len,buf,false); N2kMsgDbgln();
2646+
N2kMsgRxDbgStart("Received frame, can ID:"); N2kMsgRxDbg(canId); N2kMsgRxDbg(" len:"); N2kMsgRxDbg(len); N2kMsgRxDbg(" data:"); DbgPrintBuf(len,buf,false); N2kMsgRxDbgln();
26352647
MsgIndex=SetN2kCANBufMsg(canId,len,buf);
26362648
if (MsgIndex<MaxN2kCANMsgs) {
26372649
if ( !HandleReceivedSystemMessage(MsgIndex) ) {
2638-
N2kMsgDbgStart(" - Non system message, MsgIndex: "); N2kMsgDbgln(MsgIndex);
2650+
N2kMsgRxDbgStart(" - Non system message, MsgIndex: "); N2kMsgRxDbgln(MsgIndex);
26392651
ForwardMessage(N2kCANMsgBuf[MsgIndex]);
26402652
}
26412653
// N2kCANMsgBuf[MsgIndex].N2kMsg.Print(Serial);
26422654
RunMessageHandlers(N2kCANMsgBuf[MsgIndex].N2kMsg);
26432655
N2kCANMsgBuf[MsgIndex].FreeMessage();
2644-
N2kMsgDbgStart(" - Free message, MsgIndex: "); N2kMsgDbg(MsgIndex); N2kMsgDbgln();
2656+
N2kMsgRxDbgStart(" - Free message, MsgIndex: "); N2kMsgRxDbg(MsgIndex); N2kMsgRxDbgln();
26452657
}
26462658
}
26472659

0 commit comments

Comments
 (0)