Skip to content

Commit

Permalink
Merge pull request freebsd#136 from so14k/syslog
Browse files Browse the repository at this point in the history
- Add support for logging to syslog.
  • Loading branch information
bapt committed Feb 6, 2012
2 parents f5525bf + 96a1146 commit c2f82d5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
3 changes: 2 additions & 1 deletion libpkg/pkg.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ typedef enum _pkg_config_key {
PKG_CONFIG_HANDLE_RC_SCRIPTS = 6,
PKG_CONFIG_ASSUME_ALWAYS_YES = 7,
PKG_CONFIG_REPOS = 8,
PKG_CONFIG_PLIST_KEYWORDS_DIR = 9
PKG_CONFIG_PLIST_KEYWORDS_DIR = 9,
PKG_CONFIG_SYSLOG = 10
} pkg_config_key;

typedef enum {
Expand Down
15 changes: 14 additions & 1 deletion libpkg/pkg_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ static struct config_entry c[] = {
"PLIST_KEYWORDS_DIR",
NULL,
{ NULL }
},
[PKG_CONFIG_SYSLOG] = {
BOOL,
"SYSLOG",
"YES",
{ NULL }
}
};

Expand Down Expand Up @@ -201,8 +207,15 @@ pkg_config_bool(pkg_config_key key, bool *val)
if (c[key].val != NULL && (
strcasecmp(c[key].val, "yes") == 0 ||
strcasecmp(c[key].val, "true" ) == 0 ||
strcasecmp(c[key].val, "on" ) == 0))
strcasecmp(c[key].val, "on" ) == 0)) {
*val = true;
}
else if (c[key].val == NULL && c[key].def != NULL && (
strcasecmp(c[key].def, "yes") == 0 ||
strcasecmp(c[key].def, "true" ) == 0 ||
strcasecmp(c[key].def, "on" ) == 0)) {
*val = true;
}

return (EPKG_OK);
}
Expand Down
5 changes: 5 additions & 0 deletions pkg/add.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include <pkg.h>

#include "add.h"
#include "pkg_event.h"

static int
is_url(const char * const pattern)
Expand Down Expand Up @@ -42,6 +43,7 @@ exec_add(int argc, char **argv)
int retcode = EPKG_OK;
int i;
int failedpkgcount = 0;
struct pkg *p = NULL;

if (argc < 2) {
usage_add();
Expand All @@ -66,6 +68,9 @@ exec_add(int argc, char **argv)
file = path;
} else
file = argv[i];

pkg_open(&p, file, NULL);
pkg_emit_install_begin(p);

if ((retcode = pkg_add(db, file, 0)) != EPKG_OK) {
sbuf_cat(failedpkgs, argv[i]);
Expand Down
21 changes: 20 additions & 1 deletion pkg/event.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include <sys/param.h>
#include <string.h>
#include <err.h>
#include <syslog.h>
#include <stdarg.h>

#include "pkg.h"
#include "progressmeter.h"
Expand All @@ -18,6 +20,9 @@ event_callback(void *data, struct pkg_event *ev)
int *debug = data;
(void)debug;
const char *name, *version, *newversion;
bool syslog_enabled;

pkg_config_bool(PKG_CONFIG_SYSLOG, &syslog_enabled);

switch(ev->type) {
case PKG_EVENT_ERRNO:
Expand All @@ -44,9 +49,12 @@ event_callback(void *data, struct pkg_event *ev)
break;
case PKG_EVENT_INSTALL_FINISHED:
printf(" done\n");
pkg_get(ev->e_install_finished.pkg, PKG_MESSAGE, &message);
pkg_get(ev->e_install_finished.pkg, PKG_MESSAGE, &message,
PKG_NAME, &name, PKG_VERSION, &version);
if (message != NULL && message[0] != '\0')
printf("%s\n", message);
if (syslog_enabled)
syslog(LOG_NOTICE, "Installed: %s-%s", name, version);
break;
case PKG_EVENT_INTEGRITYCHECK_BEGIN:
printf("Checking integrity...");
Expand All @@ -62,6 +70,11 @@ event_callback(void *data, struct pkg_event *ev)
break;
case PKG_EVENT_DEINSTALL_FINISHED:
printf(" done\n");
if (syslog_enabled) {
pkg_get(ev->e_deinstall_finished.pkg, PKG_NAME, &name,
PKG_VERSION, &version);
syslog(LOG_NOTICE, "Deinstalled: %s-%s", name, version);
}
break;
case PKG_EVENT_UPGRADE_BEGIN:
pkg_get(ev->e_upgrade_finished.pkg, PKG_NAME, &name, PKG_VERSION, &version,
Expand All @@ -71,6 +84,12 @@ event_callback(void *data, struct pkg_event *ev)
break;
case PKG_EVENT_UPGRADE_FINISHED:
printf(" done\n");
if (syslog_enabled) {
pkg_get(ev->e_upgrade_finished.pkg, PKG_NAME, &name,
PKG_VERSION, &version, PKG_NEWVERSION, &newversion);
syslog(LOG_NOTICE, "Upgrading: %s-%s to %s-%s",
name, version, name, newversion);
}
break;
case PKG_EVENT_REQUIRED:
pkg = ev->e_required.pkg;
Expand Down
1 change: 1 addition & 0 deletions pkg/pkg.conf.sample
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ PUBKEY : /etc/ssl/pkg.conf
HANDLE_RC_SCRIPTS : NO
PKG_MULTIREPOS : NO
ASSUME_ALWAYS_YES : NO
SYSLOG : YES

# Repository definitions
repos:
Expand Down

0 comments on commit c2f82d5

Please sign in to comment.