From 4807caad927d71fc10dd09259356260e365c4834 Mon Sep 17 00:00:00 2001
From: Jakub Filak <jfilak@redhat.com>
Date: Tue, 29 Oct 2013 16:28:09 +0100
Subject: [PATCH 09/39] Add support for changing log directory
The current implementation writes to a file in CWD.
This patch allows users to pass a path to a directory in output command
line argument. If a value of the output argument points to a directory,
the log file path is set to $output/abrt_checker_$PID.log
Related to rhbz#1023081
Related to rhbz#1055581
Signed-off-by: Jakub Filak <jfilak@redhat.com>
---
README | 8 +++++--
src/abrt-checker.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
test/CMakeLists.txt | 7 ++++++
3 files changed, 80 insertions(+), 3 deletions(-)
diff --git a/README b/README
index 773a2f3..28cefda 100644
--- a/README
+++ b/README
@@ -34,11 +34,15 @@ Example3:
- this example shows how to configure the log output destination
- output option is designed for this purpose
- by default abrt-java-connector prints the log to abrt_checker_$PID.log file in the current directory
-- the first command prints agent's output to /tmp/abrt-agent.log file
+- the first command prints logs to /tmp/abrt_checker_$PID.log
+
+$ java -agentlib:abrt-java-connector=output=/tmp $MyClass -platform.jvmtiSupported true
+
+- the second command prints agent's output to /tmp/abrt-agent.log file
$ java -agentlib:abrt-java-connector=output=/tmp/abrt-agent.log $MyClass -platform.jvmtiSupported true
-- the second command completely disables logging to file
+- the thirs command completely disables logging to file
$ java -agentlib:abrt-java-connector=output= $MyClass -platform.jvmtiSupported true
diff --git a/src/abrt-checker.c b/src/abrt-checker.c
index 23e76b0..c403d00 100644
--- a/src/abrt-checker.c
+++ b/src/abrt-checker.c
@@ -37,6 +37,8 @@
#include <sys/types.h>
#include <unistd.h>
#include <linux/limits.h>
+#include <sys/stat.h>
+#include <errno.h>
/* Shared macros and so on */
#include "abrt-checker.h"
@@ -238,6 +240,43 @@ static const char *get_default_log_file_name()
/*
+ * Appends file_name to *path and returns a pointer to result. Returns NULL on
+ * error and leaves *path untouched.
+ */
+static char *append_file_to_path(char **path, const char *file_name)
+{
+ if (NULL == file_name)
+ {
+ return NULL;
+ }
+
+ const size_t outlen = strlen(*path);
+ const int need_trailing = (*path)[outlen -1] != '/';
+ char *result = malloc(outlen + strlen(file_name) + need_trailing + 1);
+ if (NULL == result)
+ {
+ fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": malloc(): out of memory\n");
+ return NULL;
+ }
+
+ char *tmp = strcpy(result, *path);
+ tmp += outlen;
+ if (need_trailing)
+ {
+ *tmp = '/';
+ ++tmp;
+ }
+
+ strcpy(tmp, file_name);
+
+ free(*path);
+ *path = result;
+ return result;
+}
+
+
+
+/*
* Gets the log file
*/
static FILE *get_log_file()
@@ -248,7 +287,34 @@ static FILE *get_log_file()
&& DISABLED_LOG_OUTPUT != outputFileName)
{
/* try to open output log file */
- const char *fn = (outputFileName != NULL ? outputFileName : get_default_log_file_name());
+ const char *fn = outputFileName;
+ if (NULL != fn)
+ {
+ struct stat sb;
+ if (0 > stat(fn, &sb))
+ {
+ if (ENOENT != errno)
+ {
+ fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": cannot stat log file %s: %s\n", fn, strerror(errno));
+ return NULL;
+ }
+ }
+ else if (S_ISDIR(sb.st_mode))
+ {
+ fn = append_file_to_path(&outputFileName, get_default_log_file_name());
+ }
+ }
+ else
+ {
+ fn = get_default_log_file_name();
+ }
+
+ if (NULL == fn)
+ {
+ fprintf(stderr, __FILE__ ":" STRINGIZE(__LINE__) ": cannot build log file name.");
+ return NULL;
+ }
+
VERBOSE_PRINT("Path to the log file: %s\n", fn);
fout = fopen(fn, "wt");
if (NULL == fout)
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 5d3aa0d..4e0d836 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -258,6 +258,13 @@ add_custom_target(
)
add_test(test_no_log_file make run_no_log_file)
+add_custom_target(
+ run_log_file_in_directory
+ COMMAND rm -rf ${CMAKE_CURRENT_BINARY_DIR}/lid && mkdir ${CMAKE_CURRENT_BINARY_DIR}/lid && LD_LIBRARY_PATH=${CMAKE_BINARY_DIR}/src ${Java_JAVA_EXECUTABLE} -agentlib:${AGENT_NAME}=output=${CMAKE_CURRENT_BINARY_DIR}/lid Test || test -n `find ${CMAKE_CURRENT_BINARY_DIR}/lid -name "abrt_checker_*.log"`
+ DEPENDS AbrtChecker ${TEST_JAVA_TARGETS}
+ WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
+)
+add_test(test_log_file_in_directory make run_log_file_in_directory)
get_directory_property(all_run_targets ALL_RUN_TARGETS)
add_custom_target(run_all DEPENDS ${all_run_targets})
--
1.8.3.1