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

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