[pulseaudio-discuss] [PATCH 1/2] Log feature:Add a new log target to a file descriptor

Maarten Bosmans mkbosmans at gmail.com
Wed Mar 9 08:31:25 PST 2011


2011/3/9 Vincent Becker <vincentx.becker at intel.com>:
> @@ -185,7 +193,23 @@ int pa_daemon_conf_set_log_target(pa_daemon_conf *c, const char *string) {
>     } else if (!strcmp(string, "stderr")) {
>         c->auto_log_target = 0;
>         c->log_target = PA_LOG_STDERR;
> -    } else
> +    } else if (pa_startswith(string, "file:")) {
> +        char file_path[512];
> +
> +        strncpy(file_path, string+5, sizeof(file_path));

This is potentially unsafe, use pa_strlcpy.

> +
> +        /* Open target file with user rights */
> +        if ((log_fd = open(file_path, O_RDWR|O_TRUNC|O_CREAT, S_IRWXU)) >= 0) {
> +            c->auto_log_target = 0;
> +            c->log_target = PA_LOG_FD;
> +            pa_log_set_fd(log_fd);
> +        }
> +        else {
> +            printf("Failed to open target file %s, error : %s\n", file_path, pa_cstrerror(errno));
> +            return -1;
> +        }
> +    }
> +    else
>         return -1;
>
>     return 0;


> @@ -399,6 +407,24 @@ void pa_log_levelv_meta(
>             }
>  #endif
>
> +            case PA_LOG_FD: {
> +                if (log_fd != -1) {
> +                    char metadata[256];
> +
> +                    pa_snprintf(metadata, sizeof(metadata), "\n%c %s%s", level_to_char[level], timestamp, location);
> +
> +                    if ((write(log_fd, metadata, strlen(metadata)) < 0) || (write(log_fd, t, strlen(t)) < 0)) {
> +                        saved_errno = errno;
> +                        pa_close(log_fd);
> +                        log_fd = -1;
> +                    }
> +                }
> +                else
> +                    fprintf(stderr, "%s\n", "Invalid file descriptor. Could not write log message.");

Wouldn't this result in lots and lots of the same messages to stderr?
It seems better to move this message to the case when the write fails,
or just print the log message here, or both.

> +
> +                break;
> +            }
> +
>             case PA_LOG_NULL:
>             default:
>                 break;

Maarten


More information about the pulseaudio-discuss mailing list