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,const char *fmt,...)
82{
83 if ((curMask&mask) && prio<=curPrio)
84 {
85 va_list args;
86 va_start(args,fmt);
87 vfprintf(g_debugFile, fmt, args);
88 va_end(args);
89 }
90}
91
92static char asciiToLower(char in) {
93 if (in <= 'Z' && in >= 'A')
94 return in - ('Z' - 'z');
95 return in;
96}
97
98static uint64_t labelToEnumValue(const QCString &l)
99{
100 std::string s = l.str();
101 std::transform(s.begin(),s.end(),s.begin(),asciiToLower);
102 auto it = s_labels.find(s);
103 return (it!=s_labels.end()) ? it->second : Debug::DebugMask::Quiet;
104}
105
107{
108 uint64_t retVal = labelToEnumValue(lab);
109 if (retVal == Debug::Stderr)
110 {
111 g_debugFile = stderr;
112 }
113 else
114 {
115 curMask = static_cast<DebugMask>(curMask | retVal);
116 }
117 return retVal!=0;
118}
119
120void Debug::setFlag(const DebugMask mask)
121{
122 curMask = static_cast<DebugMask>(curMask | mask);
123}
124
126{
127 curMask = static_cast<DebugMask>(curMask & ~mask);
128}
129
131{
132 curPrio = p;
133}
134
136{
137 return (curMask & mask)!=0;
138}
139
141{
142 for (const auto &v : s_labels)
143 {
144 msg("\t%s\n",v.first.c_str());
145 }
146}
147
148//------------------------------------------------------------------------
149DebugLex::DebugLex(Debug::DebugMask mask,const char *lexName,const char *fileName) : m_mask(mask), m_lexName(lexName), m_fileName(fileName)
150{
152}
153
158
159void DebugLex::print(Debug::DebugMask mask,const char *state,const char *lexName,const char *fileName)
160{
161 if (fileName && *fileName)
162 {
163 if (Debug::isFlagSet(mask))
164 {
165 fprintf(stderr,"%s lexical analyzer: %s (for: %s)\n",state, lexName, fileName);
166 }
167 }
168 else
169 {
170 if (Debug::isFlagSet(mask))
171 {
172 fprintf(stderr,"%s lexical analyzer: %s\n",state, lexName);
173 }
174 }
175}
176
177//------------------------------------------------------------------------
178
179class Timer
180{
181 public:
182 void start()
183 {
184 m_startTime = std::chrono::steady_clock::now();
185 }
187 {
188 return static_cast<double>(
189 std::chrono::duration_cast<
190 std::chrono::microseconds>(
191 std::chrono::steady_clock::now() - m_startTime).count()) / 1000000.0;
192 }
193 private:
194 std::chrono::time_point<std::chrono::steady_clock> m_startTime;
195};
196
198
200{
202}
203
205{
207}
208
DebugMask
Definition debug.h:27
@ Lex_fortranscanner
Definition debug.h:60
@ Lex_doctokenizer
Definition debug.h:58
@ Lex
Definition debug.h:50
@ Rtf
Definition debug.h:42
@ NoLineNo
Definition debug.h:41
@ Lex_pre
Definition debug.h:63
@ Lex_defargs
Definition debug.h:57
@ Alias
Definition debug.h:45
@ Lex_pycode
Definition debug.h:64
@ Layout
Definition debug.h:49
@ Markdown
Definition debug.h:36
@ Lex_declinfo
Definition debug.h:56
@ Tag
Definition debug.h:44
@ Lex_lexscanner
Definition debug.h:62
@ FilterOutput
Definition debug.h:37
@ Lex_sqlcode
Definition debug.h:67
@ Lex_code
Definition debug.h:51
@ Lex_pyscanner
Definition debug.h:65
@ Cite
Definition debug.h:40
@ ExtCmd
Definition debug.h:35
@ Sections
Definition debug.h:47
@ PrintTree
Definition debug.h:33
@ Time
Definition debug.h:34
@ Quiet
Definition debug.h:28
@ Lex_xml
Definition debug.h:69
@ Stderr
Definition debug.h:48
@ Plantuml
Definition debug.h:38
@ Formula
Definition debug.h:32
@ Lex_commentcnv
Definition debug.h:52
@ Lex_vhdlcode
Definition debug.h:68
@ Lex_constexp
Definition debug.h:55
@ Lex_xmlcode
Definition debug.h:70
@ Lex_fortrancode
Definition debug.h:59
@ Lex_commentscan
Definition debug.h:53
@ Lex_lexcode
Definition debug.h:61
@ Qhp
Definition debug.h:43
@ Entries
Definition debug.h:46
@ Lex_configimpl
Definition debug.h:54
@ Preprocessor
Definition debug.h:29
@ Lex_scanner
Definition debug.h:66
@ CommentCnv
Definition debug.h:30
@ FortranFixed2Free
Definition debug.h:39
@ CommentScan
Definition debug.h:31
static void printFlags()
Definition debug.cpp:140
static DebugMask curMask
Definition debug.h:85
static void print(DebugMask mask, int prio, const char *fmt,...)
Definition debug.cpp:81
static void setPriority(int p)
Definition debug.cpp:130
static int curPrio
Definition debug.h:86
static void clearFlag(const DebugMask mask)
Definition debug.cpp:125
static bool isFlagSet(const DebugMask mask)
Definition debug.cpp:135
static double elapsedTime()
Definition debug.cpp:204
static void startTimer()
Definition debug.cpp:199
static bool setFlagStr(const QCString &label)
Definition debug.cpp:106
static void setFlag(const DebugMask mask)
Definition debug.cpp:120
DebugLex(Debug::DebugMask mask, const char *lexName, const char *fileName)
Definition debug.cpp:149
~DebugLex()
Definition debug.cpp:154
Debug::DebugMask m_mask
Definition debug.h:98
QCString m_fileName
Definition debug.h:100
static void print(Debug::DebugMask mask, const char *state, const char *lexName, const char *fileName)
Definition debug.cpp:159
QCString m_lexName
Definition debug.h:99
This is an alternative implementation of QCString.
Definition qcstring.h:101
const std::string & str() const
Definition qcstring.h:526
void start()
Definition debug.cpp:182
std::chrono::time_point< std::chrono::steady_clock > m_startTime
Definition debug.cpp:194
double elapsedTimeS()
Definition debug.cpp:186
static FILE * g_debugFile
Definition debug.cpp:76
static Timer g_runningTime
Definition debug.cpp:197
static std::map< std::string, Debug::DebugMask > s_labels
Definition debug.cpp:29
static uint64_t labelToEnumValue(const QCString &l)
Definition debug.cpp:98
static char asciiToLower(char in)
Definition debug.cpp:92
void msg(const char *fmt,...)
Definition message.cpp:98
Definition trace.h:153
const char * qPrint(const char *s)
Definition qcstring.h:661