From da5158ffdee49811619b096b6951285f02758a7f Mon Sep 17 00:00:00 2001
From: WallK <wallykrasiy@gmail.com>
Date: Tue, 15 Oct 2024 08:29:00 +0300
Subject: [PATCH 1/4] GDScript Logger POC

---
 tools/configurator/scripts/class_functions.gd | 54 ++++++++++++++-----
 1 file changed, 42 insertions(+), 12 deletions(-)

diff --git a/tools/configurator/scripts/class_functions.gd b/tools/configurator/scripts/class_functions.gd
index a61ee1f4..e3222c5a 100644
--- a/tools/configurator/scripts/class_functions.gd
+++ b/tools/configurator/scripts/class_functions.gd
@@ -98,19 +98,49 @@ func multi_state(section: String, state: String) -> String:
 		state = "mixed"
 	return state
 		
+# func logger_bash(log_type: String, log_text: String) -> void:
+# 	# Type of log messages:
+# 	# log d - debug message: maybe in the future we can decide to hide them in main builds or if an option is toggled
+# 	# log i - normal informational message
+# 	# log w - waring: something is not expected but it's not a big deal
+# 	# log e - error: something broke
+# 	var log_header_text = "gdc_"
+# 	log_header_text+=log_text
+# 	log_parameters = ["log", log_type, log_header_text]
+# 	log_result = await run_thread_command(wrapper_command,log_parameters, false)
+# 	#log_result = await run_thread_command("find",["$HOME", "-name", "*.xml","-print"], false)
+# 	#print (log_result["exit_code"])
+# 	#print (log_result["output"])
+
 func logger(log_type: String, log_text: String) -> void:
-	# Type of log messages:
-	# log d - debug message: maybe in the future we can decide to hide them in main builds or if an option is toggled
-	# log i - normal informational message
-	# log w - waring: something is not expected but it's not a big deal
-	# log e - error: something broke
-	var log_header_text = "gdc_"
-	log_header_text+=log_text
-	log_parameters = ["log", log_type, log_header_text]
-	log_result = await run_thread_command(wrapper_command,log_parameters, false)
-	#log_result = await run_thread_command("find",["$HOME", "-name", "*.xml","-print"], false)
-	#print (log_result["exit_code"])
-	#print (log_result["output"])
+	var log_path: String = '/var/config/retrodeck/logs/gd_logs.log'
+	var log_file: FileAccess = FileAccess.open(log_path, FileAccess.WRITE)
+	var log_line: String = "GD "
+	match log_type:
+		'w':
+			log_line += "Warning "
+			print("Warning, mate")
+		'e':
+			log_line += "Error "
+			print("Error, mate")
+		'i':
+			log_line += "Info "
+			print("Info, mate")
+		'd':
+			log_line += "Debug "
+			print("Debug, mate")
+		_:
+			print("No idea, mate")
+	log_line += log_text
+	print(log_line)
+	if log_file:
+		log_file.seek_end()
+		log_file.store_line(log_line)
+		log_file.close()
+	else:
+		print("Something wrong with log file")
+
+# logger("e", "Could not open file: %s" % file_path)
 	
 func array_to_string(arr: Array) -> String:
 	var text: String

From 78b066ba8d125b21fe8103f42a4c849914ca301b Mon Sep 17 00:00:00 2001
From: WallK <wallykrasiy@gmail.com>
Date: Tue, 15 Oct 2024 08:43:54 +0300
Subject: [PATCH 2/4] Try to create log folder

---
 tools/configurator/scripts/class_functions.gd | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/tools/configurator/scripts/class_functions.gd b/tools/configurator/scripts/class_functions.gd
index e3222c5a..72b4eea0 100644
--- a/tools/configurator/scripts/class_functions.gd
+++ b/tools/configurator/scripts/class_functions.gd
@@ -113,8 +113,12 @@ func multi_state(section: String, state: String) -> String:
 # 	#print (log_result["output"])
 
 func logger(log_type: String, log_text: String) -> void:
+	var log_dir_path: String = "/var/config/retrodeck/logs/"
 	var log_path: String = '/var/config/retrodeck/logs/gd_logs.log'
+
+	var log_dir: DirAccess = DirAccess.open(log_dir_path)
 	var log_file: FileAccess = FileAccess.open(log_path, FileAccess.WRITE)
+
 	var log_line: String = "GD "
 	match log_type:
 		'w':
@@ -133,14 +137,19 @@ func logger(log_type: String, log_text: String) -> void:
 			print("No idea, mate")
 	log_line += log_text
 	print(log_line)
+	
+	if not log_dir:
+		log_dir = DirAccess.open("res://") #open something valid to create an instance
+		if log_dir.make_dir_recursive(log_dir_path) != OK:
+			print("Something wrong with log directory")
+			return
+
 	if log_file:
 		log_file.seek_end()
 		log_file.store_line(log_line)
 		log_file.close()
 	else:
 		print("Something wrong with log file")
-
-# logger("e", "Could not open file: %s" % file_path)
 	
 func array_to_string(arr: Array) -> String:
 	var text: String

From cb1316f7e1df6789925df26ca56ccd3cb0d03872 Mon Sep 17 00:00:00 2001
From: WallK <wallykrasiy@gmail.com>
Date: Thu, 17 Oct 2024 08:08:29 +0300
Subject: [PATCH 3/4] Don't truncate the log file

