[Jderobot-admin] jderobot-r1160 - in trunk/src/stable/libs: . log

rocapal en jderobot.org rocapal en jderobot.org
Dom Feb 16 13:05:36 CET 2014


Author: rocapal
Date: 2014-02-16 13:05:36 +0100 (Sun, 16 Feb 2014)
New Revision: 1160

Added:
   trunk/src/stable/libs/log/
   trunk/src/stable/libs/log/CMakeLists.txt
   trunk/src/stable/libs/log/Logger.cpp
   trunk/src/stable/libs/log/Logger.h
Log:
#164 added logger


Added: trunk/src/stable/libs/log/CMakeLists.txt
===================================================================
--- trunk/src/stable/libs/log/CMakeLists.txt	                        (rev 0)
+++ trunk/src/stable/libs/log/CMakeLists.txt	2014-02-16 12:05:36 UTC (rev 1160)
@@ -0,0 +1,10 @@
+
+
+set(SRC_FILES Logger.h Logger.cpp) 
+ 
+ADD_LIBRARY(logger SHARED ${SRC_FILES})
+
+target_link_libraries(logger ${Boost_LIBRARIES})
+
+SET_PROPERTY(TARGET logger PROPERTY SOVERSION 0.1.0)
+ 

Added: trunk/src/stable/libs/log/Logger.cpp
===================================================================
--- trunk/src/stable/libs/log/Logger.cpp	                        (rev 0)
+++ trunk/src/stable/libs/log/Logger.cpp	2014-02-16 12:05:36 UTC (rev 1160)
@@ -0,0 +1,161 @@
+/*
+ *  Copyright (C) 2014 Jderobot Developers
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ *  Authors : Roberto Calvo Palomino <rocapal [at] gsyc [dot] urjc [es]>
+ *
+ */
+
+#include <fstream>
+#include <sstream>
+#include <boost/filesystem.hpp>
+#include "Logger.h"
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+namespace jderobot
+{
+
+static Logger* mInstace;
+static pthread_mutex_t mSingleLock = PTHREAD_MUTEX_INITIALIZER;
+
+const static std::string levelStr[] =  {"", "[INFO]", "[WARNING]", "[ERROR]"};
+const static std::string colorsStr[] = {"\x1b[0m", "\x1b[1;32m", "\x1b[1;33m", "\x1b[1;31m"};
+
+
+Logger* Logger::getInstance()
+{
+	if (mInstace == NULL)
+	{
+		pthread_mutex_lock(&mSingleLock);
+		if (mInstace == NULL)
+		{
+			mInstace = new Logger();
+		}
+		pthread_mutex_unlock(&mSingleLock);
+	}
+
+	return mInstace;
+}
+
+Logger::Logger(): mLogLevel(INFO), mScrenLevel(DEBUG), mFs(NULL), mWriteLock(PTHREAD_MUTEX_INITIALIZER), mLastLevel(-1)
+{
+}
+
+Logger::~Logger()
+{
+	mFs->close();
+}
+
+
+void Logger::setLogFile(std::string logFile)
+{
+	mLogFile = logFile;
+
+	std::string dirName = mLogFile;
+	dirName.erase(std::find(dirName.rbegin(), dirName.rend(), '/').base(), dirName.end());
+
+	// Check dir and create it
+	if ( !boost::filesystem::exists(dirName))
+	{
+		boost::filesystem::path dir(dirName);
+		boost::filesystem::create_directories(dir);
+	}
+
+	mFs= new std::ofstream(mLogFile.c_str(), std::ofstream::app);
+
+}
+
+void Logger::setLogLevel(Levels level)
+{
+	mLogLevel = level;
+}
+
+void Logger::setScreenLevel(Levels level)
+{
+	mScrenLevel = level;
+}
+
+void Logger::trace (Levels level, std::string message)
+{
+	pthread_mutex_lock(&mWriteLock);
+	boost::posix_time::ptime logTime = boost::posix_time::second_clock::local_time();
+
+	mLogDateTime.str("");
+	mLogDateTime.clear();
+
+	mLogDateTime << logTime.date().year() << "-" << logTime.date().month().as_number() << "-"  << logTime.date().day()  << "_";
+	mLogDateTime << logTime.time_of_day().hours() << ":" << logTime.time_of_day().minutes()  << ":" << logTime.time_of_day().seconds();
+	mLogDateTime << " ";
+
+
+	if (mFs != NULL)
+	{
+		*mFs << mLogDateTime.str();
+
+		if (mLogLevel <= level)
+		{
+			if (mLastLevel != level)
+				*mFs << levelStr[level] << " ";
+
+			*mFs << message << std::endl;
+		}
+
+		mFs->flush();
+	}
+	trace_screen(level, message);
+	pthread_mutex_unlock(&mWriteLock);
+}
+
+void Logger::trace_screen (Levels level, std::string message)
+{
+
+	if (level < mScrenLevel)
+		return;
+
+	std::cout << colorsStr[level];
+
+	if (mLastLevel != level)
+	{
+		mLastLevel = level;
+		std::cout << levelStr[level] << " ";
+	}
+
+	std::cout << message << std::endl;
+	std::cout << "\x1b[0m";
+}
+
+void Logger::debug (std::string message)
+{
+	trace(DEBUG, message);
+
+}
+
+void Logger::info (std::string message)
+{
+	trace(INFO, message);
+}
+
+void Logger::warning (std::string message)
+{
+	trace(WARNING, message);
+}
+
+void Logger::error (std::string message)
+{
+	trace(ERROR, message);
+}
+
+
+}

