This is an automated email from Gerrit.

Tomas Vanek ([email protected]) just uploaded a new patch set to Gerrit, which you 
can find at http://openocd.zylin.com/6193

-- gerrit

commit 25db1c7e595bbb3e967181e0e41cb1fd811096fc
Author: Tomas Vanek <[email protected]>
Date:   Mon Apr 26 11:21:48 2021 +0200

    helper/command, log: provide fallback for command_print()
    
    Without this change command_print() does not output anything if there
    is no valid pointer to struct command_invocation.
    
    Enable fallback for the case we have no valid struct command_invocation
    or its members (ctx, output). Redirect the text output to the log,
    using LOG_LVL_INFO.
    
    There is a drawback: command_print() is logged to the debug log with
    line and file information of helper/command.c, not the caller.
    To get pointer to the caller we would have to change command_print()
    to a macro, too painful change for a minimal gain.
    
    Change-Id: Ib40fb22f5c36a3cea53a8d71c63b0e882475332a
    Signed-off-by: Tomas Vanek <[email protected]>

diff --git a/src/helper/command.c b/src/helper/command.c
index 80e297b..86adbcf 100644
--- a/src/helper/command.c
+++ b/src/helper/command.c
@@ -482,48 +482,39 @@ void command_output_text(struct command_context *context, 
const char *data)
                context->output_handler(context, data);
 }
 
-void command_print_sameline(struct command_invocation *cmd, const char 
*format, ...)
+static void command_puts(struct command_invocation *cmd, const char *string)
 {
-       char *string;
+       if (string == NULL)
+               return;
 
+       if (cmd && cmd->ctx && cmd->output)
+               Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
+       else
+               log_puts(LOG_LVL_INFO, __FILE__, __LINE__, __func__, string);
+}
+
+void command_print_sameline(struct command_invocation *cmd, const char 
*format, ...)
+{
        va_list ap;
        va_start(ap, format);
 
-       string = alloc_vprintf(format, ap);
-       if (string != NULL && cmd) {
-               /* we want this collected in the log + we also want to pick it 
up as a tcl return
-                * value.
-                *
-                * The latter bit isn't precisely neat, but will do for now.
-                */
-               Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
-               /* We already printed it above
-                * command_output_text(context, string); */
-               free(string);
-       }
+       char *string = alloc_vprintf(format, ap);
+       command_puts(cmd, string);
+       free(string);
 
        va_end(ap);
 }
 
 void command_print(struct command_invocation *cmd, const char *format, ...)
 {
-       char *string;
-
        va_list ap;
        va_start(ap, format);
 
-       string = alloc_vprintf(format, ap);
-       if (string != NULL && cmd) {
-               strcat(string, "\n");   /* alloc_vprintf guaranteed the buffer 
to be at least one
-                                        *char longer */
-               /* we want this collected in the log + we also want to pick it 
up as a tcl return
-                * value.
-                *
-                * The latter bit isn't precisely neat, but will do for now.
-                */
-               Jim_AppendString(cmd->ctx->interp, cmd->output, string, -1);
-               /* We already printed it above
-                * command_output_text(context, string); */
+       char *string = alloc_vprintf(format, ap);
+       if (string != NULL) {
+               strcat(string, "\n");   /* alloc_vprintf guaranteed the buffer 
to be
+                                                                * at least one 
char longer */
+               command_puts(cmd, string);
                free(string);
        }
 
diff --git a/src/helper/command.h b/src/helper/command.h
index 68f4c14..3d29963 100644
--- a/src/helper/command.h
+++ b/src/helper/command.h
@@ -361,8 +361,21 @@ struct command_context *copy_command_context(struct 
command_context *cmd_ctx);
  */
 void command_done(struct command_context *context);
 
+/**
+ * Appends a formated string and a newline char to cmd->output object
+ * which is later passed as the return value of the command.
+ * If cmd (or cmd->ctx or cmd->output) is NULL, the string
+ * is logged at LOG_LVL_INFO
+ * @param cmd pointer to command_invocation structure or NULL
+ * @param format printf style format
+ */
 void command_print(struct command_invocation *cmd, const char *format, ...)
 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
+/**
+ * Same as command_print except no newline is appended.
+ * @param cmd pointer to command_invocation structure or NULL
+ * @param format printf style format
+ */
 void command_print_sameline(struct command_invocation *cmd, const char 
*format, ...)
 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 2, 3)));
 int command_run_line(struct command_context *context, char *line);
diff --git a/src/helper/log.c b/src/helper/log.c
index 7440b70..988f652 100644
--- a/src/helper/log.c
+++ b/src/helper/log.c
@@ -84,9 +84,9 @@ static void log_forward(const char *file, unsigned line, 
const char *function, c
  * target_request.c).
  *
  */
-static void log_puts(enum log_levels level,
+void log_puts(enum log_levels level,
        const char *file,
-       int line,
+       unsigned int line,
        const char *function,
        const char *string)
 {
diff --git a/src/helper/log.h b/src/helper/log.h
index f2ba0da..4a7f4da 100644
--- a/src/helper/log.h
+++ b/src/helper/log.h
@@ -59,6 +59,8 @@ enum log_levels {
        LOG_LVL_DEBUG_IO = 4,
 };
 
+void log_puts(enum log_levels level, const char *file, unsigned int line,
+           const char *function, const char *string);
 void log_printf(enum log_levels level, const char *file, unsigned line,
                const char *function, const char *format, ...)
 __attribute__ ((format (PRINTF_ATTRIBUTE_FORMAT, 5, 6)));

-- 

Reply via email to