| 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 << " --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 |
|
|---|
| 32 | void 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'
|
|---|
| 49 | int 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 | gLog << s << endl;
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if (in!=&cin)
|
|---|
| 105 | delete in;
|
|---|
| 106 |
|
|---|
| 107 | return 0;
|
|---|
| 108 | }
|
|---|