Added: trunk/src/stable/libs/log/Logger.h
===================================================================
--- trunk/src/stable/libs/log/Logger.h	                        (rev 0)
+++ trunk/src/stable/libs/log/Logger.h	2014-02-16 12:05:36 UTC (rev 1160)
@@ -0,0 +1,135 @@
+/*
+ *  Copyright (C) 2014 Jderobot Developers
+ *
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see http://www.gnu.org/licenses/.
+ *
+ *  Authors : Roberto Calvo Palomino <rocapal [at] gsyc [dot] urjc [es]>
+ *
+ */
+
+#ifndef LOG_H
+#define LOG_H
+
+#include <pthread.h>
+#include <fstream>
+#include <vector>
+
+
+namespace jderobot
+{
+	enum Levels{
+
+		DEBUG=0,
+		INFO,
+		WARNING,
+		ERROR
+	};
+
+
+	/**
+	 * Logger class implements a thread safe logger, implemented as singleton.
+	 * This logger allows save traces in file and show the traces in the screen simultaneously.
+	 */
+	class Logger
+	{
+
+	public:
+
+		/**
+		 * \brief Singleton to get the instance. Just one object in memory for execution
+		 */
+
+		static Logger* getInstance();
+
+		/**
+		 * \brief Set the name of file to save every traces.
+		 * It's not mandatory config, if you don't configure this field
+		 * just see the traces in the screen
+		 *
+		 * @param logFile The name of file
+		 *
+		 */
+		void setLogFile (std::string logFile);
+
+		/**
+		 * \brief Set log level for log file. Just the upper levers will be saved in file.
+		 *
+		 * @param levels The level of log file
+		 *
+		 */
+		void setLogLevel (enum Levels);
+
+		/**
+		 * \brief Set log level for screen. Just the upper levers will be shown in the screen
+		 *
+		 * @param levels The level of screen log
+		 *
+		 */
+		void setScreenLevel (enum Levels);
+
+		/**
+		 * \brief Save log message as debug level
+		 *
+		 * @param message The message saved
+		 *
+		 */
+		void debug (std::string message);
+
+		/**
+		 * \brief Save log message as info level
+		 *
+		 * @param message The message saved
+		 *
+		 */
+		void info (std::string message);
+
+		/**
+		 * \brief Save log message as warning level
+		 *
+		 * @param message The message saved
+		 *
+		 */
+		void warning (std::string message);
+
+		/**
+		 * \brief Save log message as error level
+		 *
+		 * @param message The message saved
+		 *
+		 */
+		void error (std::string message);
+
+	private:
+
+		Logger();
+		~Logger();
+
+		void trace (Levels level, std::string message);
+
+		void trace_screen (Levels level, std::string message);
+
+		std::string mLogFile;
+		std::ofstream* mFs;
+
+		Levels mLogLevel;
+		Levels mScrenLevel;
+		int mLastLevel;
+
+		pthread_mutex_t mWriteLock;
+
+		std::stringstream mLogDateTime;
+	};
+}
+
+#endif



More information about the Jderobot-admin mailing list