---
 tools/configurator/scripts/class_functions.gd | 24 ++++---------------
 1 file changed, 5 insertions(+), 19 deletions(-)

diff --git a/tools/configurator/scripts/class_functions.gd b/tools/configurator/scripts/class_functions.gd
index 72b4eea0..c6918075 100644
--- a/tools/configurator/scripts/class_functions.gd
+++ b/tools/configurator/scripts/class_functions.gd
@@ -98,41 +98,27 @@ func multi_state(section: String, state: String) -> String:
 		state = "mixed"
 	return state
 		
-# func logger_bash(log_type: String, log_text: String) -> void:
-# 	# Type of log messages:
-# 	# log d - debug message: maybe in the future we can decide to hide them in main builds or if an option is toggled
-# 	# log i - normal informational message
-# 	# log w - waring: something is not expected but it's not a big deal
-# 	# log e - error: something broke
-# 	var log_header_text = "gdc_"
-# 	log_header_text+=log_text
-# 	log_parameters = ["log", log_type, log_header_text]
-# 	log_result = await run_thread_command(wrapper_command,log_parameters, false)
-# 	#log_result = await run_thread_command("find",["$HOME", "-name", "*.xml","-print"], false)
-# 	#print (log_result["exit_code"])
-# 	#print (log_result["output"])
-
 func logger(log_type: String, log_text: String) -> void:
 	var log_dir_path: String = "/var/config/retrodeck/logs/"
 	var log_path: String = '/var/config/retrodeck/logs/gd_logs.log'
 
 	var log_dir: DirAccess = DirAccess.open(log_dir_path)
-	var log_file: FileAccess = FileAccess.open(log_path, FileAccess.WRITE)
+	var log_file: FileAccess = FileAccess.open(log_path, FileAccess.READ_WRITE)
 
 	var log_line: String = "GD "
 	match log_type:
 		'w':
 			log_line += "Warning "
-			print("Warning, mate")
+			# print("Warning, mate")
 		'e':
 			log_line += "Error "
-			print("Error, mate")
+			# print("Error, mate")
 		'i':
 			log_line += "Info "
-			print("Info, mate")
+			# print("Info, mate")
 		'd':
 			log_line += "Debug "
-			print("Debug, mate")
+			# print("Debug, mate")
 		_:
 			print("No idea, mate")
 	log_line += log_text

From 3c026ac652d801813a9524bc71f6de787277e5f2 Mon Sep 17 00:00:00 2001
From: WallK <wallykrasiy@gmail.com>
Date: Thu, 17 Oct 2024 08:45:33 +0300
Subject: [PATCH 4/4] Timestamp, fix append

---
 tools/configurator/scripts/class_functions.gd | 31 ++++++++++++++-----
 1 file changed, 24 insertions(+), 7 deletions(-)

diff --git a/tools/configurator/scripts/class_functions.gd b/tools/configurator/scripts/class_functions.gd
index c6918075..44ca6f43 100644
--- a/tools/configurator/scripts/class_functions.gd
+++ b/tools/configurator/scripts/class_functions.gd
@@ -103,33 +103,50 @@ func logger(log_type: String, log_text: String) -> void:
 	var log_path: String = '/var/config/retrodeck/logs/gd_logs.log'
 
 	var log_dir: DirAccess = DirAccess.open(log_dir_path)
-	var log_file: FileAccess = FileAccess.open(log_path, FileAccess.READ_WRITE)
+	var log_file: FileAccess
+
+	var log_header: String = " GD "
+
+	var datetime = Time.get_datetime_dict_from_system()
+	var msec = Time.get_ticks_msec() #very very fake ms, TODO
+
+	var timestamp: String = "[%d-%02d-%02d %02d:%02d:%02d.%03d]" % [
+	datetime.year, datetime.month, datetime.day, 
+	datetime.hour, datetime.minute, datetime.second, msec] # fake ms
+
+	var log_line: String = timestamp + log_header
 
-	var log_line: String = "GD "
 	match log_type:
 		'w':
-			log_line += "Warning "
+			log_line += "[Warning] "
 			# print("Warning, mate")
 		'e':
-			log_line += "Error "
+			log_line += "[Error] "
 			# print("Error, mate")
 		'i':
-			log_line += "Info "
+			log_line += "[Info] "
 			# print("Info, mate")
 		'd':
-			log_line += "Debug "
+			log_line += "[Debug] "
 			# print("Debug, mate")
 		_:
+			log_line += " "
 			print("No idea, mate")
 	log_line += log_text
-	print(log_line)
+	# print(log_line)
 	
 	if not log_dir:
 		log_dir = DirAccess.open("res://") #open something valid to create an instance
+		print(log_dir.make_dir_recursive(log_dir_path))
 		if log_dir.make_dir_recursive(log_dir_path) != OK:
 			print("Something wrong with log directory")
 			return
 
+	if not FileAccess.open(log_path, FileAccess.READ): 
+		log_file = FileAccess.open(log_path, FileAccess.WRITE_READ) # to create a file if not there
+	else:
+		log_file = FileAccess.open(log_path, FileAccess.READ_WRITE) # to not truncate
+
 	if log_file:
 		log_file.seek_end()
 		log_file.store_line(log_line)