Doxygen
Loading...
Searching...
No Matches
debug.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 <stdarg.h>
17#include <algorithm>
18#include <stdio.h>
19#include <map>
20#include <string>
21#include <chrono>
22
23#include "debug.h"
24#include "message.h"
25#include "qcstring.h"
26
27//------------------------------------------------------------------------
28
29static std::map< std::string, Debug::DebugMask > s_labels =
30{
31 { "preprocessor", Debug::Preprocessor },
32 { "nolineno", Debug::NoLineNo },
33 { "commentcnv", Debug::CommentCnv },
34 { "commentscan", Debug::CommentScan },
35 { "formula", Debug::Formula },
36 { "printtree", Debug::PrintTree },
37 { "time", Debug::Time },
38 { "extcmd", Debug::ExtCmd },
39 { "markdown", Debug::Markdown },
40 { "filteroutput", Debug::FilterOutput },
41 { "plantuml", Debug::Plantuml },
42 { "fortranfixed2free", Debug::FortranFixed2Free },
43 { "cite", Debug::Cite },
44 { "rtf", Debug::Rtf },
45 { "qhp", Debug::Qhp },
46 { "tag", Debug::Tag },
47 { "alias", Debug::Alias },
48 { "entries", Debug::Entries },
49 { "sections", Debug::Sections },
50 { "stderr", Debug::Stderr },
51 { "layout", Debug::Layout },
52 { "mermaid", Debug::Mermaid },
53 { "lex", Debug::Lex },
54 { "lex:code", Debug::Lex_code },
55 { "lex:commentcnv", Debug::Lex_commentcnv },
56 { "lex:commentscan", Debug::Lex_commentscan },
57 { "lex:configimpl", Debug::Lex_configimpl },
58 { "lex:constexp", Debug::Lex_constexp },
59 { "lex:declinfo", Debug::Lex_declinfo },
60 { "lex:defargs", Debug::Lex_defargs },
61 { "lex:doctokenizer", Debug::Lex_doctokenizer },
62 { "lex:fortrancode", Debug::Lex_fortrancode },
63 { "lex:fortranscanner", Debug::Lex_fortranscanner },
64 { "lex:lexcode", Debug::Lex_lexcode },
65 { "lex:lexscanner", Debug::Lex_lexscanner },
66 { "lex:pre", Debug::Lex_pre },
67 { "lex:pycode", Debug::Lex_pycode },
68 { "lex:pyscanner", Debug::Lex_pyscanner },
69 { "lex:scanner", Debug::Lex_scanner },
70 { "lex:sqlcode", Debug::Lex_sqlcode },
71 { "lex:vhdlcode", Debug::Lex_vhdlcode },
72 { "lex:xml", Debug::Lex_xml },
73 { "lex:xmlcode", Debug::Lex_xmlcode },
74};
75
76//------------------------------------------------------------------------
77static FILE *g_debugFile = stdout;
78
80int Debug::curPrio = 0;
81
82void Debug::print_(DebugMask mask, int prio, fmt::string_view fmt, fmt::format_args args)
83{
84 if ((curMask&mask) && prio<=curPrio)
85 {
86 fmt::print(g_debugFile,"{}",fmt::vformat(fmt,args));
87 }
88}
89
90static char asciiToLower(char in) {
91 if (in <= 'Z' && in >= 'A')
92 return in - 'A' + 'a';
93 return in;
94}
95
96static uint64_t labelToEnumValue(const QCString &l)
97{
98 std::string s = l.str();
99 std::transform(s.begin(),s.end(),s.begin(),asciiToLower);
100 auto it = s_labels.find(s);
101 return (it!=s_labels.end()) ? it->second : Debug::DebugMask::Quiet;
102}
103
105{
106 uint64_t retVal = labelToEnumValue(lab);
107 if (retVal == Debug::Stderr)
108 {
109 g_debugFile = stderr;
110 }
111 else
112 {
113 curMask = static_cast<DebugMask>(curMask | retVal);
114 }
115 return retVal!=0;
116}
117
118void Debug::setFlag(const DebugMask mask)
119{
120 curMask = static_cast<DebugMask>(curMask | mask);
121}
122
124{
125 curMask = static_cast<DebugMask>(curMask & ~mask);
126}
127
129{
130 curPrio = p;
131}
132
134{
135 return (curMask & mask)!=0;
136}
137
139{
140 for (const auto &v : s_labels)
141 {
142 msg("\t{}\n",v.first);
143 }
144}
145
146//------------------------------------------------------------------------
147DebugLex::DebugLex(Debug::DebugMask mask,const char *lexName,const char *fileName) : m_mask(mask), m_lexName(lexName), m_fileName(fileName)
148{
150}
151
156
158 const char *state,
159 const char *lexName,
160 const char *fileName)
161{
162 if (!Debug::isFlagSet(mask)) return;
163
164 if (fileName && *fileName)
165 {
166 fprintf(stderr, "%s lexical analyzer: %s (for: %s)\n", state, lexName, fileName);
167 }
168 else
169 {
170 fprintf(stderr, "%s lexical analyzer: %s\n", state, lexName);
171 }
172}
173
174//------------------------------------------------------------------------
175
176class Timer
177{
178 public:
179 void start()
180 {
181 m_startTime = std::chrono::steady_clock::now();
182 }
184 {
185 return static_cast<double>(
186 std::chrono::duration_cast<
187 std::chrono::microseconds>(
188 std::chrono::steady_clock::now() - m_startTime).count()) / 1000000.0;
189 }
190 private:
191 std::chrono::time_point<std::chrono::steady_clock> m_startTime;
192};
193
195
197{
198 g_runningTime.start();
199}
200
202{
203 return g_runningTime.elapsedTimeS();
204}
205
DebugMask
Definition debug.h:28
@ Lex_fortranscanner
Definition debug.h:62
@ Lex_doctokenizer
Definition debug.h:60
@ Lex
Definition debug.h:52
@ Rtf
Definition debug.h:43
@ NoLineNo
Definition debug.h:42
@ Mermaid
Definition debug.h:51
@ Lex_pre
Definition debug.h:65
@ Lex_defargs
Definition debug.h:59
@ Alias
Definition debug.h:46
@ Lex_pycode
Definition debug.h:66
@ Layout
Definition debug.h:50
@ Markdown
Definition debug.h:37
@ Lex_declinfo
Definition debug.h:58
@ Tag
Definition debug.h:45
@ Lex_lexscanner
Definition debug.h:64
@ FilterOutput
Definition debug.h:38
@ Lex_sqlcode
Definition debug.h:69
@ Lex_code
Definition debug.h:53
@ Lex_pyscanner
Definition debug.h:67
@ Cite
Definition debug.h:41
@ ExtCmd
Definition debug.h:36
@ Sections
Definition debug.h:48
@ PrintTree
Definition debug.h:34
@ Time
Definition debug.h:35
@ Quiet
Definition debug.h:29
@ Lex_xml
Definition debug.h:71
@ Stderr
Definition debug.h:49
@ Plantuml
Definition debug.h:39
@ Formula
Definition debug.h:33
@ Lex_commentcnv
Definition debug.h:54
@ Lex_vhdlcode
Definition debug.h:70
@ Lex_constexp
Definition debug.h:57
@ Lex_xmlcode
Definition debug.h:72
@ Lex_fortrancode
Definition debug.h:61
@ Lex_commentscan
Definition debug.h:55
@ Lex_lexcode
Definition debug.h:63
@ Qhp
Definition debug.h:44
@ Entries
Definition debug.h:47
@ Lex_configimpl
Definition debug.h:56
@ Preprocessor
Definition debug.h:30
@ Lex_scanner
Definition debug.h:68
@ CommentCnv
Definition debug.h:31
@ FortranFixed2Free
Definition debug.h:40
@ CommentScan
Definition debug.h:32
static void printFlags()
Definition debug.cpp:138
static DebugMask curMask
Definition debug.h:93
static void setPriority(int p)
Definition debug.cpp:128
static int curPrio
Definition debug.h:94
static void clearFlag(const DebugMask mask)
Definition debug.cpp:123
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:133
static double elapsedTime()
Definition debug.cpp:201
static void startTimer()
Definition debug.cpp:196
static bool setFlagStr(const QCString &label)
Definition debug.cpp:104
static void print_(DebugMask mask, int prio, fmt::string_view fmt, fmt::format_args args)
Definition debug.cpp:82
static void setFlag(const DebugMask mask)
Definition debug.cpp:118
DebugLex(Debug::DebugMask mask, const char *lexName, const char *fileName)
Definition debug.cpp:147
~DebugLex()
Definition debug.cpp:152
Debug::DebugMask m_mask
Definition debug.h:106
QCString m_fileName
Definition debug.h:108
static void print(Debug::DebugMask mask, const char *state, const char *lexName, const char *fileName)
Definition debug.cpp:157
QCString m_lexName
Definition debug.h:107
This is an alternative implementation of QCString.
Definition qcstring.h:101
const std::string & str() const
Definition qcstring.h:552
void start()
Definition debug.cpp:179
std::chrono::time_point< std::chrono::steady_clock > m_startTime
Definition debug.cpp:191
double elapsedTimeS()
Definition debug.cpp:183
static FILE * g_debugFile
Definition debug.cpp:77
static Timer g_runningTime
Definition debug.cpp:194
static std::map< std::string, Debug::DebugMask > s_labels
Definition debug.cpp:29
static uint64_t labelToEnumValue(const QCString &l)
Definition debug.cpp:96
static char asciiToLower(char in)
Definition debug.cpp:90
#define msg(fmt,...)
Definition message.h:94
Definition message.h:144
const char * qPrint(const char *s)
Definition qcstring.h:687