add caller prefix to alpm_logaction

prefix defaults to "UNKOWN" if null or an empty string is provided.

Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
Signed-off-by: Allan McRae <allan@archlinux.org>
This commit is contained in:
Andrew Gregory 2013-01-18 20:42:21 -05:00 committed by Allan McRae
parent 8308c7b320
commit 66a9b53141
11 changed files with 68 additions and 50 deletions

View file

@ -121,7 +121,8 @@ static int perform_extraction(alpm_handle_t *handle, struct archive *archive,
} else if(ret != ARCHIVE_OK) { } else if(ret != ARCHIVE_OK) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not extract %s (%s)\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("could not extract %s (%s)\n"),
origname, archive_error_string(archive)); origname, archive_error_string(archive));
alpm_logaction(handle, "error: could not extract %s (%s)\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
"error: could not extract %s (%s)\n",
origname, archive_error_string(archive)); origname, archive_error_string(archive));
return 1; return 1;
} }
@ -133,8 +134,8 @@ static int try_rename(alpm_handle_t *handle, const char *src, const char *dest)
if(rename(src, dest)) { if(rename(src, dest)) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
src, dest, strerror(errno)); src, dest, strerror(errno));
alpm_logaction(handle, "error: could not rename %s to %s (%s)\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
src, dest, strerror(errno)); "error: could not rename %s to %s (%s)\n", src, dest, strerror(errno));
return 1; return 1;
} }
return 0; return 0;
@ -184,8 +185,8 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
if(alpm_list_find(handle->noextract, entryname, _alpm_fnmatch)) { if(alpm_list_find(handle->noextract, entryname, _alpm_fnmatch)) {
_alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoExtract, skipping extraction\n", _alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoExtract, skipping extraction\n",
entryname); entryname);
alpm_logaction(handle, "note: %s is in NoExtract, skipping extraction\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
entryname); "note: %s is in NoExtract, skipping extraction\n", entryname);
archive_read_data_skip(archive); archive_read_data_skip(archive);
return 0; return 0;
} }
@ -224,7 +225,8 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
_alpm_log(handle, ALPM_LOG_WARNING, _("directory permissions differ on %s\n" _alpm_log(handle, ALPM_LOG_WARNING, _("directory permissions differ on %s\n"
"filesystem: %o package: %o\n"), entryname, lsbuf.st_mode & mask, "filesystem: %o package: %o\n"), entryname, lsbuf.st_mode & mask,
entrymode & mask); entrymode & mask);
alpm_logaction(handle, "warning: directory permissions differ on %s\n" alpm_logaction(handle, ALPM_CALLER_PREFIX,
"warning: directory permissions differ on %s\n"
"filesystem: %o package: %o\n", entryname, lsbuf.st_mode & mask, "filesystem: %o package: %o\n", entryname, lsbuf.st_mode & mask,
entrymode & mask); entrymode & mask);
} }
@ -348,7 +350,8 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
errors++; errors++;
} else { } else {
_alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), filename, newpath); _alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), filename, newpath);
alpm_logaction(handle, "warning: %s saved as %s\n", filename, newpath); alpm_logaction(handle, ALPM_CALLER_PREFIX,
"warning: %s saved as %s\n", filename, newpath);
} }
} }
free(newpath); free(newpath);
@ -399,8 +402,8 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
} else { } else {
_alpm_log(handle, ALPM_LOG_WARNING, _("%s installed as %s\n"), _alpm_log(handle, ALPM_LOG_WARNING, _("%s installed as %s\n"),
filename, newpath); filename, newpath);
alpm_logaction(handle, "warning: %s installed as %s\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
filename, newpath); "warning: %s installed as %s\n", filename, newpath);
} }
free(newpath); free(newpath);
} }
@ -416,7 +419,8 @@ needbackup_cleanup:
/* change the path to a .pacnew extension */ /* change the path to a .pacnew extension */
_alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoUpgrade -- skipping\n", filename); _alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoUpgrade -- skipping\n", filename);
_alpm_log(handle, ALPM_LOG_WARNING, _("extracting %s as %s.pacnew\n"), filename, filename); _alpm_log(handle, ALPM_LOG_WARNING, _("extracting %s as %s.pacnew\n"), filename, filename);
alpm_logaction(handle, "warning: extracting %s as %s.pacnew\n", filename, filename); alpm_logaction(handle, ALPM_CALLER_PREFIX,
"warning: extracting %s as %s.pacnew\n", filename, filename);
strncat(filename, ".pacnew", PATH_MAX - strlen(filename)); strncat(filename, ".pacnew", PATH_MAX - strlen(filename));
} else { } else {
_alpm_log(handle, ALPM_LOG_DEBUG, "extracting %s\n", filename); _alpm_log(handle, ALPM_LOG_DEBUG, "extracting %s\n", filename);
@ -518,7 +522,8 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
/* prepare directory for database entries so permission are correct after /* prepare directory for database entries so permission are correct after
changelog/install script installation (FS#12263) */ changelog/install script installation (FS#12263) */
if(_alpm_local_db_prepare(db, newpkg)) { if(_alpm_local_db_prepare(db, newpkg)) {
alpm_logaction(handle, "error: could not create database entry %s-%s\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
"error: could not create database entry %s-%s\n",
newpkg->name, newpkg->version); newpkg->name, newpkg->version);
handle->pm_errno = ALPM_ERR_DB_WRITE; handle->pm_errno = ALPM_ERR_DB_WRITE;
ret = -1; ret = -1;
@ -609,12 +614,14 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
if(is_upgrade) { if(is_upgrade) {
_alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while upgrading %s\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while upgrading %s\n"),
newpkg->name); newpkg->name);
alpm_logaction(handle, "error: problem occurred while upgrading %s\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
"error: problem occurred while upgrading %s\n",
newpkg->name); newpkg->name);
} else { } else {
_alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while installing %s\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while installing %s\n"),
newpkg->name); newpkg->name);
alpm_logaction(handle, "error: problem occurred while installing %s\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
"error: problem occurred while installing %s\n",
newpkg->name); newpkg->name);
} }
} }
@ -629,7 +636,8 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
if(_alpm_local_db_write(db, newpkg, INFRQ_ALL)) { if(_alpm_local_db_write(db, newpkg, INFRQ_ALL)) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not update database entry %s-%s\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("could not update database entry %s-%s\n"),
newpkg->name, newpkg->version); newpkg->name, newpkg->version);
alpm_logaction(handle, "error: could not update database entry %s-%s\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
"error: could not update database entry %s-%s\n",
newpkg->name, newpkg->version); newpkg->name, newpkg->version);
handle->pm_errno = ALPM_ERR_DB_WRITE; handle->pm_errno = ALPM_ERR_DB_WRITE;
ret = -1; ret = -1;

