You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by we...@apache.org on 2019/04/26 19:36:48 UTC

[mynewt-core] branch master updated: sys/log: Add setting for maximum log entry size when writing. (#1782)

This is an automated email from the ASF dual-hosted git repository.

wes3 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git


The following commit(s) were added to refs/heads/master by this push:
     new 59e54ad  sys/log: Add setting for maximum log entry size when writing. (#1782)
59e54ad is described below

commit 59e54ad46417ae57c5fd436d5fd1882b2b5e7978
Author: wes3 <wi...@runtime.io>
AuthorDate: Fri Apr 26 12:36:42 2019 -0700

    sys/log: Add setting for maximum log entry size when writing. (#1782)
    
    * sys/log: Add setting for maximum log entry size when writing.
    
    This commit adds the ability to set a maximum log entry for a given log. By default, the log maximum entry length is set to 0. A value of 0 for this field means that no check should be made for maximum log entry length. If set to a non-zero value, the log write routine will check the log body length (does nt include the size of the log entry header) and if it exceeds the maximum, OS_ENOMEM will be returned and the too_long log stat will be incremented. A new API was added named log_se [...]
    
    * sys/log: Add setting for maximum log entry size when writing.
    
    Address review comment (inconsistent error checking of log_chk_max_entry_len return code).
---
 sys/log/full/include/log/log.h | 13 ++++++++++
 sys/log/full/src/log.c         | 59 ++++++++++++++++++++++++++++++++++++++++++
 sys/log/stub/include/log/log.h | 15 +++++++++++
 3 files changed, 87 insertions(+)

diff --git a/sys/log/full/include/log/log.h b/sys/log/full/include/log/log.h
index f1162b4..ca34162 100644
--- a/sys/log/full/include/log/log.h
+++ b/sys/log/full/include/log/log.h
@@ -180,6 +180,7 @@ STATS_SECT_START(logs)
     STATS_SECT_ENTRY(drops)
     STATS_SECT_ENTRY(errs)
     STATS_SECT_ENTRY(lost)
+    STATS_SECT_ENTRY(too_long)
 STATS_SECT_END
 
 #define LOG_STATS_INC(log, name)        STATS_INC(log->l_stats, name)
@@ -196,6 +197,7 @@ struct log {
     STAILQ_ENTRY(log) l_next;
     log_append_cb *l_append_cb;
     uint8_t l_level;
+    uint16_t l_max_entry_len;   /* Log body length; if 0 disables check. */
 #if MYNEWT_VAL(LOG_STATS)
     STATS_SECT_DECL(logs) l_stats;
 #endif
@@ -626,6 +628,17 @@ void log_set_level(struct log *log, uint8_t level);
  */
 uint8_t log_get_level(const struct log *log);
 
+/**
+ * @brief Set maximum length of an entry in the log. If set to
+ *        0, no check will be made for maximum write length.
+ *        Note that this is maximum log body length; the log
+ *        entry header is not included in the check.
+ *
+ * @param log                   Log to set max entry length
+ * @param level                 New max entry length
+ */
+void log_set_max_entry_len(struct log *log, uint16_t max_entry_len);
+
 #if MYNEWT_VAL(LOG_STORAGE_INFO)
 /**
  * Return information about log storage
diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c
index 8dd58d6..d3773c6 100644
--- a/sys/log/full/src/log.c
+++ b/sys/log/full/src/log.c
@@ -69,6 +69,7 @@ STATS_NAME_START(logs)
   STATS_NAME(logs, drops)
   STATS_NAME(logs, errs)
   STATS_NAME(logs, lost)
+  STATS_NAME(logs, too_long)
 STATS_NAME_END(logs)
 #endif
 
@@ -345,6 +346,7 @@ log_register(char *name, struct log *log, const struct log_handler *lh,
     log->l_arg = arg;
     log->l_level = level;
     log->l_append_cb = NULL;
+    log->l_max_entry_len = 0;
 
     if (!log_registered(log)) {
         STAILQ_INSERT_TAIL(&g_log_list, log, l_next);
@@ -408,6 +410,22 @@ log_chk_type(uint8_t etype)
 }
 
 static int
+log_chk_max_entry_len(struct log *log, uint16_t len)
+{
+    int rc;
+
+    rc = OS_OK;
+    if (log->l_max_entry_len != 0) {
+        if (len > log->l_max_entry_len) {
+            LOG_STATS_INC(log, too_long);
+            rc = OS_ENOMEM;
+        }
+    }
+
+    return rc;
+}
+
+static int
 log_append_prepare(struct log *log, uint8_t module, uint8_t level,
                    uint8_t etype, struct log_entry_hdr *ue)
 {
@@ -501,6 +519,11 @@ log_append_typed(struct log *log, uint8_t module, uint8_t level, uint8_t etype,
 
     LOG_STATS_INC(log, writes);
 
+    rc = log_chk_max_entry_len(log, len);
+    if (rc != OS_OK) {
+        goto err;
+    }
+
     hdr = (struct log_entry_hdr *)data;
     rc = log_append_prepare(log, module, level, etype, hdr);
     if (rc != 0) {
@@ -529,6 +552,12 @@ log_append_body(struct log *log, uint8_t module, uint8_t level, uint8_t etype,
     int rc;
 
     LOG_STATS_INC(log, writes);
+
+    rc = log_chk_max_entry_len(log, body_len);
+    if (rc != OS_OK) {
+        return rc;
+    }
+
     rc = log_append_prepare(log, module, level, etype, &hdr);
     if (rc != 0) {
         LOG_STATS_INC(log, drops);
@@ -552,6 +581,7 @@ log_append_mbuf_typed_no_free(struct log *log, uint8_t module, uint8_t level,
 {
     struct log_entry_hdr *hdr;
     struct os_mbuf *om;
+    uint16_t len;
     int rc;
 
     /* Remove a loyer of indirection for convenience. */
@@ -569,6 +599,21 @@ log_append_mbuf_typed_no_free(struct log *log, uint8_t module, uint8_t level,
         goto err;
     }
 
+    /*
+     * Check that the log body length is less than the maximum entry. This code
+     * may appear a bit odd in that it checks that the length is greater than
+     * a log entry header length. The reason for this check is to ensure any
+     * error handling of this case to be the same as it was before the
+     * maximum entry length was checked.
+     */
+    len = os_mbuf_len(om);
+    if (len > LOG_ENTRY_HDR_SIZE) {
+        rc = log_chk_max_entry_len(log, len - LOG_ENTRY_HDR_SIZE);
+        if (rc != OS_OK) {
+            goto drop;
+        }
+    }
+
     hdr = (struct log_entry_hdr *)om->om_data;
 
     rc = log_append_prepare(log, module, level, etype, hdr);
@@ -619,6 +664,7 @@ log_append_mbuf_body_no_free(struct log *log, uint8_t module, uint8_t level,
                              uint8_t etype, struct os_mbuf *om)
 {
     struct log_entry_hdr hdr;
+    uint16_t len;
     int rc;
 
     LOG_STATS_INC(log, writes);
@@ -627,6 +673,12 @@ log_append_mbuf_body_no_free(struct log *log, uint8_t module, uint8_t level,
         goto err;
     }
 
+    len = os_mbuf_len(om);
+    rc = log_chk_max_entry_len(log, len);
+    if (rc != OS_OK) {
+        goto drop;
+    }
+
     rc = log_append_prepare(log, module, level, etype, &hdr);
     if (rc != 0) {
         LOG_STATS_INC(log, drops);
@@ -901,3 +953,10 @@ log_get_level(const struct log *log)
     assert(log);
     return log->l_level;
 }
+
+void
+log_set_max_entry_len(struct log *log, uint16_t max_entry_len)
+{
+    assert(log);
+    log->l_max_entry_len = max_entry_len;
+}
diff --git a/sys/log/stub/include/log/log.h b/sys/log/stub/include/log/log.h
index 11964cd..f767894 100644
--- a/sys/log/stub/include/log/log.h
+++ b/sys/log/stub/include/log/log.h
@@ -109,6 +109,21 @@ static inline uint8_t log_get_level(const struct log *log)
     return 0;
 }
 
+/**
+ * @brief Set maximum length of an entry in the log. If set to
+ *        0, no check will be made for maximum write length.
+ *        Note that this is maximum log body length; the log
+ *        entry header is not included in the check.
+ *
+ * @param log                   Log to set max entry length
+ * @param level                 New max entry length
+ */
+static inline void
+log_set_max_entry_len(struct log *log, uint16_t max_entry_len)
+{
+    return;
+}
+
 #define log_printf(...)
 
 /*