Browse Source

Refactor the logger to not attempt to write to logfile on iOS. May fix #15

Matt Coburn 5 years ago
parent
commit
058fd02636
3 changed files with 24 additions and 9 deletions
  1. 1 1
      Build-iOS/Build-iOS.command
  2. 1 1
      CMakeLists.txt
  3. 22 7
      Source/Native/custom/enet_logging.h

+ 1 - 1
Build-iOS/Build-iOS.command

@@ -46,7 +46,7 @@ then
 		DEBUG_STATUS=1
 	fi
 	
-	cmake $CODE_ROOT -B$CODE_ROOT/build -G Xcode -DCMAKE_TOOLCHAIN_FILE=$UPPER_ROOT/MobileToolchains/ios.toolchain.cmake -DPLATFORM=OS -DENABLE_ARC=0 -DENABLE_VISIBILITY=0 -DENET_DEBUG=$DEBUG_STATUS -DENET_STATIC=1 -DENET_SHARED=0 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO	
+	cmake $CODE_ROOT -B$CODE_ROOT/build -G Xcode -DCMAKE_TOOLCHAIN_FILE=$UPPER_ROOT/MobileToolchains/ios.toolchain.cmake -DPLATFORM=OS -DENABLE_ARC=0 -DENABLE_VISIBILITY=0 -DENET_ON_APPLE_IOS=1 -DENET_DEBUG=$DEBUG_STATUS -DENET_STATIC=1 -DENET_SHARED=0 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_ALLOWED=NO	
 	
 	if [ $? -eq 0 ] 
 	then

+ 1 - 1
CMakeLists.txt

@@ -24,7 +24,7 @@ set(ENET_LIB_NAME enet)
 cmake_minimum_required(VERSION 3.1)
 project(${ENET_LIB_NAME} LANGUAGES C)
 
-set(ENET_DEBUG OFF CACHE BOOL "Do debug things")
+set(ENET_DEBUG OFF CACHE BOOL "Enables debug and trace logging")
 set(ENET_PLUGIN_DIR_BASE "${CMAKE_CURRENT_SOURCE_DIR}/Unity/Plugins")
 set(ENET_PLUGIN_DIR_ARCH "x86_64")
 set(ENET_DEFINES -DENET_NO_PRAGMA_LINK -DENET_DLL)

+ 22 - 7
Source/Native/custom/enet_logging.h

@@ -4,6 +4,7 @@
 #include <stdarg.h>
 #include <stdio.h>
 
+// TODO: Make better filenames; ie. enet_log.pid.txt
 #define ENET_LOG_FILE "enet_log.txt"
 
 static FILE *enet_log_fp = NULL;
@@ -29,24 +30,38 @@ static const char *const enet_log_type_names[] = {
 
 static inline void enet_log(enum enet_log_type type, const char *func, int line, const char *fmt, ...)
 {
-	if (!enet_log_fp) enet_log_fp = fopen(ENET_LOG_FILE, "a");
-	if (!enet_log_fp) return;
-
 	va_list args;
 	time_t tstamp = time(NULL);
 	struct tm *local_time = localtime(&tstamp);
 	char time_buf[64];
 
 	time_buf[strftime(time_buf, sizeof(time_buf), "%Y-%m-%d %H:%M:%S", local_time)] = '\0';
-	fprintf(enet_log_fp, "%s [%s] [%s:%d] ", time_buf, enet_log_type_names[type], func, line);
 
+#if ENET_ON_APPLE_IOS
+	// https://github.com/SoftwareGuy/ENet-CSharp/issues/15
+	// iOS Debugging - File Access Permission (#blameApple)
+	// Can't write to files without the file permission... so don't do that if we're on Apple.
+
+	// Prefix
+	printf("%s [%s] [%s:%d] ", time_buf, enet_log_type_names[type], func, line);
+
+	va_start(args, fmt);	
+	vprintf(fmt, args);	
+	va_end(args);
+
+	printf("\n");
+#else
+	// Open the log file
+	if (!enet_log_fp) enet_log_fp = fopen(ENET_LOG_FILE, "a");
+	if (!enet_log_fp) return;
+
+	fprintf(enet_log_fp, "%s [%s] [%s:%d] \n", time_buf, enet_log_type_names[type], func, line);
 	va_start(args, fmt);
 	vfprintf(enet_log_fp, fmt, args);
 	va_end(args);
 
-	fprintf(enet_log_fp, "\n");
 	fflush(enet_log_fp);
-}
-
+#endif
 
+}
 #endif