Răsfoiți Sursa

Working on the logger

Panagiotis Christopoulos Charitos 15 ani în urmă
părinte
comite
3f738b344d
4 a modificat fișierele cu 173 adăugiri și 23 ștergeri
  1. 0 1
      build/debug/Makefile
  2. 77 2
      src/Core/Logger.cpp
  3. 89 20
      src/Core/Logger.h
  4. 7 0
      src/Main.cpp

Fișier diff suprimat deoarece este prea mare
+ 0 - 1
build/debug/Makefile


+ 77 - 2
src/Core/Logger.cpp

@@ -1,5 +1,5 @@
-#include "Logger.h"
 #include <cstdio>
+#include "Logger.h"
 
 
 Logger* Logger::instance = NULL;
@@ -10,15 +10,90 @@ Logger* Logger::instance = NULL;
 //======================================================================================================================
 Logger& Logger::operator<<(const char* val)
 {
-	printf("Adding %s\n", val);
+	append(val, strlen(val));
 	return *this;
 }
 
+
 //======================================================================================================================
 // operator<< [Logger& (*funcPtr)(Logger&)]                                                                            =
 //======================================================================================================================
 Logger& Logger::operator<<(Logger& (*funcPtr)(Logger&))
 {
 	printf("Got some func\n");
+	if(funcPtr == endl)
+	{
+		append("\n", 1);
+		flush();
+	}
 	return *this;
 }
+
+
+//======================================================================================================================
+//                                                                                                                     =
+//======================================================================================================================
+Logger& Logger::operator<<(const LoggerSender& sender)
+{
+	file = sender.file;
+	line = sender.line;
+	func = sender.func;
+	return *this;
+}
+
+
+//======================================================================================================================
+// execCommonConstructionCode                                                                                          =
+//======================================================================================================================
+void Logger::execCommonConstructionCode()
+{
+	sptr = &streamBuf[0];
+	memset(sptr, '?', STREAM_SIZE);
+	func = file = "error";
+	line = -1;
+}
+
+
+//======================================================================================================================
+// append                                                                                                              =
+//======================================================================================================================
+void Logger::append(const char* cstr, int len)
+{
+	if(len > STREAM_SIZE - 1)
+	{
+		return;
+	}
+
+	int charsLeft = &streamBuf[STREAM_SIZE - 1] - sptr; // Leaving an extra char for the '\0'
+
+	// Overflow
+	if(len > charsLeft)
+	{
+		// Handle overflow
+		memcpy(sptr, cstr, charsLeft);
+		sptr += charsLeft;
+		flush();
+		append(cstr + charsLeft, len - charsLeft);
+		return;
+	}
+
+	memcpy(sptr, cstr, len);
+	sptr += len;
+}
+
+
+//======================================================================================================================
+// flush                                                                                                               =
+//======================================================================================================================
+void Logger::flush()
+{
+	*sptr = '\0';
+	sig(file, line, func, &streamBuf[0]);
+
+	// Reset
+	sptr = &streamBuf[0];
+	func = file = "error";
+	line = -1;
+
+	printf("Flushing: |%s|\n", &streamBuf[0]);
+}

+ 89 - 20
src/Core/Logger.h

@@ -2,38 +2,68 @@
 #define LOGGER_H
 
 #include <boost/array.hpp>
