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 
push_error_list(char * msg)30 static void push_error_list(char *msg)
31 {
32 	struct errlist *e;
33 
34 	e = xcalloc(1, sizeof(struct errlist));
35 	e->errmsg = xstrdup(msg);
36 	e->next = NULL;
37 
38 	if (error_list_head) {
39 		error_list_tail->next = e;
40 		error_list_tail = e;
41 	} else {
42 		error_list_head = error_list_tail = e;
43 	}
44 }
45 
free_error_list(void)46 void free_error_list(void)
47 {
48 	struct errlist *err, *err_tmp;
49 
50 	err = error_list_head;
51 	while (err != NULL) {
52 		free(err->errmsg);
53 		err_tmp = err;
54 		err = err->next;
55 		free(err_tmp);
56 	}
57 }
58 
print_error_list(void)59 void print_error_list(void)
60 {
61 	struct errlist *err = error_list_head;
62 
63 	if (err) {
64 		fprintf(stderr, "Collected errors:\n");
65 		/* Here we print the errors collected and free the list */
66 		while (err != NULL) {
67 			fprintf(stderr, " * %s", err->errmsg);
68 			err = err->next;
69 		}
70 	}
71 }
72 
opkg_message(message_level_t level,const char * fmt,...)73 void opkg_message(message_level_t level, const char *fmt, ...)
74 {
75 	va_list ap;
76 
77 	if (conf->verbosity < level)
78 		return;
79 
80 	if (conf->opkg_vmessage) {
81 		/* Pass the message to libopkg users. */
82 		va_start(ap, fmt);
83 		conf->opkg_vmessage(level, fmt, ap);
84 		va_end(ap);
85 		return;
86 	}
87 
88 	va_start(ap, fmt);
89 
90 	if (level == ERROR) {
91 #define MSG_LEN 4096
92 		char msg[MSG_LEN];
93 		int ret;
94 		ret = vsnprintf(msg, MSG_LEN, fmt, ap);
95 		if (ret < 0) {
96 			fprintf(stderr, "%s: encountered an output or encoding"
97 				" error during vsnprintf.\n", __FUNCTION__);
98 			va_end(ap);
99 			exit(EXIT_FAILURE);
100 		}
101 		if (ret >= MSG_LEN) {
102 			fprintf(stderr, "%s: Message truncated.\n",
103 				__FUNCTION__);
104 		}
105 		push_error_list(msg);
106 	} else {
107 		if (vprintf(fmt, ap) < 0) {
108 			fprintf(stderr, "%s: encountered an output or encoding"
109 				" error during vprintf.\n", __FUNCTION__);
110 			exit(EXIT_FAILURE);
111 		}
112 	}
113 
114 	va_end(ap);
115 }
116