-
Notifications
You must be signed in to change notification settings - Fork 15
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
jupe
wants to merge
3
commits into
master
Choose a base branch
from
stream_support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $@ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 { | ||
|
@@ -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. */ | ||
|
@@ -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 = { | ||
|
@@ -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, | ||
|
@@ -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); | ||
|
@@ -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; | ||
|
@@ -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*)) | ||
{ | ||
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 *)) | ||
{ | ||
|
@@ -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; | ||
|
@@ -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) { | ||
|
@@ -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) { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. previous |
||
//if (retval < 0) | ||
//{ // stream write error happened. what we could do? } | ||
} | ||
//return tmp data pointer back to the beginning | ||
mbed_trace_reset_tmp(); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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