Doxygen
Loading...
Searching...
No Matches
message.cpp File Reference
#include <cstdio>
#include <cstdlib>
#include <mutex>
#include <atomic>
#include "config.h"
#include "debug.h"
#include "portable.h"
#include "message.h"
#include "doxygen.h"
#include "fileinfo.h"
#include "dir.h"
+ Include dependency graph for message.cpp:

Go to the source code of this file.

Functions

static void format_warn (const QCString &file, int line, const QCString &text)
 
static void handle_warn_as_error ()
 
static void do_warn (const QCString &file, int line, const char *prefix, fmt::string_view fmt, fmt::format_args args)
 
void msg_ (fmt::string_view fmt, fmt::format_args args)
 
void warn_ (WarningType type, const QCString &file, int line, fmt::string_view fmt, fmt::format_args args)
 
void warn_uncond_ (fmt::string_view fmt, fmt::format_args args)
 
void err_ (fmt::string_view fmt, fmt::format_args args)
 
void err_full_ (const QCString &file, int line, fmt::string_view fmt, fmt::format_args args)
 
void term_ (fmt::string_view fmt, fmt::format_args args)
 
QCString warn_line (const QCString &file, int line)
 
void warn_flush ()
 
void initWarningFormat ()
 
void finishWarnExit ()
 

Variables

static QCString g_warnFormat
 
static QCString g_warnLineFormat
 
static const char * g_warningStr = "warning: "
 
static const char * g_errorStr = "error: "
 
static FILE * g_warnFile = stderr
 
static WARN_AS_ERROR_t g_warnBehavior = WARN_AS_ERROR_t::NO
 
static QCString g_warnlogFile
 
static bool g_warnlogTemp = false
 
static std::atomic_bool g_warnStat = false
 
static std::mutex g_mutex
 

Function Documentation

◆ do_warn()

static void do_warn ( const QCString & file,
int line,
const char * prefix,
fmt::string_view fmt,
fmt::format_args args )
static

Definition at line 109 of file message.cpp.

110{
111 format_warn(file,line,QCString(prefix+fmt::vformat(fmt,args)));
113}
constexpr auto prefix
Definition anchor.cpp:44
This is an alternative implementation of QCString.
Definition qcstring.h:101
static void format_warn(const QCString &file, int line, const QCString &text)
Definition message.cpp:43
static void handle_warn_as_error()
Definition message.cpp:88
Definition message.h:144

References format_warn(), handle_warn_as_error(), and prefix.

Referenced by warn_().

◆ err_()

void err_ ( fmt::string_view fmt,
fmt::format_args args )

Definition at line 159 of file message.cpp.

160{
161 fmt::print(g_warnFile,"{}{}",g_errorStr,fmt::vformat(fmt,args));
163}
static const char * g_errorStr
Definition message.cpp:33
static FILE * g_warnFile
Definition message.cpp:34

References g_errorStr, g_warnFile, and handle_warn_as_error().

Referenced by err_fmt().

◆ err_full_()

void err_full_ ( const QCString & file,
int line,
fmt::string_view fmt,
fmt::format_args args )

Definition at line 167 of file message.cpp.

168{
169 format_warn(file,line,QCString(g_errorStr+fmt::vformat(fmt,args)));
170}

References format_warn(), and g_errorStr.

Referenced by err_full_fmt().

◆ finishWarnExit()

void finishWarnExit ( )

Definition at line 276 of file message.cpp.

277{
278 fflush(stdout);
279 if (g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT && g_warnlogFile != "-")
280 {
282 g_warnFile = nullptr;
283 }
284 if (g_warnStat && g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT && g_warnlogFile != "-")
285 {
286
287 std::ifstream warnFile = Portable::openInputStream(g_warnlogFile);
288 if (!warnFile.is_open())
289 {
290 g_warnFile = stderr;
291 err("Cannot open warnings file '{}' for reading\n",g_warnlogFile);
292 }
293 else
294 {
295 std::string line;
296 while (getline(warnFile,line))
297 {
298 fmt::print(stderr,"{}\n",line);
299 }
300 warnFile.close();
301 }
302 }
303
305
306 if (g_warnStat && (g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS ||
307 g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT))
308 {
309 exit(1);
310 }
311}
static bool g_warnlogTemp
Definition message.cpp:37
static std::atomic_bool g_warnStat
Definition message.cpp:38
static WARN_AS_ERROR_t g_warnBehavior
Definition message.cpp:35
static QCString g_warnlogFile
Definition message.cpp:36
#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
int fclose(FILE *f)
Definition portable.cpp:386

