[pacman-dev] [PATCH v2 1/2] add caller prefix to alpm_logaction

Andrew Gregory andrew.gregory.8 at gmail.com
Fri Jan 18 20:42:21 EST 2013


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

Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
---
 lib/libalpm/add.c     | 36 ++++++++++++++++++++++--------------
 lib/libalpm/alpm.h    |  3 ++-
 lib/libalpm/log.c     | 17 ++++-------------
 lib/libalpm/log.h     |  2 ++
 lib/libalpm/remove.c  | 10 ++++++----
 lib/libalpm/util.c    | 13 +++++++++----
 lib/libalpm/util.h    |  2 +-
 src/pacman/callback.c | 24 ++++++++++++++----------
 src/pacman/pacman.c   |  3 ++-
 src/pacman/pacman.h   |  2 ++
 src/pacman/sync.c     |  6 ++++--
 11 files changed, 68 insertions(+), 50 deletions(-)

diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index 57c9300..9b67813 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -121,7 +121,8 @@ static int perform_extraction(alpm_handle_t *handle, struct archive *archive,
 	} else if(ret != ARCHIVE_OK) {
 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not extract %s (%s)\n"),
 				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));
 		return 1;
 	}
@@ -133,8 +134,8 @@ static int try_rename(alpm_handle_t *handle, const char *src, const char *dest)
 	if(rename(src, dest)) {
 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
 				src, dest, strerror(errno));
-		alpm_logaction(handle, "error: could not rename %s to %s (%s)\n",
-				src, dest, strerror(errno));
+		alpm_logaction(handle, ALPM_CALLER_PREFIX,
+				"error: could not rename %s to %s (%s)\n", src, dest, strerror(errno));
 		return 1;
 	}
 	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)) {
 		_alpm_log(handle, ALPM_LOG_DEBUG, "%s is in NoExtract, skipping extraction\n",
 				entryname);
-		alpm_logaction(handle, "note: %s is in NoExtract, skipping extraction\n",
-				entryname);
+		alpm_logaction(handle, ALPM_CALLER_PREFIX,
+				"note: %s is in NoExtract, skipping extraction\n", entryname);
 		archive_read_data_skip(archive);
 		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"
 								"filesystem: %o  package: %o\n"), entryname, lsbuf.st_mode & 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,
 							entrymode & mask);
 				}
@@ -348,7 +350,8 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
 						errors++;
 					} else {
 						_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);
@@ -399,8 +402,8 @@ static int extract_single_file(alpm_handle_t *handle, struct archive *archive,
 				} else {
 					_alpm_log(handle, ALPM_LOG_WARNING, _("%s installed as %s\n"),
 							filename, newpath);
-					alpm_logaction(handle, "warning: %s installed as %s\n",
-							filename, newpath);
+					alpm_logaction(handle, ALPM_CALLER_PREFIX,
+							"warning: %s installed as %s\n", filename, newpath);
 				}
 				free(newpath);
 			}
@@ -416,7 +419,8 @@ needbackup_cleanup:
 			/* 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_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));
 		} else {
 			_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
 	   changelog/install script installation (FS#12263) */
 	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);
 		handle->pm_errno = ALPM_ERR_DB_WRITE;
 		ret = -1;
@@ -609,12 +614,14 @@ static int commit_single_pkg(alpm_handle_t *handle, alpm_pkg_t *newpkg,
 			if(is_upgrade) {
 				_alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while upgrading %s\n"),
 						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);
 			} else {
 				_alpm_log(handle, ALPM_LOG_ERROR, _("problem occurred while installing %s\n"),
 						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);
 			}
 		}
