1 /*
2 Bacula(R) - The Network Backup Solution
3
4 Copyright (C) 2000-2020 Kern Sibbald
5
6 The original author of Bacula is Kern Sibbald, with contributions
7 from many others, a complete list can be found in the file AUTHORS.
8
9 You may use this file and others of this release according to the
10 license defined in the LICENSE file, which includes the Affero General
11 Public License, v3.0 ("AGPLv3") and some additional permissions and
12 terms pursuant to its AGPLv3 Section 7.
13
14 This notice must be preserved when any source code is
15 conveyed and/or propagated.
16
17 Bacula(R) is a registered trademark of Kern Sibbald.
18 */
19
20 /*
21 * JobLog Class
22 *
23 * Dirk Bartley, March 2007
24 *
25 */
26
27 #include "bat.h"
28 #include "joblog.h"
29
JobLog(QString & jobId,QTreeWidgetItem * parentTreeWidgetItem)30 JobLog::JobLog(QString &jobId, QTreeWidgetItem *parentTreeWidgetItem) : Pages()
31 {
32 setupUi(this);
33 pgInitialize(tr("JobLog"), parentTreeWidgetItem);
34 QTreeWidgetItem* thisitem = mainWin->getFromHash(this);
35 thisitem->setIcon(0,QIcon(QString::fromUtf8(":images/joblog.png")));
36 m_cursor = new QTextCursor(textEdit->document());
37
38 m_jobId = jobId;
39 getFont();
40 populateText();
41
42 dockPage();
43 setCurrent();
44 }
45
getFont()46 void JobLog::getFont()
47 {
48 QFont font = textEdit->font();
49
50 QString dirname;
51 m_console->getDirResName(dirname);
52 QSettings settings(dirname, "bat");
53 settings.beginGroup("Console");
54 font.setFamily(settings.value("consoleFont", "Courier").value<QString>());
55 font.setPointSize(settings.value("consolePointSize", 10).toInt());
56 font.setFixedPitch(settings.value("consoleFixedPitch", true).toBool());
57 settings.endGroup();
58 textEdit->setFont(font);
59 }
60
61 /*
62 * Populate the text in the window
63 */
populateText()64 void JobLog::populateText()
65 {
66 QString query;
67 query = "SELECT Time, LogText FROM Log WHERE JobId='" + m_jobId + "' order by Time";
68
69 /* This could be a log item */
70 if (mainWin->m_sqlDebug) {
71 Pmsg1(000, "Log query cmd : %s\n", query.toUtf8().data());
72 }
73
74 QStringList results;
75 if (m_console->sql_cmd(query, results)) {
76
77 if (!results.size()) {
78 QMessageBox::warning(this, tr("Bat"),
79 tr("There were no results!\n"
80 "It is possible you may need to add \"catalog = all\" "
81 "to the Messages resource for this job.\n"), QMessageBox::Ok);
82 return;
83 }
84
85 QString jobstr("JobId "); /* FIXME: should this be translated ? */
86 jobstr += m_jobId;
87
88 QString htmlbuf("<html><body><b>" + tr("Log records for job %1").arg(m_jobId) );
89 htmlbuf += "</b><table>";
90
91 /* Iterate through the lines of results. */
92 QString field;
93 QStringList fieldlist;
94 QString lastTime;
95 QString lastSvc;
96 foreach (QString resultline, results) {
97 fieldlist = resultline.split("\t");
98
99 if (fieldlist.size() < 2)
100 continue;
101
102 htmlbuf +="<tr>";
103
104 QString curTime = fieldlist[0].trimmed();
105
106 field = fieldlist[1].trimmed();
107 int colon = field.indexOf(":");
108 if (colon > 0) {
109 /* string is like <service> <jobId xxxx>: ..."
110 * we split at ':' then remove the jobId xxxx string (always the same) */
111 QString curSvc(field.left(colon).replace(jobstr,"").trimmed());
112 if (curSvc == lastSvc && curTime == lastTime) {
113 curTime.clear();
114 curSvc.clear();
115 } else {
116 lastTime = curTime;
117 lastSvc = curSvc;
118 }
119 htmlbuf += "<td>" + curTime + "</td>";
120 htmlbuf += "<td><p>" + curSvc + "</p></td>";
121
122 /* rest of string is marked as pre-formatted (here trimming should
123 * be avoided, to preserve original formatting) */
124 QString msg(field.mid(colon+2));
125 if (msg.startsWith( tr("Error:")) ) { /* FIXME: should really be translated ? */
126 /* error msg, use a specific class */
127 htmlbuf += "<td><pre class=err>" + msg + "</pre></td>";
128 } else {
129 htmlbuf += "<td><pre>" + msg + "</pre></td>";
130 }
131 } else {
132 /* non standard string, place as-is */
133 if (curTime == lastTime) {
134 curTime.clear();
135 } else {
136 lastTime = curTime;
137 }
138 htmlbuf += "<td>" + curTime + "</td>";
139 htmlbuf += "<td><pre>" + field + "</pre></td>";
140 }
141
142 htmlbuf += "</tr>";
143
144 } /* foreach resultline */
145
146 htmlbuf += "</table></body></html>";
147
148 /* full text ready. Here a custom sheet is used to align columns */
149 QString logSheet("p,pre,.err {margin-left: 10px} .err {color:#FF0000;}");
150 textEdit->document()->setDefaultStyleSheet(logSheet);
151 textEdit->document()->setHtml(htmlbuf);
152 textEdit->moveCursor(QTextCursor::Start);
153
154 } /* if results from query */
155
156 }
157
158