Doxygen
Loading...
Searching...
No Matches
message.cpp
Go to the documentation of this file.
1/******************************************************************************
2 *
3 * Copyright (C) 1997-2020 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 <cstdio>
17#include <cstdlib>
18#include <mutex>
19#include <atomic>
20#include <unordered_set>
21
22#include "config.h"
23#include "debug.h"
24#include "portable.h"
25#include "message.h"
26#include "doxygen.h"
27#include "fileinfo.h"
28#include "dir.h"
29#include "md5.h"
30
31// globals
34static const char * g_warningStr = "warning: ";
35static const char * g_errorStr = "error: ";
36static FILE * g_warnFile = stderr;
37static WARN_AS_ERROR_t g_warnBehavior = WARN_AS_ERROR_t::NO;
39static bool g_warnlogTemp = false;
40static std::atomic_bool g_warnStat = false;
41static std::mutex g_mutex;
42static std::unordered_set<std::string> g_warnHash;
43
44//-----------------------------------------------------------------------------------------
45
46static bool checkWarnMessage(QCString result)
47{
48 uint8_t md5_sig[16];
49 char sigStr[33];
50 MD5Buffer(result.data(),result.length(),md5_sig);
51 MD5SigToString(md5_sig,sigStr);
52
53 return g_warnHash.insert(sigStr).second;
54}
55
56static void format_warn(const QCString &file,int line,const QCString &text)
57{
58 QCString fileSubst = file.isEmpty() ? "<unknown>" : file;
59 QCString lineSubst; lineSubst.setNum(line);
60 QCString textSubst = text;
61 QCString versionSubst;
62 // substitute markers by actual values
63 QCString msgText =
69 "$file",fileSubst
70 ),
71 "$line",lineSubst
72 ),
73 "$version",versionSubst
74 ),
75 "$text",textSubst
76 );
77 if (g_warnBehavior == WARN_AS_ERROR_t::YES)
78 {
79 msgText += " (warning treated as error, aborting now)";
80 }
81 msgText += '\n';
82
83 {
84 std::unique_lock<std::mutex> lock(g_mutex);
85 // print resulting message
86 if (checkWarnMessage(msgText)) fwrite(msgText.data(),1,msgText.length(),g_warnFile);
87 }
88 if (g_warnBehavior == WARN_AS_ERROR_t::YES)
89 {
90 if (g_warnFile != stderr && !Config_getBool(QUIET))
91 {
92 msg("See '{}' for the reason of termination.\n",g_warnlogFile);
93 }
94 exit(1);
95 }
96 g_warnStat = true;
97}
98
99//-----------------------------------------------------------------------------------------
100
102{
103 if (g_warnBehavior == WARN_AS_ERROR_t::YES)
104 {
105 {
106 std::unique_lock<std::mutex> lock(g_mutex);
107 QCString msgText = " (warning treated as error, aborting now)\n";
108 fwrite(msgText.data(),1,msgText.length(),g_warnFile);
109 if (g_warnFile != stderr && !Config_getBool(QUIET))
110 {
111 // cannot use `msg` due to the mutex
112 fprintf(stdout,"See '%s' for the reason of termination.\n",qPrint(g_warnlogFile));
113 }
114 }
115 exit(1);
116 }
117 g_warnStat = true;
118}
119
120//-----------------------------------------------------------------------------------------
121
122static void do_warn(const QCString &file, int line, const char *prefix, fmt::string_view fmt, fmt::format_args args)
123{
124 format_warn(file,line,QCString(prefix+fmt::vformat(fmt,args)));
126}
127
128//-----------------------------------------------------------------------------------------
129
130void msg_(fmt::string_view fmt, fmt::format_args args)
131{
132 if (!Config_getBool(QUIET))
133 {
134 std::unique_lock<std::mutex> lock(g_mutex);
136 {
137 fmt::print("{:.3f} sec: ",(static_cast<double>(Debug::elapsedTime())));
138 }
139 fmt::print("{}",fmt::vformat(fmt,args));
140 }
141}
142
143//-----------------------------------------------------------------------------------------
144
145void warn_(WarningType type, const QCString &file, int line, fmt::string_view fmt, fmt::format_args args)
146{
147 bool enabled = false;
148 switch (type)
149 {
150 case WarningType::Generic: enabled = Config_getBool(WARNINGS); break;
151 case WarningType::Undocumented: enabled = Config_getBool(WARN_IF_UNDOCUMENTED); break;
152 case WarningType::IncompleteDoc: enabled = Config_getBool(WARN_IF_INCOMPLETE_DOC); break;
153 case WarningType::DocError: enabled = Config_getBool(WARN_IF_DOC_ERROR); break;
154 case WarningType::Layout: enabled = Config_getBool(WARN_LAYOUT_FILE); break;
155 }
156 if (enabled)
157 {
158 do_warn(file, line, g_warningStr, fmt, args);
159 }
160}
161
162//-----------------------------------------------------------------------------------------
163
164void warn_uncond_(fmt::string_view fmt, fmt::format_args args)
165{
166 {
167 std::unique_lock<std::mutex> lock(g_mutex);
168 if (checkWarnMessage(g_errorStr+fmt::vformat(fmt,args))) fmt::print(g_warnFile,"{}{}",g_warningStr,vformat(fmt,args));
169 }
171}
172
173//-----------------------------------------------------------------------------------------
174
175void err_(fmt::string_view fmt, fmt::format_args args)
176{
177 {
178 std::unique_lock<std::mutex> lock(g_mutex);
179 if (checkWarnMessage(g_errorStr+fmt::vformat(fmt,args))) fmt::print(g_warnFile,"{}{}",g_errorStr,fmt::vformat(fmt,args));
180 }
182}
183
184//-----------------------------------------------------------------------------------------
185
186void err_full_(const QCString &file, int line, fmt::string_view fmt, fmt::format_args args)
187{
188 format_warn(file,line,QCString(g_errorStr+fmt::vformat(fmt,args)));
189}
190
191//-----------------------------------------------------------------------------------------
192
193void term_(fmt::string_view fmt, fmt::format_args args)
194{
195 {
196 std::unique_lock<std::mutex> lock(g_mutex);
197 if (checkWarnMessage(g_errorStr+fmt::vformat(fmt,args))) fmt::print(g_warnFile, "{}{}", g_errorStr, fmt::vformat(fmt,args));
198 if (g_warnFile != stderr)
199 {
200 size_t l = strlen(g_errorStr);
201 for (size_t i=0; i<l; i++) fmt::print(g_warnFile, " ");
202 fmt::print(g_warnFile, "{}\n", "Exiting...");
203 if (!Config_getBool(QUIET))
204 {
205 // cannot use `msg` due to the mutex
206 fmt::print("See '{}' for the reason of termination.\n",g_warnlogFile);
207 }
208 }
209 }
210 exit(1);
211}
212
213//-----------------------------------------------------------------------------------------
214
215QCString warn_line(const QCString &file,int line)
216{
217 QCString fileSubst = file.isEmpty() ? "<unknown>" : file;
218 QCString lineSubst; lineSubst.setNum(line);
219 return substitute(
222 "$file",fileSubst
223 ),
224 "$line",lineSubst
225 );
226}
227
228//-----------------------------------------------------------------------------------------
229
231{
232 fflush(g_warnFile);
233}
234
235//-----------------------------------------------------------------------------------------
236
238{
239 g_warnFormat = Config_getString(WARN_FORMAT);
240 g_warnLineFormat = Config_getString(WARN_LINE_FORMAT);
241 g_warnBehavior = Config_getEnum(WARN_AS_ERROR);
242 g_warnlogFile = Config_getString(WARN_LOGFILE);
243 if (g_warnlogFile.isEmpty() && g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT)
244 {
245 uint32_t pid = Portable::pid();
246 g_warnlogFile.sprintf("doxygen_warnings_temp_%d.tmp",pid);
247 g_warnlogTemp = true;
248 }
249
250 if (!g_warnlogFile.isEmpty())
251 {
252 if (g_warnlogFile == "-")
253 {
254 g_warnFile = stdout;
255 }
256 else
257 {
258 FileInfo fi(g_warnlogFile.str());
259 Dir d(fi.dirPath().c_str());
260 if (!d.exists() && !d.mkdir(fi.dirPath().c_str()))
261 {
262 // point it to something valid, because warn() relies on it
263 g_warnFile = stderr;
264 err("Cannot create directory for '{}', redirecting 'WARN_LOGFILE' output to 'stderr'\n",g_warnlogFile);
265 }
266 else if (!(g_warnFile = Portable::fopen(g_warnlogFile,"w")))
267 {
268 // point it to something valid, because warn() relies on it
269 g_warnFile = stderr;
270 err("Cannot open '{}' for writing, redirecting 'WARN_LOGFILE' output to 'stderr'\n",g_warnlogFile);
271 }
272 }
273 }
274 else
275 {
276 g_warnFile = stderr;
277 }
278 if (g_warnBehavior != WARN_AS_ERROR_t::NO)
279 {
281 }
282
283 // make sure the g_warnFile is closed in case we call exit and it is still open
284 std::atexit([](){
285 if (g_warnFile && g_warnFile!=stderr && g_warnFile!=stdout)
286 {
288 g_warnFile = nullptr;
289 }
290 });
291}
292
293//-----------------------------------------------------------------------------------------
294
296{
297 fflush(stdout);
298 if (g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT && g_warnlogFile != "-")
299 {
301 g_warnFile = nullptr;
302 }
303 if (g_warnStat && g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT && g_warnlogFile != "-")
304 {
305
306 std::ifstream warnFile = Portable::openInputStream(g_warnlogFile);
307 if (!warnFile.is_open())
308 {
309 g_warnFile = stderr;
310 err("Cannot open warnings file '{}' for reading\n",g_warnlogFile);
311 }
312 else
313 {
314 std::string line;
315 while (getline(warnFile,line))
316 {
317 fmt::print(stderr,"{}\n",line);
318 }
319 warnFile.close();
320 }
321 }
322
324
325 if (g_warnStat && (g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS ||
326 g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT))
327 {
328 exit(1);
329 }
330}
constexpr auto prefix
Definition anchor.cpp:44
@ Time
Definition debug.h:35
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
static double elapsedTime()
Definition debug.cpp:201
Class representing a directory in the file system.
Definition dir.h:75
bool mkdir(const std::string &path, bool acceptsAbsPath=true) const
Definition dir.cpp:295
bool exists() const
Definition dir.cpp:257
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
std::string dirPath(bool absPath=true) const
Definition fileinfo.cpp:137
This is an alternative implementation of QCString.
Definition qcstring.h:101
size_t length() const
Returns the length of the string, not counting the 0-terminator.
Definition qcstring.h:153
bool isEmpty() const
Returns TRUE iff the string is empty.
Definition qcstring.h:150
QCString & setNum(short n)
Definition qcstring.h:444
const char * data() const
Returns a pointer to the contents of the string in the form of a 0-terminated C string.
Definition qcstring.h:159
#define Config_getBool(name)
Definition config.h:33
#define Config_getString(name)
Definition config.h:32
#define Config_getEnum(name)
Definition config.h:35
static bool checkWarnMessage(QCString result)
Definition message.cpp:46
void err_(fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:175
QCString warn_line(const QCString &file, int line)
Definition message.cpp:215
static void do_warn(const QCString &file, int line, const char *prefix, fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:122
static bool g_warnlogTemp
Definition message.cpp:39
static QCString g_warnLineFormat
Definition message.cpp:33
void warn_(WarningType type, const QCString &file, int line, fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:145
static std::atomic_bool g_warnStat
Definition message.cpp:40
static const char * g_errorStr
Definition message.cpp:35
static WARN_AS_ERROR_t g_warnBehavior
Definition message.cpp:37
void warn_uncond_(fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:164
static const char * g_warningStr
Definition message.cpp:34
void initWarningFormat()
Definition message.cpp:237
static QCString g_warnFormat
Definition message.cpp:32
void warn_flush()
Definition message.cpp:230
static std::unordered_set< std::string > g_warnHash
Definition message.cpp:42
static std::mutex g_mutex
Definition message.cpp:41
static FILE * g_warnFile
Definition message.cpp:36
void msg_(fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:130
static void format_warn(const QCString &file, int line, const QCString &text)
Definition message.cpp:56
void finishWarnExit()
Definition message.cpp:295
void term_(fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:193
static void handle_warn_as_error()
Definition message.cpp:101
void err_full_(const QCString &file, int line, fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:186
static QCString g_warnlogFile
Definition message.cpp:38
#define msg(fmt,...)
Definition message.h:94
WarningType
Definition message.h:26
@ IncompleteDoc
Definition message.h:29
@ Undocumented
Definition message.h:28
#define err(fmt,...)
Definition message.h:127
std::ifstream openInputStream(const QCString &name, bool binary=false, bool openAtEnd=false)
Definition portable.cpp:676
void unlink(const QCString &fileName)
Definition portable.cpp:561
FILE * fopen(const QCString &fileName, const QCString &mode)
Definition portable.cpp:366
uint32_t pid()
Definition portable.cpp:265
int fclose(FILE *f)
Definition portable.cpp:386
Definition message.h:144
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 qcstring.cpp:477
const char * qPrint(const char *s)
Definition qcstring.h:672