~ubuntu-branches/ubuntu/quantal/akonadi/quantal

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
/*
    Copyright (c) 2008 Volker Krause <vkrause@kde.org>

    Inspired by kdelibs/kdecore/io/kdebug.h
    Copyright (C) 1997 Matthias Kalle Dalheimer (kalle@kde.org)
                  2002 Holger Freyther (freyther@kde.org)

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library 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 Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "akdebug.h"
#include "akcrash.h"
#include "akstandarddirs.h"

#include <libs/xdgbasedirs_p.h>
using Akonadi::XdgBaseDirs;

#include <QDir>
#include <QFile>
#include <QFileInfo>
#include <QMutex>

#include <cassert>

class FileDebugStream : public QIODevice
{
  public:
    FileDebugStream() :
      mType( QtCriticalMsg )
    {
      open( WriteOnly );
    }

    bool isSequential() const { return true; }
    qint64 readData(char *, qint64) { return 0;  }
    qint64 readLineData(char *, qint64) { return 0; }
    qint64 writeData(const char *data, qint64 len)
    {
      QByteArray buf = QByteArray::fromRawData(data, len);

      if ( !mFileName.isEmpty() ) {
        QFile outputFile( mFileName );
        outputFile.open( QIODevice::WriteOnly | QIODevice::Append | QIODevice::Unbuffered );
        outputFile.write( data, len );
        outputFile.putChar( '\n' );
        outputFile.close();
      }

      qt_message_output( mType, buf.trimmed().constData() );
      return len;
    }

    void setFileName( const QString &fileName )
    {
      mFileName = fileName;
    }

    void setType( QtMsgType type )
    {
      mType = type;
    }
  private:
    QString mFileName;
    QtMsgType mType;
};

class DebugPrivate
{
  public:
    DebugPrivate() :
      fileStream( new FileDebugStream() )
    {
    }

    ~DebugPrivate()
    {
      delete fileStream;
    }

    QString errorLogFileName() const
    {
      return AkStandardDirs::saveDir( "data" )
          + QDir::separator()
          + name
          + QString::fromLatin1( ".error" );
    }

    QDebug stream( QtMsgType type )
    {
      QMutexLocker locker( &mutex );
      if ( type == QtDebugMsg )
        return qDebug();
      fileStream->setType( type );
      return QDebug( fileStream );
    }

    void setName( const QString &appName )
    {
      // Keep only the executable name, e.g. akonadi_control
      name = appName.mid( appName.lastIndexOf( QLatin1Char('/') ) + 1 );
      fileStream->setFileName( errorLogFileName() );
    }

    QMutex mutex;
    FileDebugStream *fileStream;
    QString name;
};

Q_GLOBAL_STATIC( DebugPrivate, sInstance )

QDebug akFatal()
{
  return sInstance()->stream( QtFatalMsg );
}

QDebug akError()
{
  return sInstance()->stream( QtCriticalMsg );
}

QDebug akDebug()
{
  return sInstance()->stream( QtDebugMsg );
}

void akInit( const QString &appName )
{
  AkonadiCrash::init();
  sInstance()->setName( appName );

  QFileInfo infoOld( sInstance()->errorLogFileName() + QString::fromLatin1(".old") );
  if ( infoOld.exists() ) {
    QFile fileOld( infoOld.absoluteFilePath() );
    const bool success = fileOld.remove();
    if ( !success )
      qFatal( "Cannot remove old log file - running on a readlony filesystem maybe?" );
  }
  QFileInfo info( sInstance()->errorLogFileName() );
  if ( info.exists() ) {
    QFile file( info.absoluteFilePath() );
    const bool success = file.rename( sInstance()->errorLogFileName() + QString::fromLatin1(".old") );
    if ( !success )
      qFatal( "Cannot rename log file - running on a readonly filesystem maybe?" );
  }
}

QString getEnv( const char *name, const QString &defaultValue )
{
  const QString v = QString::fromLocal8Bit( qgetenv( name ) );
  return !v.isEmpty() ? v : defaultValue;
}