Browse Source

Log: fix year bug & allow buffer type selection & Linux implementation & Add comments

Lou Yihua 7 years ago
parent
commit
76ae394280
4 changed files with 119 additions and 15 deletions
  1. 24 3
      unix/unix.cpp
  2. 22 9
      util.c
  3. 72 2
      util.h
  4. 1 1
      win32/win32.cpp

+ 24 - 3
unix/unix.cpp

@@ -2,9 +2,11 @@
 #include <stdlib.h>
 #include <string.h>
 
-#include "../../global.h"
-#include "../../util.h"
-#include "../../palcfg.h"
+#include "global.h"
+#include "util.h"
+#include "palcfg.h"
+
+#include <syslog.h>
 
 #ifndef PAL_NO_LAUNCH_UI
 
@@ -250,6 +252,22 @@ UTIL_Platform_Init(
    char* argv[]
 )
 {
+#if defined(_DEBUG)
+	openlog("sdlpal", LOG_PERROR | LOG_PID, LOG_USER);
+	UTIL_LogSetOutput([](LOGLEVEL level, const char* str, const char*)->void {
+		int priority = LOG_DEBUG;
+		switch (level)
+		{
+		case LOGLEVEL_VERBOSE: priority = LOG_DEBUG; break;
+		case LOGLEVEL_DEBUG:   priority = LOG_DEBUG; break;
+		case LOGLEVEL_INFO:    priority = LOG_INFO; break;
+		case LOGLEVEL_WARNING: priority = LOG_WARNING; break;
+		case LOGLEVEL_ERROR:   priority = LOG_ERR; break;
+		case LOGLEVEL_FATAL:   priority = LOG_EMERG; break;
+		}
+		syslog(priority, "%s", str);
+	}, 1024, TRUE);
+#endif
 #if !defined(UNIT_TEST) && !defined(PAL_NO_LAUNCH_UI)
    if (gConfig.fLaunchSetting)
    {
@@ -270,4 +288,7 @@ UTIL_Platform_Quit(
    VOID
 )
 {
+#if defined(_DEBUG)
+	closelog();
+#endif
 }

+ 22 - 9
util.c

@@ -581,8 +581,10 @@ UTIL_Platform_Quit(
 */
 
 static LOGCALLBACK _log_callback = NULL;
+static char *_global_log_buffer = NULL;
 static int _max_log_length = 0;
 static LOGLEVEL _min_loglevel = LOGLEVEL_WARNING;
+static const int _log_extra_length = 32;
 
 static const char * const _loglevel_str[] = {
 	"[VERBOSE]",
@@ -596,11 +598,21 @@ static const char * const _loglevel_str[] = {
 void
 UTIL_LogSetOutput(
 	LOGCALLBACK    callback,
-	int            maxloglen
+	int            maxloglen,
+	BOOL           staticbuffer
 )
 {
 	_log_callback = callback;
 	_max_log_length = maxloglen;
+	if (staticbuffer)
+	{
+		_global_log_buffer = (char *)realloc(_global_log_buffer, maxloglen + _log_extra_length);
+	}
+	else
+	{
+		free(_global_log_buffer);
+		_global_log_buffer = NULL;
+	}
 }
 
 void
@@ -613,25 +625,26 @@ UTIL_LogOutput(
 	va_list      va;
 	time_t       tv = time(NULL);
 	struct tm   *tmval = localtime(&tv);
+	char        *buf = _global_log_buffer;
 	LOGCALLBACK  callback = _log_callback;
 	int          maxloglen = _max_log_length;
-	char        *buf;
+	int          local_alloc = (buf == NULL);
 
-	if (level < _min_loglevel || !callback || maxloglen <= 0 ||
-		NULL == (buf = (char *)malloc(maxloglen + 32)))
-		return;
+	if (level < _min_loglevel || !callback || maxloglen <= 0) return;
+	if (local_alloc) buf = (char *)malloc(maxloglen + _log_extra_length);
+	if (NULL == buf) return;
 
-	snprintf(buf, 32, "%04d-%02d-%02d %02d:%02d:%02d %s: ",
-		tmval->tm_year, tmval->tm_mon, tmval->tm_mday,
+	snprintf(buf, _log_extra_length, "%04d-%02d-%02d %02d:%02d:%02d %s: ",
+		tmval->tm_year + 1900, tmval->tm_mon, tmval->tm_mday,
 		tmval->tm_hour, tmval->tm_min, tmval->tm_sec,
 		_loglevel_str[level > LOGLEVEL_MAX ? LOGLEVEL_MAX : level]);
 
 	va_start(va, fmt);
-	vsnprintf(buf + 31, maxloglen + 1, fmt, va);
+	vsnprintf(buf + _log_extra_length - 1, maxloglen + 1, fmt, va);
 	va_end(va);
 
 	callback(level, buf, buf + 31);
-	free(buf);
+	if (local_alloc) free(buf);
 }
 
 void

+ 72 - 2
util.h

@@ -152,14 +152,68 @@ typedef enum LOGLEVEL
 	LOGLEVEL_MAX = LOGLEVEL_FATAL,
 } LOGLEVEL;
 
-typedef void(*LOGCALLBACK)(LOGLEVEL, const char *, const char *);
+/*++
+  Purpose:
 
+    The pointer to callback function that produces actual log output.
+
+  Parameters:
+
+    [IN]  level    - The log level of this output call.
+	[IN]  full_log - The full log string produced by UTIL_LogOutput.
+	[IN]  user_log - The log string produced by user-provided format.
+
+  Return value:
+
+    None.
+
+--*/
+typedef void(*LOGCALLBACK)(LOGLEVEL level, const char *full_log, const char *user_log);
+
+/*++
+  Purpose:
+
+    Initialize the internal log system.
+
+  Parameters:
+
+    [IN]  callback     - The callback function to be called at each
+	                     call of UTIL_LogOutput.
+    [IN]  maxloglen    - The max buffer size that holds the output
+	                     correspoind to user-provided format string,
+					     not including the terminal null-character.
+    [IN]  staticbuffer - Whether UTIL_LogOutput should generate the
+	                     output string into a one-time allocated global
+						 buffer, or to a per-call allocated locall buffer.
+
+  Return value:
+
+    None.
+
+--*/
 void
 UTIL_LogSetOutput(
 	LOGCALLBACK    callback,
-	int            maxloglen
+	int            maxloglen,
+	BOOL           staticbuffer
 );
 
+/*++
+  Purpose:
+
+    Set the minimal log level that could be output.
+	Any level below this level will produce no output.
+
+  Parameters:
+
+    [IN]  minlevel - The minimal log level, must be within the
+	                 range [LOGLEVEL_MIN, LOGLEVEL_MAX].
+
+  Return value:
+
+    None.
+
+--*/
 void
 UTIL_LogOutput(
 	LOGLEVEL       level,
@@ -167,6 +221,22 @@ UTIL_LogOutput(
 	...
 );
 
+/*++
+  Purpose:
+
+    Set the minimal log level that could be output.
+	Any level below this level will produce no output.
+
+  Parameters:
+
+    [IN]  minlevel - The minimal log level, must be within the
+	                 range [LOGLEVEL_MIN, LOGLEVEL_MAX].
+
+  Return value:
+
+    None.
+
+--*/
 void
 UTIL_LogSetLevel(
 	LOGLEVEL       minlevel

+ 1 - 1
win32/win32.cpp

@@ -289,7 +289,7 @@ extern "C" int UTIL_Platform_Init(int argc, char* argv[])
 	// Defaults log to debug output on debug builds
 	UTIL_LogSetOutput([](LOGLEVEL, const char* str, const char*)->void {
 		OutputDebugStringA(str);
-	}, 1024);
+	}, 1024, TRUE);
 #endif
 
 	g_hInstance = GetModuleHandle(nullptr);