summaryrefslogtreecommitdiffstats
path: root/src/pacman/util.c
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2007-06-07 21:42:26 +0200
committerDan McGee <dan@archlinux.org>2007-06-09 18:57:49 +0200
commitd317acaee8e9c20056a84ee0fc0089cc6610a412 (patch)
tree1078baa1f9d40c61aa92462d85c258eec2caa8ba /src/pacman/util.c
parent430a19c604e474e90451e6421448a04abe348e6d (diff)
downloadpacman-d317acaee8e9c20056a84ee0fc0089cc6610a412.tar.gz
pacman-d317acaee8e9c20056a84ee0fc0089cc6610a412.tar.xz
Add a series of pm_printf functions to pacman frontend
Add pm_printf, pm_fprintf, and pm_vfprintf to the pacman frontend for use by debug printing and other output messages from pacman. These will be incorporated into the log callback functions in the next iteration of changes. Signed-off-by: Dan McGee <dan@archlinux.org>
Diffstat (limited to 'src/pacman/util.c')
-rw-r--r--src/pacman/util.c87
1 files changed, 80 insertions, 7 deletions
diff --git a/src/pacman/util.c b/src/pacman/util.c
index eda32524..a0829e64 100644
--- a/src/pacman/util.c
+++ b/src/pacman/util.c
@@ -42,8 +42,6 @@
#include "util.h"
#include "conf.h"
-#define LOG_STR_LEN 256
-
extern config_t *config;
/* gets the current screen column width */
@@ -408,7 +406,6 @@ void display_targets(const alpm_list_t *syncpkgs)
/* TODO there must be a better way */
int yesno(char *fmt, ...)
{
- char str[LOG_STR_LEN];
char response[32];
va_list args;
@@ -417,11 +414,9 @@ int yesno(char *fmt, ...)
}
va_start(args, fmt);
- vsnprintf(str, LOG_STR_LEN, fmt, args);
- va_end(args);
-
/* Use stderr so questions are always displayed when redirecting output */
- fprintf(stderr, str);
+ vfprintf(stderr, fmt, args);
+ va_end(args);
if(fgets(response, 32, stdin)) {
if(strlen(response) != 0) {
@@ -435,4 +430,82 @@ int yesno(char *fmt, ...)
return(0);
}
+int pm_printf(pmloglevel_t level, const char *format, ...)
+{
+ int ret;
+ va_list args;
+
+ /* print the message using va_arg list */
+ va_start(args, format);
+ ret = pm_vfprintf(stdout, level, format, args);
+ va_end(args);
+
+ return(ret);
+}
+
+int pm_fprintf(FILE *stream, pmloglevel_t level, const char *format, ...)
+{
+ int ret;
+ va_list args;
+
+ /* print the message using va_arg list */
+ va_start(args, format);
+ ret = pm_vfprintf(stream, level, format, args);
+ va_end(args);
+
+ return(ret);
+}
+
+int pm_vfprintf(FILE *stream, pmloglevel_t level, const char *format, va_list args)
+{
+ int ret = 0;
+
+ /* if current logmask does not overlap with level, do not print msg */
+ if(!(config->logmask & level)) {
+ return ret;
+ }
+
+#if defined(PACMAN_DEBUG)
+ /* If debug is on, we'll timestamp the output */
+ if(config->logmask & PM_LOG_DEBUG) {
+ time_t t;
+ struct tm *tmp;
+ char timestr[10] = {0};
+
+ t = time(NULL);
+ tmp = localtime(&t);
+ strftime(timestr, 9, "%H:%M:%S", tmp);
+ timestr[8] = '\0';
+
+ printf("[%s] ", timestr);
+ }
+#endif
+
+ /* print a prefix to the message */
+ switch(level) {
+ case PM_LOG_DEBUG:
+ fprintf(stream, _("debug: "));
+ break;
+ case PM_LOG_ERROR:
+ fprintf(stream, _("error: "));
+ break;
+ case PM_LOG_WARNING:
+ fprintf(stream, _("warning: "));
+ break;
+ case PM_LOG_FUNCTION:
+ /* TODO we should increase the indent level when this occurs so we can see
+ * program flow easier. It'll be fun */
+ fprintf(stream, _("function: "));
+ break;
+ default:
+ break;
+ }
+
+ /* print the message using va_arg list */
+ ret = vfprintf(stream, format, args);
+ /* TEMP HACK because libalpm strings don't have \n */
+ fprintf(stream, "\n");
+ return(ret);
+}
+
/* vim: set ts=2 sw=2 noet: */