View file

@ -268,7 +268,8 @@ typedef enum _alpm_loglevel_t {
} alpm_loglevel_t; } alpm_loglevel_t;
typedef void (*alpm_cb_log)(alpm_loglevel_t, const char *, va_list); typedef void (*alpm_cb_log)(alpm_loglevel_t, const char *, va_list);
int alpm_logaction(alpm_handle_t *handle, const char *fmt, ...); int alpm_logaction(alpm_handle_t *handle, const char *prefix,
const char *fmt, ...);
/** /**
* Events. * Events.

View file

@ -35,10 +35,12 @@
/** A printf-like function for logging. /** A printf-like function for logging.
* @param handle the context handle * @param handle the context handle
* @param prefix caller-specific prefix for the log
* @param fmt output format * @param fmt output format
* @return 0 on success, -1 on error (pm_errno is set accordingly) * @return 0 on success, -1 on error (pm_errno is set accordingly)
*/ */
int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *fmt, ...) int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *prefix, const
char *fmt, ...)
{ {
int ret; int ret;
va_list args; va_list args;
@ -62,20 +64,9 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *fmt, ...)
} }
va_start(args, fmt); va_start(args, fmt);
ret = _alpm_logaction(handle, fmt, args); ret = _alpm_logaction(handle, prefix, fmt, args);
va_end(args); va_end(args);
/* TODO We should add a prefix to log strings depending on who called us.
* If logaction was called by the frontend:
* USER: <the frontend log>
* and if called internally:
* ALPM: <the library log>
* Moreover, the frontend should be able to choose its prefix
* (USER by default?):
* pacman: "PACMAN"
* kpacman: "KPACMAN"
* This would allow us to share the log file between several frontends
* and know who does what */
return ret; return ret;
} }

View file

