FACT++  1.0
showlog.cc
Go to the documentation of this file.
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 
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_switch(), "Process a file which already contains color codes")
22  ("strip,s", po_switch(), "Strip color codes completely")
23  ;
24 
25  po::positional_options_description p;
26  p.add("file", -1); // The first positional options
27 
28  conf.AddOptions(control);
29  conf.SetArgumentPositions(p);
30 }
31 
32 void PrintUsage()
33 {
34  cout <<
35  "showlog - Log file converter\n"
36  "\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"
43  "\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 "
47  "is read.\n"
48  "\n"
49  "Usage: showlog [-c] [-vN] [-b start] [-e end] [file1 ...]\n"
50  " or: showlog [-c] [-vN] [-b start] [-e end] YYYYMMDD\n";
51  cout << endl;
52 }
53 
54 void PrintHelp()
55 {
56  cout <<
57  "\n"
58  "Examples:\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"
63  "\n";
64  cout << endl;
65 }
66 
67 
68 void showlog(string fname, const Time &tbeg, const Time &tend, int16_t severity, bool color, bool strip)
69 {
70  // Alternatives
71  // \x1B\[[0-9;]*[mK]
72  // \x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]
73  // \x1B\[([0-9]{1,3}((;[0-9]{1,3})*)?)?[m|K]
74  const boost::regex reg("\x1B\[([0-9]{1,3}(;[0-9]{1,3})?[a-zA-Z]");
75 
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);
80 
81  if (!fname.empty())
82  cerr << "Reading " << fname << endl;
83 
84  ifstream fin(fname.empty() ? "/dev/stdin" : fname.c_str());
85  if (!fin)
86  throw runtime_error(strerror(errno));
87 
88  string buffer;
89 
90  WindowLog log;
91 
92  Time tprev;
93 
94  while (getline(fin, buffer, '\n'))
95  {
96  if (color || strip)
97  buffer = boost::regex_replace(buffer, reg, "");
98 
99  if (buffer.size()==0)
100  continue;
101 
102  if (buffer.size()>18)
103  {
104  const string tm = buffer.substr(4, 15);
105 
106  const Time t("1970-01-01 "+tm);
107 
108  if (tbeg.IsValid() && !tend.IsValid() && t<tbeg)
109  continue;
110 
111  if (tend.IsValid() && !tbeg.IsValid() && t>tend)
112  continue;
113 
114  if (tbeg.IsValid() && tend.IsValid())
115  {
116  if (tend>tbeg)
117  {
118  if (t<tbeg)
119  continue;
120  if (t>tend)
121  continue;
122  }
123  else
124  {
125  if (t>tbeg)
126  continue;
127  if (t<tend)
128  continue;
129  }
130  }
131  }
132 
133  if (buffer.size()>1 && !strip)
134  {
135  int16_t lvl = -1;
136  switch (buffer[1])
137  {
138  case ' ': lvl = 7; break; // kDebug
139  case '#': lvl = 6; break; // kComment
140  case '-': lvl = 5; break; // kMessage
141  case '>': lvl = 4; break;
142  case 'I': lvl = 3; break; // kInfo
143  case 'W': lvl = 2; break; // kWarn
144  case 'E': lvl = 1; break; // kError/kAlarm
145  case '!': lvl = 0; break; // kFatal
146  }
147 
148  if (lvl>severity)
149  continue;
150 
151  switch (buffer[1])
152  {
153  case ' ': log << kBlue; break; // kDebug
154  case '#': log << kDefault; break; // kComment
155  case '-': log << kDefault; break; // kMessage
156  case '>': log << kBold; break;
157  case 'I': log << kGreen; break; // kInfo
158  case 'W': log << kYellow; break; // kWarn
159  case 'E': log << kRed; break; // kError/kAlarm
160  case '!': log << kRed << kBlink; break; // kFatal
161  }
162  }
163 
164  (strip?cout:log) << buffer << endl;
165  }
166 }
167 
168 int main(int argc, const char* argv[])
169 {
170  Configuration conf(argv[0]);
172  SetupConfiguration(conf);
173 
174  if (!conf.DoParse(argc, argv, PrintHelp))
175  return 127;
176 
177  const vector<string> files = conf.Vec<string>("file");
178 
179  Time tbeg(Time::none);
180  Time tend(Time::none);
181 
182  if (conf.Has("begin"))
183  {
184  std::stringstream stream;
185  stream << "1970-01-01 " << conf.Get<string>("begin");
186  stream >> Time::iso >> tbeg;
187  }
188 
189  if (conf.Has("end"))
190  {
191  std::stringstream stream;
192  stream << "1970-01-01 " << conf.Get<string>("end");
193  stream >> Time::iso >> tend;
194  }
195 
196  if (files.size()==0)
197  showlog("", tbeg, tend, conf.Get<int16_t>("verbose"), conf.Get<bool>("color"), conf.Get<bool>("strip"));
198 
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"));
201 
202  return 0;
203 }
Set color Green.
Definition: WindowLog.h:18
Adds some functionality to boost::posix_time::ptime for our needs.
Definition: Time.h:30
A C++ ostream to an ncurses window supporting attributes and colors.
Definition: WindowLog.h:50
void SetPrintUsage(const std::function< void(void)> &func)
T Get(const std::string &var)
Set color Yellow.
Definition: WindowLog.h:19
Set attribute Blink.
Definition: WindowLog.h:34
Set color Red.
Definition: WindowLog.h:17
static const _time_format iso
set to format to the sql format (without the fraction of seconds)
Definition: Time.h:44
Set default colors.
Definition: WindowLog.h:16
po::typed_value< bool > * po_switch()
STL namespace.
void showlog(string fname, const Time &tbeg, const Time &tend, int16_t severity, bool color, bool strip)
Definition: showlog.cc:68
std::vector< T > Vec(const std::string &var)
void SetArgumentPositions(const po::positional_options_description &desc)
void SetupConfiguration(Configuration &conf)
Definition: showlog.cc:13
void PrintUsage()
Definition: showlog.cc:32
bool Has(const std::string &var)
void AddOptions(const po::options_description &opt, bool visible=true)
Definition: Configuration.h:92
Set color Blue.
Definition: WindowLog.h:20
bool IsValid() const
Definition: Time.h:90
Commandline parsing, resource file parsing and database access.
Definition: Configuration.h:9
int buffer[BUFFSIZE]
Definition: db_dim_client.c:14
std::string Form(const char *fmt,...)
Definition: tools.cc:45
int main(int argc, const char *argv[])
Definition: showlog.cc:168
TT t
Definition: test_client.c:26
function color(col)
Definition: color.js:31
bool DoParse(int argc, const char **argv, const std::function< void()> &func=std::function< void()>())
Do not initialize the time.
Definition: Time.h:51
Set attribute Bold.
Definition: WindowLog.h:36
void PrintHelp()
Definition: showlog.cc:54