Blame SOURCES/0009-Add-support-for-changing-log-directory.patch

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