From: Steven Rostedt <rostedt@goodmis.org>
To: "Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com>
Cc: linux-trace-devel@vger.kernel.org
Subject: Re: [PATCH v2 6/8] trace-cmd: Move plog() function to libtracecmd.
Date: Wed, 28 Aug 2019 16:15:35 -0400 [thread overview]
Message-ID: <20190828161535.00e11800@gandalf.local.home> (raw)
In-Reply-To: <20190814084712.28188-7-tz.stoyanov@gmail.com>
On Wed, 14 Aug 2019 11:47:06 +0300
"Tzvetomir Stoyanov (VMware)" <tz.stoyanov@gmail.com> wrote:
> plog() function writes logs into a log file. It is used in
> libtracecmd and its implementation should be there.
> The function is moved from trace-cmd into the library, and 2
> additional APIs are implemented:
> int trace_set_log_file(char *logfile); - use it to set
> the log file.
> void plog_error(const char *fmt, ...); - use it to log
> an error message into the file.
>
> The plog() function is used also from pdie() in trace-cmd.
> pdie() depends on trace-cmd context and cannot be moved to
> the library. It is reimplemented as macros, in order to utilize
> the new plog() library function.
>
> Signed-off-by: Tzvetomir Stoyanov (VMware) <tz.stoyanov@gmail.com>
> ---
> include/trace-cmd/trace-cmd.h | 4 ++
> include/trace-cmd/trace-msg.h | 3 --
> lib/trace-cmd/trace-util.c | 71 +++++++++++++++++++++++++++++++++++
> tracecmd/trace-listen.c | 69 ++++------------------------------
> 4 files changed, 83 insertions(+), 64 deletions(-)
>
> diff --git a/include/trace-cmd/trace-cmd.h b/include/trace-cmd/trace-cmd.h
> index b96de04..8db0686 100644
> --- a/include/trace-cmd/trace-cmd.h
> +++ b/include/trace-cmd/trace-cmd.h
> @@ -398,6 +398,10 @@ struct hook_list {
> struct hook_list *tracecmd_create_event_hook(const char *arg);
> void tracecmd_free_hooks(struct hook_list *hooks);
>
> +void plog(const char *fmt, ...);
> +void plog_error(const char *fmt, ...);
If these are going to become visible in the library, then they need to
have a prefix "tracecmd_" attached to them.
> +int trace_set_log_file(char *logfile);
> +
> /* --- Hack! --- */
> int tracecmd_blk_hack(struct tracecmd_input *handle);
>
> diff --git a/include/trace-cmd/trace-msg.h b/include/trace-cmd/trace-msg.h
> index b7fe10b..aab8a69 100644
> --- a/include/trace-cmd/trace-msg.h
> +++ b/include/trace-cmd/trace-msg.h
> @@ -12,7 +12,4 @@
>
> extern unsigned int page_size;
>
> -void plog(const char *fmt, ...);
> -void pdie(const char *fmt, ...);
> -
> #endif /* _TRACE_MSG_H_ */
> diff --git a/lib/trace-cmd/trace-util.c b/lib/trace-cmd/trace-util.c
> index b5ce84f..8c1a0a0 100644
> --- a/lib/trace-cmd/trace-util.c
> +++ b/lib/trace-cmd/trace-util.c
> @@ -31,6 +31,8 @@ int tracecmd_disable_plugins;
> static int tracecmd_quiet;
> static bool tracecmd_debug;
>
> +static FILE *trace_logfp;
As this is a static variable, we only need to call it logfp.
> +
> static struct registered_plugin_options {
> struct registered_plugin_options *next;
> struct tep_plugin_option *options;
> @@ -1716,3 +1718,72 @@ void __weak *malloc_or_die(unsigned int size)
> die("malloc");
> return data;
> }
> +
> +#define LOG_BUF_SIZE 1024
> +static void __plog(const char *prefix, const char *fmt, va_list ap, FILE *fp)
> +{
> + static int newline = 1;
> + char buf[LOG_BUF_SIZE];
> + int r;
> +
> + r = vsnprintf(buf, LOG_BUF_SIZE, fmt, ap);
> +
> + if (r > LOG_BUF_SIZE)
> + r = LOG_BUF_SIZE;
> +
> + if (trace_logfp) {
> + if (newline)
> + fprintf(trace_logfp, "[%d]%s%.*s", getpid(), prefix, r, buf);
> + else
> + fprintf(trace_logfp, "[%d]%s%.*s", getpid(), prefix, r, buf);
> + newline = buf[r - 1] == '\n';
> + fflush(trace_logfp);
> + return;
> + }
> +
> + fprintf(fp, "%.*s", r, buf);
> +}
> +
> +void plog(const char *fmt, ...)
> +{
> + va_list ap;
> +
> + va_start(ap, fmt);
> + __plog("", fmt, ap, stdout);
> + va_end(ap);
> + /* Make sure it gets to the screen, in case we crash afterward */
> + fflush(stdout);
> +}
> +
> +void plog_error(const char *fmt, ...)
> +{
> + va_list ap;
> + char *str = "";
> +
> + va_start(ap, fmt);
> + __plog("Error: ", fmt, ap, stderr);
> + va_end(ap);
> + if (errno)
> + str = strerror(errno);
> + if (trace_logfp)
> + fprintf(trace_logfp, "\n%s\n", str);
> + else
> + fprintf(stderr, "\n%s\n", str);
> +}
> +
> +/**
> + * trace_set_log_file - Set file for logging
> + * @logfile: Name of the log file
> + *
> + * Returns 0 on successful completion or -1 in case of error
> + */
> +int trace_set_log_file(char *logfile)
Should it be called tracecmd_set_logfile()?
-- Steve
> +{
> + if (trace_logfp)
> + fclose(trace_logfp);
> + trace_logfp = fopen(logfile, "w");
> + if (!trace_logfp)
> + return -1;
> + return 0;
> +}
> +
next prev parent reply other threads:[~2019-08-28 20:15 UTC|newest]
Thread overview: 24+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-14 8:47 [PATCH v2 0/8] Separate trace-cmd and libtracecmd code Tzvetomir Stoyanov (VMware)
2019-08-14 8:47 ` [PATCH v2 1/8] trace-cmd: Move trace-cmd-local.h from the application to the library Tzvetomir Stoyanov (VMware)
2019-08-14 8:47 ` [PATCH v2 2/8] trace-cmd: Move trace-output.c into the library code Tzvetomir Stoyanov (VMware)
2019-08-14 8:47 ` [PATCH v2 3/8] trace-cmd: Move trace-msg.c into the library Tzvetomir Stoyanov (VMware)
2019-08-14 8:47 ` [PATCH v2 4/8] trace-cmd: Move trace-cmd global variable "quiet" to libtracecmd Tzvetomir Stoyanov (VMware)
2019-08-28 19:59 ` Steven Rostedt
2019-08-29 11:39 ` Tzvetomir Stoyanov
2019-08-29 16:38 ` Steven Rostedt
2019-08-14 8:47 ` [PATCH v2 5/8] trace-cmd: Move trace-cmd global variable "debug" " Tzvetomir Stoyanov (VMware)
2019-08-28 20:01 ` Steven Rostedt
2019-08-14 8:47 ` [PATCH v2 6/8] trace-cmd: Move plog() function " Tzvetomir Stoyanov (VMware)
2019-08-28 20:15 ` Steven Rostedt [this message]
2019-08-14 8:47 ` [PATCH v2 7/8] trace-cmd: Move trace-cmd APIs from trace-cmd.h to trace-local.h Tzvetomir Stoyanov (VMware)
2019-08-28 20:17 ` Steven Rostedt
2019-08-14 8:47 ` [PATCH v2 8/8] trace-cmd: Move tracecmd_stack_tracer_status() function to libtracecmd Tzvetomir Stoyanov (VMware)
2019-08-28 20:21 ` Steven Rostedt
2019-09-03 12:24 ` Tzvetomir Stoyanov
2019-08-14 8:47 ` [PATCH v5 0/3] Add new trace-cmd record options: "--proc-map" Tzvetomir Stoyanov (VMware)
2019-08-27 23:28 ` Steven Rostedt
2019-08-14 8:47 ` [PATCH v5 1/3] trace-cmd: Extend ptrace logic to work with multiple filtered pids Tzvetomir Stoyanov (VMware)
2019-08-27 23:31 ` Steven Rostedt
2019-08-14 8:47 ` [PATCH v5 2/3] trace-cmd: Save the tracee address map into the trace.dat file Tzvetomir Stoyanov (VMware)
2019-08-14 8:47 ` [PATCH v5 3/3] trace-cmd: Add option to execute traced process as given user Tzvetomir Stoyanov (VMware)
2019-08-28 20:25 ` [PATCH v2 0/8] Separate trace-cmd and libtracecmd code Steven Rostedt
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190828161535.00e11800@gandalf.local.home \
--to=rostedt@goodmis.org \
--cc=linux-trace-devel@vger.kernel.org \
--cc=tz.stoyanov@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).