[pacman-dev] [PATCH 3/4] log.c: add alpm_plogaction

Andrew Gregory andrew.gregory.8 at gmail.com
Thu Dec 20 17:09:49 EST 2012


Signed-off-by: Andrew Gregory <andrew.gregory.8 at gmail.com>
---
 lib/libalpm/add.c    | 22 +++++++++++-----------
 lib/libalpm/alpm.h   |  1 +
 lib/libalpm/log.c    | 37 +++++++++++++++++++++++++++++++++++++
 lib/libalpm/remove.c |  6 +++---
 lib/libalpm/util.c   |  2 +-
 5 files changed, 53 insertions(+), 15 deletions(-)

diff --git a/lib/libalpm/add.c b/lib/libalpm/add.c
index 7ef6530..86f4c64 100644
--- a/lib/libalpm/add.c
+++ b/lib/libalpm/add.c
@@ -121,7 +121,7 @@ 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_plogaction(handle, "ALPM", "error: could not extract %s (%s)\n",
 				origname, archive_error_string(archive));
 		return 1;
 	}
@@ -133,7 +133,7 @@ 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",
+		alpm_plogaction(handle, "ALPM", "error: could not rename %s to %s (%s)\n",
 				src, dest, strerror(errno));
 		return 1;
 	}
@@ -184,7 +184,7 @@ 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",
+		alpm_plogaction(handle, "ALPM", "note: %s is in NoExtract, skipping extraction\n",
 				entryname);
 		archive_read_data_skip(archive);
 		return 0;
@@ -224,7 +224,7 @@ 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_plogaction(handle, "ALPM", "warning: directory permissions differ on %s\n"
 							"filesystem: %o  package: %o\n", entryname, lsbuf.st_mode & mask,
 							entrymode & mask);
 				}
@@ -348,7 +348,7 @@ 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_plogaction(handle, "ALPM", "warning: %s saved as %s\n", filename, newpath);
 					}
 				}
 				free(newpath);
@@ -399,7 +399,7 @@ 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",
+					alpm_plogaction(handle, "ALPM", "warning: %s installed as %s\n",
 							filename, newpath);
 				}
 				free(newpath);
@@ -416,7 +416,7 @@ 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_plogaction(handle, "ALPM", "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 +518,7 @@ 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_plogaction(handle, "ALPM", "error: could not create database entry %s-%s\n",
 				newpkg->name, newpkg->version);
 		handle->pm_errno = ALPM_ERR_DB_WRITE;
 		ret = -1;
@@ -609,12 +609,12 @@ 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_plogaction(handle, "ALPM", "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_plogaction(handle, "ALPM", "error: problem occurred while installing %s\n",
 						newpkg->name);
 			}
 		}
@@ -629,7 +629,7 @@ 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_plogaction(handle, "ALPM", "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 3388735..bece1e2 100644
--- a/lib/libalpm/alpm.h
+++ b/lib/libalpm/alpm.h
@@ -272,6 +272,7 @@ typedef enum _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_plogaction(alpm_handle_t *handle, const char *prefix, const char *fmt, ...);
 
 /**
  * Events.
diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c
index 0b48d26..4119b5d 100644
--- a/lib/libalpm/log.c
+++ b/lib/libalpm/log.c
@@ -35,6 +35,43 @@
 
 /** A printf-like function for logging.
  * @param handle the context handle
+ * @param prefix log message prefix
+ * @param fmt output format
+ * @return 0 on success, -1 on error (pm_errno is set accordingly)
+ */
+int SYMEXPORT alpm_plogaction(alpm_handle_t *handle, const char *prefix,
+		const char *fmt, ...)
+{
+	int ret;
+	va_list args;
+
+	ASSERT(handle != NULL, return -1);
+
+	/* check if the logstream is open already, opening it if needed */
+	if(handle->logstream == NULL) {
+		handle->logstream = fopen(handle->logfile, "a");
+		/* if we couldn't open it, we have an issue */
+		if(handle->logstream == NULL) {
+			if(errno == EACCES) {
+				handle->pm_errno = ALPM_ERR_BADPERMS;
+			} else if(errno == ENOENT) {
+				handle->pm_errno = ALPM_ERR_NOT_A_DIR;
+			} else {
+				handle->pm_errno = ALPM_ERR_SYSTEM;
+			}
+			return -1;
+		}
+	}
+
+	va_start(args, fmt);
+	ret = _alpm_logaction(handle, prefix, fmt, args);
+	va_end(args);
+
+	return ret;
+}
+
+/** A printf-like function for logging.
+ * @param handle the context handle
  * @param fmt output format
  * @return 0 on success, -1 on error (pm_errno is set accordingly)
  */
diff --git a/lib/libalpm/remove.c b/lib/libalpm/remove.c
index 4e1c127..f47b377 100644
--- a/lib/libalpm/remove.c
+++ b/lib/libalpm/remove.c
@@ -479,13 +479,13 @@ 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_plogaction(handle, "ALPM", "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_plogaction(handle, "ALPM", "warning: %s saved as %s\n", file, newpath);
 					free(newpath);
 					return 0;
 				}
@@ -497,7 +497,7 @@ 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",
+			alpm_plogaction(handle, "ALPM", "error: cannot remove %s (%s)\n",
 					file, strerror(errno));
 			return -1;
 		}
diff --git a/lib/libalpm/util.c b/lib/libalpm/util.c
index 8a6f27c..cb90a07 100644
--- a/lib/libalpm/util.c
+++ b/lib/libalpm/util.c
@@ -572,7 +572,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_plogaction(handle, "ALPM-SCRIPTLET", "%s", line);
 				EVENT(handle, ALPM_EVENT_SCRIPTLET_INFO, line, NULL);
 			}
 			fclose(pipe_file);
-- 
1.8.0.2



More information about the pacman-dev mailing list