Blame plugins/syslog.c

2ff057
#include "system.h"
2ff057
2ff057
#include <syslog.h>
2ff057
2ff057
#include <rpm/rpmts.h>
2ff057
#include "lib/rpmplugin.h"
2ff057
2ff057
struct logstat {
2ff057
    int logging;
2ff057
    unsigned int scriptfail;
2ff057
    unsigned int pkgfail;
2ff057
};
2ff057
2ff057
static rpmRC syslog_init(rpmPlugin plugin, rpmts ts)
2ff057
{
2ff057
    /* XXX make this configurable? */
2ff057
    const char * log_ident = "[RPM]";
2ff057
    struct logstat * state = rcalloc(1, sizeof(*state));
2ff057
2ff057
    rpmPluginSetData(plugin, state);
2ff057
    openlog(log_ident, (LOG_PID), LOG_USER);
2ff057
    return RPMRC_OK;
2ff057
}
2ff057
2ff057
static void syslog_cleanup(rpmPlugin plugin)
2ff057
{
2ff057
    struct logstat * state = rpmPluginGetData(plugin);
2ff057
    free(state);
2ff057
    closelog();
2ff057
}
2ff057
2ff057
static rpmRC syslog_tsm_pre(rpmPlugin plugin, rpmts ts)
2ff057
{
2ff057
    struct logstat * state = rpmPluginGetData(plugin);
2ff057
    
2ff057
    /* Reset counters */
2ff057
    state->scriptfail = 0;
2ff057
    state->pkgfail = 0;
2ff057
2ff057
    /* Assume we are logging but... */
2ff057
    state->logging = 1;
2ff057
2ff057
    /* ...don't log test transactions */
2ff057
    if (rpmtsFlags(ts) & (RPMTRANS_FLAG_TEST|RPMTRANS_FLAG_BUILD_PROBS))
2ff057
	state->logging = 0;
2ff057
2ff057
    /* ...don't log chroot transactions */
2ff057
    if (!rstreq(rpmtsRootDir(ts), "/"))
2ff057
	state->logging = 0;
2ff057
2ff057
    if (state->logging) {
2ff057
	syslog(LOG_NOTICE, "Transaction ID %x started", rpmtsGetTid(ts));
2ff057
    }
2ff057
2ff057
    return RPMRC_OK;
2ff057
}
2ff057
2ff057
static rpmRC syslog_tsm_post(rpmPlugin plugin, rpmts ts, int res)
2ff057
{
2ff057
    struct logstat * state = rpmPluginGetData(plugin);
2ff057
2ff057
    if (state->logging) {
2ff057
	if (state->pkgfail || state->scriptfail) {
2ff057
	    syslog(LOG_WARNING, "%u elements failed, %u scripts failed",
2ff057
		   state->pkgfail, state->scriptfail);
2ff057
	}
2ff057
	syslog(LOG_NOTICE, "Transaction ID %x finished: %d",
2ff057
		rpmtsGetTid(ts), res);
2ff057
    }
2ff057
2ff057
    state->logging = 0;
2ff057
    return RPMRC_OK;
2ff057
}
2ff057
2ff057
static rpmRC syslog_psm_post(rpmPlugin plugin, rpmte te, int res)
2ff057
{
2ff057
    struct logstat * state = rpmPluginGetData(plugin);
2ff057
2ff057
    if (state->logging) {
2ff057
	int lvl = LOG_NOTICE;
2ff057
	const char *op = (rpmteType(te) == TR_ADDED) ? "install" : "erase";
2ff057
	const char *outcome = "success";
2ff057
	/* XXX: Permit configurable header queryformat? */
2ff057
	const char *pkg = rpmteNEVRA(te);
2ff057
2ff057
	if (res != RPMRC_OK) {
2ff057
	    lvl = LOG_WARNING;
2ff057
	    outcome = "failure";
2ff057
	    state->pkgfail++;
2ff057
	}
2ff057
2ff057
	syslog(lvl, "%s %s: %s", op, pkg, outcome);
2ff057
    }
2ff057
    return RPMRC_OK;
2ff057
}
2ff057
2ff057
static rpmRC syslog_scriptlet_post(rpmPlugin plugin,
2ff057
					const char *s_name, int type, int res)
2ff057
{
2ff057
    struct logstat * state = rpmPluginGetData(plugin);
2ff057
2ff057
    if (state->logging && res) {
2ff057
	syslog(LOG_WARNING, "scriptlet %s failure: %d\n", s_name, res);
2ff057
	state->scriptfail++;
2ff057
    }
2ff057
    return RPMRC_OK;
2ff057
}
2ff057
2ff057
struct rpmPluginHooks_s syslog_hooks = {
2ff057
    .init = syslog_init,
2ff057
    .cleanup = syslog_cleanup,
2ff057
    .tsm_pre = syslog_tsm_pre,
2ff057
    .tsm_post = syslog_tsm_post,
2ff057
    .psm_post = syslog_psm_post,
2ff057
    .scriptlet_post = syslog_scriptlet_post,
2ff057
};