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