瀏覽代碼

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.
Grissiom 11 年之前
父節點
當前提交
299cccfb26
共有 2 個文件被更改,包括 41 次插入4 次删除
  1. 5 4
      components/utilities/logtrace/log_trace.c
  2. 36 0
      components/utilities/logtrace/log_trace.h

+ 5 - 4
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);
 }
 

+ 36 - 0
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 */