1 #include <boost/regex.hpp> 15 po::options_description control(
"Showlog");
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_switch(),
"Process a file which already contains color codes")
22 (
"strip,s",
po_switch(),
"Strip color codes completely")
25 po::positional_options_description p;
35 "showlog - Log file converter\n" 37 "This tool can be used to convert the log-files written by the\n" 38 "datalogger back to colored output, limit the displayed time\n" 39 "range and limit the displayed severity of the messages.\n" 40 "Note that this tool will not work by default on logs containing\n" 41 "already colored output as the logs directly written by the programs.\n" 42 "Use -c or --color to process color coded files.\n" 44 "The default is to read from stdin if no filoename as given. If, as " 45 "a filename, just a number between 2000000 and 21000000 is given, " 46 "e.g. 20111016 a log with the name /fact/aux/2011/10/16/20111016.log " 49 "Usage: showlog [-c] [-vN] [-b start] [-e end] [file1 ...]\n" 50 " or: showlog [-c] [-vN] [-b start] [-e end] YYYYMMDD\n";
59 " cat temperature.log | showlog -c -v2\n" 60 " showlog -c temperature.log -v2\n" 61 " cat 20130909.log | showlog -v2\n" 62 " showlog 20130909.log -v2\n" 74 const boost::regex reg(
"\x1B\[([0-9]{1,3}(;[0-9]{1,3})?[a-zA-Z]");
76 const uint32_t night = atoi(fname.c_str());
77 if (night>20000000 && night<21000000 &&to_string(night)==fname)
78 fname =
Tools::Form(
"/fact/aux/%04d/%02d/%02d/%d.log",
79 night/10000, (night/100)%100, night%100, night);
82 cerr <<
"Reading " << fname << endl;
84 ifstream fin(fname.empty() ?
"/dev/stdin" : fname.c_str());
86 throw runtime_error(strerror(errno));
94 while (getline(fin, buffer,
'\n'))
97 buffer = boost::regex_replace(buffer, reg,
"");
102 if (buffer.size()>18)
104 const string tm = buffer.substr(4, 15);
106 const Time t(
"1970-01-01 "+tm);
133 if (buffer.size()>1 && !strip)
138 case ' ': lvl = 7;
break;
139 case '#': lvl = 6;
break;
140 case '-': lvl = 5;
break;
141 case '>': lvl = 4;
break;
142 case 'I': lvl = 3;
break;
143 case 'W': lvl = 2;
break;
144 case 'E': lvl = 1;
break;
145 case '!': lvl = 0;
break;
153 case ' ': log <<
kBlue;
break;
156 case '>': log <<
kBold;
break;
157 case 'I': log <<
kGreen;
break;
158 case 'W': log <<
kYellow;
break;
159 case 'E': log <<
kRed;
break;
160 case '!': log << kRed <<
kBlink;
break;
164 (strip?cout:log) << buffer << endl;
168 int main(
int argc,
const char* argv[])
177 const vector<string> files = conf.
Vec<
string>(
"file");
182 if (conf.
Has(
"begin"))
184 std::stringstream stream;
185 stream <<
"1970-01-01 " << conf.
Get<
string>(
"begin");
191 std::stringstream stream;
192 stream <<
"1970-01-01 " << conf.
Get<
string>(
"end");
197 showlog(
"", tbeg, tend, conf.
Get<int16_t>(
"verbose"), conf.
Get<
bool>(
"color"), conf.
Get<
bool>(
"strip"));
199 for (
auto it=files.begin(); it!=files.end(); it++)
200 showlog(*it, tbeg, tend, conf.
Get<int16_t>(
"verbose"), conf.
Get<
bool>(
"color"), conf.
Get<
bool>(
"strip"));
Adds some functionality to boost::posix_time::ptime for our needs.
A C++ ostream to an ncurses window supporting attributes and colors.
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
static const _time_format iso
set to format to the sql format (without the fraction of seconds)
po::typed_value< bool > * po_switch()
void showlog(string fname, const Time &tbeg, const Time &tend, int16_t severity, bool color, bool strip)
std::vector< T > Vec(const std::string &var)
void SetArgumentPositions(const po::positional_options_description &desc)
void SetupConfiguration(Configuration &conf)
bool Has(const std::string &var)
void AddOptions(const po::options_description &opt, bool visible=true)
Commandline parsing, resource file parsing and database access.
int main(int argc, const char *argv[])
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
Do not initialize the time.