Skip to content

Commit

Permalink
Merge pull request #1 from ERNICommunity/tr-print-vararg
Browse files Browse the repository at this point in the history
add vararg print macro and function to DbgTracePort
  • Loading branch information
dniklaus committed May 19, 2017
2 parents 5c48753 + 5fbb813 commit 11c88a5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
19 changes: 15 additions & 4 deletions DbgTracePort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@
* Author: niklausd
*/

#include "DbgTraceContext.h"
#include "DbgTraceOut.h"
#include "DbgTracePort.h"
#include "DbgCliCommand.h"
#include <DbgTraceContext.h>
#include <DbgTraceOut.h>
#include <DbgTracePort.h>
#include <DbgCliCommand.h>

#ifdef ARDUINO
#include <Arduino.h>
Expand All @@ -17,6 +17,7 @@
#endif
#include <stdio.h>
#include <string.h>
#include <stdarg.h>

//-----------------------------------------------------------------------------
// concrete command class DbgCli_Command_ChangeOut
Expand Down Expand Up @@ -372,6 +373,16 @@ DbgTrace_Port::~DbgTrace_Port()
}
}

void DbgTrace_Port::printStrFormat(const char* format, ...)
{
char stream[s_cTraceBufSize];
va_list args;
va_start(args, format);
vsnprintf(stream, s_cTraceBufSize, format, args);
va_end(args);
printStr(stream);
}

void DbgTrace_Port::printStr(const char* str)
{
if(0 != m_out)
Expand Down
10 changes: 9 additions & 1 deletion DbgTracePort.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class DbgTrace_Context;
class DbgTrace_Out;
class DbgCli_Topic;

#define TR_PRINTF(PORT, LEVEL, args...) do { if ((PORT)->getLevel()>=(LEVEL)) (PORT)->printStrFormat(args); } while (0);
#define TR_PRINT_STR(PORT, LEVEL, MSG) do { if (((PORT)->getLevel()>=(LEVEL))) (PORT)->printStr((MSG)); } while (0);
#define TR_PRINT_LONG(PORT, LEVEL, MSG) do { if (((PORT)->getLevel()>=(LEVEL))) (PORT)->printLong((MSG)); } while (0);
#define TR_PRINT_DBL(PORT, LEVEL, MSG) do { if (((PORT)->getLevel()>=(LEVEL))) (PORT)->printDbl((MSG)); } while (0);
Expand Down Expand Up @@ -104,6 +105,13 @@ class DbgTrace_Port
*/
DbgTrace_Level::Level getLevel() { return m_level; }

/**
* @brief Helper method to print a formatted string. Don't call this function directly, use the defined macros.
* @param format Const char format string to be printed out.
* @param var_arg Variable list of paramters to be printed according to placeholders in the format string.
*/
void printStrFormat(const char* format, ...);

/**
* @brief Helper method to print a string. Don't call this function directly, use the defined macros.
* @param str Const char message to be printed out.
Expand Down Expand Up @@ -148,7 +156,7 @@ class DbgTrace_Port
static const unsigned int s_cMaxPortTagLength = 16; /*!< Max. number of characters for a tag name of a trace port. */
static const unsigned int s_cTestTimeStamp = 10; /*!< Max. number of characters for the printed time-stamp using the test environment. */
static const unsigned int s_cArduinoTimeStamp = 12; /*!< Max. number of characters for the printed time-stamp using arduino. */
static const unsigned int s_cTraceBufSize = s_cMaxPortTagLength + s_cMaxPortTagLength + 40; /*!< Max. number of characters of the hole printed message. */
static const unsigned int s_cTraceBufSize = s_cMaxPortTagLength + s_cMaxPortTagLength + 100; /*!< Max. number of characters of the hole printed message. */

private:

Expand Down

0 comments on commit 11c88a5

Please sign in to comment.