From 299cccfb2655423de544db00ed09485df212b3c7 Mon Sep 17 00:00:00 2001 From: Grissiom Date: Thu, 10 Apr 2014 21:18:38 +0800 Subject: [PATCH] logtrace: add log_session_lvl log_session_lvl is suitable for performance critical places where in most cases, the log is turned off by level. If the session is const and the level is greater than session->lvl, the whole function will be optimized out. --- components/utilities/logtrace/log_trace.c | 9 +++--- components/utilities/logtrace/log_trace.h | 36 +++++++++++++++++++++++ 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/components/utilities/logtrace/log_trace.c b/components/utilities/logtrace/log_trace.c index fa9cb8d34..d06046590 100644 --- a/components/utilities/logtrace/log_trace.c +++ b/components/utilities/logtrace/log_trace.c @@ -255,8 +255,9 @@ static rt_size_t _lg_parse_session( return 0; } -void _lg_fmtout( - const struct log_trace_session *session, const char *fmt, va_list argptr) +void __logtrace_vfmtout(const struct log_trace_session *session, + const char *fmt, + va_list argptr) { /* 1 for ']' */ static char _trace_buf[1+LOG_TRACE_BUFSZ]; @@ -303,7 +304,7 @@ void log_trace(const char *fmt, ...) return; va_start(args, fmt); - _lg_fmtout(session, fmt, args); + __logtrace_vfmtout(session, fmt, args); va_end(args); } FINSH_FUNCTION_EXPORT(log_trace, log trace); @@ -321,7 +322,7 @@ void log_session(const struct log_trace_session *session, const char *fmt, ...) return; va_start(args, fmt); - _lg_fmtout(session, fmt, args); + __logtrace_vfmtout(session, fmt, args); va_end(args); } diff --git a/components/utilities/logtrace/log_trace.h b/components/utilities/logtrace/log_trace.h index 04f89ac84..1e8cc4aba 100644 --- a/components/utilities/logtrace/log_trace.h +++ b/components/utilities/logtrace/log_trace.h @@ -132,6 +132,42 @@ void log_trace(const char *fmt, ...); */ void log_session(const struct log_trace_session *session, const char *fmt, ...); +extern void __logtrace_vfmtout(const struct log_trace_session *session, + const char *fmt, + va_list argptr); + +rt_inline void __logtrace_fmtout(const struct log_trace_session *session, + const char *fmt, ...) +{ + va_list args; + + va_start(args, fmt); + __logtrace_vfmtout(session, fmt, args); + va_end(args); +} + +/** + * log with numeric level + * + * The prototype of this function is: + * + * void log_session_lvl(struct log_trace_session *session, + * int lvl, + * const char *fmt, ...); + * + * If the @session is const and @level is greater than @session->lvl, the whole + * function will be optimized out. This is suitable for performance critical + * places where in most cases, the log is turned off by level. + */ +#define log_session_lvl(session, level, fmt, ...) \ + do { \ + if ((level) > (session)->lvl) \ + { \ + break; \ + } \ + __logtrace_fmtout(session, fmt, ##__VA_ARGS__); \ + } while (0) + /* here comes the global part. All sessions share the some output backend. */ /** get the backend device */