@ -22,6 +22,8 @@
#include "alpm.h" #include "alpm.h"
#define ALPM_CALLER_PREFIX "ALPM"
void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag,
const char *fmt, ...) __attribute__((format(printf,3,4))); const char *fmt, ...) __attribute__((format(printf,3,4)));

View file

@ -479,13 +479,15 @@ static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
if(rename(file, newpath)) { if(rename(file, newpath)) {
_alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
file, newpath, strerror(errno)); file, newpath, strerror(errno));
alpm_logaction(handle, "error: could not rename %s to %s (%s)\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
"error: could not rename %s to %s (%s)\n",
file, newpath, strerror(errno)); file, newpath, strerror(errno));
free(newpath); free(newpath);
return -1; return -1;
} }
_alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), file, newpath); _alpm_log(handle, ALPM_LOG_WARNING, _("%s saved as %s\n"), file, newpath);
alpm_logaction(handle, "warning: %s saved as %s\n", file, newpath); alpm_logaction(handle, ALPM_CALLER_PREFIX,
"warning: %s saved as %s\n", file, newpath);
free(newpath); free(newpath);
return 0; return 0;
} }
@ -497,8 +499,8 @@ static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
if(unlink(file) == -1) { if(unlink(file) == -1) {
_alpm_log(handle, ALPM_LOG_ERROR, _("cannot remove %s (%s)\n"), _alpm_log(handle, ALPM_LOG_ERROR, _("cannot remove %s (%s)\n"),
file, strerror(errno)); file, strerror(errno));
alpm_logaction(handle, "error: cannot remove %s (%s)\n", alpm_logaction(handle, ALPM_CALLER_PREFIX,
file, strerror(errno)); "error: cannot remove %s (%s)\n", file, strerror(errno));
return -1; return -1;
} }
} }

View file

@ -453,10 +453,15 @@ ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path,
* @return 0 or number of characters written on success, vfprintf return value * @return 0 or number of characters written on success, vfprintf return value
* on error * on error
*/ */
int _alpm_logaction(alpm_handle_t *handle, const char *fmt, va_list args) int _alpm_logaction(alpm_handle_t *handle, const char *prefix,
const char *fmt, va_list args)
{ {
int ret = 0; int ret = 0;
if(!(prefix && *prefix)) {
prefix = "UNKNOWN";
}
if(handle->usesyslog) { if(handle->usesyslog) {
/* we can't use a va_list more than once, so we need to copy it /* we can't use a va_list more than once, so we need to copy it
* so we can use the original when calling vfprintf below. */ * so we can use the original when calling vfprintf below. */
@ -474,9 +479,9 @@ int _alpm_logaction(alpm_handle_t *handle, const char *fmt, va_list args)
tm = localtime(&t); tm = localtime(&t);
/* Use ISO-8601 date format */ /* Use ISO-8601 date format */
fprintf(handle->logstream, "[%04d-%02d-%02d %02d:%02d] ", fprintf(handle->logstream, "[%04d-%02d-%02d %02d:%02d] [%s] ",
tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
tm->tm_hour, tm->tm_min); tm->tm_hour, tm->tm_min, prefix);
ret = vfprintf(handle->logstream, fmt, args); ret = vfprintf(handle->logstream, fmt, args);
fflush(handle->logstream); fflush(handle->logstream);
} }
@ -568,7 +573,7 @@ int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[])
char line[PATH_MAX]; char line[PATH_MAX];
if(fgets(line, PATH_MAX, pipe_file) == NULL) if(fgets(line, PATH_MAX, pipe_file) == NULL)
break; break;
alpm_logaction(handle, "%s", line); alpm_logaction(handle, "ALPM-SCRIPTLET", "%s", line);
EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL); EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL);
} }
fclose(pipe_file); fclose(pipe_file);

View file

@ -120,7 +120,7 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,
alpm_list_t *list, int breakfirst); alpm_list_t *list, int breakfirst);
ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count); ssize_t _alpm_files_in_directory(alpm_handle_t *handle, const char *path, int full_count);
int _alpm_logaction(alpm_handle_t *handle, const char *fmt, va_list args); int _alpm_logaction(alpm_handle_t *handle, const char *prefix, const char *fmt, va_list args);
int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]); int _alpm_run_chroot(alpm_handle_t *handle, const char *cmd, char *const argv[]);
int _alpm_ldconfig(alpm_handle_t *handle); int _alpm_ldconfig(alpm_handle_t *handle);
int _alpm_str_cmp(const void *s1, const void *s2); int _alpm_str_cmp(const void *s1, const void *s2);

