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

kleopatra

  • sources
  • kde-4.12
  • 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 #include <QDebug>
48 
49 #include <boost/weak_ptr.hpp>
50 #include <cassert>
51 #include <cstdio>
52 
53 using namespace boost;
54 using namespace Kleo;
55 
56 class Log::Private {
57  Log* const q;
58 public:
59  explicit Private( Log* qq ) : q( qq ), m_ioLoggingEnabled( false ), m_logFile( 0 ) {}
60  ~Private();
61  bool m_ioLoggingEnabled;
62  QString m_outputDirectory;
63  FILE* m_logFile;
64 };
65 
66 Log::Private::~Private()
67 {
68  if ( m_logFile )
69  fclose( m_logFile );
70 }
71 
72 void Log::messageHandler( QtMsgType type, const char* msg )
73 {
74  Q_UNUSED( type )
75  FILE* const file = Log::instance()->logFile();
76  if ( !file ) {
77  fprintf( stderr, "Log::messageHandler[!file]: %s", msg );
78  return;
79  }
80 
81  qint64 toWrite = strlen( msg );
82  while ( toWrite > 0 )
83  {
84  const qint64 written = fprintf( file, "%s", msg );
85  if ( written == -1 )
86  return;
87  toWrite -= written;
88  }
89  //append newline:
90  while ( fprintf( file, "\n" ) == 0 ) ;
91  fflush( file );
92 }
93 
94 shared_ptr<const Log> Log::instance() {
95  return mutableInstance();
96 }
97 
98 shared_ptr<Log> Log::mutableInstance() {
99  static weak_ptr<Log> self;
100  try {
101  return shared_ptr<Log>( self );
102  } catch ( const bad_weak_ptr & ) {
103  const shared_ptr<Log> s( new Log );
104  self = s;
105  return s;
106  }
107 }
108 
109 Log::Log() : d( new Private( this ) )
110 {
111 }
112 
113 Log::~Log()
114 {
115 }
116 
117 FILE* Log::logFile() const
118 {
119  return d->m_logFile;
120 }
121 
122 void Log::setIOLoggingEnabled( bool enabled )
123 {
124  d->m_ioLoggingEnabled = enabled;
125 }
126 
127 bool Log::ioLoggingEnabled() const
128 {
129  return d->m_ioLoggingEnabled;
130 }
131 
132 QString Log::outputDirectory() const
133 {
134  return d->m_outputDirectory;
135 }
136 
137 void Log::setOutputDirectory( const QString& path )
138 {
139  if ( d->m_outputDirectory == path )
140  return;
141  d->m_outputDirectory = path;
142  assert( !d->m_logFile );
143  const QString lfn = path + QLatin1String("/kleo-log");
144  d->m_logFile = fopen( QDir::toNativeSeparators( lfn ).toLocal8Bit().constData(), "a" );
145  assert( d->m_logFile );
146 }
147 
148 shared_ptr<QIODevice> Log::createIOLogger( const shared_ptr<QIODevice>& io, const QString& prefix, OpenMode mode ) const
149 {
150  if ( !d->m_ioLoggingEnabled )
151  return io;
152 
153  shared_ptr<IODeviceLogger> logger( new IODeviceLogger( io ) );
154 
155  const QString timestamp = QDateTime::currentDateTime().toString( QLatin1String("yyMMdd-hhmmss") );
156 
157  const QString fn = d->m_outputDirectory + QLatin1Char('/') + prefix + QLatin1Char('-') + timestamp + QLatin1Char('-') + KRandom::randomString( 4 );
158  shared_ptr<QFile> file( new QFile( fn ) );
159 
160  if ( !file->open( QIODevice::WriteOnly ) )
161  throw Exception( gpg_error( GPG_ERR_EIO ), i18n( "Log Error: Could not open log file \"%1\" for writing.", fn ) );
162 
163  if ( mode & Read )
164  logger->setReadLogDevice( file );
165  else // Write
166  logger->setWriteLogDevice( file );
167 
168  return logger;
169 }
170 
Kleo::Formatting::type
QString type(const GpgME::Key &key)
iodevicelogger.h
Kleo::Log::OpenMode
OpenMode
Definition: log.h:53
Kleo::Log::logFile
FILE * logFile() const
Definition: log.cpp:117
Kleo::Log::Read
Definition: log.h:54
boost::shared_ptr
Definition: encryptemailcontroller.h:51
d
#define d
Definition: adduseridcommand.cpp:90
Kleo::Log::setIOLoggingEnabled
void setIOLoggingEnabled(bool enabled)
Definition: log.cpp:122
Kleo::Log::ioLoggingEnabled
bool ioLoggingEnabled() const
Definition: log.cpp:127
Kleo::Log::createIOLogger
boost::shared_ptr< QIODevice > createIOLogger(const boost::shared_ptr< QIODevice > &wrapped, const QString &prefix, OpenMode mode) const
Definition: log.cpp:148
Kleo::IODeviceLogger
Definition: iodevicelogger.h:44
self
static ReaderStatus * self
Definition: readerstatus.cpp:87
q
#define q
Definition: adduseridcommand.cpp:91
Kleo::Log::setOutputDirectory
void setOutputDirectory(const QString &path)
Definition: log.cpp:137
Kleo::Log::~Log
~Log()
Definition: log.cpp:113
log.h
Kleo::Log::outputDirectory
QString outputDirectory() const
Definition: log.cpp:132
Kleo::Log
Definition: log.h:50
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:56:41 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

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