Line data Source code
1 : // Snap Websites Servers -- handle messages for QXmlQuery
2 : // Copyright (c) 2014-2019 Made to Order Software Corp. All Rights Reserved
3 : //
4 : // This program is free software; you can redistribute it and/or modify
5 : // it under the terms of the GNU General Public License as published by
6 : // the Free Software Foundation; either version 2 of the License, or
7 : // (at your option) any later version.
8 : //
9 : // This program is distributed in the hope that it will be useful,
10 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : // GNU General Public License for more details.
13 : //
14 : // You should have received a copy of the GNU General Public License
15 : // along with this program; if not, write to the Free Software
16 : // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 :
18 :
19 : // self
20 : //
21 : #include "snapwebsites/qxmlmessagehandler.h"
22 :
23 :
24 : // snapwebsites lib
25 : //
26 : #include "snapwebsites/qstring_stream.h"
27 : #include "snapwebsites/log.h"
28 : #include "snapwebsites/snap_exception.h"
29 :
30 :
31 : // Qt lib
32 : //
33 : #include <QDomDocument>
34 : #include <QFile>
35 :
36 :
37 : // included last
38 : //
39 : #include <snapdev/poison.h>
40 :
41 :
42 :
43 :
44 : namespace snap
45 : {
46 :
47 :
48 0 : QMessageHandler::QMessageHandler(QObject *parent_object)
49 0 : : QAbstractMessageHandler(parent_object)
50 : //, f_had_msg(false) -- auto-init
51 : {
52 0 : }
53 :
54 :
55 : #pragma GCC diagnostic push
56 : #pragma GCC diagnostic ignored "-Wunused-parameter"
57 0 : void QMessageHandler::handleMessage(QtMsgType type, QString const & description, QUrl const & identifier, QSourceLocation const & sourceLocation)
58 : {
59 0 : QDomDocument doc("description");
60 0 : doc.setContent(description, true, nullptr, nullptr, nullptr);
61 0 : QDomElement root(doc.documentElement());
62 : // TODO: note that the description may include <span>, <b>, <i>, ...
63 0 : f_error_description = root.text();
64 0 : f_error_type = type;
65 :
66 0 : if(f_error_type == QtFatalMsg
67 0 : && f_error_description.startsWith("Entity")
68 0 : && f_error_description.endsWith("not declared."))
69 : {
70 0 : f_has_entities = true;
71 0 : return;
72 : }
73 :
74 : //std::cerr << "URI A = [" << identifier.toString() << "]\n";
75 : //std::cerr << "URI B = [" << sourceLocation.uri().toString() << "]\n";
76 : //std::cerr << "MESSAGE = [" << f_error_description << "]\n";
77 :
78 : // avoid "variable unused" warnings
79 0 : if(type != QtWarningMsg
80 0 : || !f_error_description.startsWith("The variable")
81 0 : || !f_error_description.endsWith("is unused"))
82 : {
83 : // TODO: determine whether QtDebugMsg should not turn this flag
84 : // on; although I'm not too sure how you get debug messages
85 : // in the first place...
86 0 : f_had_msg = true;
87 :
88 0 : char const * type_msg(nullptr);
89 0 : logging::log_level_t level(logging::log_level_t::LOG_LEVEL_OFF);
90 0 : switch(type)
91 : {
92 0 : case QtDebugMsg:
93 0 : type_msg = "debug";
94 0 : level = logging::log_level_t::LOG_LEVEL_DEBUG;
95 0 : break;
96 :
97 0 : case QtWarningMsg:
98 0 : type_msg = "warning";
99 0 : level = logging::log_level_t::LOG_LEVEL_WARNING;
100 0 : break;
101 :
102 0 : case QtCriticalMsg:
103 0 : type_msg = "critical";
104 0 : level = logging::log_level_t::LOG_LEVEL_ERROR;
105 0 : break;
106 :
107 : //case QtFatalMsg:
108 0 : default:
109 0 : type_msg = "fatal error";
110 0 : level = logging::log_level_t::LOG_LEVEL_FATAL;
111 0 : break;
112 :
113 : }
114 :
115 : {
116 0 : logging::logger l(level, __FILE__, __func__, __LINE__);
117 0 : l.operator () (type_msg)(":");
118 0 : QString const location(sourceLocation.uri().toString());
119 0 : if(!location.isEmpty())
120 : {
121 0 : l.operator () (location)(":");
122 : }
123 0 : if(sourceLocation.line() != 0)
124 : {
125 0 : l.operator () ("line #")(sourceLocation.line())(":");
126 : }
127 0 : if(sourceLocation.column() != 0)
128 : {
129 0 : l.operator () ("column #")(sourceLocation.column())(":");
130 : }
131 0 : l.operator () (" ")(f_error_description);
132 0 : if(!f_xsl.isEmpty())
133 : {
134 : #ifdef DEBUG
135 0 : l.operator () (" XSLT Script:\n[")(f_xsl)("]\n");
136 : static int count(0);
137 0 : QFile file_xsl(QString("/tmp/error%1-query.xsl").arg(count));
138 0 : file_xsl.open(QIODevice::WriteOnly);
139 0 : file_xsl.write(f_xsl.toUtf8());
140 0 : file_xsl.close();
141 :
142 0 : l.operator () (" in memory XML document:\n[")(f_doc)("]\n");
143 0 : QFile file_xml(QString("/tmp/error%1-document.xml").arg(count));
144 0 : file_xml.open(QIODevice::WriteOnly);
145 0 : file_xml.write(f_doc.toUtf8());
146 0 : file_xml.close();
147 0 : ++count;
148 :
149 : // to actually know who called the QXmlQuery function
150 0 : snap_exception_base::output_stack_trace(100);
151 : #else
152 : l.operator () (" Beginning of the XSLT script involved:\n")(f_xsl.left(200))
153 : ("\nBeginning of the XML script involved:\n")(f_doc.left(200));
154 : #endif
155 : }
156 : } // print log
157 : }
158 : }
159 : #pragma GCC diagnostic pop
160 :
161 :
162 6 : } // namespace snap
163 : // vim: ts=4 sw=4 et
|