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 { "lex", Debug::Lex },
53 { "lex:code", Debug::Lex_code },
54 { "lex:commentcnv", Debug::Lex_commentcnv },
55 { "lex:commentscan", Debug::Lex_commentscan },
56 { "lex:configimpl", Debug::Lex_configimpl },
57 { "lex:constexp", Debug::Lex_constexp },
58 { "lex:declinfo", Debug::Lex_declinfo },
59 { "lex:defargs", Debug::Lex_defargs },
60 { "lex:doctokenizer", Debug::Lex_doctokenizer },
61 { "lex:fortrancode", Debug::Lex_fortrancode },
62 { "lex:fortranscanner", Debug::Lex_fortranscanner },
63 { "lex:lexcode", Debug::Lex_lexcode },
64 { "lex:lexscanner", Debug::Lex_lexscanner },
65 { "lex:pre", Debug::Lex_pre },
66 { "lex:pycode", Debug::Lex_pycode },
67 { "lex:pyscanner", Debug::Lex_pyscanner },
68 { "lex:scanner", Debug::Lex_scanner },
69 { "lex:sqlcode", Debug::Lex_sqlcode },
70 { "lex:vhdlcode", Debug::Lex_vhdlcode },
71 { "lex:xml", Debug::Lex_xml },
72 { "lex:xmlcode", Debug::Lex_xmlcode },
73};
74
75//------------------------------------------------------------------------
76static FILE *g_debugFile = stdout;
77
79int Debug::curPrio = 0;
80
81void Debug::print_(DebugMask mask, int prio, fmt::string_view fmt, fmt::format_args args)
82{
83 if ((curMask&mask) && prio<=curPrio)
84 {
85 fmt::print(g_debugFile,"{}",fmt::vformat(fmt,args));
86 }
87}
88
89static char asciiToLower(char in) {
90 if (in <= 'Z' && in >= 'A')
91 return in - ('Z' - 'z');
92 return in;
93}
94
95static uint64_t labelToEnumValue(const QCString &l)
96{
97 std::string s = l.str();
98 std::transform(s.begin(),s.end(),s.begin(),asciiToLower);
99 auto it = s_labels.find(s);
100 return (it!=s_labels.end()) ? it->second : Debug::DebugMask::Quiet;
101}
102
104{
105 uint64_t retVal = labelToEnumValue(lab);
106 if (retVal == Debug::Stderr)
107 {
108 g_debugFile = stderr;
109 }
110 else
111 {
112 curMask = static_cast<DebugMask>(curMask | retVal);
113 }
114 return retVal!=0;
115}
116
117void Debug::setFlag(const DebugMask mask)
118{
119 curMask = static_cast<DebugMask>(curMask | mask);
120}
121
123{
124 curMask = static_cast<DebugMask>(curMask & ~mask);
125}
126
128{
129 curPrio = p;
130}
131
133{
134 return (curMask & mask)!=0;
135}
136
138{
139 for (const auto &v : s_labels)
140 {
141 msg("\t{}\n",v.first);
142 }
143}
144
145//------------------------------------------------------------------------
146DebugLex::DebugLex(Debug::DebugMask mask,const char *lexName,const char *fileName) : m_mask(mask), m_lexName(lexName), m_fileName(fileName)
147{
149}
150
155
156void DebugLex::print(Debug::DebugMask mask,const char *state,const char *lexName,const char *fileName)
157{
158 if (fileName && *fileName)
159 {
160 if (Debug::isFlagSet(mask))
161 {
162 fprintf(stderr,"%s lexical analyzer: %s (for: %s)\n",state, lexName, fileName);
163 }
164 }
165 else
166 {
167 if (Debug::isFlagSet(mask))
168 {
169 fprintf(stderr,"%s lexical analyzer: %s\n",state, lexName);
170 }
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:61
@ Lex_doctokenizer
Definition debug.h:59
@ Lex
Definition debug.h:51
@ Rtf
Definition debug.h:43
@ NoLineNo
Definition debug.h:42
@ Lex_pre
Definition debug.h:64
@ Lex_defargs
Definition debug.h:58
@ Alias
Definition debug.h:46
@ Lex_pycode
Definition debug.h:65
@ Layout
Definition debug.h:50
@ Markdown
Definition debug.h:37
@ Lex_declinfo
Definition debug.h:57
@ Tag
Definition debug.h:45
@ Lex_lexscanner
Definition debug.h:63
@ FilterOutput
Definition debug.h:38
@ Lex_sqlcode
Definition debug.h:68
@ Lex_code
Definition debug.h:52
@ Lex_pyscanner
Definition debug.h:66
@ 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:70
@ Stderr
Definition debug.h:49
@ Plantuml
Definition debug.h:39
@ Formula
Definition debug.h:33
@ Lex_commentcnv
Definition debug.h:53
@ Lex_vhdlcode
Definition debug.h:69
@ Lex_constexp
Definition debug.h:56
@ Lex_xmlcode
Definition debug.h:71
@ Lex_fortrancode
Definition debug.h:60
@ Lex_commentscan
Definition debug.h:54
@ Lex_lexcode
Definition debug.h:62
@ Qhp
Definition debug.h:44
@ Entries
Definition debug.h:47
@ Lex_configimpl
Definition debug.h:55
@ Preprocessor
Definition debug.h:30
@ Lex_scanner
Definition debug.h:67
@ CommentCnv
Definition debug.h:31
@ FortranFixed2Free
Definition debug.h:40
@ CommentScan
Definition debug.h:32
static void printFlags()
Definition debug.cpp:137
static DebugMask curMask
Definition debug.h:92
static void setPriority(int p)
Definition debug.cpp:127
static int curPrio
Definition debug.h:93
static void clearFlag(const DebugMask mask)
Definition debug.cpp:122
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:132
static double elapsedTime()
Definition debug.cpp:201
static void startTimer()
Definition debug.cpp:196
static bool setFlagStr(const QCString &label)
Definition debug.cpp:103
static void print_(DebugMask mask, int prio, fmt::string_view fmt, fmt::format_args args)
Definition debug.cpp:81
static void setFlag(const DebugMask mask)
Definition debug.cpp:117
DebugLex(Debug::DebugMask mask, const char *lexName, const char *fileName)
Definition debug.cpp:146
~DebugLex()
Definition debug.cpp:151
Debug::DebugMask m_mask
Definition debug.h:105
QCString m_fileName
Definition debug.h:107
static void print(Debug::DebugMask mask, const char *state, const char *lexName, const char *fileName)
Definition debug.cpp:156
QCString m_lexName
Definition debug.h:106
This is an alternative implementation of QCString.
Definition qcstring.h:101
const std::string & str() const
Definition qcstring.h:537
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:76
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:95
static char asciiToLower(char in)
Definition debug.cpp:89
#define msg(fmt,...)
Definition message.h:94
Definition message.h:144
const char * qPrint(const char *s)
Definition qcstring.h:672