message.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  *
3  * Copyright (C) 1997-2015 by Dimitri van Heesch.
4  *
5  * Permission to use, copy, modify, and distribute this software and its
6  * documentation under the terms of the GNU General Public License is hereby
7  * granted. No representations are made about the suitability of this software
8  * for any purpose. It is provided "as is" without express or implied warranty.
9  * See the GNU General Public License for more details.
10  *
11  * Documents produced by Doxygen are derivative works derived from the
12  * input used in their production; they are not affected by this license.
13  *
14  */
15 
16 #include <stdio.h>
17 #include <qdatetime.h>
18 #include "config.h"
19 #include "util.h"
20 #include "debug.h"
21 #include "doxygen.h"
22 #include "portable.h"
23 #include "filedef.h"
24 #include "message.h"
25 
27 static const char *warning_str = "warning: ";
28 static const char *error_str = "error: ";
29 //static int warnFormatOrder; // 1 = $file,$line,$text
30 // // 2 = $text,$line,$file
31 // // 3 = $line,$text,$file
32 // // 4 = $file,$text,$line
33 // // 5 = $text,$file,$line
34 // // 6 = $line,$file,$text
35 
36 static FILE *warnFile = stderr;
37 
39 {
40 // int filePos = Config_getString("WARN_FORMAT").find("$file");
41 // int linePos = Config_getString("WARN_FORMAT").find("$line");
42 // int textPos = Config_getString("WARN_FORMAT").find("$text");
43 //
44 // // sort items on position (there are 6 cases)
45 // warnFormatOrder = 1;
46 // if (filePos>linePos && filePos>textPos)
47 // {
48 // if (linePos>textPos) // $text,$line,$file
49 // {
50 // warnFormatOrder = 2;
51 // }
52 // else // $line,$text,$file
53 // {
54 // warnFormatOrder = 3;
55 // }
56 // }
57 // else if (filePos<linePos && filePos<textPos)
58 // {
59 // if (linePos>textPos) // $file,$text,$line
60 // {
61 // warnFormatOrder = 4;
62 // }
63 // }
64 // else if (filePos<linePos && filePos>textPos) // $text,$file,$line
65 // {
66 // warnFormatOrder = 5;
67 // }
68 // else // $line,$file,$text
69 // {
70 // warnFormatOrder = 6;
71 // }
72 // outputFormat =
73 // substitute(
74 // substitute(
75 // substitute(
76 // Config_getString("WARN_FORMAT"),
77 // "$file","%s"
78 // ),
79 // "$text","%s"
80 // ),
81 // "$line","%d"
82 // )+'\n';
83 
84  // replace(QRegExp("\\$file"),"%s").
85  // replace(QRegExp("\\$text"),"%s").
86  // replace(QRegExp("\\$line"),"%d")+
87  // '\n';
88 
89  outputFormat = Config_getString("WARN_FORMAT");
90 
91  if (!Config_getString("WARN_LOGFILE").isEmpty())
92  {
93  warnFile = portable_fopen(Config_getString("WARN_LOGFILE"),"w");
94  }
95  if (!warnFile) // point it to something valid, because warn() relies on it
96  {
97  warnFile = stderr;
98  }
99 
100  if (Config_getBool("WARN_AS_ERROR"))
101  {
103  }
104 }
105 
106 
107 void msg(const char *fmt, ...)
108 {
109  if (!Config_getBool("QUIET"))
110  {
112  {
113  printf("%.3f sec: ",((double)Doxygen::runningTime.elapsed())/1000.0);
114  }
115  va_list args;
116  va_start(args, fmt);
117  vfprintf(stdout, fmt, args);
118  va_end(args);
119  }
120 }
121 
122 static void format_warn(const char *file,int line,const char *text)
123 {
124  QCString fileSubst = file==0 ? "<unknown>" : file;
125  QCString lineSubst; lineSubst.setNum(line);
126  QCString textSubst = text;
127  QCString versionSubst;
128  if (file) // get version from file name
129  {
130  bool ambig;
132  if (fd)
133  {
134  versionSubst = fd->getVersion();
135  }
136  }
137  // substitute markers by actual values
138  bool warnAsError = Config_getBool("WARN_AS_ERROR");
139  QCString msgText =
140  substitute(
141  substitute(
142  substitute(
143  substitute(
144  outputFormat,
145  "$file",fileSubst
146  ),
147  "$line",lineSubst
148  ),
149  "$version",versionSubst
150  ),
151  "$text",textSubst
152  );
153  if (warnAsError)
154  {
155  msgText += " (warning treated as error, aborting now)";
156  }
157  msgText += '\n';
158 
159  // print resulting message
160  fwrite(msgText.data(),1,msgText.length(),warnFile);
161  if (warnAsError)
162  {
163  exit(1);
164  }
165 }
166 
167 static void do_warn(const char *tag, const char *file, int line, const char *prefix, const char *fmt, va_list args)
168 {
169  if (tag && !Config_getBool(tag)) return; // warning type disabled
170  const int bufSize = 40960;
171  char text[bufSize];
172  int l=0;
173  if (prefix)
174  {
175  qstrncpy(text,prefix,bufSize);
176  l=strlen(prefix);
177  }
178  vsnprintf(text+l, bufSize-l, fmt, args);
179  text[bufSize-1]='\0';
180  format_warn(file,line,text);
181 }
182 
183 void warn(const char *file,int line,const char *fmt, ...)
184 {
185  va_list args;
186  va_start(args, fmt);
187  do_warn("WARNINGS", file, line, warning_str, fmt, args);
188  va_end(args);
189 }
190 
191 void va_warn(const char *file,int line,const char *fmt,va_list args)
192 {
193  do_warn("WARNINGS", file, line, warning_str, fmt, args);
194 }
195 
196 void warn_simple(const char *file,int line,const char *text)
197 {
198  if (!Config_getBool("WARNINGS")) return; // warning type disabled
199  format_warn(file,line,QCString(warning_str) + text);
200 }
201 
202 void warn_undoc(const char *file,int line,const char *fmt, ...)
203 {
204  va_list args;
205  va_start(args, fmt);
206  do_warn("WARN_IF_UNDOCUMENTED", file, line, warning_str, fmt, args);
207  va_end(args);
208 }
209 
210 void warn_doc_error(const char *file,int line,const char *fmt, ...)
211 {
212  va_list args;
213  va_start(args, fmt);
214  do_warn("WARN_IF_DOC_ERROR", file, line, warning_str, fmt, args);
215  va_end(args);
216 }
217 
218 void warn_uncond(const char *fmt, ...)
219 {
220  va_list args;
221  va_start(args, fmt);
222  vfprintf(warnFile, (QCString(warning_str) + fmt).data(), args);
223  va_end(args);
224 }
225 
226 void err(const char *fmt, ...)
227 {
228  va_list args;
229  va_start(args, fmt);
230  vfprintf(warnFile, (QCString(error_str) + fmt).data(), args);
231  va_end(args);
232 }
233 
234 extern void err_full(const char *file,int line,const char *fmt, ...)
235 {
236  va_list args;
237  va_start(args, fmt);
238  do_warn(NULL, file, line, error_str, fmt, args);
239  va_end(args);
240 }
241 
242 void printlex(int dbg, bool enter, const char *lexName, const char *fileName)
243 {
244  const char *enter_txt = "entering";
245  const char *enter_txt_uc = "Entering";
246 
247  if (!enter)
248  {
249  enter_txt = "finished";
250  enter_txt_uc = "Finished";
251  }
252 
253  if (dbg)
254  {
255  if (fileName)
256  fprintf(stderr,"--%s lexical analyzer: %s (for: %s)\n",enter_txt, qPrint(lexName), qPrint(fileName));
257  else
258  fprintf(stderr,"--%s lexical analyzer: %s\n",enter_txt, qPrint(lexName));
259  }
260  else
261  {
262  if (fileName)
263  Debug::print(Debug::Lex,0,"%s lexical analyzer: %s (for: %s)\n",enter_txt_uc, qPrint(lexName), qPrint(fileName));
264  else
265  Debug::print(Debug::Lex,0,"%s lexical analyzer: %s\n",enter_txt_uc, qPrint(lexName));
266  }
267 }
static const char * error_str
Definition: message.cpp:28
char * qstrncpy(char *dst, const char *src, uint len)
Definition: qcstring.cpp:557
void msg(const char *fmt,...)
Definition: message.cpp:107
void printlex(int dbg, bool enter, const char *lexName, const char *fileName)
Definition: message.cpp:242
uint length() const
Definition: qcstring.h:195
bool dbg
static const char * warning_str
Definition: message.cpp:27
void warn_doc_error(const char *file, int line, const char *fmt,...)
Definition: message.cpp:210
static FileNameDict * inputNameDict
Definition: doxygen.h:108
void warn_undoc(const char *file, int line, const char *fmt,...)
Definition: message.cpp:202
static QCString args
Definition: declinfo.cpp:674
static QStrList * l
Definition: config.cpp:1044
fileName
Definition: dumpTree.py:9
FileDef * findFileDef(const FileNameDict *fnDict, const char *n, bool &ambig)
Definition: util.cpp:4963
static void print(DebugMask mask, int prio, const char *fmt,...)
Definition: debug.cpp:84
static void do_warn(const char *tag, const char *file, int line, const char *prefix, const char *fmt, va_list args)
Definition: message.cpp:167
A bunch of utility functions.
const char * data() const
Definition: qcstring.h:207
#define Config_getString(val)
Definition: config.cpp:660
#define Config_getBool(val)
Definition: config.cpp:664
void warn(const char *file, int line, const char *fmt,...)
Definition: message.cpp:183
void err(const char *fmt,...)
Definition: message.cpp:226
void err_full(const char *file, int line, const char *fmt,...)
Definition: message.cpp:234
QCString & setNum(short n)
Definition: qcstring.cpp:469
void warn_simple(const char *file, int line, const char *text)
Definition: message.cpp:196
void line(double t, double *p, double &x, double &y, double &z)
static QCString outputFormat
Definition: message.cpp:26
void initWarningFormat()
Definition: message.cpp:38
static bool isFlagSet(DebugMask mask)
Definition: debug.cpp:119
static FILE * warnFile
Definition: message.cpp:36
void va_warn(const char *file, int line, const char *fmt, va_list args)
Definition: message.cpp:191
FILE * portable_fopen(const char *fileName, const char *mode)
Definition: portable.cpp:344
const char * qPrint(const char *s)
Definition: qcstring.h:797
static QTime runningTime
Definition: doxygen.h:132
Portable versions of functions that are platform dependent.
QCString substitute(const QCString &s, const QCString &src, const QCString &dst)
substitute all occurrences of src in s by dst
Definition: util.cpp:5088
static void format_warn(const char *file, int line, const char *text)
Definition: message.cpp:122
QCString getVersion() const
Definition: filedef.h:113
void warn_uncond(const char *fmt,...)
Definition: message.cpp:218