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

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