a21a1a239cc0a428ba4e41b7b918f3accd6737b2
[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
18 #include "includes.h"
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                 printf("Collected errors:\n");
68                 /* Here we print the errors collected and free the list */
69                 while (err != NULL) {
70                         printf(" * %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 && (conf->verbosity < level))
82                 return;
83
84         va_start (ap, fmt);
85
86         if (level == ERROR) {
87                 char msg[256];
88                 vsnprintf(msg, 256, fmt, ap);
89                 push_error_list(msg);
90         } else
91                 vprintf(fmt, ap);
92
93         va_end (ap);
94 }