Browse Source

New log framework

LouYihua 7 years ago
parent
commit
423420d4be
6 changed files with 107 additions and 98 deletions
  1. 0 4
      main.c
  2. 1 1
      script.c
  3. 1 3
      text.c
  4. 58 49
      util.c
  5. 40 41
      util.h
  6. 7 0
      win32/win32.cpp

+ 0 - 4
main.c

@@ -150,8 +150,6 @@ PAL_Shutdown(
    PAL_ShutdownInput();
    VIDEO_Shutdown();
 
-   UTIL_CloseLog();
-
    SDL_Quit();
    UTIL_Platform_Quit();
 #if defined(LONGJMP_EXIT)
@@ -479,8 +477,6 @@ main(
    }
 #endif
 
-   UTIL_OpenLog();
-
 #if !defined(UNIT_TEST) || defined(UNIT_TEST_GAME_INIT)
    PAL_LoadConfig(TRUE);
 

+ 1 - 1
script.c

@@ -3044,7 +3044,7 @@ PAL_RunTriggerScript(
    {
       pScript = &(gpGlobals->g.lprgScriptEntry[wScriptEntry]);
 
-      UTIL_WriteLog(LOG_DEBUG, "[SCRIPT] %.4x: %.4x %.4x %.4x %.4x\n", wScriptEntry,
+      UTIL_LogOutput(LOGLEVEL_DEBUG, "[SCRIPT] %.4x: %.4x %.4x %.4x %.4x\n", wScriptEntry,
          pScript->wOperation, pScript->rgwOperand[0], pScript->rgwOperand[1],
          pScript->rgwOperand[2], pScript->rgwOperand[3]);
 

+ 1 - 3
text.c

@@ -260,9 +260,7 @@ PAL_ReadMessageFile(
 					else
 					{
 						// Just ignore invalid lines
-#ifdef ENABLE_LOG
-						UTIL_WriteLog(LOG_ERR, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", line);
-#endif
+						UTIL_LogOutput(LOGLEVEL_WARNING, "PAL_ReadMessageFile(): encounter invalid line '%s'!\n", buffer);
 					}
 				}
 				break;

+ 58 - 49
util.c

@@ -576,65 +576,74 @@ UTIL_Platform_Quit(
 
 #endif
 
-#ifdef ENABLE_LOG
+/*
+* Logging utilities
+*/
 
-static FILE *pLogFile = NULL;
+static LOGLEVEL _min_loglevel = LOGLEVEL_WARNING;
 
-FILE *
-UTIL_OpenLog(
-   VOID
+static void(*_log_callback)(const char *, const char *) = NULL;
+static int _max_log_length = 0;
+
+static const char * const _loglevel_str[] = {
+	"[VERBOSE]",
+	"  [DEBUG]",
+	"   [INFO]",
+	"[WARNING]",
+	"  [ERROR]",
+	"  [FATAL]",
+};
+
+void
+UTIL_LogSetOutput(
+	LOGCALLBACK    callback,
+	int            maxloglen
 )
 {
-   if ((pLogFile = fopen(va("%slog.txt", gConfig.pszSavePath), "a+")) == NULL)
-   {
-      return NULL;
-   }
-
-   return pLogFile;
+	_log_callback = callback;
+	_max_log_length = maxloglen;
 }
 
-VOID
-UTIL_CloseLog(
-   VOID
+void
+UTIL_LogOutput(
+	LOGLEVEL       level,
+	const char    *fmt,
+	...
 )
 {
-   if (pLogFile != NULL)
-   {
-      fclose(pLogFile);
-   }
+	va_list      va;
+	time_t       tv = time(NULL);
+	struct tm   *tmval = localtime(&tv);
+	LOGCALLBACK  callback = _log_callback;
+	int          maxloglen = _max_log_length;
+	char        *buf;
+
+	if (level < _min_loglevel || !callback || maxloglen <= 0 ||
+		NULL == (buf = (char *)malloc(maxloglen + 32)))
+		return;
+
+	snprintf(buf, 32, "%04d-%02d-%02d %02d:%02d:%02d %s: ",
+		tmval->tm_year, 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);
+	va_end(va);
+
+	callback(buf, buf + 31);
+	free(buf);
 }
 
-VOID
-UTIL_WriteLog(
-   int             Priority,
-   const char     *Fmt,
-   ...
+void
+UTIL_LogSetLevel(
+	LOGLEVEL       minlevel
 )
 {
-   va_list       vaa;
-   time_t        lTime;
-   struct tm    *curTime;
-   char          szDateBuf[260];
-
-   time(&lTime);
-
-   if ((Priority < LOG_EMERG) || (Priority >= LOG_LAST_PRIORITY))
-   {
-      return;
-   }
-
-   curTime = localtime(&lTime);
-   strftime(szDateBuf, 128, "%Y-%m-%d   %H:%M:%S", curTime);
-   szDateBuf[strlen(szDateBuf) - 1] = '\0'; //remove the
-
-   va_start(vaa,Fmt);
-
-   fprintf(pLogFile, "[%s]", szDateBuf);
-   vfprintf(pLogFile, Fmt, vaa);
-   fprintf(pLogFile, "\n");
-   fflush(pLogFile);
-
-   va_end(vaa);
+	if (minlevel < LOGLEVEL_MIN)
+		_min_loglevel = LOGLEVEL_MIN;
+	else if (minlevel > LOGLEVEL_MAX)
+		_min_loglevel = LOGLEVEL_MAX;
+	else
+		_min_loglevel = minlevel;
 }
-
-#endif

+ 40 - 41
util.h

@@ -25,8 +25,6 @@
 
 #include "common.h"
 
-//#define ENABLE_LOG 1
-
 PAL_C_LINKAGE_BEGIN
 
 void
@@ -110,69 +108,70 @@ UTIL_CloseFile(
    FILE                *fp
 );
 
+
+/*
+ * Platform-specific utilities
+ */
+
 BOOL
 UTIL_GetScreenSize(
-   DWORD *pdwScreenWidth,
-   DWORD *pdwScreenHeight
+	DWORD *pdwScreenWidth,
+	DWORD *pdwScreenHeight
 );
 
 BOOL
 UTIL_IsAbsolutePath(
-	LPCSTR  lpszFileName
+	const char *lpszFileName
 );
 
-INT
+int
 UTIL_Platform_Init(
-   int argc,
-   char* argv[]
+	int   argc,
+	char *argv[]
 );
 
-VOID
+void
 UTIL_Platform_Quit(
-   VOID
+	void
 );
 
-PAL_C_LINKAGE_END
 
-#define LOG_EMERG           0 /* system is unusable */
-#define LOG_ALERT           1 /* action must be taken immediately */
-#define LOG_CRIT            2 /* critical conditions */
-#define LOG_ERR             3 /* error conditions */
-#define LOG_WARNING         4 /* warning conditions */
-#define LOG_NOTICE          5 /* normal but significant condition */
-#define LOG_INFO            6 /* informational */
-#define LOG_DEBUG           7 /* debug-level messages */
-#define LOG_LAST_PRIORITY   8 /* last level */
+/*
+ * Logging utilities
+ */
 
-#ifdef ENABLE_LOG
+typedef void(*LOGCALLBACK)(const char *, const char *);
 
-PAL_C_LINKAGE_BEGIN
+typedef enum LOGLEVEL
+{
+	LOGLEVEL_MIN,
+	LOGLEVEL_VERBOSE = LOGLEVEL_MIN,
+	LOGLEVEL_DEBUG,
+	LOGLEVEL_INFO,
+	LOGLEVEL_WARNING,
+	LOGLEVEL_ERROR,
+	LOGLEVEL_FATAL,
+	LOGLEVEL_MAX = LOGLEVEL_FATAL,
+} LOGLEVEL;
 
-FILE *
-UTIL_OpenLog(
-   VOID
+void
+UTIL_LogSetOutput(
+	LOGCALLBACK    callback,
+	int            maxloglen
 );
 
-VOID
-UTIL_CloseLog(
-   VOID
+void
+UTIL_LogOutput(
+	LOGLEVEL       level,
+	const char    *fmt,
+	...
 );
 
-VOID
-UTIL_WriteLog(
-   int             Priority,
-   const char     *Fmt,
-   ...
+void
+UTIL_LogSetLevel(
+	LOGLEVEL       minlevel
 );
 
 PAL_C_LINKAGE_END
 
-#else
-
-# define UTIL_OpenLog()       ((void)(0))
-# define UTIL_CloseLog()      ((void)(0))
-# define UTIL_WriteLog(...)   ((void)(0))
-
-#endif
-
 #endif

+ 7 - 0
win32/win32.cpp

@@ -285,6 +285,13 @@ INT_PTR CALLBACK LauncherDialogProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPAR
 
 extern "C" int UTIL_Platform_Init(int argc, char* argv[])
 {
+#if defined(_DEBUG)
+	// Defaults log to debug output on debug builds
+	UTIL_LogSetOutput([](const char* str, const char* _)->void {
+		OutputDebugStringA(str);
+	}, 1024);
+#endif
+
 	g_hInstance = GetModuleHandle(nullptr);
 #if !defined(__MINGW32__) || _WIN32_WINNT > _WIN32_WINNT_WS03 // compile time switch; use `make CCFLAGS=-D_WIN32_WINNT=_WIN32_WINNT_VISTA` for vista+ only automatic language detection
 	g_wLanguage = GetThreadUILanguage();