KSystemLog – system log viewer tool
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

itemFactory.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. /***************************************************************************
  2. * Copyright (C) 2005 by Nicolas Ternisien *
  3. * nicolas.ternisien@gmail.com *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; either version 2 of the License, or *
  8. * (at your option) any later version. *
  9. * *
  10. * This program is distributed in the hope that it will be useful, *
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  13. * GNU General Public License for more details. *
  14. * *
  15. * You should have received a copy of the GNU General Public License *
  16. * along with this program; if not, write to the *
  17. * Free Software Foundation, Inc., *
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
  19. ***************************************************************************/
  20. //KDE includes
  21. #include <kdebug.h>
  22. #include <kiconloader.h>
  23. #include <tdelocale.h>
  24. //KSystemLog includes
  25. #include "logLine.h"
  26. #include "childLogLine.h"
  27. #include "parentLogLine.h"
  28. #include "logListItem.h"
  29. #include "logMode.h"
  30. #include "itemFactory.h"
  31. LogListItem* ItemFactory::createLogListItem(TQListView* view, LogLine* line) {
  32. //If it is a parent Log Line
  33. if (line->isParentLogLine()==true)
  34. return(createParentItem(view, (ParentLogLine*)line));
  35. //If it's a child Log Line
  36. if (line->isChildLogLine()==true)
  37. return(createChildItem(view, (ChildLogLine*)line));
  38. /*
  39. //If it's a Samba mode item
  40. if (line->getType()==Globals::sambaMode->id) {
  41. kdDebug() << "Samba... Di Janero!!" << endl;
  42. LogListItem* item=createBasicItem(view, line);
  43. item->setMultiLinesEnabled(true);
  44. }
  45. */
  46. //It's a normal Log Line
  47. return(createBasicItem(view, line));
  48. }
  49. LogListItem* ItemFactory::createParentItem(TQListView* view, ParentLogLine* line) {
  50. //kdDebug() << "Creating a parent item : " << line->getLogLevel()->name << endl;
  51. LogListItem* item=new LogListItem(view, line);
  52. //item->setExpandable(true);
  53. //Group by Log Level
  54. if (line->getGroupBy()==GROUP_BY_LOG_LEVEL) {
  55. item->setText(0, line->getLogLevel()->name);
  56. item->setPixmap(0, line->getLogLevel()->pixmap);
  57. if (line->getLogLevel()==Globals::informationLogLevel) {
  58. item->setOpen(true);
  59. view->ensureItemVisible(item);
  60. }
  61. else {
  62. item->setOpen(false);
  63. }
  64. }
  65. //Group by Day
  66. else if (line->getGroupBy()==GROUP_BY_DAY) {
  67. //TODO Move these tests to a dedicated static method (to be able to reuse it)
  68. TQDate today=TQDate::currentDate();
  69. TQDate yesterday=today.addDays(-1);
  70. TQDate date=line->getTime().date();
  71. if (date==today) {
  72. item->setText(0, i18n("Today"));
  73. item->setOpen(true);
  74. view->ensureItemVisible(item);
  75. }
  76. else if (date==yesterday) {
  77. item->setText(0, i18n("Yesterday"));
  78. item->setOpen(false);
  79. }
  80. else {
  81. item->setText(0, date.toString());
  82. item->setOpen(false);
  83. }
  84. item->setPixmap(0, SmallIcon(GROUP_BY_DAY_ICON));
  85. }
  86. //Group by Hour
  87. else if (line->getGroupBy()==GROUP_BY_HOUR) {
  88. TQString string(i18n("%1, %2h").arg(line->getTime().date().toString(), line->getTime().time().toString("h")));
  89. item->setText(0, string);
  90. item->setPixmap(0, SmallIcon(GROUP_BY_HOUR_ICON));
  91. TQDate today=TQDate::currentDate();
  92. TQTime time=TQTime::currentTime();
  93. if (line->getTime().date()==today && line->getTime().time().hour()==time.hour()) {
  94. kdDebug() << "Date equals, hour equals" << TQString(line->getTime().date().toString()) << " and " << TQString(line->getTime().time().toString()) << endl;
  95. item->setOpen(true);
  96. view->ensureItemVisible(item);
  97. }
  98. else {
  99. item->setOpen(false);
  100. }
  101. }
  102. //Group by Day
  103. else if (line->getGroupBy()==GROUP_BY_LOG_FILE) {
  104. item->setText(0, line->getOriginalFile());
  105. item->setPixmap(0, SmallIcon(GROUP_BY_LOG_FILE_ICON));
  106. }
  107. //Group by a specific column
  108. else {
  109. int index=ParentLogLine::getGroupedColumnIndex(line->getColumns(), line->getGroupByColumn());
  110. TQStringList& list=line->getItemList();
  111. if (index<0 || index>=(int)list.size())
  112. item->setText(0, i18n("none"));
  113. else
  114. item->setText(0, list[index]);
  115. item->setPixmap(0, SmallIcon(GROUP_BY_COLUMN_ICON));
  116. }
  117. return(item);
  118. }
  119. LogListItem* ItemFactory::createChildItem(TQListView* /*view*/, ChildLogLine* line) {
  120. LogListItem* parent=line->getParent()->getLogListItem();
  121. if (parent==NULL)
  122. kdDebug() << "Parent log list item NULL !!!" << endl;
  123. //kdDebug() << "The log level of the parent is " << parent->getLogLine()->getLogLevel()->name << endl;
  124. LogListItem* item=new LogListItem(parent, line);
  125. initItem(item);
  126. return(item);
  127. }
  128. LogListItem* ItemFactory::createBasicItem(TQListView* view, LogLine* line) {
  129. LogListItem* item=new LogListItem(view, line);
  130. initItem(item);
  131. return(item);
  132. }
  133. void ItemFactory::initItem(LogListItem* item) {
  134. LogLine* line=item->getLogLine();
  135. //If it's a Xorg Log Line
  136. if (line->getType() == Globals::xorgMode->id) {
  137. initXorgItem(item);
  138. return;
  139. }
  140. //Default Item
  141. initDefaultItem(item);
  142. }
  143. void ItemFactory::initDefaultItem(LogListItem* item) {
  144. LogLine* line=item->getLogLine();
  145. item->setText(0, line->getTime().toString(Qt::LocalDate));
  146. int i=1;
  147. TQStringList& labels=line->getItemList();
  148. TQStringList::iterator it;
  149. for(it=labels.begin(); it!=labels.end(); ++it) {
  150. item->setText(i, *it);
  151. i++;
  152. }
  153. item->setPixmap(0, line->getLogLevel()->pixmap);
  154. }
  155. void ItemFactory::initXorgItem(LogListItem* item) {
  156. LogLine* line=item->getLogLine();
  157. item->setText(0, "");
  158. int i=1;
  159. TQStringList& labels=line->getItemList();
  160. TQStringList::iterator it;
  161. for(it=labels.begin(); it!=labels.end(); ++it) {
  162. item->setText(i, *it);
  163. i++;
  164. }
  165. item->setPixmap(0, line->getLogLevel()->pixmap);
  166. }
  167. TQString ItemFactory::createFormattedText(LogLine* line) {
  168. //Special case if this is a ParentLogLine (Group By feature)
  169. if (line->isParentLogLine())
  170. return(createParentFormattedText(line));
  171. if (line->getType() == Globals::cronMode->id)
  172. return(createCronFormattedText(line));
  173. if (line->getType() == Globals::xorgMode->id)
  174. return(createXorgFormattedText(line));
  175. if (line->getType() == Globals::acpidMode->id)
  176. return(createAcpidFormattedText(line));
  177. if (line->getType() == Globals::cupsMode->id)
  178. return(createCupsFormattedText(line));
  179. if (line->getType() == Globals::cupsAccessMode->id)
  180. return(createCupsAccessFormattedText(line));
  181. if (line->getType() == Globals::apacheMode->id)
  182. return(createApacheFormattedText(line));
  183. if (line->getType() == Globals::apacheAccessMode->id)
  184. return(createApacheAccessFormattedText(line));
  185. if (line->getType() == Globals::sambaMode->id)
  186. return(createSambaFormattedText(line));
  187. //Returns the default formating
  188. return(createDefaultFormattedText(line));
  189. }
  190. TQString ItemFactory::createToolTipText(LogLine* line) {
  191. if (line->getType() == Globals::xorgMode->id)
  192. return(createXorgToolTipText(line));
  193. else
  194. return(createDefaultToolTipText(line));
  195. }
  196. TQString ItemFactory::createParentFormattedText(LogLine* line) {
  197. TQString result;
  198. ParentLogLine* parent=(ParentLogLine*) line;
  199. //All Group By cases are showed here
  200. if (parent->getGroupBy()==GROUP_BY_LOG_LEVEL) {
  201. result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(line->getLogLevel()->name));
  202. }
  203. else if (parent->getGroupBy()==GROUP_BY_DAY) {
  204. result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(line->getTime().date().toString()));
  205. }
  206. else if (parent->getGroupBy()==GROUP_BY_HOUR) {
  207. TQString string(i18n("%1, %2 hour").arg(line->getTime().date().toString(), line->getTime().time().toString("h")));
  208. result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(string));
  209. }
  210. else if (parent->getGroupBy()==GROUP_BY_LOG_FILE) {
  211. result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(line->getOriginalFile()));
  212. }
  213. else {
  214. TQStringList& list=line->getItemList();
  215. int index=ParentLogLine::getGroupedColumnIndex(parent->getColumns(), parent->getGroupByColumn());
  216. if (index<0 || index>=(int) list.size())
  217. result.append(i18n("<div align='center'><b>Group:</b> none</div>"));
  218. else
  219. result.append(i18n("<div align='center'><b>Group:</b> %1</div>").arg(list[index]));
  220. }
  221. return(result);
  222. }
  223. TQString ItemFactory::createCronFormattedText(LogLine* line) {
  224. TQString result;
  225. TQStringList& items=line->getItemList();
  226. result.append("<table>");
  227. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  228. result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
  229. result.append(labelMessageFormat(i18n("Process:"), items[1]));
  230. result.append(labelMessageFormat(i18n("User:"), items[2]));
  231. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  232. result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
  233. result.append("</table>");
  234. return(result);
  235. }
  236. TQString ItemFactory::createDefaultFormattedText(LogLine* line) {
  237. TQString result;
  238. TQStringList& items=line->getItemList();
  239. result.append("<table>");
  240. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  241. result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
  242. result.append(labelMessageFormat(i18n("Process:"), items[1]));
  243. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  244. result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
  245. result.append("</table>");
  246. return(result);
  247. }
  248. TQString ItemFactory::createAcpidFormattedText(LogLine* line) {
  249. TQString result;
  250. TQStringList& items=line->getItemList();
  251. result.append("<table>");
  252. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  253. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  254. result.append(labelMessageFormat(i18n("Type:"), items[0]));
  255. result.append("</table>");
  256. return(result);
  257. }
  258. TQString ItemFactory::createCupsFormattedText(LogLine* line) {
  259. TQString result;
  260. result.append("<table>");
  261. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  262. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  263. result.append("</table>");
  264. return(result);
  265. }
  266. TQString ItemFactory::createCupsAccessFormattedText(LogLine* line) {
  267. TQString result;
  268. TQStringList& items=line->getItemList();
  269. result.append("<table>");
  270. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  271. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  272. result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
  273. result.append(labelMessageFormat(i18n("Identification:"), items[1]));
  274. result.append(labelMessageFormat(i18n("Username:"), items[2]));
  275. result.append(labelMessageFormat(i18n("HTTP Response:"), items[3]));
  276. result.append(labelMessageFormat(i18n("Bytes Sent:"), items[4]));
  277. result.append("</table>");
  278. return(result);
  279. }
  280. TQString ItemFactory::createApacheFormattedText(LogLine* line) {
  281. TQString result;
  282. TQStringList& items=line->getItemList();
  283. result.append("<table>");
  284. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  285. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  286. result.append(labelMessageFormat(i18n("Client:"), items[0]));
  287. result.append("</table>");
  288. return(result);
  289. }
  290. TQString ItemFactory::createApacheAccessFormattedText(LogLine* line) {
  291. TQString result;
  292. TQStringList& items=line->getItemList();
  293. result.append("<table>");
  294. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  295. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  296. result.append(labelMessageFormat(i18n("Hostname:"), items[0]));
  297. result.append(labelMessageFormat(i18n("Identification:"), items[1]));
  298. result.append(labelMessageFormat(i18n("Username:"), items[2]));
  299. result.append(labelMessageFormat(i18n("HTTP Response:"), items[3]));
  300. result.append(labelMessageFormat(i18n("Bytes Sent:"), items[4]));
  301. result.append(labelMessageFormat(i18n("Agent Identity:"), items[5]));
  302. result.append(labelMessageFormat(i18n("HTTP Request:"), items[6]));
  303. result.append("</table>");
  304. return(result);
  305. }
  306. TQString ItemFactory::createSambaFormattedText(LogLine* line) {
  307. TQString result;
  308. TQStringList& items=line->getItemList();
  309. result.append("<table>");
  310. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  311. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  312. result.append(labelMessageFormat(i18n("Source File:"), items[0]));
  313. result.append(labelMessageFormat(i18n("Function:"), items[1]));
  314. result.append(labelMessageFormat(i18n("Line:"), items[2]));
  315. result.append("</table>");
  316. return(result);
  317. }
  318. TQString ItemFactory::createXorgFormattedText(LogLine* line) {
  319. //It uses the same formating than the Tooltip
  320. return(createXorgToolTipText(line));
  321. }
  322. TQString ItemFactory::createDefaultToolTipText(LogLine* line) {
  323. TQString result;
  324. result.append("<table>");
  325. result.append(labelMessageFormat(i18n("Date:"), line->getTime().toString(Qt::LocalDate)));
  326. result.append(labelMessageFormat(i18n("Level:"), line->getLogLevel()->name));
  327. result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
  328. result.append("</table>");
  329. return(result);
  330. }
  331. TQString ItemFactory::createXorgToolTipText(LogLine* line) {
  332. TQString result;
  333. TQStringList& items=line->getItemList();
  334. result.append("<table>");
  335. if (items[0].isEmpty())
  336. result.append(labelMessageFormat(i18n("Type:"), i18n("none")));
  337. else
  338. result.append(labelMessageFormat(i18n("Type:"), items[0]));
  339. result.append(labelMessageFormat(i18n("Original file:"), line->getOriginalFile()));
  340. result.append("</table>");
  341. return(result);
  342. }
  343. TQString ItemFactory::labelMessageFormat(TQString label, TQString value) {
  344. return ("<tr><td align='right'><b><nobr>" + label + "</nobr></b></td><td>" + messageFormat(value) + "</td></tr>");
  345. }
  346. TQString ItemFactory::messageFormat(TQString& message) {
  347. TQString transformation(message);
  348. transformation.replace(TQRegExp("&"), "&amp;");
  349. transformation.replace(TQRegExp("<"), "&lt;");
  350. transformation.replace(TQRegExp(">"), "&gt;");
  351. return(transformation);
  352. }