childprot/log.cpp

48 lines
1.0 KiB
C++
Raw Normal View History

2013-03-27 23:09:15 +01:00
/*
* log.cpp
*
* Created on: 27.03.2013
2013-03-27 23:23:29 +01:00
* Copier: wn
*
* I found this here:
* http://stackoverflow.com/questions/2638654/redirect-c-stdclog-to-syslog-on-unix
2013-03-27 23:09:15 +01:00
*/
#include <syslog.h>
2013-03-27 23:21:56 +01:00
#include <string.h>
#include <stdio.h>
2013-03-27 23:09:15 +01:00
#include <iostream>
#include "log.hpp"
Log::Log(std::string ident, int facility) {
facility_ = facility;
priority_ = LOG_DEBUG;
strncpy(ident_, ident.c_str(), sizeof(ident_));
ident_[sizeof(ident_)-1] = '\0';
openlog(ident_, LOG_PID, facility_);
}
int Log::sync() {
if (buffer_.length()) {
syslog(priority_, "%s", buffer_.c_str());
buffer_.erase();
priority_ = LOG_DEBUG; // default to debug for each message
}
return 0;
}
int Log::overflow(int c) {
if (c != EOF) {
buffer_ += static_cast<char>(c);
} else {
sync();
}
return c;
}
std::ostream& operator<< (std::ostream& os, const LogPriority& log_priority) {
static_cast<Log *>(os.rdbuf())->priority_ = (int)log_priority;
return os;
}