Browse Source

Add 'LogLevel' configuration option & make logging always enabled

LouYihua 7 years ago
parent
commit
6d5b0d1bda
9 changed files with 37 additions and 41 deletions
  1. 0 2
      android/app/src/main/cpp/android_jni.cpp
  2. 12 0
      common.h
  3. 4 0
      palcfg.c
  4. 2 0
      palcfg.h
  5. 10 13
      unix/unix.cpp
  6. 7 8
      util.c
  7. 0 12
      util.h
  8. 1 3
      win32/win32.cpp
  9. 1 3
      winrt/SDLPal.Common/WinRTUtil.cpp

+ 0 - 2
android/app/src/main/cpp/android_jni.cpp

@@ -185,7 +185,6 @@ UTIL_Platform_Init(
    char* argv[]
 )
 {
-#if defined(DEBUG)
 	UTIL_LogSetOutput([](LOGLEVEL level, const char*, const char* str)->void {
 		const static int level_mapping[] = {
 			ANDROID_LOG_VERBOSE,
@@ -196,7 +195,6 @@ UTIL_Platform_Init(
 		};
 		__android_log_print(level_mapping[level], TAG, "%s", str);
 	}, 1024, TRUE);
-#endif
 
    gConfig.fLaunchSetting = FALSE;
    return 0;

+ 12 - 0
common.h

@@ -214,4 +214,16 @@ typedef const WCHAR        *LPCWSTR;
 
 #define PAL_fread(buf, elem, num, fp) if (fread((buf), (elem), (num), (fp)) < (num)) return -1
 
+typedef enum tagLOGLEVEL
+{
+	LOGLEVEL_MIN,
+	LOGLEVEL_VERBOSE = LOGLEVEL_MIN,
+	LOGLEVEL_DEBUG,
+	LOGLEVEL_INFO,
+	LOGLEVEL_WARNING,
+	LOGLEVEL_ERROR,
+	LOGLEVEL_FATAL,
+	LOGLEVEL_MAX = LOGLEVEL_FATAL,
+} LOGLEVEL;
+
 #endif

+ 4 - 0
palcfg.c

@@ -45,6 +45,8 @@ static const ConfigItem gConfigItems[PALCFG_ALL_MAX] = {
 	{ PALCFG_USETOUCHOVERLAY,   PALCFG_BOOLEAN,  "UseTouchOverlay",   15, MAKE_VALUE(PAL_HAS_TOUCH,                 FALSE,                 TRUE) },
 
 	{ PALCFG_SURROUNDOPLOFFSET, PALCFG_INTEGER,  "SurroundOPLOffset", 17, MAKE_VALUE(384,                           INT32_MIN,             INT32_MAX) },
+	{ PALCFG_LOGLEVEL,          PALCFG_INTEGER,  "LogLevel",           8, MAKE_VALUE(LOGLEVEL_MAX,                  LOGLEVEL_MIN,          LOGLEVEL_MAX) },
+
 	{ PALCFG_AUDIOBUFFERSIZE,   PALCFG_UNSIGNED, "AudioBufferSize",   15, MAKE_VALUE(PAL_AUDIO_DEFAULT_BUFFER_SIZE, 2,                     32768) },
 	{ PALCFG_CODEPAGE,          PALCFG_UNSIGNED, "CodePage",           8, MAKE_VALUE(CP_BIG5,                       CP_MIN,                CP_MAX - 1) },            // Default for BIG5
 	{ PALCFG_OPLSAMPLERATE,     PALCFG_UNSIGNED, "OPLSampleRate",     13, MAKE_VALUE(49716,                         0,                     UINT32_MAX) },
@@ -400,6 +402,7 @@ PAL_LoadConfig(
 	gConfig.iAudioChannels = values[PALCFG_STEREO].bValue ? 2 : 1;
 
 	gConfig.iSurroundOPLOffset = values[PALCFG_SURROUNDOPLOFFSET].iValue;
+	gConfig.iLogLevel = values[PALCFG_LOGLEVEL].iValue;
 
 	gConfig.iSampleRate = values[PALCFG_SAMPLERATE].uValue;
 	gConfig.iOPLSampleRate = values[PALCFG_OPLSAMPLERATE].uValue;
@@ -443,6 +446,7 @@ PAL_SaveConfig(
 		sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_USETOUCHOVERLAY), gConfig.fUseTouchOverlay); fputs(buf, fp);
 
 		sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_SURROUNDOPLOFFSET), gConfig.iSurroundOPLOffset); fputs(buf, fp);
+		sprintf(buf, "%s=%d\n", PAL_ConfigName(PALCFG_LOGLEVEL), gConfig.iLogLevel); fputs(buf, fp);
 
 		sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_AUDIOBUFFERSIZE), gConfig.wAudioBufferSize); fputs(buf, fp);
 		sprintf(buf, "%s=%u\n", PAL_ConfigName(PALCFG_CODEPAGE), gConfig.uCodePage); fputs(buf, fp);

