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

Last change on this file since 9157 was 9141, checked in by tbretz, 16 years ago
*** empty log message ***
File size: 4.7 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 arg.RemoveRootArgs();
84
85 const Bool_t kNoColors = arg.HasOnly("--no-colors") || arg.HasOnly("-a");
86
87 gLog.Setup(arg);
88
89 if (arg.GetNumOptions()>0)
90 {
91 gLog << warn << "WARNING - Unknown command line options..." << endl;
92 arg.Print("options");
93 gLog << endl;
94 }
95
96 //
97 // check for the right usage of the program
98 //
99 if (arg.GetNumArguments()>2)
100 {
101 Usage();
102 return 2;
103 }
104
105 //
106 // Something special for datacenter access
107 //
108 TString kInput = arg.GetArgumentStr(0);
109 if (arg.GetNumArguments()==2)
110 {
111 const Int_t num = arg.GetArgumentStr(1).Atoi();
112 TString file = "/magic/data/";
113 file += kInput;
114 file += Form("/%04d/%08d/", num/10000, num);
115 file += kInput;
116 file += Form("%08d.log", num);
117
118 kInput = file;
119
120 gLog << inf << "Inflated file name: " << kInput << endl;
121 }
122
123
124 // casts necessary for gcc 2.95.3
125 istream *in = arg.GetNumArguments()>0 ? (istream*)new ifstream(kInput) : (istream*)&cin;
126 if (!*in)
127 {
128 gLog << err << "Cannot open file " << kInput << ": " << strerror(errno) << endl;
129 return 2;
130 }
131
132 TString s;
133 while (1)
134 {
135 s.ReadLine(*in);
136 if (!*in)
137 break;
138
139 if (kNoColors)
140 RemoveAnsi(s);
141 else
142 s.ReplaceAll("", "\033");
143
144 const char *end = s.Data()+s.Length();
145 for (const char *c=s.Data(); c<end; c++)
146 {
147 char *to = NULL;
148 Int_t n = -1;
149
150 if (c+1<end-1 && *c=='\033' && *(c+1)=='[')
151 n = strtol(c+2, &to, 10);
152
153 if (to==NULL || *to!='m')
154 {
155 gLog << *c;
156 continue;
157 }
158
159 c = to;
160
161 switch (n)
162 {
163 case 0:
164 if (c<end-1)
165 gLog << flush << all;
166 continue;
167 case 31:
168 gLog << flush << err;
169 continue;
170 case 32:
171 gLog << flush << inf;
172 continue;
173 case 33:
174 gLog << flush << warn;
175 continue;
176 case 34:
177 gLog << flush << dbg;
178 continue;
179 }
180 gLog << flush << "\033[" << n << "m";
181 }
182 gLog << endl << all;
183 }
184
185 if (in!=&cin)
186 delete in;
187
188 return 1;
189}
Note: See TracBrowser for help on using the repository browser.