Browse Source

More logging for #520

Michał W. Urbańczyk 15 năm trước cách đây
mục cha
commit
d69ae9aa4c
5 tập tin đã thay đổi với 75 bổ sung31 xóa
  1. 14 15
      global.h
  2. 1 0
      hch/CHeroHandler.cpp
  3. 30 0
      lib/Connection.cpp
  4. 24 10
      lib/Connection.h
  5. 6 6
      lib/VCMI_Lib.cpp

+ 14 - 15
global.h

@@ -311,10 +311,13 @@ void delNull(T* &ptr) //deleted pointer and sets it to NULL
 #include "CConsoleHandler.h"
 extern DLL_EXPORT std::ostream *logfile;
 extern DLL_EXPORT CConsoleHandler *console;
-template <int lvl> class CLogger //logger, prints log info to console and saves in file
+
+class CLogger //logger, prints log info to console and saves in file
 {
+	const int lvl;
+
 public:
-	CLogger<lvl>& operator<<(std::ostream& (*fun)(std::ostream&))
+	CLogger& operator<<(std::ostream& (*fun)(std::ostream&))
 	{
 		if(lvl < CONSOLE_LOGGING_LEVEL)
 			std::cout << fun;
@@ -324,33 +327,29 @@ public:
 	}
 
 	template<typename T> 
-	CLogger<lvl> & operator<<(const T & data)
+	CLogger & operator<<(const T & data)
 	{
 		if(lvl < CONSOLE_LOGGING_LEVEL)
 		{
 			if(console)
-			{
 				console->print(data,lvl);
-			}
 			else
-			{
 				std::cout << data << std::flush;
-			}
 		}
 		if((lvl < FILE_LOGGING_LEVEL) && logfile)
-		{
 			*logfile << data << std::flush;
-		}
 		return *this;
 	}
+
+	CLogger(const int Lvl) : lvl(Lvl) {}
 };
 
-extern DLL_EXPORT CLogger<0> tlog0; //green - standard progress info
-extern DLL_EXPORT CLogger<1> tlog1; //red - big errors
-extern DLL_EXPORT CLogger<2> tlog2; //magenta - major warnings
-extern DLL_EXPORT CLogger<3> tlog3; //yellow - minor warnings
-extern DLL_EXPORT CLogger<4> tlog4; //white - detailed log info
-extern DLL_EXPORT CLogger<5> tlog5; //gray - minor log info
+extern DLL_EXPORT CLogger tlog0; //green - standard progress info
+extern DLL_EXPORT CLogger tlog1; //red - big errors
+extern DLL_EXPORT CLogger tlog2; //magenta - major warnings
+extern DLL_EXPORT CLogger tlog3; //yellow - minor warnings
+extern DLL_EXPORT CLogger tlog4; //white - detailed log info
+extern DLL_EXPORT CLogger tlog5; //gray - minor log info
 
 //XXX pls dont - 'debug macros' are usually more trouble than it's worth
 #define HANDLE_EXCEPTION  \

+ 1 - 0
hch/CHeroHandler.cpp

@@ -412,6 +412,7 @@ void CHeroHandler::loadHeroes()
 		std::ifstream inp;
 		dump.clear();
 		inp.open(DATA_DIR "/config/specials.txt"); //loading hero specials
+		assert(inp);
 		specialInfo dummy;
 		si32 hid;
 		inp.ignore(100, '\n');

+ 30 - 0
lib/Connection.cpp

@@ -208,6 +208,16 @@ bool CConnection::isOpen() const
 	return socket && connected;
 }
 
+void CConnection::reportState(CLogger &out)
+{
+	out << "CConnection\n";
+	if(socket && socket->is_open())
+	{
+		out << "\tWe have an open and valid socket\n";
+		out << "\t" << socket->available() <<" bytes awaiting\n";
+	}
+}
+
 CSaveFile::CSaveFile( const std::string &fname )
 	:sfile(NULL)
 {
@@ -234,6 +244,7 @@ void CSaveFile::close()
 
 void CSaveFile::openNextFile(const std::string &fname)
 {
+	fName = fname;
 	close();
 	sfile = new std::ofstream(fname.c_str(),std::ios::binary);
 	if(!(*sfile))
@@ -248,6 +259,15 @@ void CSaveFile::openNextFile(const std::string &fname)
 	}
 }
 
