summaryrefslogtreecommitdiff
path: root/noncore/settings/sysinfo/sysloginfo.cpp
blob: 21427f49fdb7cc24fa590cd872523b2e7eb7f75e (plain)
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
/**********************************************************************
** SyslogInfo
**
** Display Syslog information
**
** Copyright (C) 2004, Michael Lauer
**                    mickey@tm.informatik.uni-frankfurt.de
**                    http://www.Vanille.de
**
** This file may be distributed and/or modified under the terms of the
** GNU General Public License version 2 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
**********************************************************************/

#include "sysloginfo.h"
#include "detail.h"

/* OPIE */
#include <opie2/olistview.h>
#include <opie2/oconfig.h>
using namespace Opie::Core;
using namespace Opie::Ui;

/* QT */
#include <qcombobox.h>
#include <qfile.h>
#include <qlayout.h>
#include <qmessagebox.h>
#include <qpushbutton.h>
#include <qsocketnotifier.h>
#include <qtextbrowser.h>
#include <qtimer.h>
#include <qwhatsthis.h>
#include <qtextview.h>

/* STD */
#include <sys/klog.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>

#define SYSLOG_READ 2
#define SYSLOG_READ_ALL 3
#define SYSLOG_READ_ALL_CLEAR 4
#define SYSLOG_UNREAD 9

#undef APPEND

const unsigned int bufsize = 16384;
char buf[bufsize];

SyslogInfo::SyslogInfo( QWidget* parent,  const char* name, WFlags fl )
        : QWidget( parent, name, fl )
{
    QGridLayout *layout = new QGridLayout( this );
    layout->setSpacing( 2 );
    layout->setMargin( 0 );

    syslogview = new QTextView( this );
    syslogview->setTextFormat( PlainText );
    OConfig cfg( "qpe" );
    cfg.setGroup( "Appearance" );
    syslogview->setFont( QFont( "Fixed", cfg.readNumEntry( "FontSize", 10 ) ) );
    layout->addWidget( syslogview, 0, 0 );
    syslogview->setText( "..." );

    memset( buf, 0, bufsize );
    ::klogctl( SYSLOG_READ_ALL, buf, bufsize );
    syslogview->setText( buf );

#ifdef APPEND
    fd = ::open( "/proc/kmsg", O_RDONLY|O_SYNC );
    if ( fd == -1 )
    {
        syslogview->setText( "Couldn't open /proc/kmsg: " + QString( strerror( errno ) ) );
        return;
    }
    QSocketNotifier *sn = new QSocketNotifier( fd, QSocketNotifier::Read, this );
    QObject::connect( sn, SIGNAL(activated(int)), this, SLOT(updateData()) );
#else
    QPushButton* pb = new QPushButton( "&Refresh", this );
    layout->addWidget( pb, 1, 0 );
    QObject::connect( pb, SIGNAL(clicked()), this, SLOT(updateData()) );
#endif
}

SyslogInfo::~SyslogInfo()
{
    if ( fd != -1 ) ::close( fd );
}

void SyslogInfo::updateData()
{
    qDebug( "SyslogInfo: updateData" );
#ifdef APPEND
    memset( buf, 0, bufsize );
    int num = ::read( fd, buf, bufsize );
    if ( num ) // -1 = error (permission denied)
    {
        syslogview->append( "\n" );
        syslogview->append( buf );
        qDebug( "SyslogInfo: adding '%s'", buf );
    }
#else
    memset( buf, 0, bufsize );
    ::klogctl( SYSLOG_READ_ALL, buf, bufsize );
    syslogview->setText( buf );
    syslogview->ensureVisible( 0, syslogview->contentsHeight() );
#endif
}