6564329ef8298ad5284223583504d44411a5dbb4
[blender-staging.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 #include "BLI_dynstr.h"
34
35 #include "BKE_report.h"
36
37 #include <stdarg.h>
38 #include <stdio.h>
39 #include <string.h>
40
41 #ifdef _WIN32
42 #ifndef vsnprintf
43 #define vsnprintf _vsnprintf
44 #endif
45 #endif
46
47 static char *report_type_str(int type)
48 {
49         switch(type) {
50                 case RPT_DEBUG: return "Debug";
51                 case RPT_INFO: return "Info";
52                 case RPT_OPERATOR: return "Operator";
53                 case RPT_WARNING: return "Warning";
54                 case RPT_ERROR: return "Error";
55                 case RPT_ERROR_INVALID_INPUT: return "Invalid Input Error";
56                 case RPT_ERROR_INVALID_CONTEXT: return "Invalid Context Error";
57                 case RPT_ERROR_OUT_OF_MEMORY: return "Out Of Memory Error";
58                 default: return "Undefined Type";
59         }
60 }
61
62 void BKE_reports_init(ReportList *reports, int flag)
63 {
64         if(!reports)
65                 return;
66
67         memset(reports, 0, sizeof(ReportList));
68
69         reports->storelevel= RPT_INFO;
70         reports->printlevel= RPT_INFO;
71         reports->flag= flag;
72 }
73
74 void BKE_reports_clear(ReportList *reports)
75 {
76         Report *report;
77
78         if(!reports)
79                 return;
80
81         for(report=reports->list.first; report; report=report->next)
82                 MEM_freeN(report->message);
83
84         BLI_freelistN(&reports->list);
85 }
86
87 void BKE_report(ReportList *reports, ReportType type, const char *message)
88 {
89         Report *report;
90         int len;
91
92         if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
93                 printf("%s: %s\n", report_type_str(type), message);
94                 fflush(stdout); /* this ensures the message is printed before a crash */
95         }
96
97         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
98                 report= MEM_callocN(sizeof(Report), "Report");
99                 report->type= type;
100                 report->typestr= report_type_str(type);
101
102                 len= strlen(message);
103                 report->message= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
104                 memcpy(report->message, message, sizeof(char)*(len+1));
105                 
106                 BLI_addtail(&reports->list, report);
107         }
108 }
109
110 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
111 {
112         DynStr *ds;
113         Report *report;
114         va_list args;
115
116         if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
117                 va_start(args, format);
118                 vprintf(format, args);
119                 va_end(args);
120                 fflush(stdout); /* this ensures the message is printed before a crash */
121         }
122
123         if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
124                 report= MEM_callocN(sizeof(Report), "Report");
125
126                 ds= BLI_dynstr_new();
127                 va_start(args, format);
128                 BLI_dynstr_vappendf(ds, format, args);
129                 va_end(args);
130
131                 report->message= BLI_dynstr_get_cstring(ds);
132
133                 BLI_dynstr_free(ds);
134
135                 report->type= type;
136                 report->typestr= report_type_str(type);
137
138                 BLI_addtail(&reports->list, report);
139         }
140 }
141
142 void BKE_reports_prepend(ReportList *reports, const char *prepend)
143 {
144         Report *report;
145         DynStr *ds;
146
147         if(!reports)
148                 return;
149
150         for(report=reports->list.first; report; report=report->next) {
151                 ds= BLI_dynstr_new();
152
153                 BLI_dynstr_append(ds, prepend);
154                 BLI_dynstr_append(ds, report->message);
155                 MEM_freeN(report->message);
156
157                 report->message= BLI_dynstr_get_cstring(ds);
158
159                 BLI_dynstr_free(ds);
160         }
161 }
162
163 void BKE_reports_prependf(ReportList *reports, const char *prepend, ...)
164 {
165         Report *report;
166         DynStr *ds;
167         va_list args;
168
169         if(!reports)
170                 return;
171
172         for(report=reports->list.first; report; report=report->next) {
173                 ds= BLI_dynstr_new();
174                 va_start(args, prepend);
175                 BLI_dynstr_vappendf(ds, prepend, args);
176                 va_end(args);
177
178                 BLI_dynstr_append(ds, report->message);
179                 MEM_freeN(report->message);
180
181                 report->message= BLI_dynstr_get_cstring(ds);
182
183                 BLI_dynstr_free(ds);
184         }
185 }
186
187 ReportType BKE_report_print_level(ReportList *reports)
188 {
189         if(!reports)
190                 return RPT_ERROR;
191
192         return reports->printlevel;
193 }
194
195 void BKE_report_print_level_set(ReportList *reports, ReportType level)
196 {
197         if(!reports)
198                 return;
199
200         reports->printlevel= level;
201 }
202
203 ReportType BKE_report_store_level(ReportList *reports)
204 {
205         if(!reports)
206                 return RPT_ERROR;
207
208         return reports->storelevel;
209 }
210
211 void BKE_report_store_level_set(ReportList *reports, ReportType level)
212 {
213         if(!reports)
214                 return;
215
216         reports->storelevel= level;
217 }
218
219 char *BKE_reports_string(ReportList *reports, ReportType level)
220 {
221         Report *report;
222         DynStr *ds;
223         char *cstring;
224
225         if(!reports)
226                 return NULL;
227
228         ds= BLI_dynstr_new();
229         for(report=reports->list.first; report; report=report->next)
230                 if(report->type >= level)
231                         BLI_dynstr_appendf(ds, "%s: %s\n", report->typestr, report->message);
232
233         if (BLI_dynstr_get_len(ds))
234                 cstring= BLI_dynstr_get_cstring(ds);
235         else
236                 cstring= NULL;
237
238         BLI_dynstr_free(ds);
239         return cstring;
240 }
241
242 void BKE_reports_print(ReportList *reports, ReportType level)
243 {
244         char *cstring = BKE_reports_string(reports, level);
245         
246         if (cstring == NULL)
247                 return;
248         
249         printf("%s", cstring);
250         fflush(stdout);
251         MEM_freeN(cstring);
252 }
253