View file

@ -31,6 +31,7 @@
#include <alpm.h> #include <alpm.h>
/* pacman */ /* pacman */
#include "pacman.h"
#include "callback.h" #include "callback.h"
#include "util.h" #include "util.h"
#include "conf.h" #include "conf.h"
@ -176,9 +177,10 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
} }
break; break;
case ALPM_EVENT_ADD_DONE: case ALPM_EVENT_ADD_DONE:
alpm_logaction(config->handle, "installed %s (%s)\n", alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
alpm_pkg_get_name(data1), "installed %s (%s)\n",
alpm_pkg_get_version(data1)); alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1));
display_optdepends(data1); display_optdepends(data1);
break; break;
case ALPM_EVENT_REMOVE_START: case ALPM_EVENT_REMOVE_START:
@ -187,9 +189,10 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
} }
break; break;
case ALPM_EVENT_REMOVE_DONE: case ALPM_EVENT_REMOVE_DONE:
alpm_logaction(config->handle, "removed %s (%s)\n", alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
alpm_pkg_get_name(data1), "removed %s (%s)\n",
alpm_pkg_get_version(data1)); alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1));
break; break;
case ALPM_EVENT_UPGRADE_START: case ALPM_EVENT_UPGRADE_START:
if(config->noprogressbar) { if(config->noprogressbar) {
@ -197,10 +200,11 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
} }
break; break;
case ALPM_EVENT_UPGRADE_DONE: case ALPM_EVENT_UPGRADE_DONE:
alpm_logaction(config->handle, "upgraded %s (%s -> %s)\n", alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
alpm_pkg_get_name(data1), "upgraded %s (%s -> %s)\n",
alpm_pkg_get_version(data2), alpm_pkg_get_name(data1),
alpm_pkg_get_version(data1)); alpm_pkg_get_version(data2),
alpm_pkg_get_version(data1));
display_new_optdepends(data2, data1); display_new_optdepends(data2, data1);
break; break;
case ALPM_EVENT_INTEGRITY_START: case ALPM_EVENT_INTEGRITY_START:

View file

@ -744,7 +744,8 @@ static void cl_to_log(int argc, char *argv[])
*p++ = ' '; *p++ = ' ';
} }
strcpy(p, argv[i]); strcpy(p, argv[i]);
alpm_logaction(config->handle, "Running '%s'\n", cl_text); alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
"Running '%s'\n", cl_text);
free(cl_text); free(cl_text);
} }

View file

@ -22,6 +22,8 @@
#include <alpm_list.h> #include <alpm_list.h>
#define PACMAN_CALLER_PREFIX "PACMAN"
/* database.c */ /* database.c */
int pacman_database(alpm_list_t *targets); int pacman_database(alpm_list_t *targets);
/* deptest.c */ /* deptest.c */

View file

@ -787,7 +787,8 @@ static int sync_trans(alpm_list_t *targets)
if(config->op_s_upgrade) { if(config->op_s_upgrade) {
printf(_(":: Starting full system upgrade...\n")); printf(_(":: Starting full system upgrade...\n"));
alpm_logaction(config->handle, "starting full system upgrade\n"); alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
"starting full system upgrade\n");
if(alpm_sync_sysupgrade(config->handle, config->op_s_upgrade >= 2) == -1) { if(alpm_sync_sysupgrade(config->handle, config->op_s_upgrade >= 2) == -1) {
pm_printf(ALPM_LOG_ERROR, "%s\n", alpm_strerror(alpm_errno(config->handle))); pm_printf(ALPM_LOG_ERROR, "%s\n", alpm_strerror(alpm_errno(config->handle)));
trans_release(); trans_release();
@ -954,7 +955,8 @@ int pacman_sync(alpm_list_t *targets)
if(config->op_s_sync) { if(config->op_s_sync) {
/* grab a fresh package list */ /* grab a fresh package list */
printf(_(":: Synchronizing package databases...\n")); printf(_(":: Synchronizing package databases...\n"));
alpm_logaction(config->handle, "synchronizing package lists\n"); alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
"synchronizing package lists\n");
if(!sync_synctree(config->op_s_sync, sync_dbs)) { if(!sync_synctree(config->op_s_sync, sync_dbs)) {
return 1; return 1;
} }