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

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