source: trunk/MagicSoft/Mars/showlog.cc@ 4884

Last change on this file since 4884 was 4800, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 2.4 KB
Line 
1#include <errno.h>
2#include <fstream>
3
4#include <TRegexp.h>
5
6#include "MArgs.h"
7
8#include "MLog.h"
9#include "MLogManip.h"
10
11using namespace std;
12
13static void Usage()
14{
15 gLog << endl;
16 gLog << "Sorry the usage is:" << endl;
17 gLog << " showlog [options] filename" << endl;
18 gLog << " or" << endl;
19 gLog << " showlog [options] < filename" << endl << endl;
20 gLog.Usage();
21 gLog << " -?, -h, --help This help" << endl << endl;
22 gLog << endl;
23 gLog << " This program converts colored output made with ansi codes" << endl;
24 gLog << " (like it is done by MLog) and redirected into a file back" << endl;
25 gLog << " into colored output." << endl << endl;
26 gLog << " It cannot be used to get rid of the Ansi codes. To display" << endl;
27 gLog << " colored output with less use the option -R, eg." << endl;
28 gLog << " less -R logfile.log" << endl << endl;
29}
30
31void RemoveAnsi(TString &s)
32{
33 static const TRegexp regexp("[][[][0-9]+[m]");
34
35 int i=0;
36
37 while (1)
38 {
39 Ssiz_t len = 0;
40 Ssiz_t idx = s.Index(regexp, &len);
41 if (idx<0)
42 break;
43
44 s.Remove(idx, len);
45 }
46}
47
48// FIXME: Enhance this tool with a converter to HTMl, etc.
49// Add option for 'no-colors'
50int main(int argc, char **argv)
51{
52 MArgs arg(argc, argv);
53
54 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
55 {
56 Usage();
57 return -1;
58 }
59
60 const Bool_t kNoColors = arg.HasOnly("--no-colors") || arg.HasOnly("-a");
61
62 gLog.Setup(arg);
63
64 if (arg.GetNumOptions()>0)
65 {
66 gLog << warn << "WARNING - Unknown commandline options..." << endl;
67 arg.Print("options");
68 gLog << endl;
69 }
70
71 //
72 // check for the right usage of the program
73 //
74 if (arg.GetNumArguments()>1)
75 {
76 Usage();
77 return -1;
78 }
79
80 // casts necessary for gcc 2.95.3
81 istream *in = arg.GetNumArguments()==1 ? (istream*)new ifstream(arg.GetArgumentStr(0)) : (istream*)&cin;
82 if (!*in)
83 {
84 gLog << "Cannot open file " << arg.GetArgumentStr(0) << ": " << strerror(errno) << endl;
85 return -1;
86 }
87
88 TString s;
89 while (1)
90 {
91 s.ReadLine(*in);
92 if (!*in)
93 break;
94
95 if (kNoColors)
96 RemoveAnsi(s);
97 else
98 s.ReplaceAll("", "\033");
99 gLog << s << endl;
100 }
101
102 if (in!=&cin)
103 delete in;
104
105 return 0;
106}
Note: See TracBrowser for help on using the repository browser.