1/*
2 KSysGuard, the KDE System Guard
3
4 Copyright (c) 2003 Tobias Koenig <tokoe@kde.org>
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of version 2 of the GNU General Public
8 License as published by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19*/
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25#include "Command.h"
26#include "ccont.h"
27#include "conf.h"
28#include "ksysguardd.h"
29
30#include "logfile.h"
31
32static CONTAINER LogFiles = 0;
33static unsigned long counter = 1;
34
35typedef struct {
36 char name[ 256 ];
37 FILE* fh;
38 unsigned long id;
39} LogFileEntry;
40
41extern CONTAINER LogFileList;
42
43/*
44================================ public part =================================
45*/
46
47void initLogFile( struct SensorModul* sm )
48{
49 char monitor[ 1024 ];
50 ConfigLogFile *entry;
51
52 registerCommand( "logfile_register", registerLogFile );
53 registerCommand( "logfile_unregister", unregisterLogFile );
54 registerCommand( "logfile_registered", printRegistered );
55
56 for ( entry = first_ctnr( LogFileList ); entry; entry = next_ctnr( LogFileList ) ) {
57 FILE* fp;
58 /* Register the log file only if we can actually read the file. */
59 if ( ( fp = fopen( entry->path, "r" ) ) != NULL ) {
60 snprintf( monitor, 1024, "logfiles/%s", entry->name );
61 registerMonitor( monitor, "logfile", printLogFile, printLogFileInfo, sm );
62 registerLogFile(entry->name);
63 fclose( fp );
64 }
65 }
66
67 LogFiles = new_ctnr();
68}
69
70void exitLogFile( void )
71{
72 destr_ctnr( LogFiles, free );
73}
74
75void printLogFile( const char* cmd )
76{
77 char line[ 1024 ];
78 unsigned long id;
79 LogFileEntry *entry;
80
81 sscanf( cmd, "%*s %lu", &id );
82
83 for ( entry = first_ctnr( LogFiles ); entry; entry = next_ctnr( LogFiles ) ) {
84 if ( entry->id == id ) {
85 while ( fgets( line, 1024, entry->fh ) != NULL )
86 output( "%s", line );
87
88 /* delete the EOF */
89 clearerr( entry->fh );
90 }
91 }
92
93 output( "\n" );
94}
95
96void printLogFileInfo( const char* cmd )
97{
98 (void)cmd;
99 output( "LogFile\n" );
100}
101
102void registerLogFile( const char* cmd )
103{
104 char name[ 257 ];
105 FILE* file;
106 LogFileEntry *entry;
107 int i;
108
109 memset( name, 0, sizeof( name ) );
110 sscanf( cmd, "%*s %256s", name );
111
112 for ( i = 0; i < level_ctnr( LogFileList ); i++ ) {
113 ConfigLogFile *conf = get_ctnr( LogFileList, i );
114 if ( !strcmp( conf->name, name ) ) {
115 if ( ( file = fopen( conf->path, "r" ) ) == NULL ) {
116 print_error( "fopen()" );
117 output( "0\n" );
118 return;
119 }
120
121 fseek( file, 0, SEEK_END );
122
123 if ( ( entry = (LogFileEntry*)malloc( sizeof( LogFileEntry ) ) ) == NULL ) {
124 print_error( "malloc()" );
125 output( "0\n" );
126 fclose(file);
127 return;
128 }
129
130 entry->fh = file;
131 strncpy( entry->name, conf->name, 256 );
132 entry->id = counter;
133
134 push_ctnr( LogFiles, entry );
135
136 output( "%lu\n", counter );
137 counter++;
138
139 return;
140 }
141 }
142
143 output( "\n" );
144}
145
146void unregisterLogFile( const char* cmd )
147{
148 unsigned long id;
149 LogFileEntry *entry;
150
151 sscanf( cmd, "%*s %lu", &id );
152
153 for ( entry = first_ctnr( LogFiles ); entry; entry = next_ctnr( LogFiles ) ) {
154 if ( entry->id == id ) {
155 fclose( entry->fh );
156 free( remove_ctnr( LogFiles ) );
157 output( "\n" );
158 return;
159 }
160 }
161
162 output( "\n" );
163}
164
165void printRegistered( const char* cmd )
166{
167 LogFileEntry *entry;
168
169 (void)cmd;
170 for ( entry = first_ctnr( LogFiles ); entry; entry = next_ctnr( LogFiles ) )
171 output( "%s:%lu\n", entry->name, entry->id );
172
173 output( "\n" );
174}
175