]> git.alsa-project.org Git - alsa-lib.git/commitdiff
error: add priority and interface strings to the log messages
authorJaroslav Kysela <perex@perex.cz>
Thu, 6 Nov 2025 13:45:33 +0000 (14:45 +0100)
committerJaroslav Kysela <perex@perex.cz>
Fri, 7 Nov 2025 17:09:26 +0000 (18:09 +0100)
Signed-off-by: Jaroslav Kysela <perex@perex.cz>
include/error.h
src/error.c

index 8b9b85913a7d8759695640fb8fa639bb4fe73885..7e4ab6a7aa59240d9775369c0de70fe3de01fbf8 100644 (file)
@@ -54,6 +54,7 @@ const char *snd_strerror(int errnum);
 #define SND_LOG_INFO           3       /**< info priority level */
 #define SND_LOG_DEBUG          4       /**< debug priority level */
 #define SND_LOG_TRACE          5       /**< trace priority level */
+#define SND_LOG_LAST           SND_LOG_TRACE
 
 #define SND_ILOG_CORE           1      /**< core library code */
 #define SND_ILOG_CONFIG                2       /**< configuration parsing and operations */
@@ -67,6 +68,7 @@ const char *snd_strerror(int errnum);
 #define SND_ILOG_UCM           10      /**< UCM API */
 #define SND_ILOG_TOPOLOGY      11      /**< topology API */
 #define SND_ILOG_ASERVER       12      /**< aserver */
+#define SND_ILOG_LAST          SND_ILOG_ASERVER
 
 /**
  * \brief Log handler callback.
index e077cb9503f1cf7eee9b453c543703952b7bf8b8..56ff27c78688449eb30c1dddd0f97415c0626971 100644 (file)
@@ -83,6 +83,61 @@ snd_lib_log_handler_t snd_lib_log_set_local(snd_lib_log_handler_t func)
        return old;
 }
 
+/**
+ * Array of log priority level names.
+ */
+static const char *snd_log_prio_names[SND_LOG_LAST + 1] = {
+       [0] = NULL,
+       [SND_LOG_ERROR] = "error",
+       [SND_LOG_WARN] = "warning",
+       [SND_LOG_INFO] = "info",
+       [SND_LOG_DEBUG] = "debug",
+       [SND_LOG_TRACE] = "trace",
+};
+
+/**
+ * Array of interface names.
+ */
+static const char *snd_ilog_interface_names[SND_ILOG_LAST + 1] = {
+       [0] = NULL,
+       [SND_ILOG_CORE] = "core",
+       [SND_ILOG_CONFIG] = "config",
+       [SND_ILOG_CONTROL] = "control",
+       [SND_ILOG_HWDEP] = "hwdep",
+       [SND_ILOG_TIMER] = "timer",
+       [SND_ILOG_RAWMIDI] = "rawmidi",
+       [SND_ILOG_PCM] = "pcm",
+       [SND_ILOG_MIXER] = "mixer",
+       [SND_ILOG_SEQUENCER] = "sequencer",
+       [SND_ILOG_UCM] = "ucm",
+       [SND_ILOG_TOPOLOGY] = "topology",
+       [SND_ILOG_ASERVER] = "aserver",
+};
+
+/**
+ * \brief Function to convert log priority level to text.
+ * \param prio Priority value (SND_LOG_*).
+ * \return The textual representation of the priority level, or NULL if invalid.
+ */
+const char *snd_lib_log_priority(int prio)
+{
+       if (prio >= 0 && prio <= SND_LOG_TRACE)
+               return snd_log_prio_names[prio];
+       return NULL;
+}
+
+/**
+ * \brief Function to convert interface code to text.
+ * \param interface Interface (SND_ILOG_*).
+ * \return The textual representation of the interface code, or NULL if invalid.
+ */
+const char *snd_lib_log_interface(int interface)
+{
+       if (interface >= 0 && interface <= SND_ILOG_TOPOLOGY)
+               return snd_ilog_interface_names[interface];
+       return NULL;
+}
+
 /**
  * \brief The default log handler function.
  * \param prio Priority value (SND_LOG_*).
@@ -100,6 +155,8 @@ snd_lib_log_handler_t snd_lib_log_set_local(snd_lib_log_handler_t func)
  */
 static void snd_lib_vlog_default(int prio, int interface, const char *file, int line, const char *function, int errcode, const char *fmt, va_list arg)
 {
+       const char *text;
+
        if (local_log) {
                local_log(prio, interface, file, line, function, errcode, fmt, arg);
                return;
@@ -109,6 +166,15 @@ static void snd_lib_vlog_default(int prio, int interface, const char *file, int
                return;
        }
        fprintf(stderr, "ALSA lib %s:%i:(%s) ", file, line, function);
+
+       text = snd_lib_log_priority(prio);
+       if (text)
+               fprintf(stderr, "[%s] ", text);
+
+       text = snd_lib_log_interface(interface);
+       if (text)
+               fprintf(stderr, "[%s] ", text);
+
        vfprintf(stderr, fmt, arg);
        if (errcode)
                fprintf(stderr, ": %s", snd_strerror(errcode));