References err, Portable::fclose(), g_warnBehavior, g_warnFile, g_warnlogFile, g_warnlogTemp, g_warnStat, Portable::openInputStream(), and Portable::unlink().

Referenced by generateOutput().

◆ format_warn()

static void format_warn ( const QCString & file,
int line,
const QCString & text )
static

Definition at line 43 of file message.cpp.

44{
45 QCString fileSubst = file.isEmpty() ? "<unknown>" : file;
46 QCString lineSubst; lineSubst.setNum(line);
47 QCString textSubst = text;
48 QCString versionSubst;
49 // substitute markers by actual values
50 QCString msgText =
56 "$file",fileSubst
57 ),
58 "$line",lineSubst
59 ),
60 "$version",versionSubst
61 ),
62 "$text",textSubst
63 );
64 if (g_warnBehavior == WARN_AS_ERROR_t::YES)
65 {
66 msgText += " (warning treated as error, aborting now)";
67 }
68 msgText += '\n';
69
70 {
71 std::unique_lock<std::mutex> lock(g_mutex);
72 // print resulting message
73 fwrite(msgText.data(),1,msgText.length(),g_warnFile);
74 }
75 if (g_warnBehavior == WARN_AS_ERROR_t::YES)
76 {
77 if (g_warnFile != stderr && !Config_getBool(QUIET))
78 {
79 msg("See '{}' for the reason of termination.\n",g_warnlogFile);
80 }
81 exit(1);
82 }
83 g_warnStat = true;
84}
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
static QCString g_warnFormat
Definition message.cpp:30
static std::mutex g_mutex
Definition message.cpp:39
#define msg(fmt,...)
Definition message.h:94
QCString substitute(const QCString &s, const QCString &src, const QCString &dst)
substitute all occurrences of src in s by dst
Definition qcstring.cpp:477

References Config_getBool, QCString::data(), g_mutex, g_warnBehavior, g_warnFile, g_warnFormat, g_warnlogFile, g_warnStat, QCString::isEmpty(), QCString::length(), msg, QCString::setNum(), and substitute().

Referenced by do_warn(), and err_full_().

◆ handle_warn_as_error()

static void handle_warn_as_error ( )
static

Definition at line 88 of file message.cpp.

89{
90 if (g_warnBehavior == WARN_AS_ERROR_t::YES)
91 {
92 {
93 std::unique_lock<std::mutex> lock(g_mutex);
94 QCString msgText = " (warning treated as error, aborting now)\n";
95 fwrite(msgText.data(),1,msgText.length(),g_warnFile);
96 if (g_warnFile != stderr && !Config_getBool(QUIET))
97 {
98 // cannot use `msg` due to the mutex
99 fprintf(stdout,"See '%s' for the reason of termination.\n",qPrint(g_warnlogFile));
100 }
101 }
102 exit(1);
103 }
104 g_warnStat = true;
105}
const char * qPrint(const char *s)
Definition qcstring.h:672

References Config_getBool, QCString::data(), g_mutex, g_warnBehavior, g_warnFile, g_warnlogFile, g_warnStat, QCString::length(), and qPrint().

Referenced by do_warn(), err_(), and warn_uncond_().

◆ initWarningFormat()

void initWarningFormat ( )

Definition at line 218 of file message.cpp.

