Skip to content

Commit

Permalink
Fixed logging for the !@#$teenth time.
Browse files Browse the repository at this point in the history
  • Loading branch information
rdadolf committed May 14, 2014
1 parent 25fe004 commit d43dd9b
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions log.hh
Expand Up @@ -4,6 +4,7 @@
#include <string>
#include <sstream>
#include <iostream>
#include <iomanip> // setprecision
#include <fstream>
#include <unistd.h>
#include <sys/time.h>
Expand Down Expand Up @@ -31,50 +32,35 @@ public:
return ls;
}

LogState &operator<< (String rhs) {
template<typename T>LogState &operator<< (T rhs) {
logfile_ << rhs;
logfile_.flush();
return *this;
}
};

// FIXME: Template this class so we don't have n versions of operator<<
// : buffer_ -> stringstream, template operator<<
class LogInstance {
private:
String buffer_;
std::stringstream buffer_;
const char *mode_, *file_;
int line_;
public:
LogInstance(const char *mode, const char *file, const int line) : mode_(mode), file_(file), line_(line) {};
LogInstance& operator<< (String rhs) {
buffer_.append(rhs);
return *this;
}
LogInstance& operator<< (const char* rhs) {
buffer_.append(rhs);
return *this;
LogInstance(const char *mode, const char *file, const int line) : mode_(mode), file_(file), line_(line), buffer_("") {
}
LogInstance& operator<< (int rhs) {
buffer_.append(String(rhs));
return *this;
}
LogInstance& operator<< (Json rhs) {
buffer_.append(rhs.unparse());

template<typename T> LogInstance& operator<< (T rhs) {
buffer_ << rhs;
return *this;
}
~LogInstance() {
struct timeval tv;
char prefix[70];
String final_buffer;

gettimeofday(&tv, 0);
snprintf(prefix, 70, "%ld.%06d [%d:%s:%d] %s: ",
tv.tv_sec, (int)tv.tv_usec, getpid(), file_, line_, mode_);
final_buffer += prefix;
final_buffer += buffer_;
final_buffer += "\n";
LogState::get() << final_buffer;
LogState::get() << std::fixed << std::setprecision(6)
<< double(tv.tv_sec*1000000+tv.tv_usec)/1000000.
<< std::setprecision(0)
<< " [" << getpid() << ":" << file_ << ":" << line_ << "] "
<< mode_ << ": " << buffer_.str() << "\n";
}
};

Expand Down

0 comments on commit d43dd9b

Please sign in to comment.