Add overlay_root config option. Opkg checks this location for available space.
[oweals/opkg-lede.git] / libopkg / opkg_message.c
index 38f16df192a23c57b4c766be31b2c70ca22256cc..7114e3af0f481a86e0d54c3acbf47861ad95b44d 100644 (file)
@@ -1,5 +1,6 @@
-/* opkg_message.c - the itsy package management system
+/* opkg_message.c - the opkg package management system
 
+   Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
    Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
 
    This program is free software; you can redistribute it and/or
    General Public License for more details.
 */
 
+#include <stdio.h>
 
-#include "opkg.h"
 #include "opkg_conf.h"
 #include "opkg_message.h"
+#include "libbb/libbb.h"
 
-#ifndef OPKG_LIB
+struct errlist {
+       char *errmsg;
+       struct errlist *next;
+};
 
-void
-opkg_message (opkg_conf_t * conf, message_level_t level, char *fmt, ...)
+static struct errlist *error_list_head, *error_list_tail;
+
+static void
+push_error_list(char *msg)
 {
-       va_list ap;
+       struct errlist *e;
 
-       if (conf && (conf->verbosity < level))
-       {
-               return;
-       }
-       else
-       {
+       e = xcalloc(1,  sizeof(struct errlist));
+       e->errmsg = xstrdup(msg);
+       e->next = NULL;
 
-               va_start (ap, fmt);
-               vprintf (fmt, ap);
-               va_end (ap);
+       if (error_list_head) {
+               error_list_tail->next = e;
+               error_list_tail = e;
+       } else {
+               error_list_head = error_list_tail = e;
        }
 }
 
-#else
+void
+free_error_list(void)
+{
+       struct errlist *err, *err_tmp;
 
-#include "libopkg.h"
+       err = error_list_head;
+       while (err != NULL) {
+               free(err->errmsg);
+               err_tmp = err;
+               err = err->next;
+               free(err_tmp);
+       }
+}
 
-//#define opkg_message(conf, level, fmt, arg...) opkg_cb_message(conf, level, fmt, ## arg)
+void
+print_error_list(void)
+{
+       struct errlist *err = error_list_head;
+
+       if (err) {
+               printf("Collected errors:\n");
+               /* Here we print the errors collected and free the list */
+               while (err != NULL) {
+                       printf(" * %s", err->errmsg);
+                       err = err->next;
+               }
+       }
+}
 
 void
-opkg_message (opkg_conf_t * conf, message_level_t level, char *fmt, ...)
+opkg_message (message_level_t level, const char *fmt, ...)
 {
        va_list ap;
-       char ts[256];
 
-       if (opkg_cb_message)
-       {
+       if (conf->verbosity < level)
+               return;
+
+       if (conf->opkg_vmessage) {
+               /* Pass the message to libopkg users. */
                va_start (ap, fmt);
-               vsnprintf (ts,256,fmt, ap);
+               conf->opkg_vmessage(level, fmt, ap);
                va_end (ap);
-               opkg_cb_message(conf,level,ts);
+               return;
        }
+
+       va_start (ap, fmt);
+
+       if (level == ERROR) {
+#define MSG_LEN 4096
+               char msg[MSG_LEN];
+               int ret;
+               ret = vsnprintf(msg, MSG_LEN, fmt, ap);
+               if (ret < 0) {
+                       fprintf(stderr, "%s: encountered an output or encoding"
+                                       " error during vsnprintf.\n",
+                                       __FUNCTION__);
+                       va_end (ap);
+                       exit(EXIT_FAILURE);
+               }
+               if (ret >= MSG_LEN) {
+                       fprintf(stderr, "%s: Message truncated.\n",
+                                       __FUNCTION__);
+               }
+               push_error_list(msg);
+       } else {
+               if (vprintf(fmt, ap) < 0) {
+                       fprintf(stderr, "%s: encountered an output or encoding"
+                                       " error during vprintf.\n",
+                                       __FUNCTION__);
+                       exit(EXIT_FAILURE);
+               }
+       }
+
+       va_end (ap);
 }
-#endif