| 1 |
#include "adt/logs.h" |
| 2 |
|
| 3 |
#include <boost/algorithm/string/case_conv.hpp> |
| 4 |
|
| 5 |
#include <iomanip> |
| 6 |
#include <cstring> |
| 7 |
|
| 8 |
#include <sys/time.h> |
| 9 |
|
| 10 |
std::ofstream logstream::fh ("client.log"); |
| 11 |
|
| 12 |
static timeval |
| 13 |
now () |
| 14 |
{ |
| 15 |
timeval now; |
| 16 |
gettimeofday (&now, NULL); |
| 17 |
return now; |
| 18 |
} |
| 19 |
|
| 20 |
static timeval const boot = now (); |
| 21 |
|
| 22 |
logstream::logstream (char const *file, int line, char const *func) |
| 23 |
: file (file) |
| 24 |
, line (line) |
| 25 |
, func (func) |
| 26 |
{ |
| 27 |
timeval const now = ::now (); |
| 28 |
timeval sub; |
| 29 |
timersub (&now, &boot, &sub); |
| 30 |
|
| 31 |
fh << '[' << std::setfill (' ') << std::setw (3) << sub.tv_sec << '.' << std::setfill ('0') << std::setw (6) << sub.tv_usec << "] "; |
| 32 |
} |
| 33 |
|
| 34 |
logstream const & |
| 35 |
logstream::operator () (char const *channel) const |
| 36 |
{ |
| 37 |
fh << '<' << boost::to_upper_copy (std::string (channel)) << "> "; |
| 38 |
fh << strrchr (file, '/') + 1 << ':' << line << '(' << func << "): "; |
| 39 |
return *this; |
| 40 |
} |
| 41 |
|
| 42 |
logstream::~logstream () |
| 43 |
{ |
| 44 |
fh.flush (); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
void |
| 49 |
log_failure (char const *file, int line, char const *func, char const *cond) |
| 50 |
{ |
| 51 |
logstream (file, line, func) ("error") << cond << '\n'; |
| 52 |
} |