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

Last change on this file since 6977 was 6949, checked in by tbretz, 20 years ago
*** empty log message ***
File size: 3.7 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 // Check whether it is an empty line
103 if (s=="\033[0m")
104 {
105 gLog << endl;
106 continue;
107 }
108
109 while (!s.IsNull())
110 {
111 Ssiz_t pos = s.First("\033[");
112 if (pos<0)
113 {
114 gLog << s;
115 break;
116 }
117
118 gLog << s(0, pos);
119 s.Remove(0, pos+1);
120 if (s.BeginsWith("0m") && s.Length()>3)
121 {
122 gLog << all;
123 s.Remove(0, 2);
124 continue;
125 }
126 if (s.BeginsWith("31m"))
127 {
128 gLog << err;
129 s.Remove(0, 3);
130 continue;
131 }
132 if (s.BeginsWith("32m"))
133 {
134 gLog << inf;
135 s.Remove(0, 3);
136 continue;
137 }
138 if (s.BeginsWith("33m"))
139 {
140 gLog << warn;
141 s.Remove(0, 3);
142 continue;
143 }
144 if (s.BeginsWith("34m"))
145 {
146 gLog << dbg;
147 s.Remove(0, 3);
148 continue;
149 }
150 gLog << "\033[";
151 }
152 gLog << endl;
153 }
154
155 if (in!=&cin)
156 delete in;
157
158 return 1;
159}
Note: See TracBrowser for help on using the repository browser.