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

[features]: 实现中断上下文打印输出特性支持 #149

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
3 changes: 3 additions & 0 deletions easylogger/inc/elog_cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,8 @@
#define ELOG_BUF_OUTPUT_ENABLE
/* buffer size for buffered output mode */
#define ELOG_BUF_OUTPUT_BUF_SIZE (ELOG_LINE_BUF_SIZE * 10)
/*---------------------------------------------------------------------------*/
/* enable use elog in isr context */
// #define ELOG_USING_ISR_LOG

#endif /* _ELOG_CFG_H_ */
61 changes: 46 additions & 15 deletions easylogger/port/elog_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* Function: Portable interface for each platform.
* Created on: 2015-04-28
*/

#include <elog.h>

/**
Expand All @@ -37,7 +37,7 @@ ElogErrCode elog_port_init(void) {
ElogErrCode result = ELOG_NO_ERR;

/* add your code here */

return result;
}

Expand All @@ -58,27 +58,58 @@ void elog_port_deinit(void) {
* @param size log size
*/
void elog_port_output(const char *log, size_t size) {


/* add your code here */

}

/**
* interrupt get nest level
*/
void elog_port_interrupt_get_nest(void) {

/* add your code here */

}

#ifdef ELOG_USING_ISR_LOG

/**
* output lock in isr context
*/
void elog_port_output_lock_isr(void) {

/* add your code here */

}

/**
* output unlock in isr context
*/
void elog_port_output_unlock_isr(void) {

/* add your code here */

}

#endif

/**
* output lock
*/
void elog_port_output_lock(void) {

/* add your code here */

}

/**
* output unlock
*/
void elog_port_output_unlock(void) {

/* add your code here */

}

/**
Expand All @@ -87,9 +118,9 @@ void elog_port_output_unlock(void) {
* @return current time
*/
const char *elog_port_get_time(void) {

/* add your code here */

}

/**
Expand All @@ -98,9 +129,9 @@ const char *elog_port_get_time(void) {
* @return current process name
*/
const char *elog_port_get_p_info(void) {

/* add your code here */

}

/**
Expand All @@ -109,7 +140,7 @@ const char *elog_port_get_p_info(void) {
* @return current thread name
*/
const char *elog_port_get_t_info(void) {

/* add your code here */
}

}
84 changes: 70 additions & 14 deletions easylogger/src/elog.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@
/* EasyLogger object */
static EasyLogger elog;
/* every line log's buffer */
static char log_buf[ELOG_LINE_BUF_SIZE] = { 0 };
static char thread_log_buf[ELOG_LINE_BUF_SIZE] = { 0 };
#ifdef ELOG_USING_ISR_LOG
static char isr_log_buf[ELOG_LINE_BUF_SIZE] = { 0 };
#endif
/* level output info */
static const char *level_output_info[] = {
[ELOG_LVL_ASSERT] = "A/",
Expand Down Expand Up @@ -147,9 +150,29 @@ static void elog_set_filter_tag_lvl_default(void);
void (*elog_assert_hook)(const char* expr, const char* func, size_t line);

extern void elog_port_output(const char *log, size_t size);
extern int elog_port_interrupt_get_nest(void);

#ifdef ELOG_USING_ISR_LOG
extern void elog_port_output_lock_isr(void);
extern void elog_port_output_unlock_isr(void);
#endif

extern void elog_port_output_lock(void);
extern void elog_port_output_unlock(void);

static char *get_log_buf(void)
{
if (elog_port_interrupt_get_nest() == 0) {
return thread_log_buf;
} else {
#ifdef ELOG_USING_ISR_LOG
return isr_log_buf;
#else
return NULL;
#endif
}
}

/**
* EasyLogger initialize.
*
Expand Down Expand Up @@ -211,7 +234,7 @@ void elog_deinit(void) {
if (!elog.init_ok) {
return ;
}

#ifdef ELOG_ASYNC_OUTPUT_ENABLE
elog_async_deinit();
#endif
Expand All @@ -230,7 +253,7 @@ void elog_start(void) {
if (!elog.init_ok) {
return ;
}

/* enable output */
elog_set_output_enabled(true);

Expand Down Expand Up @@ -280,7 +303,7 @@ void elog_set_output_enabled(bool enabled) {
#ifdef ELOG_COLOR_ENABLE
/**
* set log text color enable or disable
*
*
* @param enabled TRUE: enable FALSE:disable
*/
void elog_set_text_color_enabled(bool enabled) {
Expand Down Expand Up @@ -365,26 +388,40 @@ void elog_set_filter_kw(const char *keyword) {
}

/**
* lock output
* lock output
*/
void elog_output_lock(void) {
if (elog.output_lock_enabled) {
elog_port_output_lock();
elog.output_is_locked_before_disable = true;
if(elog_port_interrupt_get_nest() == 0) {
if (elog.output_lock_enabled) {
elog_port_output_lock();
elog.output_is_locked_before_disable = true;
} else {
elog.output_is_locked_before_enable = true;
}
} else {
elog.output_is_locked_before_enable = true;
#ifdef ELOG_USING_ISR_LOG
if (elog.output_lock_enabled)
elog_port_output_lock_isr();
#endif
}
}

/**
* unlock output
*/
void elog_output_unlock(void) {
if (elog.output_lock_enabled) {
elog_port_output_unlock();
elog.output_is_locked_before_disable = false;
if(elog_port_interrupt_get_nest() == 0) {
if (elog.output_lock_enabled) {
elog_port_output_unlock();
elog.output_is_locked_before_disable = false;
} else {
elog.output_is_locked_before_enable = false;
}
} else {
elog.output_is_locked_before_enable = false;
#ifdef ELOG_USING_ISR_LOG
if (elog.output_lock_enabled)
elog_port_output_unlock_isr();
#endif
}
}

Expand Down Expand Up @@ -506,13 +543,19 @@ uint8_t elog_get_filter_tag_lvl(const char *tag)
void elog_raw_output(const char *format, ...) {
va_list args;
size_t log_len = 0;
char *log_buf = NULL;
int fmt_result;

/* check output enabled */
if (!elog.output_enabled) {
return;
}

log_buf = get_log_buf();
if (log_buf == NULL) {
return;
}

/* args point to the first variable parameter */
va_start(args, format);

Expand Down Expand Up @@ -563,6 +606,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f
extern const char *elog_port_get_p_info(void);
extern const char *elog_port_get_t_info(void);

char *log_buf = NULL;
size_t tag_len = strlen(tag), log_len = 0, newline_len = strlen(ELOG_NEWLINE_SIGN);
char line_num[ELOG_LINE_NUM_MAX_LEN + 1] = { 0 };
char tag_sapce[ELOG_FILTER_TAG_MAX_LEN / 2 + 1] = { 0 };
Expand All @@ -581,6 +625,12 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f
} else if (!strstr(tag, elog.filter.tag)) { /* tag filter */
return;
}

log_buf = get_log_buf();
if (log_buf == NULL) {
return;
}

/* args point to the first variable parameter */
va_start(args, format);
/* lock output */
Expand Down Expand Up @@ -654,7 +704,7 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f
/* package func info */
if (get_fmt_enabled(level, ELOG_FMT_FUNC)) {
log_len += elog_strcpy(log_len, log_buf + log_len, func);

}
log_len += elog_strcpy(log_len, log_buf + log_len, ")");
}
Expand Down Expand Up @@ -856,6 +906,7 @@ void elog_hexdump(const char *name, uint8_t width, const void *buf, uint16_t siz

uint16_t i, j;
uint16_t log_len = 0;
char *log_buf = NULL;
const uint8_t *buf_p = buf;
char dump_string[8] = {0};
int fmt_result;
Expand All @@ -871,6 +922,11 @@ void elog_hexdump(const char *name, uint8_t width, const void *buf, uint16_t siz
return;
}

log_buf = get_log_buf();
if (log_buf == NULL) {
return;
}

/* lock output */
elog_output_lock();

Expand Down