File: ColorTail.cc

package info (click to toggle)
colortail 0.3.3-1
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, sid, squeeze, stretch, wheezy
  • size: 596 kB
  • ctags: 169
  • sloc: cpp: 1,547; sh: 705; makefile: 27
file content (260 lines) | stat: -rw-r--r-- 6,368 bytes parent folder | download
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
    colortail -- output last part of file(s) in color.
    Copyright (C) 2009  Joakim Andersson <ja@joakimandersson.se>

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

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

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include <iostream>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <stdio.h>

#include "ColorTail.h"
#include "TailFile.h"
#include "OptionsParser.h"
#include "Colorizer.h"

using namespace std;

// the constructor
ColorTail::ColorTail()
{
   // nothing yet
}

// the destructor
ColorTail::~ColorTail()
{
   // free the memory for the TailFile objects in the list
   free_list_items();
}

void ColorTail::free_list_items()
{
   // removes all the elements from the list and frees the memory the objects
   // uses

   TailFile *tmp;

   // go through all the elements in the list,
   // and free each TailFile object
   while (!m_tailfiles.is_empty())
   {
      tmp = m_tailfiles.first_element();
      m_tailfiles.remove_first();
      delete tmp;
   }
}

// the starting method
int ColorTail::start(int argc, char **argv)
{
   // parse options
   OptionsParser optParser;
   Options *options = optParser.parse(argc, argv);

   assert (options != NULL);
   
   // the options has been stripped from argc and argv, just the
   // files to tail and the program namn left

   // tailfile counter
   int tailfile_counter = 0;

   // iterate through all the files to tail
   for (int i = optind ; i < argc ; i++)
   {
      // new TailFile object
      TailFile *new_tailfile = new TailFile();

      Colorizer *colorizer = NULL;
      
      // check if colors
      if (options->color)
      {
	 // color output
	 // check if global config file
	 if (options->global_cfg_file)
	 {
	    // global config file
	    // check if there is a config file
	    if (options->nr_cfg_files > 0)
	    {
	       // yes there is a first config file
	       colorizer = new Colorizer(options->cfg_filenames[0]);

	       // open the tailfile
	       new_tailfile->open(argv[i], colorizer);
	    }
	    else
	    {
	       // no config file
	       // print error
	       //cout << "colortail: Couldn't open global color config file. Skipping colors for the " << argv[i] << " file." << endl;
               // open the tailfile without colorizer
	       //new_tailfile->open(argv[i], NULL);
	       string cade = "/etc/colortail/conf.colortail";
			char* ccade = new char[cade.length()+1];
			strcpy(ccade, cade.c_str());
			colorizer = new Colorizer(ccade);
			new_tailfile->open(argv[i], colorizer);
	    }  
	 }
	 else
	 {
	    // individual config files
	    // check if there is a config file
	    if (options->nr_cfg_files > tailfile_counter
		&& options->cfg_filenames[tailfile_counter] != NULL)
	    {
	       // there is a config file
	       colorizer = new Colorizer
		  (options->cfg_filenames[tailfile_counter]);

	       // open the tailfile
	       new_tailfile->open(argv[i], colorizer);
	    }
	    else
	    {
	       // no config file
	       // no error message because it can be cfgfile1,,cfgfile2 
	       // open the tailfile without colorizer
	       //new_tailfile->open(argv[i], NULL);
			string cade = "/etc/colortail/conf.colortail";
			char* ccade = new char[cade.length()+1];
			strcpy(ccade, cade.c_str());
			colorizer = new Colorizer(ccade);
			new_tailfile->open(argv[i], colorizer);	       
	    }
	 }
      }
      else
      {
	 // no colors

	 // open the tailfile without colorizer
	 new_tailfile->open(argv[i], NULL);
      }

      // add the tailfile to the end of the tailfile list
      m_tailfiles.add_last(new_tailfile);

      // increase the tailfile counter
      tailfile_counter++;
   }
   
   // check if not follow-mode
   if (options->follow == 0)
   {
      // not follow-mode, just print the tails of the files in the list

      // make an iterator
      ListIterator<TailFile*> itr(m_tailfiles);
      TailFile *current_file;
      
      // iterate through the file list
      for (itr.init() ; !itr ; ++itr)
      {
	 current_file = itr();

	 // check if verbose mode
	 if (options->verbose)
	 {
	    // print filename
	    current_file->printFilename();
	 }   
	 
	 // print the specified number of rows
	 current_file->print(options->rows);
      }
   }
   else
   {
      // follow mode

      // make an iterator
      ListIterator<TailFile*> itr(m_tailfiles);
      TailFile *current_file;

      // iterate through the file list and print the no of rows wanted
      for (itr.init() ; !itr ; ++itr)
      {
	 current_file = itr();

	 // check if more than zero rows
	 if (options->rows > 0)
	 {
	    // check if verbose mode
	    if (options->verbose)
	    {
	       // print filename
	       current_file->printFilename();
	    }
	 }
	 
	 // print the specified number of rows
	 current_file->print(options->rows);
      }

      // the "forever" part

      // keeps track of the last file a line was printed from
      char *last_filename = NULL;
      int changed = 0;
      while (1)
      {
	 // iterate through the file list and check if the
	 // file size has changed.

	 for (itr.init() ; !itr ; ++itr)
	 {
	    current_file = itr();

	    int n = current_file->more_to_read();
	    if (n != 0)
	    {
	       // file size has changed
	       // print the change, if there is a '\n'
	       current_file->follow_print(n, options->verbose,last_filename);

	       // get the filename of this file
	       last_filename = current_file->get_filename();

	       // set the changed boolean
	       changed = 1;
	    }
	 }

	 // check if changed
	 if (changed)
	 {
	    // something changed

	    // clear the flag
	    changed = 0;
	 }
	 else
	 {
	    // nothing changed

	    // sleep one second
	    sleep(1);
	 }
      }
   }
   return 0;
}