+void CSaveFile::reportState(CLogger &out)
+{
+	out << "CSaveFile" << std::endl;
+	if(sfile && *sfile)
+	{
+		out << "\tOpened " << fName << "\n\tPosition: " << sfile->tellp() << std::endl;
+	}
+}
+
 CLoadFile::CLoadFile( const std::string &fname, bool requireLatest )
 :sfile(NULL)
 {
@@ -274,6 +294,7 @@ void CLoadFile::close()
 
 void CLoadFile::openNextFile(const std::string &fname, bool requireLatest)
 {
+	fName = fname;
 	sfile = new std::ifstream(fname.c_str(),std::ios::binary);
 	if(!(*sfile))
 	{
@@ -303,6 +324,15 @@ void CLoadFile::openNextFile(const std::string &fname, bool requireLatest)
 	}
 }
 
+void CLoadFile::reportState(CLogger &out)
+{
+	out << "CLoadFile" << std::endl;
+	if(sfile && *sfile)
+	{
+		out << "\tOpened " << fName << "\n\tPosition: " << sfile->tellg() << std::endl;
+	}
+}
+
 CTypeList::CTypeList()
 {
 	registerTypes(*this);

+ 24 - 10
lib/Connection.h

@@ -251,6 +251,8 @@ public:
 	CSerializer();
 	~CSerializer();
 
+	virtual void reportState(CLogger &out){};
+
 	template <typename T>
 	void registerVectoredType(const std::vector<T*> *Vector, const si32 T::*IdPtr)
 	{
@@ -722,11 +724,20 @@ public:
 			loadedPointers[pid] = (void*)ptr; //add loaded pointer to our lookup map; cast is to avoid errors with const T* pt
 	}
 
+#define READ_CHECK_U32(x)			\
+	boost::uint32_t length;			\
+	*this >> length;				\
+	if(length > 50000)				\
+	{								\
+		tlog2 << "Warning: very big length: " << length << "\n" ;\
+		reportState(tlog2);			\
+	};
+
+
 	template <typename T>
 	void loadSerializable(std::vector<T> &data)
 	{
-		boost::uint32_t length;
-		*this >> length;
+		READ_CHECK_U32(length);
 		data.resize(length);
 		for(ui32 i=0;i<length;i++)
 			*this >> data[i];
@@ -734,8 +745,7 @@ public:
 	template <typename T>
 	void loadSerializable(std::set<T> &data)
 	{
-		boost::uint32_t length;
-		*this >> length;
+		READ_CHECK_U32(length);
 		T ins;
 		for(ui32 i=0;i<length;i++)
 		{
@@ -746,8 +756,7 @@ public:
 	template <typename T>
 	void loadSerializable(std::list<T> &data)
 	{
-		boost::uint32_t length;
-		*this >> length;
+		READ_CHECK_U32(length);
 		T ins;
 		for(ui32 i=0;i<length;i++)
 		{
@@ -764,8 +773,7 @@ public:
 	template <typename T1, typename T2>
 	void loadSerializable(std::map<T1,T2> &data)
 	{
-		ui32 length;
-		*this >> length;
+		READ_CHECK_U32(length);
 		T1 t;
 		for(ui32 i=0;i<length;i++)
 		{
@@ -775,8 +783,7 @@ public:
 	}
 	void loadSerializable(std::string &data)
 	{
-		ui32 length;
-		*this >> length;
+		READ_CHECK_U32(length);
 		data.resize(length);
 		this->This()->read((void*)data.c_str(),length);
 	}
@@ -792,13 +799,16 @@ class DLL_EXPORT CSaveFile
 		*this << std::string("This function makes stuff working.");
 	}
 public:
+	std::string fName;
 	std::ofstream *sfile;
+
 	CSaveFile(const std::string &fname);
 	~CSaveFile();
 	int write(const void * data, unsigned size);
 
 	void close();
 	void openNextFile(const std::string &fname);
+	void reportState(CLogger &out);
 };
 
 class DLL_EXPORT CLoadFile
@@ -810,13 +820,16 @@ class DLL_EXPORT CLoadFile
 		*this >> dummy;
 	}
 public:
+	std::string fName;
 	std::ifstream *sfile;
+
 	CLoadFile(const std::string &fname, bool requireLatest = true);
 	~CLoadFile();
 	int read(const void * data, unsigned size);
 
 	void close();
 	void openNextFile(const std::string &fname, bool requireLatest);
+	void reportState(CLogger &out);
 };
 
 typedef boost::asio::basic_stream_socket < boost::asio::ip::tcp , boost::asio::stream_socket_service<boost::asio::ip::tcp>  > TSocket;
@@ -829,6 +842,7 @@ class DLL_EXPORT CConnection
 	CConnection(void);
 
 	void init();
+	void reportState(CLogger &out);
 public:
 	boost::mutex *rmx, *wmx; // read/write mutexes
 	TSocket * socket;

+ 6 - 6
lib/VCMI_Lib.cpp

@@ -27,12 +27,12 @@ LibClasses * VLC = NULL;
 DLL_EXPORT CLodHandler *bitmaph = NULL, 
 	*spriteh = NULL;
 
-DLL_EXPORT CLogger<0> tlog0;
-DLL_EXPORT CLogger<1> tlog1;
-DLL_EXPORT CLogger<2> tlog2;
-DLL_EXPORT CLogger<3> tlog3;
-DLL_EXPORT CLogger<4> tlog4;
-DLL_EXPORT CLogger<5> tlog5;
+DLL_EXPORT CLogger tlog0(0);
+DLL_EXPORT CLogger tlog1(1);
+DLL_EXPORT CLogger tlog2(2);
+DLL_EXPORT CLogger tlog3(3);
+DLL_EXPORT CLogger tlog4(4);
+DLL_EXPORT CLogger tlog5(5);
 
 DLL_EXPORT CConsoleHandler *console = NULL;
 DLL_EXPORT std::ostream *logfile = NULL