source: tags/Mars-V0.9.6/showlog.cc@ 10394

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