219{
220 g_warnFormat = Config_getString(WARN_FORMAT);
221 g_warnLineFormat = Config_getString(WARN_LINE_FORMAT);
222 g_warnBehavior = Config_getEnum(WARN_AS_ERROR);
223 g_warnlogFile = Config_getString(WARN_LOGFILE);
224 if (g_warnlogFile.isEmpty() && g_warnBehavior == WARN_AS_ERROR_t::FAIL_ON_WARNINGS_PRINT)
225 {
226 uint32_t pid = Portable::pid();
227 g_warnlogFile.sprintf("doxygen_warnings_temp_%d.tmp",pid);
228 g_warnlogTemp = true;
229 }
230
231 if (!g_warnlogFile.isEmpty())
232 {
233 if (g_warnlogFile == "-")
234 {
235 g_warnFile = stdout;
236 }
237 else
238 {
239 FileInfo fi(g_warnlogFile.str());
240 Dir d(fi.dirPath().c_str());
241 if (!d.exists() && !d.mkdir(fi.dirPath().c_str()))
242 {
243 // point it to something valid, because warn() relies on it
244 g_warnFile = stderr;
245 err("Cannot create directory for '{}', redirecting 'WARN_LOGFILE' output to 'stderr'\n",g_warnlogFile);
246 }
247 else if (!(g_warnFile = Portable::fopen(g_warnlogFile,"w")))
248 {
249 // point it to something valid, because warn() relies on it
250 g_warnFile = stderr;
251 err("Cannot open '{}' for writing, redirecting 'WARN_LOGFILE' output to 'stderr'\n",g_warnlogFile);
252 }
253 }
254 }
255 else
256 {
257 g_warnFile = stderr;
258 }
259 if (g_warnBehavior != WARN_AS_ERROR_t::NO)
260 {
262 }
263
264 // make sure the g_warnFile is closed in case we call exit and it is still open
265 std::atexit([](){
266 if (g_warnFile && g_warnFile!=stderr && g_warnFile!=stdout)
267 {
269 g_warnFile = nullptr;
270 }
271 });
272}
Class representing a directory in the file system.
Definition dir.h:75
Minimal replacement for QFileInfo.
Definition fileinfo.h:23
#define Config_getString(name)
Definition config.h:32
#define Config_getEnum(name)
Definition config.h:35
static QCString g_warnLineFormat
Definition message.cpp:31
static const char * g_warningStr
Definition message.cpp:32
FILE * fopen(const QCString &fileName, const QCString &mode)
Definition portable.cpp:366
uint32_t pid()
Definition portable.cpp:265

References Config_getEnum, Config_getString, FileInfo::dirPath(), err, Dir::exists(), Portable::fclose(), Portable::fopen(), g_errorStr, g_warnBehavior, g_warnFile, g_warnFormat, g_warningStr, g_warnLineFormat, g_warnlogFile, g_warnlogTemp, Dir::mkdir(), and Portable::pid().

Referenced by checkConfiguration().

◆ msg_()

void msg_ ( fmt::string_view fmt,
fmt::format_args args )

Definition at line 117 of file message.cpp.

118{
119 if (!Config_getBool(QUIET))
120 {
121 std::unique_lock<std::mutex> lock(g_mutex);
123 {
124 fmt::print("{:.3f} sec: ",(static_cast<double>(Debug::elapsedTime())));
125 }
126 fmt::print("{}",fmt::vformat(fmt,args));
127 }
128}
@ Time
Definition debug.h:35
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
static double elapsedTime()
Definition debug.cpp:201

References Config_getBool, Debug::elapsedTime(), g_mutex, Debug::isFlagSet(), and Debug::Time.

Referenced by msg_fmt().

◆ term_()

void term_ ( fmt::string_view fmt,
fmt::format_args args )

Definition at line 174 of file message.cpp.

175{
176 {
177 std::unique_lock<std::mutex> lock(g_mutex);
178 fmt::print(g_warnFile, "{}{}", g_errorStr, fmt::vformat(fmt,args));
179 if (g_warnFile != stderr)
180 {
181 size_t l = strlen(g_errorStr);
182 for (size_t i=0; i<l; i++) fmt::print(g_warnFile, " ");
183 fmt::print(g_warnFile, "{}\n", "Exiting...");
184 if (!Config_getBool(QUIET))
185 {
186 // cannot use `msg` due to the mutex
187 fmt::print("See '{}' for the reason of termination.\n",g_warnlogFile);
188 }
189 }
190 }
191 exit(1);
192}

References Config_getBool, g_errorStr, g_mutex, g_warnFile, and g_warnlogFile.

