• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdepim API Reference
  • KDE Home
  • Contact Us
 

kleopatra

  • sources
  • kde-4.14
  • kdepim
  • kleopatra
  • utils
log.cpp
Go to the documentation of this file.
1 /* -*- mode: c++; c-basic-offset:4 -*-
2  utils/log.cpp
3 
4  This file is part of Kleopatra, the KDE keymanager
5  Copyright (c) 2008 Klarälvdalens Datakonsult AB
6 
7  Kleopatra is free software; you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation; either version 2 of the License, or
10  (at your option) any later version.
11 
12  Kleopatra is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 
21  In addition, as a special exception, the copyright holders give
22  permission to link the code of this program with any edition of
23  the Qt library by Trolltech AS, Norway (or with modified versions
24  of Qt that use the same license as Qt), and distribute linked
25  combinations including the two. You must obey the GNU General
26  Public License in all respects for all of the code used other than
27  Qt. If you modify this file, you may extend this exception to
28  your version of the file, but you are not obligated to do so. If
29  you do not wish to do so, delete this exception statement from
30  your version.
31 */
32 
33 #include <config-kleopatra.h>
34 
35 #include "log.h"
36 #include "iodevicelogger.h"
37 
38 #include <kleo/exception.h>
39 
40 #include <KLocalizedString>
41 #include <KRandom>
42 
43 #include <QDateTime>
44 #include <QDir>
45 #include <QFile>
46 #include <QString>
47 
48 #ifndef Q_MOC_RUN
49 #include <boost/weak_ptr.hpp>
50 #endif
51 #include <cassert>
52 #include <cstdio>
53 
54 using namespace boost;
55 using namespace Kleo;
56 
57 class Log::Private {
58  Log* const q;
59 public:
60  explicit Private( Log* qq ) : q( qq ), m_ioLoggingEnabled( false ), m_logFile( 0 ) {}
61  ~Private();
62  bool m_ioLoggingEnabled;
63  QString m_outputDirectory;
64  FILE* m_logFile;
65 };
66 
67 Log::Private::~Private()
68 {
69  if ( m_logFile )
70  fclose( m_logFile );
71 }
72 
73 void Log::messageHandler( QtMsgType type, const char* msg )
74 {
75  Q_UNUSED( type )
76  FILE* const file = Log::instance()->logFile();
77  if ( !file ) {
78  fprintf( stderr, "Log::messageHandler[!file]: %s", msg );
79  return;
80  }
81 
82  qint64 toWrite = strlen( msg );
83  while ( toWrite > 0 )
84  {
85  const qint64 written = fprintf( file, "%s", msg );
86  if ( written == -1 )
87  return;
88  toWrite -= written;
89  }
90  //append newline:
91  while ( fprintf( file, "\n" ) == 0 ) ;
92  fflush( file );
93 }
94 
95 shared_ptr<const Log> Log::instance() {
96  return mutableInstance();
97 }
98 
99 shared_ptr<Log> Log::mutableInstance() {
100  static weak_ptr<Log> self;
101  try {
102  return shared_ptr<Log>( self );
103  } catch ( const bad_weak_ptr & ) {
104  const shared_ptr<Log> s( new Log );
105  self = s;
106  return s;
107  }
108 }
109 
110 Log::Log() : d( new Private( this ) )
111 {
112 }
113 
114 Log::~Log()
115 {
116 }
117 
118 FILE* Log::logFile() const
119 {
120  return d->m_logFile;
121 }
122 
123 void Log::setIOLoggingEnabled( bool enabled )
124 {
125  d->m_ioLoggingEnabled = enabled;
126 }
127 
128 bool Log::ioLoggingEnabled() const
129 {
130  return d->m_ioLoggingEnabled;
131 }
132 
133 QString Log::outputDirectory() const
134 {
135  return d->m_outputDirectory;
136 }
137 
138 void Log::setOutputDirectory( const QString& path )
139 {
140  if ( d->m_outputDirectory == path )
141  return;
142  d->m_outputDirectory = path;
143  assert( !d->m_logFile );
144  const QString lfn = path + QLatin1String("/kleo-log");
145  d->m_logFile = fopen( QDir::toNativeSeparators( lfn ).toLocal8Bit().constData(), "a" );
146  assert( d->m_logFile );
147 }
148 
149 shared_ptr<QIODevice> Log::createIOLogger( const shared_ptr<QIODevice>& io, const QString& prefix, OpenMode mode ) const
150 {
151  if ( !d->m_ioLoggingEnabled )
152  return io;
153 
154  shared_ptr<IODeviceLogger> logger( new IODeviceLogger( io ) );
155 
156  const QString timestamp = QDateTime::currentDateTime().toString( QLatin1String("yyMMdd-hhmmss") );
157 
158  const QString fn = d->m_outputDirectory + QLatin1Char('/') + prefix + QLatin1Char('-') + timestamp + QLatin1Char('-') + KRandom::randomString( 4 );
159  shared_ptr<QFile> file( new QFile( fn ) );
160 
161  if ( !file->open( QIODevice::WriteOnly ) )
162  throw Exception( gpg_error( GPG_ERR_EIO ), i18n( "Log Error: Could not open log file \"%1\" for writing.", fn ) );
163 
164  if ( mode & Read )
165  logger->setReadLogDevice( file );
166  else // Write
167  logger->setWriteLogDevice( file );
168 
169  return logger;
170 }
171 
QDateTime::toString
QString toString(Qt::DateFormat format) const
QDir::toNativeSeparators
QString toNativeSeparators(const QString &pathName)
Kleo::Formatting::type
QString type(const GpgME::Key &key)
iodevicelogger.h
Kleo::Log::OpenMode
OpenMode
Definition: log.h:54
QFile
Kleo::Log::logFile
FILE * logFile() const
Definition: log.cpp:118
Kleo::Log::Read
Definition: log.h:55
boost::shared_ptr
Definition: encryptemailcontroller.h:51
d
#define d
Definition: adduseridcommand.cpp:89
Kleo::Log::setIOLoggingEnabled
void setIOLoggingEnabled(bool enabled)
Definition: log.cpp:123
Kleo::Log::ioLoggingEnabled
bool ioLoggingEnabled() const
Definition: log.cpp:128
Kleo::Log::createIOLogger
boost::shared_ptr< QIODevice > createIOLogger(const boost::shared_ptr< QIODevice > &wrapped, const QString &prefix, OpenMode mode) const
Definition: log.cpp:149
QString
Kleo::IODeviceLogger
Definition: iodevicelogger.h:46
QLatin1Char
QDateTime::currentDateTime
QDateTime currentDateTime()
QLatin1String
self
static ReaderStatus * self
Definition: readerstatus.cpp:86
q
#define q
Definition: adduseridcommand.cpp:90
Kleo::Log::setOutputDirectory
void setOutputDirectory(const QString &path)
Definition: log.cpp:138
Kleo::Log::~Log
~Log()
Definition: log.cpp:114
log.h
Kleo::Log::outputDirectory
QString outputDirectory() const
Definition: log.cpp:133
Kleo::Log
Definition: log.h:51
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:33:11 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kleopatra

Skip menu "kleopatra"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members

kdepim API Reference

Skip menu "kdepim API Reference"
  • akonadi_next
  • akregator
  • blogilo
  • calendarsupport
  • console
  •   kabcclient
  •   konsolekalendar
  • kaddressbook
  • kalarm
  •   lib
  • kdgantt2
  • kjots
  • kleopatra
  • kmail
  • knode
  • knotes
  • kontact
  • korgac
  • korganizer
  • ktimetracker
  • libkdepim
  • libkleo
  • libkpgp
  • mailcommon
  • messagelist
  • messageviewer
  • pimprint

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal