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 | int i=0;
|
---|
36 |
|
---|
37 | while (1)
|
---|
38 | {
|
---|
39 | Ssiz_t len = 0;
|
---|
40 | Ssiz_t idx = s.Index(regexp, &len);
|
---|
41 | if (idx<0)
|
---|
42 | break;
|
---|
43 |
|
---|
44 | s.Remove(idx, len);
|
---|
45 | }
|
---|
46 | }
|
---|
47 |
|
---|
48 | // FIXME: Enhance this tool with a converter to HTMl, etc.
|
---|
49 | // Add option for 'no-colors'
|
---|
50 | int main(int argc, char **argv)
|
---|
51 | {
|
---|
52 | MArgs arg(argc, argv);
|
---|
53 |
|
---|
54 | if (arg.HasOnly("-?") || arg.HasOnly("-h") || arg.HasOnly("--help"))
|
---|
55 | {
|
---|
56 | Usage();
|
---|
57 | return -1;
|
---|
58 | }
|
---|
59 |
|
---|
60 | const Bool_t kNoColors = arg.HasOnly("--no-colors") || arg.HasOnly("-a");
|
---|
61 |
|
---|
62 | gLog.Setup(arg);
|
---|
63 |
|
---|
64 | if (arg.GetNumOptions()>0)
|
---|
65 | {
|
---|
66 | gLog << warn << "WARNING - Unknown commandline options..." << endl;
|
---|
67 | arg.Print("options");
|
---|
68 | gLog << endl;
|
---|
69 | }
|
---|
70 |
|
---|
71 | //
|
---|
72 | // check for the right usage of the program
|
---|
73 | //
|
---|
74 | if (arg.GetNumArguments()>1)
|
---|
75 | {
|
---|
76 | Usage();
|
---|
77 | return -1;
|
---|
78 | }
|
---|
79 |
|
---|
80 | // casts necessary for gcc 2.95.3
|
---|
81 | istream *in = arg.GetNumArguments()==1 ? (istream*)new ifstream(arg.GetArgumentStr(0)) : (istream*)&cin;
|
---|
82 | if (!*in)
|
---|
83 | {
|
---|
84 | gLog << "Cannot open file " << arg.GetArgumentStr(0) << ": " << strerror(errno) << endl;
|
---|
85 | return -1;
|
---|
86 | }
|
---|
87 |
|
---|
88 | TString s;
|
---|
89 | while (1)
|
---|
90 | {
|
---|
91 | s.ReadLine(*in);
|
---|
92 | if (!*in)
|
---|
93 | break;
|
---|
94 |
|
---|
95 | if (kNoColors)
|
---|
96 | RemoveAnsi(s);
|
---|
97 | else
|
---|
98 | s.ReplaceAll("", "\033");
|
---|
99 | gLog << s << endl;
|
---|
100 | }
|
---|
101 |
|
---|
102 | if (in!=&cin)
|
---|
103 | delete in;
|
---|
104 |
|
---|
105 | return 0;
|
---|
106 | }
|
---|