summaryrefslogblamecommitdiffstats
path: root/src/Log.cpp
blob: a1b4c784fd06c9b7d72d8bfa533a7e5b4e8d1123 (plain) (tree)
1
2
3
4
5
6
7
8
9


                                                                                              
                


                  
                               
 

                      
                   


                 
                        

                                
      






                                       
                      



                                
                                                              
 
                                                                           

















                         

                          
                             
         






















                                            
                       











                                             
                       













                                                    
                                                                 

                        
                                                  

                       

                       
                            







                                                

                     





                                                                                                                                                            

                   
                                                      



                               
























































                                                                                                                                                                   
 



                                                                                     






















                                                      

      


















                                                                                                                                                    


                                                                                                                                                                                                    

              

                                                                             
                                                  






                         
                                          
 
                        
                                    
                               






                        
                                           
 
                            




 

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules

#include "Log.h"

#include <fstream>
#include <ctime>
#include "OSSupport/IsThread.h"

#ifdef __linux
#include <sys/ioctl.h>
#include <unistd.h>
#endif // __linux


#if defined(ANDROID_NDK)
	#include <android/log.h>
	#include "ToJava.h"
#endif




cLog* cLog::s_Log = NULL;

cLog::cLog(const AString & a_FileName )
	: m_File(NULL)
{
	s_Log = this;

	// create logs directory
	cFile::CreateFolder(FILE_IO_PREFIX + AString("logs"));

	OpenLog((FILE_IO_PREFIX + AString("logs/") + a_FileName).c_str() );
}





cLog::~cLog()
{
	CloseLog();
	s_Log = NULL;
}





cLog* cLog::GetInstance()
{
	if (s_Log != NULL)
	{
		return s_Log;
	}

	new cLog("log.txt");
	return s_Log;
}





void cLog::CloseLog()
{
	if( m_File )
		fclose (m_File);
	m_File = 0;
}





void cLog::OpenLog( const char* a_FileName )
{
	if(m_File) fclose (m_File);
	#ifdef _MSC_VER
	fopen_s( &m_File, a_FileName, "a+" );
	#else
	m_File = fopen(a_FileName, "a+" );
	#endif
}





void cLog::ClearLog()
{
	#ifdef _MSC_VER
	if( fopen_s( &m_File, "log.txt", "w" ) == 0)
		fclose (m_File);
	#else
	m_File = fopen("log.txt", "w" );
	if( m_File )
		fclose (m_File);
	#endif
	m_File = 0;
}





bool cLog::LogReplaceLine(const char * a_Format, va_list argList)
{
	AString Message;
	AppendVPrintf(Message, a_Format, argList);

	time_t rawtime;
	time(&rawtime);

	struct tm* timeinfo;

	#ifdef _MSC_VER
		struct tm timeinforeal;
		timeinfo = &timeinforeal;
		localtime_s(timeinfo, &rawtime);
	#else
		timeinfo = localtime(&rawtime);
	#endif

	AString Line;
	#ifdef _DEBUG
		Printf(Line, "[%04x|%02d:%02d:%02d] %s", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str());
	#else
		Printf(Line, "[%02d:%02d:%02d] %s", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str());
	#endif

	if (m_File)
	{
		fprintf(m_File, "%s\n", Line.c_str());
		fflush(m_File);
	}

	// Print to console:
	#if defined(ANDROID_NDK)
		//__android_log_vprint(ANDROID_LOG_ERROR,"MCServer", a_Format, argList);
		__android_log_print(ANDROID_LOG_ERROR, "MCServer", "%s", Line.c_str());
		//CallJavaFunction_Void_String(g_JavaThread, "AddToLog", Line );
	#else
		
		size_t LineLength = Line.length();
		#ifdef _WIN32
			HANDLE Output = GetStdHandle(STD_OUTPUT_HANDLE);

			CONSOLE_SCREEN_BUFFER_INFO csbi;
			GetConsoleScreenBufferInfo(Output, &csbi); // Obtain console window information

			if ((size_t)((csbi.srWindow.Right - csbi.srWindow.Left) + 1) < LineLength) // Will text overrun width? If so, do not do a replace operation
			{
				printf("\n%s", Line.c_str()); // We are at line to be replaced, but since we can't, print normally with a newline
				return false;
			}

			for (size_t X = 0; X != (size_t)(csbi.srWindow.Right - csbi.srWindow.Left); ++X)
			{
				fputs(" ", stdout); // Clear current line in preparation for new text
			}
		#else // _WIN32
			// Try to get console width; if text overruns, print normally with a newline
			#ifdef TIOCGSIZE
				struct ttysize ts;
				ioctl(STDIN_FILENO, TIOCGSIZE, &ts);

				if (ts.ts_cols < LineLength)
				{
					printf("\n%s", Line.c_str());
					return false;
				}
			#elif defined(TIOCGWINSZ)
				struct winsize ts;
				ioctl(STDIN_FILENO, TIOCGWINSZ, &ts);

				if (ts.ws_col < LineLength)
				{
					printf("\n%s", Line.c_str());
					return false;
				}
			#else
				printf("\n%s", Line.c_str());
				return false;
			#endif

			fputs("\033[K", stdout); // Clear current line
		#endif // Not _WIN32

		printf("\r%s", Line.c_str());

		#ifdef __linux
			fputs("\033[1B", stdout); // Move down one line
		#endif // __linux
	#endif // ANDROID_NDK

	#if defined (_WIN32) && defined(_DEBUG)
		// In a Windows Debug build, output the log to debug console as well:
		OutputDebugStringA((Line + "\n").c_str());
	#endif  // _WIN32

	return true;
}





void cLog::Log(const char * a_Format, va_list argList)
{
	AString Message;
	AppendVPrintf(Message, a_Format, argList);

	time_t rawtime;
	time ( &rawtime );
	
	struct tm* timeinfo;
#ifdef _MSC_VER
	struct tm timeinforeal;
	timeinfo = &timeinforeal;
	localtime_s(timeinfo, &rawtime );
#else
	timeinfo = localtime( &rawtime );
#endif

	AString Line;
	#ifdef _DEBUG
	Printf(Line, "[%04x|%02d:%02d:%02d] %s", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str());
	#else
	Printf(Line, "[%02d:%02d:%02d] %s", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, Message.c_str());
	#endif

	if (m_File)
	{
		fprintf(m_File, "%s\n", Line.c_str());
		fflush(m_File);
	}

	// Print to console:
	#if defined(ANDROID_NDK)
	//__android_log_vprint(ANDROID_LOG_ERROR,"MCServer", a_Format, argList);
	__android_log_print(ANDROID_LOG_ERROR, "MCServer", "%s", Line.c_str() );
	//CallJavaFunction_Void_String(g_JavaThread, "AddToLog", Line );
	#else
	// If something requests the current line to be rewritten (LogReplaceLine), on Linux it clears the current line, but that moves the cursor to the end of that line, so we reset it here (\r)
	// Doesn't affect anything normally - cursor should already be at beginning of line
	printf("\r%s", Line.c_str());
	#endif

	#if defined (_WIN32) && defined(_DEBUG)
	// In a Windows Debug build, output the log to debug console as well:
	OutputDebugStringA((Line + "\n").c_str());
	#endif  // _WIN32
}





void cLog::Log(const char * a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	Log(a_Format, argList);
	va_end(argList);
}





void cLog::SimpleLog(const char * a_String)
{
	Log("%s", a_String);
}