+#include <boost/signals2.hpp>
+#include <boost/lexical_cast.hpp>
 
 
-/// The logger singleton class
+struct LoggerSender;
+
+
+/// The logger singleton class. The logger cannot print errors or throw exceptions, it has to recover somehow
 class Logger
 {
 	public:
+		typedef boost::signals2::signal<void (const char*, int, const char*, const char*)> Signal;
+
 		static Logger& getInstance();
 
-		/*Logger& operator<<(const bool& val);
-		Logger& operator<<(const short& val);
-		Logger& operator<<(const unsigned short& val);
-		Logger& operator<<(const int& val);
-		Logger& operator<<(const unsigned int& val);
-		Logger& operator<<(const long& val);
-		Logger& operator<<(const unsigned long& val);
-		Logger& operator<<(const float& val);
-		Logger& operator<<(const double& val);
-		Logger& operator<<(const long double& val);
-		Logger& operator<<(const void* val);*/
-		Logger& operator<<(const char* val);
+		/// @name Numeric operators
+		/// @{
+		Logger& operator<<(const bool& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const short& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const unsigned short& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const int& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const unsigned int& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const long& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const unsigned long& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const float& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const double& val) {return appendUsingLexicalCast(val);}
+		Logger& operator<<(const long double& val) {return appendUsingLexicalCast(val);}
+		/// @}
 
+		Logger& operator<<(const void* val);
+		Logger& operator<<(const char* val);
 		Logger& operator<<(Logger& (*funcPtr)(Logger&));
-
+		Logger& operator<<(const LoggerSender& sender);
 
 	private:
 		static Logger* instance;
-		static const int STREAM_SIZE = 512;
-		boost::array<char, STREAM_SIZE> stream;
+		static const int STREAM_SIZE = 25;
+		boost::array<char, STREAM_SIZE> streamBuf;
+		char* sptr; ///< Pointer to @ref streamBuf
+		Signal sig; ///< The signal
+		const char* func;
+		const char* file;
+		int line;
 
-		Logger() {}
-		Logger(const Logger&) {}
+		/// @name Ensure its singleton
+		/// @{
+		Logger() {execCommonConstructionCode();}
+		Logger(const Logger&) {execCommonConstructionCode();}
 		void operator=(const Logger&) {}
+		/// @}
+
+		/// Called by all the constructors
+		void execCommonConstructionCode();
+
+		/// Appends to streamBuf. On overflow it writes what it cans and flushes
+		void append(const char* cstr, int len);
+
+		/// Append finalize streamBuf and send the signal
+		void flush();
+
+		template<typename Type>
+		Logger& appendUsingLexicalCast(const Type& val);
 };
 
 
@@ -47,8 +77,47 @@ inline Logger& Logger::getInstance()
 }
 
 
-/// @todo
-inline Logger& endl(Logger& in) {}
+template<typename Type>
+Logger& Logger::appendUsingLexicalCast(const Type& val)
+{
+	std::string out;
+	try
+	{
+		out = boost::lexical_cast<std::string>(val);
+	}
+	catch(...)
+	{
+		out = "*error*";
+	}
+	append(out.c_str(), out.length());
+	return *this;
+}
+
+
+//======================================================================================================================
+// Non-members                                                                                                         =
+//======================================================================================================================
+
+/// Add a new line and flush the Logger
+inline Logger& endl(Logger& logger)
+{
+	return logger;
+}
+
+
+struct LoggerSender
+{
+	const char* file;
+	int line;
+	const char* func;
+};
+
+///
+inline LoggerSender setSender(const char* file, int line, const char* func)
+{
+	LoggerSender sender = {file, line, func};
+	return sender;
+}
 
 
 #endif

+ 7 - 0
src/Main.cpp

@@ -34,6 +34,8 @@
 #include "ModelNode.h"
 #include "SkelAnimModelNodeCtrl.h"
 #include "Model.h"
+#include "Logger.h"
+
 
 // map (hard coded)
 ModelNode* floor__,* sarge,* horse,* crate, *imp;
@@ -383,6 +385,11 @@ void mainLoop()
 //======================================================================================================================
 int main(int argc, char* argv[])
 {
+	//std::stringstream ss;
+	Logger::getInstance()  << setSender(__FILE__, __LINE__, __func__) << "123456789 " << 1.23 << "+++" << endl;
+
+	return 0;
+
 	try
 	{
 		new App(argc, argv);

Unele fișiere nu au fost afișate deoarece prea multe fișiere au fost modificate în acest diff