diff options
author | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-20 18:25:26 +0200 |
---|---|---|
committer | Denys Vlasenko <dvlasenk@redhat.com> | 2010-10-20 18:25:26 +0200 |
commit | bfda214e9d634015457a7c5d8e2e1fd26d49a1fb (patch) | |
tree | ba2ae953a86e9c7715d9c98b009d72abc06820da | |
parent | 451d3d730c7d1ff93031848882e43813ee3c346f (diff) | |
download | abrt-bfda214e9d634015457a7c5d8e2e1fd26d49a1fb.tar.gz abrt-bfda214e9d634015457a7c5d8e2e1fd26d49a1fb.tar.xz abrt-bfda214e9d634015457a7c5d8e2e1fd26d49a1fb.zip |
move logger reporting to a separate program (abrt-action-print)
Usage: abrt-action-print [v] -d DIR
Print information about the crash to standard output
-v, --verbose be verbose
-d DIR Crash dump directory
As you can see, it simply writes crash dump information
to stdout, not to a file.
Logger plugin spawns a child abrt-action-print -d DIR
and pipes its output to log file.
Signed-off-by: Denys Vlasenko <dvlasenk@redhat.com>
-rw-r--r-- | abrt.spec | 1 | ||||
-rw-r--r-- | lib/plugins/Logger.cpp | 99 | ||||
-rw-r--r-- | lib/plugins/Logger.h | 3 | ||||
-rw-r--r-- | src/daemon/Makefile.am | 23 | ||||
-rw-r--r-- | src/daemon/abrt-action-print.cpp | 83 |
5 files changed, 179 insertions, 30 deletions
@@ -356,6 +356,7 @@ fi %{_sbindir}/abrt-action-save-package-data %{_bindir}/abrt-action-bugzilla %{_bindir}/abrt-action-rhtsupport +%{_bindir}/abrt-action-print %{_bindir}/abrt-action-install-debuginfo %{_bindir}/%{name}-handle-upload %config(noreplace) %{_sysconfdir}/%{name}/%{name}.conf diff --git a/lib/plugins/Logger.cpp b/lib/plugins/Logger.cpp index e3ba9660..2985208e 100644 --- a/lib/plugins/Logger.cpp +++ b/lib/plugins/Logger.cpp @@ -1,5 +1,5 @@ /* - Logger.cpp - it simple writes report to specific file + Logger.cpp - it simply writes report to a specified file Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) Copyright (C) 2009 RedHat inc. @@ -19,60 +19,107 @@ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ #include "abrtlib.h" -#include "Logger.h" #include "comm_layer_inner.h" #include "abrt_exception.h" +#include "Logger.h" + +using namespace std; CLogger::CLogger() { - m_log_path = xstrdup("/var/log/abrt.log"); - m_append_logs = true; + m_pSettings["LogPath"] = "/var/log/abrt.log"; + m_pSettings["AppendLogs"] = "yes"; } CLogger::~CLogger() { - free(m_log_path); } void CLogger::SetSettings(const map_plugin_settings_t& pSettings) { + /* Can't simply do this: + m_pSettings = pSettings; - map_plugin_settings_t::const_iterator end = pSettings.end(); - map_plugin_settings_t::const_iterator it; - it = pSettings.find("LogPath"); - if (it != end) + * - it will erase keys which aren't present in pSettings. + * Example: if Bugzilla.conf doesn't have "Login = foo", + * then there's no pSettings["Login"] and m_pSettings = pSettings + * will nuke default m_pSettings["Login"] = "", + * making GUI think that we have no "Login" key at all + * and thus never overriding it - even if it *has* an override! + */ + + map_plugin_settings_t::iterator it = m_pSettings.begin(); + while (it != m_pSettings.end()) { - free(m_log_path); - m_log_path = xstrdup(it->second.c_str()); + map_plugin_settings_t::const_iterator override = pSettings.find(it->first); + if (override != pSettings.end()) + { + VERB3 log(" logger settings[%s]='%s'", it->first.c_str(), it->second.c_str()); + it->second = override->second; + } + it++; } - it = pSettings.find("AppendLogs"); - if (it != end) - m_append_logs = string_to_bool(it->second.c_str()); } -std::string CLogger::Report(const map_crash_data_t& pCrashData, +string CLogger::Report(const map_crash_data_t& crash_data, const map_plugin_settings_t& pSettings, const char *pArgs) { - char *dsc = make_description_logger(pCrashData); - char *full_dsc = xasprintf("%s\n\n\n", dsc); - free(dsc); + const char *log_path = "/var/log/abrt.log"; + bool append_logs = true; + + map_plugin_settings_t::const_iterator end = pSettings.end(); + map_plugin_settings_t::const_iterator it; + it = pSettings.find("LogPath"); + if (it != end) + log_path = it->second.c_str(); + it = pSettings.find("AppendLogs"); + if (it != end) + append_logs = string_to_bool(it->second.c_str()); /* open, not fopen - want to set mode if we create the file, not just open */ - const char *fname = m_log_path; - int fd = open(fname, m_append_logs ? O_WRONLY|O_CREAT|O_APPEND : O_WRONLY|O_CREAT|O_TRUNC, 0600); + int fd = open(log_path, append_logs ? O_WRONLY|O_CREAT|O_APPEND : O_WRONLY|O_CREAT|O_TRUNC, 0600); if (fd < 0) - throw CABRTException(EXCEP_PLUGIN, "Can't open '%s'", fname); + throw CABRTException(EXCEP_PLUGIN, "Can't open '%s'", log_path); + + update_client(_("Writing report to '%s'"), log_path); + + /* abrt-action-print -d DIR NULL */ + char *argv[4]; + char **pp = argv; + *pp++ = (char*)"abrt-action-print"; + *pp++ = (char*)"-d"; + *pp++ = (char*)get_crash_data_item_content_or_NULL(crash_data, CD_DUMPDIR); + *pp = NULL; + int pipefds[2]; + pid_t pid = fork_execv_on_steroids(EXECFLG_OUTPUT, // + EXECFLG_ERR2OUT, + argv, + pipefds, + /* unsetenv_vec: */ NULL, + /* dir: */ NULL, + /* uid(unused): */ 0 + ); - update_client(_("Writing report to '%s'"), fname); - full_write_str(fd, full_dsc); - free(full_dsc); + /* Consume log from stdout, write it to log file */ + FILE *fp = fdopen(pipefds[0], "r"); + if (!fp) + die_out_of_memory(); + char buf[512]; + while (fgets(buf, sizeof(buf), fp)) + { + full_write_str(fd, buf); + } + fclose(fp); /* this also closes pipefds[0] */ + /* wait for child to actually exit, and prevent leaving a zombie behind */ + waitpid(pid, NULL, 0); + /* Add separating empty lines, then close log file */ + full_write_str(fd, "\n\n\n"); close(fd); - const char *format = m_append_logs ? _("The report was appended to %s") : _("The report was stored to %s"); - return ssprintf(format, m_log_path); + const char *format = append_logs ? _("The report was appended to %s") : _("The report was stored to %s"); + return ssprintf(format, log_path); } PLUGIN_INFO(REPORTER, diff --git a/lib/plugins/Logger.h b/lib/plugins/Logger.h index d2b80075..afe1b7c2 100644 --- a/lib/plugins/Logger.h +++ b/lib/plugins/Logger.h @@ -27,9 +27,6 @@ class CLogger : public CReporter { - private: - char *m_log_path; - bool m_append_logs; public: CLogger(); ~CLogger(); diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index bd839a23..06b1d44f 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -12,7 +12,8 @@ sbin_PROGRAMS = abrtd \ bin_PROGRAMS = \ abrt-action-bugzilla \ - abrt-action-rhtsupport + abrt-action-rhtsupport \ + abrt-action-print abrtd_SOURCES = \ PluginManager.h PluginManager.cpp \ @@ -200,6 +201,26 @@ abrt_action_rhtsupport_LDADD = \ ../../lib/utils/libABRTdUtils.la \ ../../lib/utils/libABRTUtils.la +abrt_action_print_SOURCES = \ + abrt-action-print.cpp +abrt_action_print_CPPFLAGS = \ + -I$(srcdir)/../../inc \ + -I$(srcdir)/../../lib/utils \ + -DBIN_DIR=\"$(bindir)\" \ + -DVAR_RUN=\"$(VAR_RUN)\" \ + -DCONF_DIR=\"$(CONF_DIR)\" \ + -DLOCALSTATEDIR='"$(localstatedir)"' \ + -DDEBUG_DUMPS_DIR=\"$(DEBUG_DUMPS_DIR)\" \ + -DDEBUG_INFO_DIR=\"$(DEBUG_INFO_DIR)\" \ + -DPLUGINS_LIB_DIR=\"$(PLUGINS_LIB_DIR)\" \ + -DPLUGINS_CONF_DIR=\"$(PLUGINS_CONF_DIR)\" \ + $(GLIB_CFLAGS) \ + -D_GNU_SOURCE \ + -Wall -Werror +abrt_action_print_LDADD = \ + ../../lib/utils/libABRTdUtils.la \ + ../../lib/utils/libABRTUtils.la + dbusabrtconfdir = ${sysconfdir}/dbus-1/system.d/ dist_dbusabrtconf_DATA = dbus-abrt.conf diff --git a/src/daemon/abrt-action-print.cpp b/src/daemon/abrt-action-print.cpp new file mode 100644 index 00000000..f4d5db1d --- /dev/null +++ b/src/daemon/abrt-action-print.cpp @@ -0,0 +1,83 @@ +/* + Write crash dump to stdout in text form. + + Copyright (C) 2009 Zdenek Prikryl (zprikryl@redhat.com) + Copyright (C) 2009 RedHat inc. + + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program; if not, write to the Free Software Foundation, Inc., + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. +*/ +#include "abrtlib.h" +#include "parse_options.h" +#include "crash_types.h" +#include "abrt_exception.h" +#include "plugin.h" /* make_description_logger */ + +#define PROGNAME "abrt-action-print" + +static void report_to_stdout(const char *dump_dir_name) +{ + struct dump_dir *dd = dd_opendir(dump_dir_name, /*flags:*/ 0); + if (!dd) + { + throw CABRTException(EXCEP_PLUGIN, _("Can't open '%s'"), dump_dir_name); + } + map_crash_data_t pCrashData; + load_crash_data_from_debug_dump(dd, pCrashData); + dd_close(dd); + + char *dsc = make_description_logger(pCrashData); + fputs(dsc, stdout); + free(dsc); +} + +static const char *dump_dir_name = "."; + +int main(int argc, char **argv) +{ + char *env_verbose = getenv("ABRT_VERBOSE"); + if (env_verbose) + g_verbose = atoi(env_verbose); + + const char *program_usage = _( + PROGNAME" [v] -d DIR\n" + "\n" + "Print information about the crash to standard output"); + enum { + OPT_v = 1 << 0, + OPT_d = 1 << 1, + }; + /* Keep enum above and order of options below in sync! */ + struct options program_options[] = { + OPT__VERBOSE(&g_verbose), + OPT_STRING('d', NULL, &dump_dir_name, "DIR", _("Crash dump directory")), + OPT_END() + }; + + /*unsigned opts =*/ parse_opts(argc, argv, program_options, program_usage); + + putenv(xasprintf("ABRT_VERBOSE=%u", g_verbose)); + + try + { + report_to_stdout(dump_dir_name); + } + catch (CABRTException& e) + { + log("%s", e.what()); + return 1; + } + + return 0; +} |