Copy Kernel Logs

Give user option to copy kernel logs to current storage

Change-Id: I0c61122fb34826ddb08977e6cbf98e3adf477acd
diff --git a/gui/action.cpp b/gui/action.cpp
index a96e15a..223d75e 100644
--- a/gui/action.cpp
+++ b/gui/action.cpp
@@ -639,13 +639,19 @@
 	operation_start("Copy Log");
 	if (!simulate)
 	{
-		string dst;
+		string dst, curr_storage;
+		int copy_kernel_log = 0;
+
+		DataManager::GetValue("tw_include_kernel_log", copy_kernel_log);
 		PartitionManager.Mount_Current_Storage(true);
-		dst = DataManager::GetCurrentStoragePath() + "/recovery.log";
+		curr_storage = DataManager::GetCurrentStoragePath();
+		dst = curr_storage + "/recovery.log";
 		TWFunc::copy_file("/tmp/recovery.log", dst.c_str(), 0755);
 		tw_set_default_metadata(dst.c_str());
+		if (copy_kernel_log)
+			TWFunc::copy_kernel_log(curr_storage);
 		sync();
-		gui_msg(Msg("copy_log=Copied recovery log to {1}.")(DataManager::GetCurrentStoragePath()));
+		gui_msg(Msg("copy_log=Copied recovery log to {1}")(dst));
 	} else
 		simulate_progress_bar();
 	operation_end(0);
diff --git a/gui/theme/common/landscape.xml b/gui/theme/common/landscape.xml
index 5c92fcc..3aed198 100644
--- a/gui/theme/common/landscape.xml
+++ b/gui/theme/common/landscape.xml
@@ -3132,6 +3132,45 @@
 			</action>
 		</page>
 
+                <page name="copylog">
+                        <template name="page"/>
+
+                        <text style="text_l">
+                                <placement x="%col1_x_header%" y="%row3_header_y%"/>
+                                <text>{@copying_log=Copy Logs to SD Card}</text>
+                        </text>
+
+                        <checkbox>
+                                <placement x="%indent%" y="%row4_y%"/>
+                                <text>{@include_kernel_log=Include Kernel Log}</text>
+                                <data variable="tw_include_kernel_log" value="1"/>
+                        </checkbox>
+
+                        <slider>
+                                <text>{@copying_log=Copy Logs to SD Card}</text>
+                                <actions>
+                                        <action function="set">tw_back=advanced</action>
+                                        <action function="set">tw_action=copylog</action>
+                                        <action function="set">tw_action_text1={@copying_log=Copy Logs to SD Card}</action>
+                                        <action function="set">tw_complete_text1={@copy_log_complete=Logs Copy Completed}</action>
+                                        <action function="page">action_page</action>
+                                </actions>
+                        </slider>
+
+                        <action>
+                                <touch key="home"/>
+                                <action function="page">main</action>
+                        </action>
+
+                        <action>
+                                <touch key="back"/>
+                                <actions>
+                                        <action function="page">advanced</action>
+                                </actions>
+                        </action>
+
+                </page>
+
 		<page name="advanced">
 			<template name="page"/>
 
@@ -3143,15 +3182,7 @@
 			<button style="main_button">
 				<placement x="%col1_x_left%" y="%row2_y%"/>
 				<text>{@copy_log_btn=Copy Log}</text>
-				<actions>
-					<action function="set">tw_back=advanced</action>
-					<action function="set">tw_action=copylog</action>
-					<action function="set">tw_text1={@copy_log_confirm=Copy Log to SD Card?}</action>
-					<action function="set">tw_action_text1={@copying_log=Copying Log to SD Card...}</action>
-					<action function="set">tw_complete_text1={@copy_log_complete=Log Copy Complete}</action>
-					<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
-					<action function="page">confirm_action</action>
-				</actions>
+				<action function="page">copylog</action>
 			</button>
 
 			<button style="main_button">
diff --git a/gui/theme/common/languages/en.xml b/gui/theme/common/languages/en.xml
index 6b05a86..f4344a0 100644
--- a/gui/theme/common/languages/en.xml
+++ b/gui/theme/common/languages/en.xml
@@ -364,8 +364,8 @@
 		<string name="set_language_btn">Set Language</string>
 		<string name="advanced_hdr">Advanced</string>
 		<string name="copy_log_confirm">Copy Log to SD Card?</string>
-		<string name="copying_log">Copying Log to SD Card...</string>
-		<string name="copy_log_complete">Log Copy Complete</string>
+		<string name="copying_log" version="2">Copying Logs to SD Card...</string>
+		<string name="copy_log_complete" version="2">Logs Copy Complete</string>
 		<string name="fix_context_btn">Fix Contexts</string>
 		<string name="part_sd_btn">Partition SD Card</string>
 		<string name="part_sd_s_btn">SD Card</string>
@@ -673,5 +673,7 @@
 		<string name="adbbackup_control_error">Cannot write to adb control channel</string>
 		<string name="twrp_adbbu_option">--twrp option is required to enable twrp adb backup</string>
 		<string name="partition_not_found">path: {1} not found in partititon list</string>
+		<string name="copy_kernel_log">Copied kernel log to {1}</string>
+		<string name="include_kernel_log">Include Kernel Log</string>
 	</resources>
 </language>
diff --git a/gui/theme/common/portrait.xml b/gui/theme/common/portrait.xml
index e9be654..11ae874 100644
--- a/gui/theme/common/portrait.xml
+++ b/gui/theme/common/portrait.xml
@@ -3216,6 +3216,45 @@
 			</action>
 		</page>
 
