Permalink
Browse files

Started to add log4cplus support.

  • Loading branch information...
1 parent f4f1724 commit e2b65184b202631a70e363edba6fa68817cf5dca @bramp committed Mar 20, 2012
Showing with 47 additions and 24 deletions.
  1. +1 −1 Makefile
  2. +1 −1 README
  3. +6 −1 src/libcec.cpp
  4. +29 −13 src/main.cpp
  5. +10 −8 src/uinput.cpp
View
@@ -1,7 +1,7 @@
#CC=clang++
#CC=g++
CFLAGS=-c -std=c++0x -g -Wall -Ilib/libcec/include
-LDFLAGS=-Llib/libcec/src/lib/.libs/ -lcec -ldl -lboost_program_options
+LDFLAGS=-Llib/libcec/src/lib/.libs/ -lcec -ldl -lboost_program_options -llog4cplus
EXECUTABLE=libcec-daemon
SOURCES=src/main.cpp src/uinput.cpp src/libcec.cpp
OBJECTS=$(SOURCES:.cpp=.o)
View
2 README
@@ -16,7 +16,7 @@ Build
git submodule update
# You might need some libraries
- sudo apt-get install libboost-program-options-dev
+ sudo apt-get install libboost-program-options-dev liblog4cplus-dev
# Now build
make
View
@@ -16,20 +16,25 @@
*/
#include "libcec.h"
+#include <cstdio>
#include <iostream>
-#include <cstddef>
#include <stdexcept>
#include <cassert>
#include <map>
+#include <log4cplus/logger.h>
+
using namespace CEC;
+using namespace log4cplus;
using std::cout;
using std::cerr;
using std::endl;
using std::hex;
using std::map;
+static Logger logger = Logger::getInstance("libcec");
+
// cecloader has to be after some #includes and using namespaces :(
#include <cecloader.h>
View
@@ -18,9 +18,11 @@
#include <boost/program_options.hpp>
-#define VERSION "libcec-daemon v0.9"
+#include <log4cplus/logger.h>
+#include <log4cplus/configurator.h>
using namespace CEC;
+using namespace log4cplus;
using std::cout;
using std::cerr;
@@ -29,26 +31,30 @@ using std::max;
using std::string;
using std::vector;
+static Logger logger = Logger::getInstance("main");
+
const vector<__u16> Main::uinputCecMap = Main::setupUinputMap();
Main & Main::instance() {
static Main main;
return main;
}
-Main::Main() : cec("Linux PC", this), uinput("libcec-daemon", uinputCecMap), running(true) {
-
- std::cerr << "Main::Main()" << std::endl;
+Main::Main() : cec(CEC_NAME, this), uinput(UINPUT_NAME, uinputCecMap), running(true) {
+ LOG4CPLUS_TRACE_STR(logger, "Main::Main()");
signal (SIGINT, &Main::signalHandler);
signal (SIGTERM, &Main::signalHandler);
}
Main::~Main() {
+ LOG4CPLUS_TRACE_STR(logger, "Main::~Main()");
stop();
}
void Main::loop() {
+ LOG4CPLUS_TRACE_STR(logger, "Main::loop()");
+
cec.open();
while (running) {
cerr << "Loop" << endl;
@@ -58,15 +64,17 @@ void Main::loop() {
}
void Main::stop() {
+ LOG4CPLUS_TRACE_STR(logger, "Main::stop()");
running = false;
}
void Main::listDevices() {
+ LOG4CPLUS_TRACE_STR(logger, "Main::listDevices()");
cec.listDevices();
}
void Main::signalHandler(int sigNum) {
- cerr << "SignalHanlder(" << sigNum << ")" << endl;
+ LOG4CPLUS_DEBUG_STR(logger, "Main::signalHandler()");
Main::instance().stop();
}
@@ -161,13 +169,12 @@ const std::vector<__u16> & Main::setupUinputMap() {
}
int Main::onCecLogMessage(const cec_log_message &message) {
- cerr << message;
-
+ LOG4CPLUS_DEBUG(logger, "Main::onCecLogMessage(" << message << ")");
return 1;
}
int Main::onCecKeyPress(const cec_keypress &key) {
- cerr << key;
+ LOG4CPLUS_DEBUG(logger, "Main::onCecKeyPress(" << key << ")");
int uinputKey = 0;
@@ -176,29 +183,33 @@ int Main::onCecKeyPress(const cec_keypress &key) {
uinputKey = uinputCecMap[key.keycode];
if (uinputKey != 0) {
- cerr << " sent " << uinputKey;
+ LOG4CPLUS_DEBUG(logger, "sent " << uinputKey);
uinput.send_event(EV_KEY, uinputKey, key.duration == 0 ? 1 : 0);
uinput.sync();
}
- cerr << endl;
return 1;
}
int Main::onCecCommand(const cec_command & command) {
- //cerr << command;
+ LOG4CPLUS_DEBUG(logger, "Main::onCecCommand(" << command << ")");
return 1;
}
int Main::onCecConfigurationChanged(const libcec_configuration & configuration) {
- //cerr << configuration;
+ LOG4CPLUS_DEBUG(logger, "Main::onCecConfigurationChanged(" << configuration << ")");
return 1;
}
int main (int argc, char *argv[]) {
+ BasicConfigurator config;
+ config.configure();
+
+ logger.setLogLevel(TRACE_LOG_LEVEL);
+
namespace po = boost::program_options;
po::options_description desc("Allowed options");
@@ -229,7 +240,12 @@ int main (int argc, char *argv[]) {
return 0;
}
- int loglevel = max((size_t)vm.count("verbose"), (size_t)2);
+ int loglevel = max(vm.count("verbose"), (size_t)2);
+ switch (loglevel) {
+ case 2: logger.setLogLevel(TRACE_LOG_LEVEL); break;
+ case 1: logger.setLogLevel(TRACE_LOG_LEVEL); break;
+ default: logger.setLogLevel(TRACE_LOG_LEVEL); break;
+ }
try {
// Create the main
View
@@ -1,18 +1,20 @@
#include "uinput.h"
#include <cstring>
-#include <iostream>
#include <stdexcept>
#include <errno.h>
#include <fcntl.h>
#include <linux/uinput.h>
#include <unistd.h>
+#include <log4cplus/logger.h>
+
+using namespace log4cplus;
+
static const char *uinput_filename[] = {"/dev/uinput", "/dev/input/uinput", "/dev/misc/uinput"};
-using std::cerr;
-using std::endl;
+static Logger logger = Logger::getInstance("uinput");
UInput::UInput(const char *dev_name, std::vector<__u16> keys) : fd(-1) {
openAll();
@@ -42,7 +44,7 @@ void UInput::openAll() {
// If all things worked, then bail
if (ret == 0) {
- cerr << "\tOpened " << uinput_filename[i] << endl;
+ LOG4CPLUS_INFO(logger, "Opened " << uinput_filename[i]);
break;
}
@@ -51,16 +53,16 @@ void UInput::openAll() {
continue;
if (ret == EACCES) {
- cerr << "Permission denied. Check you have permission to uinput." << endl;
+ LOG4CPLUS_ERROR(logger, "Permission denied. Check you have permission to uinput.");
} else {
- cerr << errno << " " << strerror(errno) << endl;
+ LOG4CPLUS_ERROR(logger, errno << " " << strerror(errno));
}
throw std::runtime_error("Failed to open uinput");
}
if (ret != 0) {
- cerr << "uinput was not found. Is the uinput module loaded?" << endl;
+ LOG4CPLUS_ERROR(logger, "uinput was not found. Is the uinput module loaded?");
throw std::runtime_error("Failed to open uinput");
}
}
@@ -104,7 +106,7 @@ void UInput::create() {
throw std::runtime_error("Failed to create uinput");
}
- cerr << "Created uinput device" << endl;
+ LOG4CPLUS_INFO(logger, "Created uinput device");
// This sleep is here, because (for some reason) you need to wait before
// sending you first uinput event.

0 comments on commit e2b6518

Please sign in to comment.