]> git.ipfire.org Git - thirdparty/freeradius-server.git/commitdiff
Add fr_perror_to_str to help with wasm debugging
authorArran Cudbard-Bell <a.cudbardb@freeradius.org>
Tue, 14 Jun 2022 18:30:58 +0000 (13:30 -0500)
committerArran Cudbard-Bell <a.cudbardb@freeradius.org>
Wed, 15 Jun 2022 16:15:44 +0000 (11:15 -0500)
src/lib/util/strerror.c
src/lib/util/strerror.h

index 5ffe513e271fe0589de77c91d4bf8c807cc85fb0..4204e716d776352c2a86678717d27269d04c06a2 100644 (file)
@@ -720,3 +720,49 @@ void fr_perror(char const *fmt, ...)
        }
        talloc_free(prefix);
 }
+
+/** Print the stack of string buffers to a thread local buffer
+ *
+ * Used by utility functions lacking their own logging infrastructure
+ *
+ * @hidecallergraph
+ *
+ * @param[in] line_sep to insert between the log lines.
+ * @param[in] fmt      to prefix all log messages with.
+ * @return
+ *     - A thread local string buffer containing the concatenated messages.
+ */
+char const *fr_perror_to_str(char const *line_sep, char const *fmt, ...)
+{
+       char const      *error;
+       char const      *subject;
+       size_t          offset;
+       char            *prefix;
+       va_list         ap;
+       fr_sbuff_t      *agg;
+
+       FR_SBUFF_TALLOC_THREAD_LOCAL(&agg, 256, SIZE_MAX);
+
+       va_start(ap, fmt);
+       prefix = talloc_vasprintf(NULL, fmt, ap);
+       if (unlikely(!prefix)) return NULL;
+       va_end(ap);
+
+       error = fr_strerror_marker_pop(&subject, &offset);
+       if (error) {
+               if (fr_sbuff_in_sprintf(agg, "%s: %s%s", prefix, error, line_sep) < 0) return NULL;
+       } else {
+               if (fr_sbuff_in_sprintf(agg, "%s%s", prefix, error, line_sep) < 0) return NULL;
+               talloc_free(prefix);
+               return NULL;
+       }
+
+       while ((error = fr_strerror_marker_pop(&subject, &offset))) {
+               if (error && (error[0] != '\0')) {
+                       if (fr_sbuff_in_sprintf(agg, "%s: %s%s", prefix, error, line_sep) < 0) return NULL;
+               }
+       }
+       talloc_free(prefix);
+
+       return fr_sbuff_start(agg);
+}
index cc1032d34b6007580af6e116649389ec55051e87..1a7f6c49b44f46a74dd023a28eba0af80937d349 100644 (file)
@@ -111,6 +111,9 @@ char const  *fr_strerror_marker_pop(char const **subject, size_t *offset) CC_HINT
 
 /** @hidecallergraph */
 void           fr_perror(char const *, ...) CC_HINT(format (printf, 1, 2));
+
+/** @hidecallergraph */
+char const     *fr_perror_to_str(char const *line_sep, char const *fmt, ...) CC_HINT(format (printf, 2, 3));
 /** @} */
 
 #ifdef __cplusplus