1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
/*
* File: LogsWidget.cpp
*
* Author: Jacob Dekel
* Created on: July 1, 2010
*
* Copyright (c) 2010 Jacob Dekel
* $Id:
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "LogWidget.h"
#include "Preferences.h"
#include "HerculesStudio.h"
#include <QTabWidget>
#include <QFile>
#include <QTextBlock>
#include <QTextStream>
#include <QClipboard>
#include <QApplication>
#include <fcntl.h>
#include <time.h>
#ifndef hFramework
#include <sys/time.h>
#endif
#define for_each_log \
for (int for_loop_i=0; for_loop_i<2; for_loop_i++)
#define current_log mLogs[for_loop_i]
PlainLogWidget::PlainLogWidget(QWidget * parent, const char * suffix)
:QTextEdit(parent), mGreen(10,120,10), mYellow(215,201,45), mRed(240,20,20), mBlack(0,0,0), mWhite(250,250,250), mLinesWritten(0)
{
mSaveLog = Preferences::getInstance().autosaveLog();
mLogFileLines = Preferences::getInstance().logFileLines();
QString parm(suffix);
setLogFileName(parm);
mIpled = false;
mDarkBackground = Preferences::getInstance().darkBackground();
connect(this, SIGNAL(copyAvailable(bool)), this, SLOT(keepSelection(bool)));
connect(this, SIGNAL(logCopy()), this, SLOT(copy()));
}
void PlainLogWidget::setLogFileName (QString& suffix)
{
getTimeStamp(true);
mLogFileName = Preferences::getInstance().logsDir().c_str();
mLogFileName += "/hercules";
mLogFileName += mTimeStamp;
if (suffix.length() != 0)
{
mLogFileName += ".";
mLogFileName += suffix;
}
mLogFileName += ".log";
}
void PlainLogWidget::append(const QByteArray & text)
{
if (Preferences::getInstance().logTimestamp())
{
getTimeStamp(false);
}
else
mTimeStamp[0] = '\0';
QByteArray s = text;
if (text.data()[0] == '<')
s = text.mid(24);
QColor keepC = textColor();
if (mDarkBackground)
{
if (strncmp(text.data(),"HHC",3) == 0)
{
if (text[8] == 'I')
setTextColor(mGreen);
else if (text.mid(8,1) =="W")
setTextColor(mYellow);
else if (text.mid(8,1) =="E")
setTextColor(mRed);
}
else
setTextColor(mWhite);
}
else
{
if (strncmp(text.data(),"HHC",3) == 0)
{
if (text[8] == 'I')
setTextColor(mGreen);
else if (text.mid(8,1) =="W")
setTextColor(mYellow);
else if (text.mid(8,1) =="E")
setTextColor(mRed);
}
else
setTextColor(mBlack);
}
int len = s.length()-1;
if (s[len] == '\n') s[len] = '\0';
QTextEdit::append(QByteArray(mTimeStamp) + s);
setTextColor(keepC);
if (QTextEdit::document()->blockCount()%mLogFileLines == 0)
{
writeToFile(Overflow);
}
}
void PlainLogWidget::getTimeStamp(bool withDate)
{
struct tm *current;
time_t now;
time(&now);
current = localtime(&now);
if (withDate)
strftime(mTimeStamp, 255, "%Y-%m-%d-%H-%M-%S", current);
else
{
strftime(mTimeStamp, 255, "%H:%M:%S", current); //"%m-%d-%y %H:%M:%S"
memcpy(mTimeStamp+8, " ", 2);
}
}
QString PlainLogWidget::toPlainText()
{
return QTextEdit::toPlainText();
}
bool PlainLogWidget::isOSLog()
{
return false;
}
void PlainLogWidget::writeToFile(WriteType type)
{
QTextDocument *oldDocument = QTextEdit::document();
if (mSaveLog || type == MenuCommand)
{
QFile file(mLogFileName);
if (!file.open(QIODevice::Append | QIODevice::Text))
{
hOutDebug(0,"Failed to open file " << mLogFileName.toStdString());
return ;
}
QTextStream out(&file);
QTextBlock block = oldDocument->begin();
// skip lines that were already written
long linecount = 0;
while(linecount < mLinesWritten && block != oldDocument->end())
{
block=block.next();
linecount++;
}
// write new lines
while(block != oldDocument->end())
{
out << block.text().toUtf8().data() << "\n";
mLinesWritten++;
block=block.next();
}
file.close();
}
if (type == Overflow)
{
oldDocument->clear();
QTextDocument *newBlock = new QTextDocument(this);
QTextEdit::setDocument(newBlock);
mLinesWritten = 0;
getTimeStamp(true);
QString sepLine = "---------------- log was saved at ";
sepLine += mTimeStamp;
sepLine += "-------------------------";
QTextEdit::append(sepLine);
}
}
void PlainLogWidget::preferencesChanged()
{
mLogFileLines = Preferences::getInstance().logFileLines(); // might have been updated
bool prevDarkBackground = mDarkBackground;
mDarkBackground = Preferences::getInstance().darkBackground();
if (mDarkBackground != prevDarkBackground)
{
QBrush brush(QColor(180,0,0));
for (QTextBlock tb=QTextEdit::document()->begin();
tb != QTextEdit::document()->end();
tb = tb.next())
{
tb.blockFormat().setForeground(mGreen);
}
}
}
void PlainLogWidget::setIpled(bool ipled)
{
mIpled = ipled;
}
void PlainLogWidget::clear()
{
QTextEdit::clear();
}
void PlainLogWidget::keepSelection(bool selected)
{
hOutDebug(4, "copyAvailable " << (selected?"y":"f"));
mSelectedTextExists = selected;
}
bool PlainLogWidget::textIsSelected()
{
return mSelectedTextExists;
}
QString PlainLogWidget::copySelectedText()
{
if (mSelectedTextExists)
{
emit logCopy();
hOutDebug(4, "clipboard: '" << QApplication::clipboard()->text().toStdString() << "'");
}
return QApplication::clipboard()->text();
}
LogWidget::LogWidget(QWidget * parent)
: PlainLogWidget(NULL), cHercIndex(0), cOsIndex(1)
{
mTabWidget = new QTabWidget(parent);
mLogs[cHercIndex] = new PlainLogWidget(this, "hercules");
mLogs[cOsIndex] = new PlainLogWidget(this, "os");
mTabWidget->addTab(mLogs[cHercIndex], "Hercules");
mTabWidget->addTab(mLogs[cOsIndex], "OS");
for_each_log
{
current_log->setReadOnly(true);
current_log->setVisible(true);
}
mIpled = false;
}
LogWidget::~LogWidget()
{
}
QTabWidget * LogWidget::tabWidget()
{
return mTabWidget;
}
void LogWidget::clear()
{
for_each_log
current_log->clear();
}
void LogWidget::setReadOnly(bool ro)
{
for_each_log
current_log->setReadOnly(ro);
}
void LogWidget::append(const QByteArray & text)
{
QByteArray s = text;
hOutDebug(3, s.data());
if (text.data()[0] == '<')
s = text.mid(24);
if (text.startsWith("HHC") || !mIpled)
mLogs[cHercIndex]->append(s);
else
mLogs[cOsIndex]->append(s);
}
void LogWidget::setFont(const QFont & font)
{
for_each_log
current_log->setFont(font);
}
void LogWidget::setTextBackgroundColor (const QColor &color)
{
for_each_log
current_log->setTextBackgroundColor(color);
}
void LogWidget::setTextColor (const QColor &color)
{
for_each_log
current_log->setTextColor(color);
}
QString LogWidget::toPlainText()
{
QString ret;
for_each_log
ret += current_log->toPlainText();
return ret;
}
bool LogWidget::empty()
{
return mLogs[cHercIndex]->document()->isEmpty() && mLogs[cOsIndex]->document()->isEmpty();
}
bool LogWidget::isOSLog()
{
return (mTabWidget->currentIndex() == 1);
}
void LogWidget::writeToFile(WriteType writeType)
{
for_each_log
current_log->writeToFile(writeType);
}
void LogWidget::preferencesChanged()
{
for_each_log
current_log->preferencesChanged();
}
void LogWidget::setIpled(bool ipled)
{
mIpled = ipled;
}
bool LogWidget::textIsSelected()
{
return mLogs[mTabWidget->currentIndex()]->textIsSelected();
}
QString LogWidget::copySelectedText()
{
return mLogs[mTabWidget->currentIndex()]->copySelectedText();
}
|