libopkg: store compressed package lists
[oweals/opkg-lede.git] / libopkg / opkg_message.c
1 /* opkg_message.c - the opkg package management system
2
3    Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4    Copyright (C) 2003 Daniele Nicolodi <daniele@grinta.net>
5
6    This program is free software; you can redistribute it and/or
7    modify it under the terms of the GNU General Public License as
8    published by the Free Software Foundation; either version 2, or (at
9    your option) any later version.
10
11    This program is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15 */
16
17 #include <stdio.h>
18
19 #include "opkg_conf.h"
20 #include "opkg_message.h"
21 #include "libbb/libbb.h"
22
23 struct errlist {
24         char *errmsg;
25         struct errlist *next;
26 };
27
28 static struct errlist *error_list_head, *error_list_tail;
29
30 static void
31 push_error_list(char *msg)
32 {
33         struct errlist *e;
34
35         e = xcalloc(1,  sizeof(struct errlist));
36         e->errmsg = xstrdup(msg);
37         e->next = NULL;
38
39         if (error_list_head) {
40                 error_list_tail->next = e;
41                 error_list_tail = e;
42         } else {
43                 error_list_head = error_list_tail = e;
44         }
45 }
46
47 void
48 free_error_list(void)
49 {
50         struct errlist *err, *err_tmp;
51
52         err = error_list_head;
53         while (err != NULL) {
54                 free(err->errmsg);
55                 err_tmp = err;
56                 err = err->next;
57                 free(err_tmp);
58         }
59 }
60
61 void
62 print_error_list(void)
63 {
64         struct errlist *err = error_list_head;
65
66         if (err) {
67                 fprintf(stderr, "Collected errors:\n");
68                 /* Here we print the errors collected and free the list */
69                 while (err != NULL) {
70                         fprintf(stderr, " * %s", err->errmsg);
71                         err = err->next;
72                 }
73         }
74 }
75
76 void
77 opkg_message (message_level_t level, const char *fmt, ...)
78 {
79         va_list ap;
80
81         if (conf->verbosity < level)
82                 return;
83
84         if (conf->opkg_vmessage) {
85                 /* Pass the message to libopkg users. */
86                 va_start (ap, fmt);
87                 conf->opkg_vmessage(level, fmt, ap);
88                 va_end (ap);
89                 return;
90         }
91
92         va_start (ap, fmt);
93
94         if (level == ERROR) {
95 #define MSG_LEN 4096
96                 char msg[MSG_LEN];
97                 int ret;
98                 ret = vsnprintf(msg, MSG_LEN, fmt, ap);
99                 if (ret < 0) {
100                         fprintf(stderr, "%s: encountered an output or encoding"
101                                         " error during vsnprintf.\n",
102                                         __FUNCTION__);
103                         va_end (ap);
104                         exit(EXIT_FAILURE);
105                 }
106                 if (ret >= MSG_LEN) {
107                         fprintf(stderr, "%s: Message truncated.\n",
108                                         __FUNCTION__);
109                 }
110                 push_error_list(msg);
111         } else {
112                 if (vprintf(fmt, ap) < 0) {
113                         fprintf(stderr, "%s: encountered an output or encoding"
114                                         " error during vprintf.\n",
115                                         __FUNCTION__);
116                         exit(EXIT_FAILURE);
117                 }
118         }
119
120         va_end (ap);
121 }