Referenced by term_fmt().

◆ warn_()

void warn_ ( WarningType type,
const QCString & file,
int line,
fmt::string_view fmt,
fmt::format_args args )

Definition at line 132 of file message.cpp.

133{
134 bool enabled = false;
135 switch (type)
136 {
137 case WarningType::Generic: enabled = Config_getBool(WARNINGS); break;
138 case WarningType::Undocumented: enabled = Config_getBool(WARN_IF_UNDOCUMENTED); break;
139 case WarningType::IncompleteDoc: enabled = Config_getBool(WARN_IF_INCOMPLETE_DOC); break;
140 case WarningType::DocError: enabled = Config_getBool(WARN_IF_DOC_ERROR); break;
141 case WarningType::Layout: enabled = Config_getBool(WARN_LAYOUT_FILE); break;
142 }
143 if (enabled)
144 {
145 do_warn(file, line, g_warningStr, fmt, args);
146 }
147}
static void do_warn(const QCString &file, int line, const char *prefix, fmt::string_view fmt, fmt::format_args args)
Definition message.cpp:109
@ IncompleteDoc
Definition message.h:29
@ Undocumented
Definition message.h:28

References Config_getBool, do_warn(), DocError, g_warningStr, Generic, IncompleteDoc, Layout, and Undocumented.

Referenced by warn_fmt().

◆ warn_flush()

void warn_flush ( )

Definition at line 211 of file message.cpp.

212{
213 fflush(g_warnFile);
214}

References g_warnFile.

Referenced by Statistics::end().

◆ warn_line()

QCString warn_line ( const QCString & file,
int line )

Definition at line 196 of file message.cpp.

197{
198 QCString fileSubst = file.isEmpty() ? "<unknown>" : file;
199 QCString lineSubst; lineSubst.setNum(line);
200 return substitute(
203 "$file",fileSubst
204 ),
205 "$line",lineSubst
206 );
207}

References g_warnLineFormat, QCString::isEmpty(), QCString::setNum(), and substitute().

Referenced by addMemberFunction(), checkPageRelations(), computePageRelations(), and findGlobalMember().

◆ warn_uncond_()

void warn_uncond_ ( fmt::string_view fmt,
fmt::format_args args )

Definition at line 151 of file message.cpp.

152{
153 fmt::print(g_warnFile,"{}{}",g_warningStr,vformat(fmt,args));
155}

References g_warnFile, g_warningStr, and handle_warn_as_error().

Referenced by warn_uncond_fmt().

Variable Documentation

◆ g_errorStr

const char* g_errorStr = "error: "
static

Definition at line 33 of file message.cpp.

Referenced by err_(), err_full_(), initWarningFormat(), and term_().

◆ g_mutex

std::mutex g_mutex
static

Definition at line 39 of file message.cpp.

Referenced by format_warn(), handle_warn_as_error(), msg_(), and term_().

◆ g_warnBehavior

WARN_AS_ERROR_t g_warnBehavior = WARN_AS_ERROR_t::NO
static

Definition at line 35 of file message.cpp.

Referenced by finishWarnExit(), format_warn(), handle_warn_as_error(), and initWarningFormat().

◆ g_warnFile

FILE* g_warnFile = stderr
static

◆ g_warnFormat

QCString g_warnFormat
static

Definition at line 30 of file message.cpp.

Referenced by format_warn(), and initWarningFormat().

◆ g_warningStr

const char* g_warningStr = "warning: "
static

Definition at line 32 of file message.cpp.

Referenced by initWarningFormat(), warn_(), and warn_uncond_().

◆ g_warnLineFormat

QCString g_warnLineFormat
static

Definition at line 31 of file message.cpp.

Referenced by initWarningFormat(), and warn_line().

◆ g_warnlogFile

QCString g_warnlogFile
static

◆ g_warnlogTemp

bool g_warnlogTemp = false
static

Definition at line 37 of file message.cpp.

Referenced by finishWarnExit(), and initWarningFormat().

◆ g_warnStat

std::atomic_bool g_warnStat = false
static

Definition at line 38 of file message.cpp.

Referenced by finishWarnExit(), format_warn(), and handle_warn_as_error().