Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce stream support #82

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions example/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
CC=cc
CFLAGS=-c -ggdb -I.. -I../yotta_modules/nanostack-libservice/mbed-client-libservice -Wfatal-errors -DMBED_CONF_MBED_TRACE_FEA_IPV6=0 -DHAVE_NS_TYPES=0 -DMBED_CONF_MBED_TRACE_ENABLE=1
LDFLAGS=
SOURCES=log_stdout.c ../source/mbed_trace.c
#SOURCES=log_file.c ../source/mbed_trace.c
OBJECTS=$(SOURCES:.c=.o)
EXECUTABLE=test

.PHONY: all

#all: stdout file class
all: $(SOURCES) $(EXECUTABLE)

clean:
rm *.o
rm ../source/*.o

$(EXECUTABLE): $(OBJECTS)
$(CC) $(LDFLAGS) $(OBJECTS) -o $@

.cpp.o:
$(CC) $(CFLAGS) $< -o $@
17 changes: 17 additions & 0 deletions example/log_file.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <stdio.h>

#define TRACE_GROUP "file"
#include "mbed-trace/mbed_trace.h"

int main(int argc, char* argv[]) {
mbed_trace_init();
uint8_t cfg = TRACE_ACTIVE_LEVEL_ALL | TRACE_CARRIAGE_RETURN;
mbed_trace_config_set(cfg);
FILE *fh = fopen("./debug.log", "a");
mbed_trace_set_pipe(fh);
tr_debug("debugging to file");
tr_info("info message to file");
tr_warn("warning essage to file");
fclose(fh);
return 0;
}
13 changes: 13 additions & 0 deletions example/log_stdout.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <stdio.h>

#define TRACE_GROUP "stdo"
#include "mbed-trace/mbed_trace.h"

int main(int argc, char* argv[]) {
mbed_trace_init();
tr_debug("test debug: %s: %d", "value", 1);
tr_info("test info");
tr_info("test info");
tr_warn("test warn");
return 0;
}
17 changes: 17 additions & 0 deletions mbed-trace/mbed_trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern "C" {
#include "ns_types.h"
#endif

#include <stdio.h>
#include <stdarg.h>

#ifndef YOTTA_CFG_MBED_TRACE
Expand Down Expand Up @@ -243,6 +244,17 @@ void mbed_trace_suffix_function_set(char* (*suffix_f)(void) );
* for e.g. to other IO device.
*/
void mbed_trace_print_function_set( void (*print_f)(const char*) );
/**
* Set FILE handle where to write trace lines
* By default it points to stdout
* When null is given default pipe (stdout) is used
*/
void mbed_trace_set_pipe(FILE *stream);
/**
* Set fputs -like function
* When null is given print function is in use (mbed_trace_print_function_set)
*/
void mbed_trace_fputs_function_set(int (*fputs_f)(const char *, FILE*));
/**
* Set trace print function for tr_cmdline()
*/
Expand Down Expand Up @@ -391,6 +403,9 @@ char* mbed_trace_array(const uint8_t* buf, uint16_t len);
#undef mbed_trace_prefix_function_set
#undef mbed_trace_suffix_function_set
#undef mbed_trace_print_function_set
#undef mbed_trace_set_pipe
#undef mbed_trace_fputs_function_set
#undef mbed_trace_set_pipe
#undef mbed_trace_cmdprint_function_set
#undef mbed_trace_mutex_wait_function_set
#undef mbed_trace_mutex_release_function_set
Expand All @@ -416,6 +431,8 @@ char* mbed_trace_array(const uint8_t* buf, uint16_t len);
#define mbed_trace_prefix_function_set(...) ((void) 0)
#define mbed_trace_suffix_function_set(...) ((void) 0)
#define mbed_trace_print_function_set(...) ((void) 0)
#define mbed_trace_set_pipe(...) ((void) 0)
#define mbed_trace_fputs_function_set(...) ((void) 0)
#define mbed_trace_cmdprint_function_set(...) ((void) 0)
#define mbed_trace_mutex_wait_function_set(...) ((void) 0)
#define mbed_trace_mutex_release_function_set(...) ((void) 0)
Expand Down
72 changes: 53 additions & 19 deletions source/mbed_trace.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@

/** default print function, just redirect str to printf */
static void mbed_trace_realloc( char **buffer, int *length_ptr, int new_length);
static void mbed_trace_default_print(const char *str);
static int mbed_trace_default_fputs(const char* input, FILE* _);
static void mbed_trace_reset_tmp(void);