@@ -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)) {
 		_alpm_log(handle, ALPM_LOG_ERROR, _("could not update database entry %s-%s\n"),
 				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);
 		handle->pm_errno = ALPM_ERR_DB_WRITE;
 		ret = -1;
diff --git a/lib/libalpm/alpm.h b/lib/libalpm/alpm.h
index 6e8c93d..f524c19 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -268,7 +268,8 @@ typedef enum _alpm_loglevel_t {
 } alpm_loglevel_t;
 
 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.
diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c
index 74143fd..a8639b4 100644
--- a/lib/libalpm/log.c
+++ b/lib/libalpm/log.c
@@ -35,10 +35,12 @@
 
 /** A printf-like function for logging.
  * @param handle the context handle
+ * @param prefix caller-specific prefix for the log
  * @param fmt output format
  * @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;
 	va_list args;
@@ -62,20 +64,9 @@ int SYMEXPORT alpm_logaction(alpm_handle_t *handle, const char *fmt, ...)
 	}
 
 	va_start(args, fmt);
-	ret = _alpm_logaction(handle, fmt, args);
+	ret = _alpm_logaction(handle, prefix, fmt, 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;
 }
 
diff --git a/lib/libalpm/log.h b/lib/libalpm/log.h
index b44ea18..dd60c85 100644
--- a/lib/libalpm/log.h
+++ b/lib/libalpm/log.h
@@ -22,6 +22,8 @@
 
 #include "alpm.h"
 
+#define ALPM_CALLER_PREFIX "ALPM"
+
 void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag,
 		const char *fmt, ...) __attribute__((format(printf,3,4)));
 
diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c
index cdc572d..50fc93c 100644
--- a/lib/libalpm/remove.c
+++ b/lib/libalpm/remove.c
@@ -479,13 +479,15 @@ static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
 					if(rename(file, newpath)) {
 						_alpm_log(handle, ALPM_LOG_ERROR, _("could not rename %s to %s (%s)\n"),
 								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));
 						free(newpath);
 						return -1;
 					}
 					_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);
 					return 0;
 				}
@@ -497,8 +499,8 @@ static int unlink_file(alpm_handle_t *handle, alpm_pkg_t *oldpkg,
 		if(unlink(file) == -1) {
 			_alpm_log(handle, ALPM_LOG_ERROR, _("cannot remove %s (%s)\n"),
 					file, strerror(errno));
-			alpm_logaction(handle, "error: cannot remove %s (%s)\n",
-					file, strerror(errno));
+			alpm_logaction(handle, ALPM_CALLER_PREFIX,
+					"error: cannot remove %s (%s)\n", file, strerror(errno));
 			return -1;
 		}
 	}
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index fd1d786..700a717 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -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
  * 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;
 
+	if(!(prefix && *prefix)) {
+		prefix = "UNKNOWN";
+	}
+
 	if(handle->usesyslog) {
 		/* 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. */
@@ -474,9 +479,9 @@ int _alpm_logaction(alpm_handle_t *handle, const char *fmt, va_list args)
 		tm = localtime(&t);
 
 		/* 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_hour, tm->tm_min);
+						tm->tm_hour, tm->tm_min, prefix);
 		ret = vfprintf(handle->logstream, fmt, args);
 		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];
 				if(fgets(line, PATH_MAX, pipe_file) == NULL)
 					break;
-				alpm_logaction(handle, "%s", line);
+				alpm_logaction(handle, "ALPM-SCRIPTLET", "%s", line);
 				EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL);
 			}
 			fclose(pipe_file);
diff --git a/lib/libalpm/util.h b/lib/libalpm/util.h
index 3a6b14a..93b6573 100644
--- a/lib/libalpm/util.h
+++ b/lib/libalpm/util.h
@@ -120,7 +120,7 @@ int _alpm_unpack(alpm_handle_t *handle, const char *archive, const char *prefix,
 		alpm_list_t *list, int breakfirst);
 
 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_ldconfig(alpm_handle_t *handle);
 int _alpm_str_cmp(const void *s1, const void *s2);
diff --git a/src/pacman/callback.c b/src/pacman/callback.c
index f53f59c..93418db 100644
--- a/src/pacman/callback.c
+++ b/src/pacman/callback.c
@@ -31,6 +31,7 @@
 #include <alpm.h>
 
 /* pacman */
+#include "pacman.h"
 #include "callback.h"
 #include "util.h"
 #include "conf.h"
@@ -176,9 +177,10 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
 			}
 			break;
 		case ALPM_EVENT_ADD_DONE:
-			alpm_logaction(config->handle, "installed %s (%s)\n",
-			         alpm_pkg_get_name(data1),
-			         alpm_pkg_get_version(data1));
+			alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
+					"installed %s (%s)\n",
+					alpm_pkg_get_name(data1),
+					alpm_pkg_get_version(data1));
 			display_optdepends(data1);
 			break;
 		case ALPM_EVENT_REMOVE_START:
@@ -187,9 +189,10 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
 			}
 			break;
 		case ALPM_EVENT_REMOVE_DONE:
-			alpm_logaction(config->handle, "removed %s (%s)\n",
-			         alpm_pkg_get_name(data1),
-			         alpm_pkg_get_version(data1));
+			alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
+					"removed %s (%s)\n",
+					alpm_pkg_get_name(data1),
+					alpm_pkg_get_version(data1));
 			break;
 		case ALPM_EVENT_UPGRADE_START:
 			if(config->noprogressbar) {
@@ -197,10 +200,11 @@ void cb_event(alpm_event_t event, void *data1, void *data2)
 			}
 			break;
 		case ALPM_EVENT_UPGRADE_DONE:
-			alpm_logaction(config->handle, "upgraded %s (%s -> %s)\n",
-			         alpm_pkg_get_name(data1),
-			         alpm_pkg_get_version(data2),
-			         alpm_pkg_get_version(data1));
+			alpm_logaction(config->handle, PACMAN_CALLER_PREFIX,
+					"upgraded %s (%s -> %s)\n",
+					alpm_pkg_get_name(data1),
+					alpm_pkg_get_version(data2),
+					alpm_pkg_get_version(data1));
 			display_new_optdepends(data2, data1);
 			break;
 		case ALPM_EVENT_INTEGRITY_START:
diff --git a/src/pacman/pacman.c b/src/pacman/pacman.c
index f3ca8b9..8448d6b 100644
--- a/src/pacman/pacman.c
+++ b/src/pacman/pacman.c
@@ -744,7 +744,8 @@ static void cl_to_log(int argc, char *argv[])
 		*p++ = ' ';
 	}
 	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);
 }
 
diff --git a/src/pacman/pacman.h b/src/pacman/pacman.h
index 4eaf318..7c9c365 100644
--- a/src/pacman/pacman.h
+++ b/src/pacman/pacman.h
@@ -22,6 +22,8 @@
 
 #include <alpm_list.h>
 
+#define PACMAN_CALLER_PREFIX "PACMAN"
+
 /* database.c */
 int pacman_database(alpm_list_t *targets);
 /* deptest.c */
diff --git a/src/pacman/sync.c b/src/pacman/sync.c
index fef2940..3decc40 100644
--- a/src/pacman/sync.c
+++ b/src/pacman/sync.c
@@ -787,7 +787,8 @@ static int sync_trans(alpm_list_t *targets)
 
 	if(config->op_s_upgrade) {
 		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) {
 			pm_printf(ALPM_LOG_ERROR, "%s\n", alpm_strerror(alpm_errno(config->handle)));
 			trans_release();
@@ -954,7 +955,8 @@ int pacman_sync(alpm_list_t *targets)
 	if(config->op_s_sync) {
 		/* grab a fresh package list */
 		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)) {
 			return 1;
 		}
-- 
1.8.1.1



More information about the pacman-dev mailing list