1
0

Added LOGREPLACELINE for line replacement

This commit is contained in:
Tiger Wang 2014-01-29 19:22:03 +00:00
parent ed7816419d
commit 7d03876a3e
5 changed files with 94 additions and 16 deletions

View File

@ -99,7 +99,7 @@ void cLog::ClearLog()
void cLog::Log(const char * a_Format, va_list argList) void cLog::Log(const char * a_Format, va_list argList, bool a_ReplaceCurrentLine)
{ {
AString Message; AString Message;
AppendVPrintf(Message, a_Format, argList); AppendVPrintf(Message, a_Format, argList);
@ -134,7 +134,34 @@ void cLog::Log(const char * a_Format, va_list argList)
__android_log_print(ANDROID_LOG_ERROR, "MCServer", "%s", Line.c_str() ); __android_log_print(ANDROID_LOG_ERROR, "MCServer", "%s", Line.c_str() );
//CallJavaFunction_Void_String(g_JavaThread, "AddToLog", Line ); //CallJavaFunction_Void_String(g_JavaThread, "AddToLog", Line );
#else #else
if (a_ReplaceCurrentLine)
{
#ifdef _WIN32
if (m_LastStringSize == 0)
{
m_LastStringSize = Line.length();
}
else if (Line.length() < m_LastStringSize) // If last printed line was longer than current, clear this line
{
for (size_t X = 0; X != m_LastStringSize; ++X)
{
fputs(" ", stdout);
}
}
#else // _WIN32
fputs("\033[K", stdout); // Clear current line
#endif
printf("\r%s", Line.c_str());
#ifdef __linux
fputs("\033[1B", stdout); // Move down one line
#endif // __linux
}
else
{
printf("%s", Line.c_str()); printf("%s", Line.c_str());
}
#endif #endif
#if defined (_WIN32) && defined(_DEBUG) #if defined (_WIN32) && defined(_DEBUG)

View File

@ -10,11 +10,11 @@ class cLog
private: private:
FILE * m_File; FILE * m_File;
static cLog * s_Log; static cLog * s_Log;
size_t m_LastStringSize = 0;
public: public:
cLog(const AString & a_FileName); cLog(const AString & a_FileName);
~cLog(); ~cLog();
void Log(const char * a_Format, va_list argList); void Log(const char * a_Format, va_list argList, bool a_ReplaceCurrentLine = false);
void Log(const char * a_Format, ...); void Log(const char * a_Format, ...);
// tolua_begin // tolua_begin
void SimpleLog(const char * a_String); void SimpleLog(const char * a_String);

View File

@ -119,14 +119,52 @@ void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
void cMCLogger::Log(const char * a_Format, va_list a_ArgList) void cMCLogger::Log(const char * a_Format, va_list a_ArgList, bool a_ShouldReplaceLine)
{ {
cCSLock Lock(m_CriticalSection); cCSLock Lock(m_CriticalSection);
if (!m_BeginLineUpdate && a_ShouldReplaceLine)
{
a_ShouldReplaceLine = false; // Print a normal line first if this is the initial replace line
m_BeginLineUpdate = true;
}
else if (m_BeginLineUpdate && !a_ShouldReplaceLine)
{
m_BeginLineUpdate = false;
}
if (a_ShouldReplaceLine)
{
#ifdef _WIN32
HANDLE Output = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(Output, &csbi);
COORD Position = { 0, csbi.dwCursorPosition.Y - 1 }; // Move cursor up one line
SetConsoleCursorPosition(Output, Position);
SetColor(csRegular); SetColor(csRegular);
m_Log->Log(a_Format, a_ArgList); m_Log->Log(a_Format, a_ArgList, a_ShouldReplaceLine);
ResetColor();
Position = { 0, csbi.dwCursorPosition.Y }; // Set cursor to original position
SetConsoleCursorPosition(Output, Position);
#else // _WIN32
fputs("\033[1A", stdout); // Move cursor up one line
SetColor(csRegular);
m_Log->Log(a_Format, a_ArgList, a_ShouldReplaceLine);
ResetColor();
#endif
}
else
{
SetColor(csRegular);
m_Log->Log(a_Format, a_ArgList, a_ShouldReplaceLine);
ResetColor(); ResetColor();
puts(""); puts("");
} }
}
@ -188,7 +226,7 @@ void cMCLogger::SetColor(eColorScheme a_Scheme)
default: ASSERT(!"Unhandled color scheme"); default: ASSERT(!"Unhandled color scheme");
} }
SetConsoleTextAttribute(g_Console, Attrib); SetConsoleTextAttribute(g_Console, Attrib);
#elif defined(__linux) && !defined(ANDROID_NDK) #elif (defined(__linux) || defined(__apple)) && !defined(ANDROID_NDK)
switch (a_Scheme) switch (a_Scheme)
{ {
case csRegular: printf("\x1b[0m"); break; // Whatever the console default is case csRegular: printf("\x1b[0m"); break; // Whatever the console default is
@ -224,6 +262,14 @@ void cMCLogger::ResetColor(void)
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
// Global functions // Global functions
void LOGREPLACELINE(const char* a_Format, ...)
{
va_list argList;
va_start(argList, a_Format);
cMCLogger::GetInstance()->Log(a_Format, argList, true);
va_end(argList);
}
void LOG(const char* a_Format, ...) void LOG(const char* a_Format, ...)
{ {
va_list argList; va_list argList;

View File

@ -21,7 +21,7 @@ public: // tolua_export
~cMCLogger(); // tolua_export ~cMCLogger(); // tolua_export
void Log(const char* a_Format, va_list a_ArgList); void Log(const char* a_Format, va_list a_ArgList, bool a_ShouldReplaceLine = false);
void Info(const char* a_Format, va_list a_ArgList); void Info(const char* a_Format, va_list a_ArgList);
void Warn(const char* a_Format, va_list a_ArgList); void Warn(const char* a_Format, va_list a_ArgList);
void Error(const char* a_Format, va_list a_ArgList); void Error(const char* a_Format, va_list a_ArgList);
@ -51,12 +51,17 @@ private:
/// Common initialization for all constructors, creates a logfile with the specified name and assigns s_MCLogger to this /// Common initialization for all constructors, creates a logfile with the specified name and assigns s_MCLogger to this
void InitLog(const AString & a_FileName); void InitLog(const AString & a_FileName);
/** Flag to show whether a 'replace line' log command has been issued
Used to decide when to put a newline */
bool m_BeginLineUpdate = false;
}; // tolua_export }; // tolua_export
extern void LOGREPLACELINE(const char* a_Format, ...);
extern void LOG(const char* a_Format, ...); extern void LOG(const char* a_Format, ...);
extern void LOGINFO(const char* a_Format, ...); extern void LOGINFO(const char* a_Format, ...);
extern void LOGWARN(const char* a_Format, ...); extern void LOGWARN(const char* a_Format, ...);

View File

@ -100,13 +100,13 @@ protected:
{ {
for (;;) for (;;)
{ {
LOG("%d chunks to load, %d chunks to generate", LOGREPLACELINE("%d chunks to load, %d chunks to generate",
m_World->GetStorage().GetLoadQueueLength(), m_World->GetStorage().GetLoadQueueLength(),
m_World->GetGenerator().GetQueueLength() m_World->GetGenerator().GetQueueLength()
); );
// Wait for 2 sec, but be "reasonably wakeable" when the thread is to finish // Wait for 0.5 sec, but be "reasonably wakeable" when the thread is to finish
for (int i = 0; i < 20; i++) for (int i = 0; i < 5; i++)
{ {
cSleep::MilliSleep(100); cSleep::MilliSleep(100);
if (m_ShouldTerminate) if (m_ShouldTerminate)
@ -152,10 +152,10 @@ protected:
{ {
for (;;) for (;;)
{ {
LOG("%d chunks remaining to light", m_Lighting->GetQueueLength() LOGREPLACELINE("%d chunks remaining to light", m_Lighting->GetQueueLength()
); );
// Wait for 2 sec, but be "reasonably wakeable" when the thread is to finish // Wait for 0.5 sec, but be "reasonably wakeable" when the thread is to finish
for (int i = 0; i < 20; i++) for (int i = 0; i < 20; i++)
{ {
cSleep::MilliSleep(100); cSleep::MilliSleep(100);