source: tags/Mars-V0.9.2/showlog.cc@ 13528

Last change on this file since 13528 was 7001, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 3.5 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 << " --version, -V Show startup message with version number" << endl;
22 gLog << " -?, -h, --help This help" << endl;
23 gLog << endl;
24 gLog << " This program converts colored output made with ansi codes" << endl;
25 gLog << " (like it is done by MLog) and redirected into a file back" << endl;
26 gLog << " into colored output." << endl << endl;
27 gLog << " It cannot be used to get rid of the Ansi codes. To display" << endl;
28 gLog << " colored output with less use the option -R, eg." << endl;
29 gLog << " less -R logfile.log" << endl << endl;
30}
31
32void RemoveAnsi(TString &s)
33{
34 static const TRegexp regexp("[][[][0-9]+[m]");
35
36 while (1)
37 {
38 Ssiz_t len = 0;
39 Ssiz_t idx = s.Index(regexp, &len);
40 if (idx<0)
41 break;
42
43 s.Remove(idx, len);
44 }
45}
46
47// FIXME: Enhance this tool with a converter to HTMl, etc.
48// Add option for 'no-colors'
49int main(int argc, char **argv)
50{
51 MArgs arg(argc, argv);
52
53 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
54 return 0;
55
56 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
57 {
58 Usage();
59 return 2;
60 }
61
62 const Bool_t kNoColors = arg.HasOnly("--no-colors") || arg.HasOnly("-a");
63
64 gLog.Setup(arg);
65
66 if (arg.GetNumOptions()>0)
67 {
68 gLog << warn << "WARNING - Unknown commandline options..." << endl;
69 arg.Print("options");
70 gLog << endl;
71 }
72
73 //
74 // check for the right usage of the program
75 //
76 if (arg.GetNumArguments()>1)
77 {
78 Usage();
79 return 2;
80 }
81
82 // casts necessary for gcc 2.95.3
83 istream *in = arg.GetNumArguments()==1 ? (istream*)new ifstream(arg.GetArgumentStr(0)) : (istream*)&cin;
84 if (!*in)
85 {
86 gLog << "Cannot open file " << arg.GetArgumentStr(0) << ": " << strerror(errno) << endl;
87 return 2;
88 }
89
90 TString s;
91 while (1)
92 {
93 s.ReadLine(*in);
94 if (!*in)
95 break;
96
97 if (kNoColors)
98 RemoveAnsi(s);
99 else
100 s.ReplaceAll("", "\033");
101
102 const char *end = s.Data()+s.Length();
103 for (const char *c=s.Data(); c<end; c++)
104 {
105 char *to = NULL;
106 Int_t n = -1;
107
108 if (c+1<end-1 && *c=='\033' && *(c+1)=='[')
109 n = strtol(c+2, &to, 10);
110
111 if (to==NULL || *to!='m')
112 {
113 gLog << *c;
114 continue;
115 }
116
117 c = to;
118
119 switch (n)
120 {
121 case 0:
122 if (c<end-1)
123 gLog << flush << all;
124 continue;
125 case 31:
126 gLog << flush << err;
127 continue;
128 case 32:
129 gLog << flush << inf;
130 continue;
131 case 33:
132 gLog << flush << warn;
133 continue;
134 case 34:
135 gLog << flush << dbg;
136 continue;
137 }
138 gLog << flush << "\033[" << n << "m";
139 }
140 gLog << endl << all;
141 }
142
143 if (in!=&cin)
144 delete in;
145
146 return 1;
147}
Note: See TracBrowser for help on using the repository browser.