2.5: Change blenloader module to use the Report system for reporting errors.
[blender.git] / source / blender / blenkernel / intern / report.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
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
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation (2008).
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 #include "MEM_guardedalloc.h"
29
30 #include "DNA_listBase.h"
31
32 #include "BLI_blenlib.h"
33
34 #include "BKE_report.h"
35
36 #include <stdarg.h>
37 #include <stdio.h>
38 #include <string.h>
39
40 #ifdef _WIN32
41 #ifndef vsnprintf
42 #define vsnprintf _vsnprintf
43 #endif
44 #endif
45
46 static char *report_type_str(int type)
47 {
48         switch(type) {
49                 case RPT_DEBUG: return "Debug";
50                 case RPT_INFO: return "Info";
51                 case RPT_WARNING: return "Warning";
52                 case RPT_ERROR: return "Error";
53                 case RPT_ERROR_INVALID_INPUT: return "Invalid Input Error";
54                 case RPT_ERROR_INVALID_CONTEXT: return "Invalid Context Error";
55                 case RPT_ERROR_OUT_OF_MEMORY: return "Out Of Memory Error";
56                 default: return "Undefined Type";
57         }
58 }
59
60 void BKE_reports_init(ReportList *reports, int flag)
61 {
62         memset(reports, 0, sizeof(ReportList));
63
64         reports->storelevel= RPT_WARNING;
65         reports->printlevel= RPT_WARNING;
66         reports->flag= flag;
67 }
68
69 void BKE_reports_clear(ReportList *reports)
70 {
71         Report *report;
72
73         for(report=reports->list.first; report; report=report->next)
74                 MEM_freeN(report->message);
75
76         BLI_freelistN(&reports->list);
77 }
78
79 void BKE_report(ReportList *reports, ReportType type, const char *message)
80 {
81         Report *report;
82         int len;
83
84         if(!reports)
85                 return;
86         
87         if(type >= RPT_ERROR)
88                 reports->flag |= RPT_HAS_ERROR;
89
90         if((reports->flag & RPT_PRINT) && (type >= reports->printlevel)) {
91                 printf("%s: %s\n", report_type_str(type), message);
92                 fflush(stdout); /* this ensures the message is printed before a crash */
93         }
94
95         if((reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
96                 report= MEM_callocN(sizeof(Report), "Report");
97                 report->type= type;
98                 report->typestr= report_type_str(type);
99
100                 len= strlen(message);
101                 report->message= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
102                 memcpy(report->message, message, sizeof(char)*(len+1));
103                 
104                 BLI_addtail(&reports->list, report);
105         }
106 }
107
108 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
109 {
110         Report *report;
111         va_list args;
112         char *message;
113         int len= 256, maxlen= 65536, retval;
114
115         if(!reports)
116                 return;
117
118         if(type >= RPT_ERROR)
119                 reports->flag |= RPT_HAS_ERROR;
120
121         if((reports->flag & RPT_PRINT) && (type >= reports->printlevel)) {
122                 va_start(args, format);
123                 vprintf(format, args);
124                 va_end(args);
125                 fflush(stdout); /* this ensures the message is printed before a crash */
126         }
127
128         if((reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
129                 while(1) {
130                         message= MEM_callocN(sizeof(char)*len+1, "ReportMessage");
131
132                         va_start(args, format);
133                         retval= vsnprintf(message, len, format, args);
134                         va_end(args);
135
136                         if(retval == -1) {
137                                 /* -1 means not enough space, but on windows it may also mean
138                                  * there is a formatting error, so we impose a maximum length */
139                                 MEM_freeN(message);
140                                 message= NULL;
141
142                                 len *= 2;
143                                 if(len > maxlen) {
144                                         fprintf(stderr, "BKE_reportf message too long or format error.\n");
145                                         break;
146                                 }
147                         }
148                         else if(retval > len) {
149                                 /* in C99 the actual length required is returned */
150                                 MEM_freeN(message);
151                                 message= NULL;
152
153                                 len= retval;
154                         }
155                         else
156                                 break;
157                 }
158
159                 if(message) {
160                         report= MEM_callocN(sizeof(Report), "Report");
161                         report->type= type;
162                         report->typestr= report_type_str(type);
163                         report->message= message;
164
165                         BLI_addtail(&reports->list, report);
166                 }
167         }
168 }
169
170 ReportType BKE_report_print_level(ReportList *reports)
171 {
172         return reports->printlevel;
173 }
174
175 void BKE_report_print_level_set(ReportList *reports, ReportType level)
176 {
177         reports->printlevel= level;
178 }
179
180 ReportType BKE_report_store_level(ReportList *reports)
181 {
182         return reports->storelevel;
183 }
184
185 void BKE_report_store_level_set(ReportList *reports, ReportType level)
186 {
187         reports->storelevel= level;
188 }
189
190 void BKE_reports_print(ReportList *reports, ReportType level)
191 {
192         Report *report;
193
194         if(!reports)
195                 return;
196         
197         for(report=reports->list.first; report; report=report->next)
198                 if(report->type >= level)
199                         printf("%s: %s\n", report->typestr, report->message);
200
201         fflush(stdout);
202 }
203