typedef struct trace_s {
Expand All @@ -116,13 +116,16 @@ typedef struct trace_s {
int tmp_data_length;
/** temporary data pointer */
char *tmp_data_ptr;

/** File stream for writing */
FILE *stream;
/** prefix function, which can be used to put time to the trace line */
char *(*prefix_f)(size_t);
/** suffix function, which can be used to some string to the end of trace line */
char *(*suffix_f)(void);
/** print out function. Can be redirect to flash for example. */
void (*printf)(const char *);
/** print out wrapper function. */
void (*puts)(const char *);
/** stream out function. Can be redirect to flash for example. */
int (*fputs)(const char *, FILE*);
/** print out function for TRACE_LEVEL_CMD */
void (*cmd_printf)(const char *);
/** mutex wait function which can be called to lock against a mutex. */
Expand All @@ -131,6 +134,7 @@ typedef struct trace_s {
void (*mutex_release_f)(void);
/** number of times the mutex has been locked */
int mutex_lock_count;
unsigned long int wr_bytes;
} trace_t;

static trace_t m_trace = {
Expand All @@ -144,7 +148,9 @@ static trace_t m_trace = {
.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN,
.prefix_f = 0,
.suffix_f = 0,
.printf = mbed_trace_default_print,
.stream = 0,
.puts = 0,
.fputs = 0,
.cmd_printf = 0,
.mutex_wait_f = 0,
.mutex_release_f = 0,
Expand All @@ -160,7 +166,10 @@ int mbed_trace_init(void)
if (m_trace.tmp_data == NULL) {
m_trace.tmp_data = MBED_TRACE_MEM_ALLOC(m_trace.tmp_data_length);
}
m_trace.trace_config = DEFAULT_TRACE_CONFIG;
m_trace.tmp_data_ptr = m_trace.tmp_data;
m_trace.stream = stdout;
m_trace.fputs = fputs;

if (m_trace.filters_exclude == NULL) {
m_trace.filters_exclude = MBED_TRACE_MEM_ALLOC(m_trace.filters_length);
Expand Down Expand Up @@ -203,7 +212,8 @@ void mbed_trace_free(void)
m_trace.tmp_data_length = DEFAULT_TRACE_TMP_LINE_LEN;
m_trace.prefix_f = 0;
m_trace.suffix_f = 0;
m_trace.printf = mbed_trace_default_print;
m_trace.puts = 0;
m_trace.fputs = 0;
m_trace.cmd_printf = 0;
m_trace.mutex_wait_f = 0;
m_trace.mutex_release_f = 0;
Expand Down Expand Up @@ -241,9 +251,28 @@ void mbed_trace_suffix_function_set(char *(*suffix_f)(void))
{
m_trace.suffix_f = suffix_f;
}
void mbed_trace_print_function_set(void (*printf)(const char *))
static int mbed_trace_default_fputs(const char* input, FILE* _)
{
if (m_trace.puts)
{
m_trace.puts(input);
return 0;
}
return EOF; // missing user defined puts function
}
void mbed_trace_print_function_set(void (*puts_f)(const char *))
{
m_trace.puts = puts_f;
m_trace.fputs = mbed_trace_default_fputs;
}
void mbed_trace_set_pipe(FILE *stream)
{
m_trace.stream = stream ? stream : stdout;
//m_trace.wr_bytes = ftell(m_trace.stream);
}
void mbed_trace_fputs_function_set(int (*fputs_f)(const char *, FILE*))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe we doesn’t need this API at all

{
m_trace.printf = printf;
m_trace.fputs = fputs_f ? fputs_f : mbed_trace_default_fputs;
}
void mbed_trace_cmdprint_function_set(void (*printf)(const char *))
{
Expand Down Expand Up @@ -300,10 +329,6 @@ static int8_t mbed_trace_skip(int8_t dlevel, const char *grp)
}
return 0;
}
static void mbed_trace_default_print(const char *str)
{
puts(str);
}
void mbed_tracef(uint8_t dlevel, const char *grp, const char *fmt, ...)
{
va_list ap;
Expand All @@ -324,16 +349,16 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)

m_trace.line[0] = 0; //by default trace is empty

if (mbed_trace_skip(dlevel, grp) || fmt == 0 || grp == 0 || !m_trace.printf) {
if (mbed_trace_skip(dlevel, grp) || fmt == 0 || grp == 0 || (!m_trace.fputs)) {
//return tmp data pointer back to the beginning
mbed_trace_reset_tmp();
goto end;
}

if ((m_trace.trace_config & TRACE_MASK_LEVEL) & dlevel) {
bool color = (m_trace.trace_config & TRACE_MODE_COLOR) != 0;
bool plain = (m_trace.trace_config & TRACE_MODE_PLAIN) != 0;
bool cr = (m_trace.trace_config & TRACE_CARRIAGE_RETURN) != 0;

int retval = 0, bLeft = m_trace.line_length;
char *ptr = m_trace.line;
if (plain == true || dlevel == TRACE_LEVEL_CMD) {
Expand All @@ -344,7 +369,7 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
m_trace.cmd_printf("\n");
} else {
//print out whole data
m_trace.printf(m_trace.line);
m_trace.fputs(m_trace.line, m_trace.stream);
}
} else {
if (color) {
Expand Down Expand Up @@ -464,12 +489,21 @@ void mbed_vtracef(uint8_t dlevel, const char* grp, const char *fmt, va_list ap)
}
if (retval > 0) {
// not used anymore
//ptr += retval;
//bLeft -= retval;
ptr += retval;
bLeft -= retval;
}
}
//print out whole data
m_trace.printf(m_trace.line);
if (retval > 0 && bLeft > 0 && m_trace.fputs != mbed_trace_default_fputs) {
// add line feeds when using stdio:fputs streaming.
// by default we use custom fputs which behaviours same way than puts
// @todo this is a bit hack solution
snprintf(ptr, bLeft, "\n");
}
//print out whole data to the writable stream
//retval =
m_trace.fputs(m_trace.line, m_trace.stream);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previous puts() was used to print line. fputs behaviour a bit differently, but maybe we should use fwrite instead because we know line lenght already and it could be faster, opinions? 🤔

//if (retval < 0)
//{ // stream write error happened. what we could do? }
}
//return tmp data pointer back to the beginning
mbed_trace_reset_tmp();
Expand Down
47 changes: 44 additions & 3 deletions test/Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST_GROUP(trace)
{
void setup()
{

buf[0] = 0;
mbed_trace_init();
mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL);
mbed_trace_print_function_set( myprint );
Expand Down Expand Up @@ -164,7 +164,10 @@ TEST(trace, PreInitConfiguration)
mbed_trace_buffer_sizes(11, 10);
mbed_trace_mutex_wait_function_set( my_mutex_wait );
mbed_trace_mutex_release_function_set( my_mutex_release );

mbed_trace_init();
mbed_trace_config_set(TRACE_MODE_PLAIN|TRACE_ACTIVE_LEVEL_ALL);
mbed_trace_print_function_set( myprint );

STRCMP_EQUAL("30:30:30*", mbed_trace_array(arr, 20));

Expand Down Expand Up @@ -284,7 +287,6 @@ TEST(trace, active_level_debug)

TEST(trace, active_level_info)
{
buf[0] = 0;
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_INFO);

mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hep");
Expand Down Expand Up @@ -456,7 +458,6 @@ TEST(trace, filters_control)
}
TEST(trace, cmd_printer)
{
buf[0] = 0;
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);
mbed_tracef(TRACE_LEVEL_CMD, "mygr", "default printer");
STRCMP_EQUAL("default printer", buf);
Expand All @@ -474,6 +475,46 @@ TEST(trace, no_printer)
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "this shoudnt be printed because printer is missing");
STRCMP_EQUAL("hello", buf);
}
TEST(trace, filestream)
{
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);
FILE* fd = fopen("./test.log", "w");
CHECK(fd);
mbed_trace_fputs_function_set(fputs);
mbed_trace_set_pipe(fd);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
CHECK_EQUAL(ftell(fd), 20);
fclose(fd);
}
TEST(trace, stream_closed)
{
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);
FILE* fd = fopen("./test.log", "w");
CHECK(fd);
mbed_trace_fputs_function_set(fputs);
mbed_trace_set_pipe(fd);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
CHECK_EQUAL(ftell(fd), 20);
fclose(fd);
CHECK_EQUAL(ftell(fd), EOF);
// doesn't crash after FILE is closed
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
}
TEST(trace, switch_stream)
{
mbed_trace_config_set(TRACE_ACTIVE_LEVEL_ALL);
FILE* fd = fopen("./test.log", "w");
CHECK(fd);
mbed_trace_fputs_function_set(fputs);
mbed_trace_set_pipe(fd);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
CHECK_EQUAL(ftell(fd), 20);
fclose(fd);
STRCMP_EQUAL("", buf);
mbed_trace_print_function_set(myprint);
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
STRCMP_EQUAL("[DBG ][mygr]: hello", buf);
}
TEST(trace, uninitialized)
{
mbed_tracef(TRACE_LEVEL_DEBUG, "mygr", "hello");
Expand Down