@@ -388,6 +388,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, ...);
+int trace_set_log_file(char *logfile);
+
/* --- Hack! --- */
int tracecmd_blk_hack(struct tracecmd_input *handle);
@@ -12,7 +12,4 @@
extern unsigned int page_size;
-void plog(const char *fmt, ...);
-void pdie(const char *fmt, ...);
-
#endif /* _TRACE_MSG_H_ */
@@ -31,6 +31,8 @@ int tracecmd_disable_plugins;
static int tracecmd_quiet;
static bool tracecmd_debug;
+static FILE *trace_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)
+{
+ if (trace_logfp)
+ fclose(trace_logfp);
+ trace_logfp = fopen(logfile, "w");
+ if (!trace_logfp)
+ return -1;
+ return 0;
+}
+
@@ -34,8 +34,6 @@ static char *output_dir;
static char *default_output_file = "trace";
static char *output_file;
-static FILE *logfp;
-
static int backlog = 5;
static int do_daemon;
@@ -44,6 +42,13 @@ static int do_daemon;
static struct tracecmd_msg_handle *stop_msg_handle;
static bool done;
+#define pdie(fmt, ...) \
+ do { \
+ plog_error(fmt, ##__VA_ARGS__); \
+ remove_pid_file(); \
+ exit(-1); \
+ } while (0)
+
#define TEMP_FILE_STR "%s.%s:%s.cpu%d", output_file, host, port, cpu
static char *get_temp_file(const char *host, const char *port, int cpu)
{
@@ -114,43 +119,6 @@ static void finish(int sig)
done = true;
}
-#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 (logfp) {
- if (newline)
- fprintf(logfp, "[%d]%s%.*s", getpid(), prefix, r, buf);
- else
- fprintf(logfp, "[%d]%s%.*s", getpid(), prefix, r, buf);
- newline = buf[r - 1] == '\n';
- fflush(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);
-}
-
static void make_pid_name(int mode, char *buf)
{
snprintf(buf, PATH_MAX, VAR_RUN_DIR "/trace-cmd-net.pid");
@@ -169,26 +137,6 @@ static void remove_pid_file(void)
unlink(buf);
}
-void pdie(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 (logfp)
- fprintf(logfp, "\n%s\n", str);
- else
- fprintf(stderr, "\n%s\n", str);
-
- remove_pid_file();
-
- exit(-1);
-}
-
static int process_udp_child(int sfd, const char *host, const char *port,
int cpu, int page_size, int use_tcp)
{
@@ -1030,8 +978,7 @@ void trace_listen(int argc, char **argv)
if (logfile) {
/* set the writes to a logfile instead */
- logfp = fopen(logfile, "w");
- if (!logfp)
+ if (trace_set_log_file(logfile) < 0)
die("creating log file %s", logfile);
}
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(-)