source: branches/MarsISDCBranchBasedOn17887/showlog.cc@ 20094

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