Skip to content

Commit

Permalink
Minor changes to debug logging for finer grained control.
Browse files Browse the repository at this point in the history
  • Loading branch information
pabbott-lumitec committed Dec 18, 2024
1 parent 88c17c2 commit 89932d5
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/N2kStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,16 @@ size_t N2kStream::print(int val, uint8_t radix) {
return print(ptr);
}

size_t N2kStream::println(void) {
return print("\r\n");
}

size_t N2kStream::println(const char *str) {
return print(str) + print("\r\n");
return print(str) + println();
}

size_t N2kStream::println(int val, uint8_t radix) {
return print(val, radix) + print("\r\n");
return print(val, radix) + println();
}

#endif
8 changes: 8 additions & 0 deletions src/N2kStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ class N2kStream {
*/
size_t print(int val, uint8_t radix = 10);

/***********************************************************************//**
* \brief Print newline to stream.
*
* \param str String data for the stream
* \return size_t
*/
size_t println(void);

/***********************************************************************//**
* \brief Print string and newline to stream.
*
Expand Down
28 changes: 20 additions & 8 deletions src/NMEA2000.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,14 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <string.h>
#include <stdlib.h>

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

// #define NMEA2000_FRAME_ERROR_DEBUG
// #define NMEA2000_FRAME_IN_DEBUG
// #define NMEA2000_FRAME_OUT_DEBUG
// #define NMEA2000_MSG_DEBUG
// #define NMEA2000_MSG_TX_DEBUG
// #define NMEA2000_MSG_RX_DEBUG // This one spams the console with every parsed message
// #define NMEA2000_BUF_DEBUG
// #define NMEA2000_DEBUG

Expand Down Expand Up @@ -68,7 +70,7 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# define N2kFrameOutDbgln(fmt, args...)
#endif

#if defined(NMEA2000_MSG_DEBUG)
#if defined(NMEA2000_MSG_TX_DEBUG)
# define N2kMsgDbgStart(fmt, args...) DebugStream.print(N2kMillis()); DebugStream.print(": "); DebugStream.print (fmt , ## args)
# define N2kMsgDbg(fmt, args...) DebugStream.print (fmt , ## args)
# define N2kMsgDbgln(fmt, args...) DebugStream.println (fmt , ## args)
Expand All @@ -78,6 +80,16 @@ OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
# define N2kMsgDbgln(fmt, args...)
#endif

#if defined(NMEA2000_MSG_RX_DEBUG)
# define N2kMsgRxDbgStart(fmt, args...) DebugStream.print(N2kMillis()); DebugStream.print(": "); DebugStream.print (fmt , ## args)
# define N2kMsgRxDbg(fmt, args...) DebugStream.print (fmt , ## args)
# define N2kMsgRxDbgln(fmt, args...) DebugStream.println (fmt , ## args)
#else
# define N2kMsgRxDbgStart(fmt, args...)
# define N2kMsgRxDbg(fmt, args...)
# define N2kMsgRxDbgln(fmt, args...)
#endif

#if defined(NMEA2000_BUF_DEBUG)
# define DbgPrintBuf(len, buf, addln) PrintBuf(&DebugStream, len, buf, addln)
#else
Expand Down Expand Up @@ -2032,7 +2044,7 @@ uint8_t tNMEA2000::SetN2kCANBufMsg(unsigned long canId, unsigned char len, unsig
);
MsgIndex++);
if (MsgIndex<MaxN2kCANMsgs) { // we found start for this message, so add data to it.
N2kMsgDbgStart("Use msg slot: "); N2kMsgDbgln(MsgIndex);
N2kMsgRxDbgStart("Use msg slot: "); N2kMsgRxDbgln(MsgIndex);
if (N2kCANMsgBuf[MsgIndex].LastFrame+1 == buf[0]) { // Right frame is coming
N2kCANMsgBuf[MsgIndex].LastFrame=buf[0];
CopyBufToCANMsg(N2kCANMsgBuf[MsgIndex],1,len,buf);
Expand All @@ -2053,7 +2065,7 @@ uint8_t tNMEA2000::SetN2kCANBufMsg(unsigned long canId, unsigned char len, unsig
FindFreeCANMsgIndex(PGN,Source,Destination,MsgIndex);
#endif
if ( MsgIndex<MaxN2kCANMsgs ) { // we found free place, so handle frame
N2kMsgDbgStart("Use msg slot: "); N2kMsgDbgln(MsgIndex);
N2kMsgRxDbgStart("Use msg slot: "); N2kMsgRxDbgln(MsgIndex);
N2kCANMsgBuf[MsgIndex].FreeMsg=false;
N2kCANMsgBuf[MsgIndex].KnownMessage=KnownMessage;
N2kCANMsgBuf[MsgIndex].SystemMessage=SystemMessage;
Expand Down Expand Up @@ -2631,17 +2643,17 @@ void tNMEA2000::ParseMessages() {

while (FramesRead<MaxReadFramesOnParse && CANGetFrame(canId,len,buf) ) { // check if data coming
FramesRead++;
N2kMsgDbgStart("Received frame, can ID:"); N2kMsgDbg(canId); N2kMsgDbg(" len:"); N2kMsgDbg(len); N2kMsgDbg(" data:"); DbgPrintBuf(len,buf,false); N2kMsgDbgln();
N2kMsgRxDbgStart("Received frame, can ID:"); N2kMsgRxDbg(canId); N2kMsgRxDbg(" len:"); N2kMsgRxDbg(len); N2kMsgRxDbg(" data:"); DbgPrintBuf(len,buf,false); N2kMsgRxDbgln();
MsgIndex=SetN2kCANBufMsg(canId,len,buf);
if (MsgIndex<MaxN2kCANMsgs) {
if ( !HandleReceivedSystemMessage(MsgIndex) ) {
N2kMsgDbgStart(" - Non system message, MsgIndex: "); N2kMsgDbgln(MsgIndex);
N2kMsgRxDbgStart(" - Non system message, MsgIndex: "); N2kMsgRxDbgln(MsgIndex);
ForwardMessage(N2kCANMsgBuf[MsgIndex]);
}
// N2kCANMsgBuf[MsgIndex].N2kMsg.Print(Serial);
RunMessageHandlers(N2kCANMsgBuf[MsgIndex].N2kMsg);
N2kCANMsgBuf[MsgIndex].FreeMessage();
N2kMsgDbgStart(" - Free message, MsgIndex: "); N2kMsgDbg(MsgIndex); N2kMsgDbgln();
N2kMsgRxDbgStart(" - Free message, MsgIndex: "); N2kMsgRxDbg(MsgIndex); N2kMsgRxDbgln();
}
}

Expand Down

0 comments on commit 89932d5

Please sign in to comment.