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 |
|
---|
11 | using namespace std;
|
---|
12 |
|
---|
13 | static 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 << " -?, -h, --help This help" << endl << endl;
|
---|
22 | gLog << endl;
|
---|
23 | gLog << " This program converts colored output made with ansi codes" << endl;
|
---|
24 | gLog << " (like it is done by MLog) and redirected into a file back" << endl;
|
---|
25 | gLog << " into colored output." << endl << endl;
|
---|
26 | gLog << " It cannot be used to get rid of the Ansi codes. To display" << endl;
|
---|
27 | gLog << " colored output with less use the option -R, eg." << endl;
|
---|
28 | gLog << " less -R logfile.log" << endl << endl;
|
---|
29 | }
|
---|
30 |
|
---|
31 | void RemoveAnsi(TString &s)
|
---|
32 | {
|
---|
33 | static const TRegexp regexp("[][[][0-9]+[m]");
|
---|
34 |
|
---|
35 | while (1)
|
---|
36 | {
|
---|
37 | Ssiz_t len = 0;
|
---|
38 | Ssiz_t idx = s.Index(regexp, &len);
|
---|
39 | if (idx<0)
|
---|
40 | break;
|
---|
41 |
|
---|
42 | s.Remove(idx, len);
|
---|
43 | }
|
---|
44 | }
|
---|
45 |
|
---|
46 | // FIXME: Enhance this tool with a converter to HTMl, etc.
|
---|
47 | // Add option for 'no-colors'
|
---|
48 | int main(int argc, char **argv)
|
---|
49 | {
|
---|
50 | MArgs arg(argc, argv);
|
---|
51 |
|
---|
52 | if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
|
---|
53 | {
|
---|
54 | Usage();
|
---|
55 | return -1;
|
---|
56 | }
|
---|
57 |
|
---|
58 | const Bool_t kNoColors = arg.HasOnly("--no-colors") || arg.HasOnly("-a");
|
---|
59 |
|
---|
60 | gLog.Setup(arg);
|
---|
61 |
|
---|
62 | if (arg.GetNumOptions()>0)
|
---|
63 | {
|
---|
64 | gLog << warn << "WARNING - Unknown commandline options..." << endl;
|
---|
65 | arg.Print("options");
|
---|
66 | gLog << endl;
|
---|
67 | }
|
---|
68 |
|
---|
69 | //
|
---|
70 | // check for the right usage of the program
|
---|
71 | //
|
---|
72 | if (arg.GetNumArguments()>1)
|
---|
73 | {
|
---|
74 | Usage();
|
---|
75 | return -1;
|
---|
76 | }
|
---|
77 |
|
---|
78 | // casts necessary for gcc 2.95.3
|
---|
79 | istream *in = arg.GetNumArguments()==1 ? (istream*)new ifstream(arg.GetArgumentStr(0)) : (istream*)&cin;
|
---|
80 | if (!*in)
|
---|
81 | {
|
---|
82 | gLog << "Cannot open file " << arg.GetArgumentStr(0) << ": " << strerror(errno) << endl;
|
---|
83 | return -1;
|
---|
84 | }
|
---|
85 |
|
---|
86 | TString s;
|
---|
87 | while (1)
|
---|
88 | {
|
---|
89 | s.ReadLine(*in);
|
---|
90 | if (!*in)
|
---|
91 | break;
|
---|
92 |
|
---|
93 | if (kNoColors)
|
---|
94 | RemoveAnsi(s);
|
---|
95 | else
|
---|
96 | s.ReplaceAll("", "\033");
|
---|
97 | gLog << s << endl;
|
---|
98 | }
|
---|
99 |
|
---|
100 | if (in!=&cin)
|
---|
101 | delete in;
|
---|
102 |
|
---|
103 | return 0;
|
---|
104 | }
|
---|