source: tags/Mars-V0.10.2/showlog.cc@ 18245

Last change on this file since 18245 was 8011, checked in by tbretz, 18 years ago
*** empty log message ***
File size: 4.6 KB
Line 
1#include <errno.h>
2#include <fstream>
3
4#include <TRegexp.h>
5
6#include "MAGIC.h"
7
8#include "MArgs.h"
9
10#include "MLog.h"
11#include "MLogManip.h"
12
13using namespace std;
14
15static void StartUpMessage()
16{
17 // 1 2 3 4 5
18 // 12345678901234567890123456789012345678901234567890
19 gLog << endl;
20 gLog << "showlog --- Mars V" << MARSVER << " compiled on <" << __DATE__ << "> using ROOT v" << ROOT_RELEASE << endl;
21 gLog << endl;
22}
23
24static void Usage()
25{
26 gLog << endl;
27 gLog << "Sorry the usage is:" << endl;
28 gLog << " showlog [options] {filename}|{location number}" << endl;
29 gLog << " or" << endl;
30 gLog << " showlog [options] < filename" << endl << endl;
31 gLog << " Arguments:" << endl;
32 gLog << " filename Input log file" << endl;
33 gLog << " location number Open file from location callisto, star, etc." << endl;
34 gLog << " of sequence or dataset number" << endl << endl;
35 gLog.Usage();
36 gLog << " --version, -V Show startup message with version number" << endl;
37 gLog << " -?, -h, --help This help" << endl;
38 gLog << endl;
39 gLog << " This program converts colored output made with ANSI codes" << endl;
40 gLog << " (like it is done by MLog) and redirected into a file back" << endl;
41 gLog << " into colored output." << endl << endl;
42 gLog << " It cannot be used to get rid of the ANSI codes. To display" << endl;
43 gLog << " colored output with less use the option -R, eg." << endl;
44 gLog << " less -R logfile.log" << endl << endl;
45}
46
47void RemoveAnsi(TString &s)
48{
49 static const TRegexp regexp("[][[][0-9]+[m]");
50
51 while (1)
52 {
53 Ssiz_t len = 0;
54 Ssiz_t idx = s.Index(regexp, &len);
55 if (idx<0)
56 break;
57
58 s.Remove(idx, len);
59 }
60}
61
62// FIXME: Enhance this tool with a converter to HTML, etc.
63// Add option for 'no-colors'
64int main(int argc, char **argv)
65{
66 if (!MARS::CheckRootVer())
67 return 0xff;
68
69 MArgs arg(argc, argv);
70
71 if (arg.HasOnly("-V") || arg.HasOnly("--version"))
72 {
73 StartUpMessage();
74 return 0;
75 }
76
77 if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
78 {
79 Usage();
80 return 2;
81 }
82
83 const Bool_t kNoColors = arg.HasOnly("--no-colors") || arg.HasOnly("-a");
84
85 gLog.Setup(arg);
86
87 if (arg.GetNumOptions()>0)
88 {
89 gLog << warn << "WARNING - Unknown command line options..." << endl;
90 arg.Print("options");
91 gLog << endl;
92 }
93
94 //
95 // check for the right usage of the program
96 //
97 if (arg.GetNumArguments()>2)
98 {
99 Usage();
100 return 2;
101 }
102
103 //
104 // Something special for datacenter access
105 //
106 TString kInput = arg.GetArgumentStr(0);
107 if (arg.GetNumArguments()==2)
108 {
109 const Int_t num = arg.GetArgumentStr(1).Atoi();
110 TString file = "/magic/data/";
111 file += kInput;
112 file += Form("/%04d/%08d/", num/10000, num);
113 file += kInput;
114 file += Form("%08d.log", num);
115
116 kInput = file;
117
118 gLog << inf << "Inflated file name: " << kInput << endl;
119 }
120
121
122 // casts necessary for gcc 2.95.3
123 istream *in = arg.GetNumArguments()>0 ? (istream*)new ifstream(kInput) : (istream*)&cin;
124 if (!*in)
125 {
126 gLog << err << "Cannot open file " << kInput << ": " << strerror(errno) << endl;
127 return 2;
128 }
129
130 TString s;
131 while (1)
132 {
133 s.ReadLine(*in);
134 if (!*in)
135 break;
136
137 if (kNoColors)
138 RemoveAnsi(s);
139 else
140 s.ReplaceAll("", "\033");
141
142 const char *end = s.Data()+s.Length();
143 for (const char *c=s.Data(); c<end; c++)
144 {
145 char *to = NULL;
146 Int_t n = -1;
147
148 if (c+1<end-1 && *c=='\033' && *(c+1)=='[')
149 n = strtol(c+2, &to, 10);
150
151 if (to==NULL || *to!='m')
152 {
153 gLog << *c;
154 continue;
155 }
156
157 c = to;
158
159 switch (n)
160 {
161 case 0:
162 if (c<end-1)
163 gLog << flush << all;
164 continue;
165 case 31:
166 gLog << flush << err;
167 continue;
168 case 32:
169 gLog << flush << inf;
170 continue;
171 case 33:
172 gLog << flush << warn;
173 continue;
174 case 34:
175 gLog << flush << dbg;
176 continue;
177 }
178 gLog << flush << "\033[" << n << "m";
179 }
180 gLog << endl << all;
181 }
182
183 if (in!=&cin)
184 delete in;
185
186 return 1;
187}
Note: See TracBrowser for help on using the repository browser.