4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21 * All rights reserved.
23 * Contributor(s): Blender Foundation (2008).
25 * ***** END GPL LICENSE BLOCK *****
28 #include "MEM_guardedalloc.h"
30 #include "DNA_listBase.h"
32 #include "BLI_blenlib.h"
33 #include "BLI_dynstr.h"
35 #include "BKE_report.h"
43 #define vsnprintf _vsnprintf
47 static char *report_type_str(int 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";
62 void BKE_reports_init(ReportList *reports, int flag)
67 memset(reports, 0, sizeof(ReportList));
69 reports->storelevel= RPT_INFO;
70 reports->printlevel= RPT_INFO;
74 void BKE_reports_clear(ReportList *reports)
81 for(report=reports->list.first; report; report=report->next)
82 MEM_freeN(report->message);
84 BLI_freelistN(&reports->list);
87 void BKE_report(ReportList *reports, ReportType type, const char *message)
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 */
97 if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
98 report= MEM_callocN(sizeof(Report), "Report");
100 report->typestr= report_type_str(type);
102 len= strlen(message);
103 report->message= MEM_callocN(sizeof(char)*(len+1), "ReportMessage");
104 memcpy(report->message, message, sizeof(char)*(len+1));
106 BLI_addtail(&reports->list, report);
110 void BKE_reportf(ReportList *reports, ReportType type, const char *format, ...)
116 if(!reports || ((reports->flag & RPT_PRINT) && (type >= reports->printlevel))) {
117 va_start(args, format);
118 vprintf(format, args);
120 fflush(stdout); /* this ensures the message is printed before a crash */
123 if(reports && (reports->flag & RPT_STORE) && (type >= reports->storelevel)) {
124 report= MEM_callocN(sizeof(Report), "Report");
126 ds= BLI_dynstr_new();
127 va_start(args, format);
128 BLI_dynstr_vappendf(ds, format, args);
131 report->message= BLI_dynstr_get_cstring(ds);
132 report->len= BLI_dynstr_get_len(ds);
136 report->typestr= report_type_str(type);
138 BLI_addtail(&reports->list, report);
142 void BKE_reports_prepend(ReportList *reports, const char *prepend)
150 for(report=reports->list.first; report; report=report->next) {
151 ds= BLI_dynstr_new();
153 BLI_dynstr_append(ds, prepend);
154 BLI_dynstr_append(ds, report->message);
155 MEM_freeN(report->message);
157 report->message= BLI_dynstr_get_cstring(ds);
158 report->len= BLI_dynstr_get_len(ds);
164 void BKE_reports_prependf(ReportList *reports, const char *prepend, ...)
173 for(report=reports->list.first; report; report=report->next) {
174 ds= BLI_dynstr_new();
175 va_start(args, prepend);
176 BLI_dynstr_vappendf(ds, prepend, args);
179 BLI_dynstr_append(ds, report->message);
180 MEM_freeN(report->message);
182 report->message= BLI_dynstr_get_cstring(ds);
183 report->len= BLI_dynstr_get_len(ds);
189 ReportType BKE_report_print_level(ReportList *reports)
194 return reports->printlevel;
197 void BKE_report_print_level_set(ReportList *reports, ReportType level)
202 reports->printlevel= level;
205 ReportType BKE_report_store_level(ReportList *reports)
210 return reports->storelevel;
213 void BKE_report_store_level_set(ReportList *reports, ReportType level)
218 reports->storelevel= level;
221 char *BKE_reports_string(ReportList *reports, ReportType level)
230 ds= BLI_dynstr_new();
231 for(report=reports->list.first; report; report=report->next)
232 if(report->type >= level)
233 BLI_dynstr_appendf(ds, "%s: %s\n", report->typestr, report->message);
235 if (BLI_dynstr_get_len(ds))
236 cstring= BLI_dynstr_get_cstring(ds);
244 void BKE_reports_print(ReportList *reports, ReportType level)
246 char *cstring = BKE_reports_string(reports, level);
251 printf("%s", cstring);