+ 2 - 0
palcfg.h

@@ -51,6 +51,7 @@ typedef enum tagPALCFG_ITEM
 	PALCFG_INTEGER_MIN = PALCFG_BOOLEAN_MAX,
 	/* Integers */
 	PALCFG_SURROUNDOPLOFFSET = PALCFG_INTEGER_MIN,
+	PALCFG_LOGLEVEL,
 	/* Integers */
 	PALCFG_INTEGER_MAX,
 
@@ -177,6 +178,7 @@ typedef struct tagCONFIGURATION
 	INT              iResampleQuality;
 	INT              iMusicVolume;
 	INT              iSoundVolume;
+	LOGLEVEL         iLogLevel;
 	MUSICTYPE        eMusicType;
 	MUSICTYPE        eCDType;
 	OPLTYPE          eOPLType;

+ 10 - 13
unix/unix.cpp

@@ -252,22 +252,19 @@ 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);
+		const static int priorities[] = {
+			LOG_DEBUG,
+			LOG_DEBUG,
+			LOG_INFO,
+			LOG_WARNING,
+			LOG_ERR,
+			LOG_EMERG
+		};
+		syslog(priorities[level], "%s", str);
 	}, 1024, TRUE);
-#endif
+
 #if !defined(UNIT_TEST) && !defined(PAL_NO_LAUNCH_UI)
    if (gConfig.fLaunchSetting)
    {

+ 7 - 8
util.c

@@ -583,7 +583,6 @@ 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[] = {
@@ -630,14 +629,14 @@ UTIL_LogOutput(
 	int          maxloglen = _max_log_length;
 	int          local_alloc = (buf == NULL);
 
-	if (level < _min_loglevel || !callback || maxloglen <= 0) return;
-	if (local_alloc) buf = (char *)malloc(maxloglen + _log_extra_length);
-	if (NULL == buf) return;
+	if (level < gConfig.iLogLevel || !callback || maxloglen <= 0) return;
+	if (local_alloc && NULL == (buf = (char *)malloc(maxloglen + _log_extra_length))) return;
+	if (level > LOGLEVEL_MAX) level = LOGLEVEL_MAX;
 
 	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]);
+		_loglevel_str[level]);
 
 	va_start(va, fmt);
 	vsnprintf(buf + _log_extra_length - 1, maxloglen + 1, fmt, va);
@@ -653,9 +652,9 @@ UTIL_LogSetLevel(
 )
 {
 	if (minlevel < LOGLEVEL_MIN)
-		_min_loglevel = LOGLEVEL_MIN;
+		gConfig.iLogLevel = LOGLEVEL_MIN;
 	else if (minlevel > LOGLEVEL_MAX)
-		_min_loglevel = LOGLEVEL_MAX;
+		gConfig.iLogLevel = LOGLEVEL_MAX;
 	else
-		_min_loglevel = minlevel;
+		gConfig.iLogLevel = minlevel;
 }

+ 0 - 12
util.h

@@ -140,18 +140,6 @@ UTIL_Platform_Quit(
  * Logging utilities
  */
 
-typedef enum LOGLEVEL
-{
-	LOGLEVEL_MIN,
-	LOGLEVEL_VERBOSE = LOGLEVEL_MIN,
-	LOGLEVEL_DEBUG,
-	LOGLEVEL_INFO,
-	LOGLEVEL_WARNING,
-	LOGLEVEL_ERROR,
-	LOGLEVEL_FATAL,
-	LOGLEVEL_MAX = LOGLEVEL_FATAL,
-} LOGLEVEL;
-
 /*++
   Purpose:
 

+ 1 - 3
win32/win32.cpp

@@ -285,12 +285,10 @@ 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
+	// Defaults log to debug output
 	UTIL_LogSetOutput([](LOGLEVEL, const char* str, const char*)->void {
 		OutputDebugStringA(str);
 	}, 1024, TRUE);
-#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

+ 1 - 3
winrt/SDLPal.Common/WinRTUtil.cpp

@@ -162,12 +162,10 @@ static int input_event_filter(const SDL_Event *lpEvent, PALINPUTSTATE *state)
 extern "C"
 INT UTIL_Platform_Init(int argc, char* argv[])
 {
-#if defined(_DEBUG)
-	// Defaults log to debug output on debug builds
+	// Defaults log to debug output
 	UTIL_LogSetOutput([](LOGLEVEL, const char* str, const char*)->void {
 		OutputDebugStringA(str);
 	}, 1024, TRUE);
-#endif
 
 	CreateRunningFile();