| 1 | #include <boost/regex.hpp>
|
|---|
| 2 |
|
|---|
| 3 | #include "Time.h"
|
|---|
| 4 | #include "tools.h"
|
|---|
| 5 | #include "WindowLog.h"
|
|---|
| 6 | #include "Configuration.h"
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 | using namespace std;
|
|---|
| 10 |
|
|---|
| 11 | // ------------------------------------------------------------------------
|
|---|
| 12 |
|
|---|
| 13 | void SetupConfiguration(Configuration &conf)
|
|---|
| 14 | {
|
|---|
| 15 | po::options_description control("Showlog");
|
|---|
| 16 | control.add_options()
|
|---|
| 17 | ("file,f", vars<string>(), "File names of log-files to be read.")
|
|---|
| 18 | ("begin,b", var<string>(), "Start time to be displayed (e.g. 20:00:12)")
|
|---|
| 19 | ("end,e", var<string>(), "End time to be displayed (e.g. 21:00:13)")
|
|---|
| 20 | ("verbose,v", var<int16_t>()->implicit_value(true)->default_value(8), "Verbosity level (0:only fatal errors, 8:everything)")
|
|---|
| 21 | ("color,c", po_bool(false), "Process a file which already contains color codes")
|
|---|
| 22 | ;
|
|---|
| 23 |
|
|---|
| 24 | po::positional_options_description p;
|
|---|
| 25 | p.add("file", -1); // The first positional options
|
|---|
| 26 |
|
|---|
| 27 | conf.AddOptions(control);
|
|---|
| 28 | conf.SetArgumentPositions(p);
|
|---|
| 29 | }
|
|---|
| 30 |
|
|---|
| 31 | void PrintUsage()
|
|---|
| 32 | {
|
|---|
| 33 | cout <<
|
|---|
| 34 | "showlog - Log file converter\n"
|
|---|
| 35 | "\n"
|
|---|
| 36 | "This tool can be used to convert the log-files written by the\n"
|
|---|
| 37 | "datalogger back to colored output, limit the displayed time\n"
|
|---|
| 38 | "range and limit the displayed severity of the messages.\n"
|
|---|
| 39 | "Note that this tool will not work by default on logs containing\n"
|
|---|
| 40 | "already colored output as the logs directly written by the programs.\n"
|
|---|
| 41 | "Use -c or --color to process color coded files.\n"
|
|---|
| 42 | "\n"
|
|---|
| 43 | "The default is to read from stdin if no filoename as given. If, as "
|
|---|
| 44 | "a filename, just a number between 2000000 and 21000000 is given, "
|
|---|
| 45 | "e.g. 20111016 a log with the name /fact/aux/2011/10/16/20111016.log "
|
|---|
| 46 | "is read.\n"
|
|---|
| 47 | "\n"
|
|---|
| 48 | "Usage: showlog [-c] [-vN] [-b start] [-e end] [file1 ...]\n"
|
|---|
| 49 | " or: showlog [-c] [-vN] [-b start] [-e end] YYYYMMDD\n";
|
|---|
| 50 | cout << endl;
|
|---|
| 51 | }
|
|---|
| 52 |
|
|---|
| 53 | void PrintHelp()
|
|---|
| 54 | {
|
|---|
| 55 | cout <<
|
|---|
| 56 | "\n"
|
|---|
| 57 | "Examples:\n"
|
|---|
| 58 | " cat temperature.log | showlog -c -v2\n"
|
|---|
| 59 | " showlog -c temperature.log -v2\n"
|
|---|
| 60 | " cat 20130909.log | showlog -v2\n"
|
|---|
| 61 | " showlog 20130909.log -v2\n"
|
|---|
| 62 | "\n";
|
|---|
| 63 | cout << endl;
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 | void showlog(string fname, const Time &tbeg, const Time &tend, int16_t severity, bool color)
|
|---|
| 68 | {
|
|---|
| 69 | // Alternatives
|
|---|
| 70 | // \x1B\[[0-9;]*[mK]
|
|---|
| 71 | // \x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]
|
|---|
| 72 | // \x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]
|
|---|
| 73 | const boost::regex reg("\x1B\[[0-9;]*[a-zA-Z]");
|
|---|
| 74 |
|
|---|
| 75 | const uint32_t night = atoi(fname.c_str());
|
|---|
| 76 | if (night>20000000 && night<21000000 &&to_string(night)==fname)
|
|---|
| 77 | fname = Tools::Form("/fact/aux/%04d/%02d/%02d/%d.log",
|
|---|
| 78 | night/10000, (night/100)%100, night%100, night);
|
|---|
| 79 |
|
|---|
| 80 | if (!fname.empty())
|
|---|
| 81 | cerr << "Reading " << fname << endl;
|
|---|
| 82 |
|
|---|
| 83 | ifstream fin(fname.empty() ? "/dev/stdin" : fname.c_str());
|
|---|
| 84 | if (!fin)
|
|---|
| 85 | throw runtime_error(strerror(errno));
|
|---|
| 86 |
|
|---|
| 87 | string buffer;
|
|---|
| 88 |
|
|---|
| 89 | WindowLog log;
|
|---|
| 90 |
|
|---|
| 91 | Time tprev;
|
|---|
| 92 |
|
|---|
| 93 | while (getline(fin, buffer, '\n'))
|
|---|
| 94 | {
|
|---|
| 95 | if (color)
|
|---|
| 96 | buffer = boost::regex_replace(buffer, reg, "");
|
|---|
| 97 |
|
|---|
| 98 | if (buffer.size()==0)
|
|---|
| 99 | continue;
|
|---|
| 100 |
|
|---|
| 101 | if (buffer.size()>18)
|
|---|
| 102 | {
|
|---|
| 103 | const string tm = buffer.substr(4, 15);
|
|---|
| 104 |
|
|---|
| 105 | const Time t("1970-01-01 "+tm);
|
|---|
| 106 |
|
|---|
| 107 | if (tbeg.IsValid() && !tend.IsValid() && t<tbeg)
|
|---|
| 108 | continue;
|
|---|
| 109 |
|
|---|
| 110 | if (tend.IsValid() && !tbeg.IsValid() && t>tend)
|
|---|
| 111 | continue;
|
|---|
| 112 |
|
|---|
| 113 | if (tbeg.IsValid() && tend.IsValid())
|
|---|
| 114 | {
|
|---|
| 115 | if (tend>tbeg)
|
|---|
| 116 | {
|
|---|
| 117 | if (t<tbeg)
|
|---|
| 118 | continue;
|
|---|
| 119 | if (t>tend)
|
|---|
| 120 | continue;
|
|---|
| 121 | }
|
|---|
| 122 | else
|
|---|
| 123 | {
|
|---|
| 124 | if (t>tbeg)
|
|---|
| 125 | continue;
|
|---|
| 126 | if (t<tend)
|
|---|
| 127 | continue;
|
|---|
| 128 | }
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | if (buffer.size()>1)
|
|---|
| 133 | {
|
|---|
| 134 | int16_t lvl = -1;
|
|---|
| 135 | switch (buffer[1])
|
|---|
| 136 | {
|
|---|
| 137 | case ' ': lvl = 7; break; // kDebug
|
|---|
| 138 | case '#': lvl = 6; break; // kComment
|
|---|
| 139 | case '-': lvl = 5; break; // kMessage
|
|---|
| 140 | case '>': lvl = 4; break;
|
|---|
| 141 | case 'I': lvl = 3; break; // kInfo
|
|---|
| 142 | case 'W': lvl = 2; break; // kWarn
|
|---|
| 143 | case 'E': lvl = 1; break; // kError/kAlarm
|
|---|
| 144 | case '!': lvl = 0; break; // kFatal
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | if (lvl>severity)
|
|---|
| 148 | continue;
|
|---|
| 149 |
|
|---|
| 150 | switch (buffer[1])
|
|---|
| 151 | {
|
|---|
| 152 | case ' ': log << kBlue; break; // kDebug
|
|---|
| 153 | case '#': log << kDefault; break; // kComment
|
|---|
| 154 | case '-': log << kDefault; break; // kMessage
|
|---|
| 155 | case '>': log << kBold; break;
|
|---|
| 156 | case 'I': log << kGreen; break; // kInfo
|
|---|
| 157 | case 'W': log << kYellow; break; // kWarn
|
|---|
| 158 | case 'E': log << kRed; break; // kError/kAlarm
|
|---|
| 159 | case '!': log << kRed << kBlink; break; // kFatal
|
|---|
| 160 | }
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | log << buffer << endl;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | int main(int argc, const char* argv[])
|
|---|
| 168 | {
|
|---|
| 169 | Configuration conf(argv[0]);
|
|---|
| 170 | conf.SetPrintUsage(PrintUsage);
|
|---|
| 171 | SetupConfiguration(conf);
|
|---|
| 172 |
|
|---|
| 173 | if (!conf.DoParse(argc, argv, PrintHelp))
|
|---|
| 174 | return 127;
|
|---|
| 175 |
|
|---|
| 176 | const vector<string> files = conf.Vec<string>("file");
|
|---|
| 177 |
|
|---|
| 178 | Time tbeg(Time::none);
|
|---|
| 179 | Time tend(Time::none);
|
|---|
| 180 |
|
|---|
| 181 | if (conf.Has("begin"))
|
|---|
| 182 | {
|
|---|
| 183 | std::stringstream stream;
|
|---|
| 184 | stream << "1970-01-01 " << conf.Get<string>("begin");
|
|---|
| 185 | stream >> Time::iso >> tbeg;
|
|---|
| 186 | }
|
|---|
| 187 |
|
|---|
| 188 | if (conf.Has("end"))
|
|---|
| 189 | {
|
|---|
| 190 | std::stringstream stream;
|
|---|
| 191 | stream << "1970-01-01 " << conf.Get<string>("end");
|
|---|
| 192 | stream >> Time::iso >> tend;
|
|---|
| 193 | }
|
|---|
| 194 |
|
|---|
| 195 | if (files.size()==0)
|
|---|
| 196 | showlog("", tbeg, tend, conf.Get<int16_t>("verbose"), conf.Get<bool>("color"));
|
|---|
| 197 |
|
|---|
| 198 | for (auto it=files.begin(); it!=files.end(); it++)
|
|---|
| 199 | showlog(*it, tbeg, tend, conf.Get<int16_t>("verbose"), conf.Get<bool>("color"));
|
|---|
| 200 |
|
|---|
| 201 | return 0;
|
|---|
| 202 | }
|
|---|