denisq

log.cpp

Jul 20th, 2014
222
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * log.cpp
  3.  *
  4.  * Copyright (C) 2011 Christoph Pfister <christophpfister@gmail.com>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License along
  17.  * with this program; if not, write to the Free Software Foundation, Inc.,
  18.  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19.  */
  20.  
  21. #include "log.h"
  22.  
  23. #include <QMutex>
  24. #include <QTextCodec>
  25. #include <QTime>
  26. #include <stdio.h>
  27.  
  28. class LogPrivate
  29. {
  30. public:
  31.     LogPrivate() : position(0)
  32.     {
  33.         buffer.reserve(8176);
  34.     }
  35.  
  36.     ~LogPrivate()
  37.     {
  38.     }
  39.  
  40.     void begin(const char *message)
  41.     {
  42.         mutex.lock();
  43.  
  44.         if (buffer.size() > (8176 - 1024)) {
  45.             buffer.remove(0, buffer.indexOf(QLatin1Char('\n'), 1023) + 1);
  46.         }
  47.  
  48.         position = buffer.size();
  49.         buffer.append(QTime::currentTime().toString(Qt::ISODate));
  50.         buffer.append(QLatin1Char(' '));
  51.         buffer.append(QLatin1String(message));
  52.     }
  53.  
  54.     void append(qint64 value)
  55.     {
  56.         buffer.append(QLatin1Char(' '));
  57.         buffer.append(QString::number(value));
  58.     }
  59.  
  60.     void append(quint64 value)
  61.     {
  62.         buffer.append(QLatin1Char(' '));
  63.         buffer.append(QString::number(value));
  64.     }
  65.  
  66.     void append(const QString &string)
  67.     {
  68.         buffer.append(QLatin1String(" \""));
  69.         buffer.append(string);
  70.         buffer.append(QLatin1Char('"'));
  71.     }
  72.  
  73.     void end()
  74.     {
  75.         buffer.append(QLatin1Char('\n'));
  76.         fprintf(stderr, "%s", QTextCodec::codecForLocale()->fromUnicode(
  77.             buffer.constData() + position, buffer.size() - position).constData());
  78.         mutex.unlock();
  79.     }
  80.  
  81.     QMutex mutex;
  82.     QString buffer;
  83.     int position;
  84. };
  85.  
  86. QString Log::getLog()
  87. {
  88.     if (data != NULL) {
  89.         QMutexLocker locker(&data->mutex);
  90.         return data->buffer;
  91.     }
  92.  
  93.     return QString();
  94. }
  95.  
  96. void Log::begin(const char *message)
  97. {
  98.     if (data == NULL) {
  99.         LogPrivate *newData = new LogPrivate();
  100.  
  101.         if (!data.testAndSetOrdered(NULL, newData)) {
  102.             // another thread won the battle
  103.             delete newData;
  104.         }
  105.     }
  106.  
  107.     data->begin(message);
  108. }
  109.  
  110. void Log::append(qint64 value)
  111. {
  112.     data->append(value);
  113. }
  114.  
  115. void Log::append(quint64 value)
  116. {
  117.     data->append(value);
  118. }
  119.  
  120. void Log::append(const QString &string)
  121. {
  122.     data->append(string);
  123. }
  124.  
  125. void Log::end()
  126. {
  127.     data->end();
  128. }
  129.  
  130. QBasicAtomicPointer<LogPrivate> Log::data = Q_BASIC_ATOMIC_INITIALIZER(0);
RAW Paste Data