+		<page name="copylog">
+			<template name="page"/>
+
+			<text style="text_l">
+				<placement x="%col1_x_header%" y="%row3_header_y%"/>
+				<text>{@copying_log=Copy Logs to SD Card}</text>
+			</text>
+
+			<checkbox>
+				<placement x="%indent%" y="%row4_y%"/>
+				<text>{@include_kernel_log=Include Kernel Log}</text>
+				<data variable="tw_include_kernel_log" value="1"/>
+			</checkbox>
+
+			<slider>
+				<text>{@copying_log=Copy Logs to SD Card}</text>
+				<actions>
+					<action function="set">tw_back=advanced</action>
+					<action function="set">tw_action=copylog</action>
+                                        <action function="set">tw_action_text1={@copying_log=Copy Logs to SD Card}</action>
+                                        <action function="set">tw_complete_text1={@copy_log_complete=Logs Copy Completed}</action>
+					<action function="page">action_page</action>
+				</actions>
+			</slider>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+
+			<action>
+				<touch key="back"/>
+				<actions>
+					<action function="page">advanced</action>
+				</actions>
+			</action>
+
+		</page>
+
 		<page name="advanced">
 			<template name="page"/>
 
@@ -3227,15 +3266,7 @@
 			<button style="main_button">
 				<placement x="%indent%" y="%row2a_y%"/>
 				<text>{@copy_log_btn=Copy Log}</text>
-				<actions>
-					<action function="set">tw_back=advanced</action>
-					<action function="set">tw_action=copylog</action>
-					<action function="set">tw_text1={@copy_log_confirm=Copy Log to SD Card?}</action>
-					<action function="set">tw_action_text1={@copying_log=Copying Log to SD Card...}</action>
-					<action function="set">tw_complete_text1={@copy_log_complete=Log Copy Complete}</action>
-					<action function="set">tw_slider_text={@swipe_to_confirm=Swipe to Confirm}</action>
-					<action function="page">confirm_action</action>
-				</actions>
+				<action function="page">copylog</action>
 			</button>
 
 			<button style="main_button">
diff --git a/gui/theme/common/watch.xml b/gui/theme/common/watch.xml
index f198409..921e72c 100644
--- a/gui/theme/common/watch.xml
+++ b/gui/theme/common/watch.xml
@@ -237,6 +237,37 @@
 			</button>
 		</page>
 
+                <page name="copylog">
+                        <template name="page"/>
+
+                        <checkbox>
+				<placement x="%indent%" y="%row1_y%"/>
+                                <text>{@include_kernel_log=Include Kernel Log}</text>
+                                <data variable="tw_include_kernel_log" value="1"/>
+                        </checkbox>
+
+			<slider>
+				<text>{@swipe_confirm=   Confirm}</text>
+				<actions>
+                                        <action function="set">tw_back=main3</action>
+                                        <action function="set">tw_action=copylog</action>
+                                        <action function="set">tw_action_text1={@copying_log=Copy Logs to SD Card}</action>
+                                        <action function="set">tw_complete_text1={@copy_log_complete=Logs Copy Completed}</action>
+					<action function="page">action_page</action>
+				</actions>
+			</slider>
+
+			<action>
+				<touch key="back"/>
+				<action function="page">main2</action>
+			</action>
+
+			<action>
+				<touch key="home"/>
+				<action function="page">main</action>
+			</action>
+                </page>
+
 		<page name="main3">
 			<template name="page"/>
 
@@ -261,15 +292,7 @@
 			<button style="main_button">
 				<placement x="%col1_x_right%" y="%row6_y%"/>
 				<text>{@copy_log_btn=Copy Log}</text>
-				<actions>
-					<action function="set">tw_back=main3</action>
-					<action function="set">tw_action=copylog</action>
-					<action function="set">tw_text1={@copy_log_confirm=Copy Log to SD Card?}</action>
-					<action function="set">tw_action_text1={@copying_log=Copying Log to SD Card...}</action>
-					<action function="set">tw_complete_text1={@copy_log_complete=Log Copy Complete}</action>
-					<action function="set">tw_slider_text={@swipe_confirm=   Confirm}</action>
-					<action function="page">confirm_action</action>
-				</actions>
+				<action function="page">copylog</action>
 			</button>
 
 			<button style="main_button">
diff --git a/twrp-functions.cpp b/twrp-functions.cpp
index cbf4730..72674ae 100644
--- a/twrp-functions.cpp
+++ b/twrp-functions.cpp
@@ -1070,4 +1070,14 @@
 	return 0;
 }
 
+void TWFunc::copy_kernel_log(string curr_storage) {
+	std::string dmesgDst = curr_storage + "/dmesg.log";
+	std::string dmesgCmd = "/sbin/dmesg";
+
+	std::string result;
+	Exec_Cmd(dmesgCmd, result);
+	write_file(dmesgDst, result);
+	gui_msg(Msg("copy_kernel_log=Copied kernel log to {1}")(dmesgDst));
+	tw_set_default_metadata(dmesgDst.c_str());
+}
 #endif // ndef BUILD_TWRPTAR_MAIN
diff --git a/twrp-functions.hpp b/twrp-functions.hpp
index 550c946..ebbe99d 100644
--- a/twrp-functions.hpp
+++ b/twrp-functions.hpp
@@ -94,6 +94,7 @@
 	static void SetPerformanceMode(bool mode); // support recovery.perf.mode
 	static void Disable_Stock_Recovery_Replace(); // Disable stock ROMs from replacing TWRP with stock recovery
 	static unsigned long long IOCTL_Get_Block_Size(const char* block_device);
+	static void copy_kernel_log(string curr_storage); // Copy Kernel Log to Current Storage (PSTORE/KMSG)
 
 private:
 	static void Copy_Log(string Source, string Destination);