]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add standard log marker function
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 17 Dec 2020 22:53:07 +0000 (16:53 -0600)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Thu, 17 Dec 2020 22:53:07 +0000 (16:53 -0600)
src/lib/server/log.h
src/lib/util/log.c
src/lib/util/log.h

index 70ae5ca33db38b2b8a102e592b03a7d2bc07274a..f2c6cc60fa23b42efb7be9ad2b0d6db6caa694b1 100644 (file)
@@ -137,6 +137,20 @@ void       log_global_free(void);
 #define _FR_LOG_DST_PERROR(_lvl, _fmt, ...) fr_log_perror(LOG_DST, _lvl, __FILE__, __LINE__, NULL, _fmt, ## __VA_ARGS__)
 #define _FR_LOG_DST_FATAL(_fmt, ...) log_fatal(LOG_DST, __FILE__, __LINE__, _fmt, ## __VA_ARGS__)
 
+/** Write a log message with marker
+ *
+ * @param[in] _lvl             log level.
+ * @param[in] _str             to markup.
+ * @param[in] _str_len         length of subject string. May be SIZE_MAX
+ *                             to print the entire string.
+ * @param[in] _marker_idx      Where to place the marker.  May be negative.
+ * @param[in] _marker          text to print at marker_idx.
+ * @param[in] _line_prefix_fmt Prefix to add to all log lines.
+ * @param[in] ...              Arguments for _line_prefix_fmt.
+ */
+#define _FR_LOG_DST_MARKER(_lvl, _str, _str_len, _marker_idx, _line_prefix_fmt, ...) \
+       fr_log_marker(LOG_DST, _lvl, __FILE__, __LINE__, _str, _str_len, _marker_idx, _line_prefix_fmt, ## __VA_ARGS__)
+
 /*
  *  Adds a default prefix to all messages in a source file
  *
@@ -150,10 +164,12 @@ void      log_global_free(void);
 #  define _FR_LOG_PREFIX(_lvl, _fmt, ...) _FR_LOG_DST(_lvl, LOG_PREFIX _fmt, LOG_PREFIX_ARGS, ## __VA_ARGS__)
 #  define _FR_LOG_PREFIX_PERROR(_lvl, _fmt, ...) _FR_LOG_DST_PERROR(_lvl, LOG_PREFIX _fmt, LOG_PREFIX_ARGS, ## __VA_ARGS__)
 #  define _FR_LOG_PREFIX_FATAL(_fmt, ...) _FR_LOG_DST_FATAL(LOG_PREFIX _fmt, LOG_PREFIX_ARGS, ## __VA_ARGS__)
+#  define _FR_LOG_PREFIX_MARKER(_lvl, _str, _str_len, _marker_idx, _marker) _FR_LOG_DST_MARKER(_lvl, _str, _str_len, _marker_idx, _marker, LOG_PREFIX, LOG_PREFIX_ARGS)
 #else
 #  define _FR_LOG_PREFIX(_lvl, _fmt, ...) _FR_LOG_DST(_lvl, LOG_PREFIX _fmt, ## __VA_ARGS__)
 #  define _FR_LOG_PREFIX_PERROR(_lvl, _fmt, ...) _FR_LOG_DST_PERROR(_lvl, LOG_PREFIX _fmt, ## __VA_ARGS__)
 #  define _FR_LOG_PREFIX_FATAL(_fmt, ...) _FR_LOG_DST_FATAL(LOG_PREFIX _fmt, ## __VA_ARGS__)
+#  define _FR_LOG_PREFIX_MARKER(_lvl, _str, _str_len, _marker_idx, _marker) _FR_LOG_DST_MARKER(_lvl, _str, _str_len, _marker_idx, _marker, LOG_PREFIX)
 #endif
 
 /** @name Log global messages
@@ -184,7 +200,9 @@ void        log_global_free(void);
 #define PWARN(_fmt, ...)       _FR_LOG_PREFIX_PERROR(L_WARN, _fmt, ## __VA_ARGS__)
 #define PERROR(_fmt, ...)      _FR_LOG_PREFIX_PERROR(L_ERR, _fmt, ## __VA_ARGS__)
 
-
+#define IMARKER(_str, _marker_idx, _marker)    _FR_LOG_PREFIX_MARKER(L_INFO, _str, SIZE_MAX, _marker_idx, _marker)
+#define WMARKER(_str, _marker_idx, _marker)    _FR_LOG_PREFIX_MARKER(L_WARN, _str, SIZE_MAX, _marker_idx, _marker)
+#define EMARKER(_str, _marker_idx, _marker)    _FR_LOG_PREFIX_MARKER(L_ERR, _str, SIZE_MAX, _marker_idx, _marker)
 /** @} */
 
 /** @name Log global debug messages (DEBUG*)
@@ -227,7 +245,7 @@ void        log_global_free(void);
 #define PDEBUG2(_fmt, ...)             _PDEBUG_LOG(L_DBG, L_DBG_LVL_2, _fmt, ## __VA_ARGS__)
 #define PDEBUG3(_fmt, ...)             _PDEBUG_LOG(L_DBG, L_DBG_LVL_3, _fmt, ## __VA_ARGS__)
 #define PDEBUG4(_fmt, ...)             _PDEBUG_LOG(L_DBG, L_DBG_LVL_MAX, _fmt, ## __VA_ARGS__)
-#define PDEBUGX(_lvl, _fmt, ...)               _PDEBUG_LOG(L_DBG, _lvl, _fmt, ## __VA_ARGS__)
+#define PDEBUGX(_lvl, _fmt, ...)       _PDEBUG_LOG(L_DBG, _lvl, _fmt, ## __VA_ARGS__)
 /** @} */
 
 /** @name Log request-specific messages (R*)
index 4f70b28cedb30e87054a13a047d1d53de906a827..630f26d00cc7374d00b27d6335d22545ebce4861 100644 (file)
@@ -666,20 +666,84 @@ int fr_log_perror(fr_log_t const *log, fr_log_type_t type, char const *file, int
 }
 
 DIAG_OFF(format-nonliteral)
+/** Print out an error marker
+ *
+ * @param[in] log              destination.
+ * @param[in] type             of log message.
+ * @param[in] file             src file the log message was generated in.
+ * @param[in] line             number the log message was generated on.
+ * @param[in] str              Subject string we're printing a marker for.
+ * @param[in] str_len          Subject string length.  Use SIZE_MAX for the
+ *                             length of the string.
+ * @param[in] marker_idx       Where to place the marker.  May be negative.
+ * @param[in] marker           text to print at marker_idx.
+ * @param[in] line_prefix_fmt  Prefix to add to the marker messages.
+ * @param[in] ...              Arguments for line_prefix_fmt.
+ */
+void fr_log_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
+                  char const *str, size_t str_len,
+                  ssize_t marker_idx, char const *marker, char const *line_prefix_fmt, ...)
+{
+       char const              *ellipses = "";
+       va_list                 ap;
+       TALLOC_CTX              *thread_log_pool = fr_log_pool_init();
+       char                    *line_prefix = NULL;
+       static char const       spaces[] = "                                                                                                                        ";
+
+       if (str_len == SIZE_MAX) str_len = strlen(str);
+
+       if (marker_idx < 0) marker_idx = marker_idx * -1;
+
+       if ((size_t)marker_idx >= sizeof(spaces)) {
+               size_t offset = (marker_idx - (sizeof(spaces) - 1)) + (sizeof(spaces) * 0.75);
+               marker_idx -= offset;
+
+               if (offset > str_len) offset = str_len;
+               str += offset;
+               str_len -= offset;
+
+               ellipses  = "... ";
+       }
+
+       if (line_prefix_fmt) {
+               va_start(ap, line_prefix_fmt);
+               line_prefix = fr_vasprintf(thread_log_pool, line_prefix_fmt, ap);
+               va_end(ap);
+       }
+
+       fr_log(log, type, file, line, "%s%s%.*s",
+              line_prefix ? line_prefix : "", ellipses, (int)str_len, str);
+       fr_log(log, type, file, line, "%s%s%.*s^ %s",
+              line_prefix ? line_prefix : "", ellipses, (int)marker_idx, spaces, marker);
+
+       if (line_prefix_fmt) talloc_free(line_prefix);
+}
+
+/** Print out hex block
+ *
+ * @param[in] log              destination.
+ * @param[in] type             of log message.
+ * @param[in] file             src file the log message was generated in.
+ * @param[in] line             number the log message was generated on.
+ * @param[in] data             to print.
+ * @param[in] data_len         length of data.
+ * @param[in] line_prefix_fmt  Prefix to add to the marker messages.
+ * @param[in] ...              Arguments for line_prefix_fmt.
+ */
 void fr_log_hex(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
-               uint8_t const *data, size_t data_len, char const *fmt, ...)
+               uint8_t const *data, size_t data_len, char const *line_prefix_fmt, ...)
 {
        size_t          i, j, len;
        char            *p;
        char            buffer[(0x10 * 3) + 1];
        TALLOC_CTX      *thread_log_pool = fr_log_pool_init();
-       char            *prefix = NULL;
+       char            *line_prefix = NULL;
 
-       if (fmt) {
+       if (line_prefix_fmt) {
                va_list ap;
 
-               va_start(ap, fmt);
-               prefix = talloc_asprintf(thread_log_pool, fmt, ap);
+               va_start(ap, line_prefix_fmt);
+               line_prefix = fr_vasprintf(thread_log_pool, line_prefix_fmt, ap);
                va_end(ap);
        }
 
@@ -689,38 +753,50 @@ void fr_log_hex(fr_log_t const *log, fr_log_type_t type, char const *file, int l
 
                for (p = buffer, j = 0; j < len; j++, p += 3) sprintf(p, "%02x ", data[i + j]);
 
-               if (fmt) {
-                       fr_log(log, type, file, line, "%pV%04x: %s",
-                              fr_box_strvalue_buffer(prefix), (int)i, buffer);
+               if (line_prefix_fmt) {
+                       fr_log(log, type, file, line, "%s%04x: %s",
+                              line_prefix, (int)i, buffer);
                } else {
                        fr_log(log, type, file, line, "%04x: %s", (int)i, buffer);
                }
        }
 
-       if (fmt) talloc_free(prefix);
+       if (line_prefix_fmt) talloc_free(line_prefix);
 }
 
+/** Print out hex block
+ *
+ * @param[in] log              destination.
+ * @param[in] type             of log message.
+ * @param[in] file             src file the log message was generated in.
+ * @param[in] line             number the log message was generated on.
+ * @param[in] data             to print.
+ * @param[in] data_len         length of data.
+ * @param[in] marker_idx       Where to place the marker.  May be negative.
+ * @param[in] marker           text to print at marker_idx.
+ * @param[in] line_prefix_fmt  Prefix to add to the marker messages.
+ * @param[in] ...              Arguments for line_prefix_fmt.
+ */
 void fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
-                      uint8_t const *data, size_t data_len, ssize_t slen,
-                      char const *error, char const *fmt, ...)
+                      uint8_t const *data, size_t data_len,
+                      ssize_t marker_idx, char const *marker, char const *line_prefix_fmt, ...)
 {
        size_t          i, j, len;
        char            *p;
        char            buffer[(0x10 * 3) + 1];
        TALLOC_CTX      *thread_log_pool = fr_log_pool_init();
 
-       char            *prefix = NULL;
+       char            *line_prefix = NULL;
        static char     spaces[3 * 0x10];       /* Bytes per line */
 
        if (!*spaces) memset(spaces, ' ', sizeof(spaces) - 1);  /* Leave a \0 */
 
-       if (slen < 0) slen = +(slen);
-
-       if (fmt) {
+       if (marker_idx < 0) marker_idx = marker_idx * -1;
+       if (line_prefix_fmt) {
                va_list ap;
 
-               va_start(ap, fmt);
-               prefix = talloc_asprintf(thread_log_pool, fmt, ap);
+               va_start(ap, line_prefix_fmt);
+               line_prefix = fr_vasprintf(thread_log_pool, line_prefix_fmt, ap);
                va_end(ap);
        }
 
@@ -730,9 +806,9 @@ void fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type, char const *file
 
                for (p = buffer, j = 0; j < len; j++, p += 3) sprintf(p, "%02x ", data[i + j]);
 
-               if (fmt) {
-                       fr_log(log, type, file, line, "%pV%04x: %s",
-                              fr_box_strvalue_buffer(prefix), (int)i, buffer);
+               if (line_prefix_fmt) {
+                       fr_log(log, type, file, line, "%s%04x: %s",
+                              line_prefix, (int)i, buffer);
                } else {
                        fr_log(log, type, file, line, "%04x: %s", (int)i, buffer);
                }
@@ -740,17 +816,18 @@ void fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type, char const *file
                /*
                 *      Marker is on this line
                 */
-               if (((size_t)slen >= i) && ((size_t)slen < (i + 0x10))) {
-                       if (fmt) {
-                               fr_log(log, type, file, line, "%pV      %.*s^ %s", fr_box_strvalue_buffer(prefix),
-                                      (int)((slen - i) * 3), spaces, error);
+               if (((size_t)marker_idx >= i) && ((size_t)marker_idx < (i + 0x10))) {
+                       if (line_prefix_fmt) {
+                               fr_log(log, type, file, line, "%s      %.*s^ %s", line_prefix,
+                                      (int)((marker_idx - i) * 3), spaces, marker);
                        } else {
-                               fr_log(log, type, file, line, "      %.*s^ %s", (int)((slen - i) * 3), spaces, error);
+                               fr_log(log, type, file, line, "      %.*s^ %s",
+                                      (int)((marker_idx - i) * 3), spaces, marker);
                        }
                }
        }
 
-       if (fmt) talloc_free(prefix);
+       if (line_prefix_fmt) talloc_free(line_prefix);
 }
 DIAG_ON(format-nonliteral)
 
index df4564bbe73e51ee0aa919c7f1b52d0cbb583850..c03c1239c8a13a4fd1903e73922fc9fcd4feff6a 100644 (file)
@@ -149,14 +149,18 @@ int       fr_log_perror(fr_log_t const *log, fr_log_type_t type,
                      char const *file, int line, fr_log_perror_format_t const *rules, char const *fmt, ...)
        CC_HINT(format (printf, 6, 7)) CC_HINT(nonnull (1));
 
-void   fr_log_hex(fr_log_t const *log, fr_log_type_t type,
-                  char const *file, int line,
-                  uint8_t const *data, size_t data_len, char const *fmt, ...)
+void   fr_log_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
+                     char const *str, size_t str_len,
+                     ssize_t marker_idx, char const *marker, char const *line_prefix_fmt, ...)
+                     CC_HINT(format (printf, 9, 10)) CC_HINT(nonnull (1,3,5,8));
+
+void   fr_log_hex(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
+                  uint8_t const *data, size_t data_len, char const *line_prefix_fmt, ...)
                   CC_HINT(format (printf, 7, 8)) CC_HINT(nonnull (1,3,5));
 
-void   fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type,
-                         char const *file, int line,
-                         uint8_t const *data, size_t data_len, ssize_t slen, char const *error, char const *fmt, ...)
+void   fr_log_hex_marker(fr_log_t const *log, fr_log_type_t type, char const *file, int line,
+                         uint8_t const *data, size_t data_len,
+                         ssize_t marker_idx, char const *marker, char const *line_prefix_fmt, ...)
                          CC_HINT(format (printf, 9, 10)) CC_HINT(nonnull (1, 3, 5, 8));
 #ifdef __cplusplus
 }