summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Gregory <andrew.gregory.8@gmail.com>2014-09-29 00:07:46 +0200
committerAndrew Gregory <andrew.gregory.8@gmail.com>2017-05-11 06:45:30 +0200
commit5c2208294dc0b21a7aa40b9eb56f64f237c48efb (patch)
tree65dbd7259c3e98df592439493a2aae34306bff92
parent1a2770afa84537b6f06d286f600172da8c31ea1f (diff)
downloadpacman-5c2208294dc0b21a7aa40b9eb56f64f237c48efb.tar.gz
pacman-5c2208294dc0b21a7aa40b9eb56f64f237c48efb.tar.xz
consolidate log functions in log.c
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com>
-rw-r--r--lib/libalpm/log.c43
-rw-r--r--lib/libalpm/log.h3
2 files changed, 46 insertions, 0 deletions
diff --git a/lib/libalpm/log.c b/lib/libalpm/log.c
index 9e986d69..f7dd5336 100644
--- a/lib/libalpm/log.c
+++ b/lib/libalpm/log.c
@@ -122,4 +122,47 @@ void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag, const char *fmt, ...
va_end(args);
}
+/** Write formatted message to log.
+ * @param handle the context handle
+ * @param format formatted string to write out
+ * @param args formatting arguments
+ * @return 0 or number of characters written on success, vfprintf return value
+ * on error
+ */
+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. */
+ va_list args_syslog;
+ va_copy(args_syslog, args);
+ vsyslog(LOG_WARNING, fmt, args_syslog);
+ va_end(args_syslog);
+ }
+
+ if(handle->logstream) {
+ time_t t;
+ struct tm *tm;
+
+ t = time(NULL);
+ tm = localtime(&t);
+
+ /* Use ISO-8601 date format */
+ 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, prefix);
+ ret = vfprintf(handle->logstream, fmt, args);
+ fflush(handle->logstream);
+ }
+
+ return ret;
+}
+
/* vim: set noet: */
diff --git a/lib/libalpm/log.h b/lib/libalpm/log.h
index 6ec04561..4f4ee58f 100644
--- a/lib/libalpm/log.h
+++ b/lib/libalpm/log.h
@@ -27,6 +27,9 @@
void _alpm_log(alpm_handle_t *handle, alpm_loglevel_t flag,
const char *fmt, ...) __attribute__((format(printf,3,4)));
+int _alpm_logaction(alpm_handle_t *handle, const char *prefix, const char *fmt, va_list args)
+ __attribute__((format(printf, 3, 0)));
+
#endif /* ALPM_LOG_H */
/* vim: set noet: */