log level option

This commit is contained in:
arm64v8a 2023-03-16 15:02:53 +09:00
parent fafe14b7c6
commit 0413f28ce7
14 changed files with 106 additions and 38 deletions

View File

@ -50,7 +50,7 @@ object Key {
const val TCP_KEEP_ALIVE_INTERVAL = "tcpKeepAliveInterval" const val TCP_KEEP_ALIVE_INTERVAL = "tcpKeepAliveInterval"
const val RULES_PROVIDER = "rulesProvider" const val RULES_PROVIDER = "rulesProvider"
const val ENABLE_LOG = "enableLog" const val LOG_LEVEL = "logLevel"
const val LOG_BUF_SIZE = "logBufSize" const val LOG_BUF_SIZE = "logBufSize"
const val MTU = "mtu" const val MTU = "mtu"
const val ALWAYS_SHOW_ADDRESS = "alwaysShowAddress" const val ALWAYS_SHOW_ADDRESS = "alwaysShowAddress"

View File

@ -77,7 +77,7 @@ class SagerNet : Application(),
filesDir.absolutePath + "/", filesDir.absolutePath + "/",
externalAssets.absolutePath + "/", externalAssets.absolutePath + "/",
DataStore.logBufSize, DataStore.logBufSize,
DataStore.enableLog, DataStore.logLevel > 0,
this this
) )

View File

@ -185,7 +185,7 @@ abstract class BoxInstance(
"--config", "--config",
configFile.absolutePath, configFile.absolutePath,
"--log-level", "--log-level",
if (DataStore.enableLog) "trace" else "warn", if (DataStore.logLevel > 0) "trace" else "warn",
"client" "client"
) )

View File

@ -106,7 +106,7 @@ object DataStore : OnPreferenceDataStoreChangeListener {
var dnsNetwork by configurationStore.stringSet(Key.DNS_NETWORK) var dnsNetwork by configurationStore.stringSet(Key.DNS_NETWORK)
var rulesProvider by configurationStore.stringToInt(Key.RULES_PROVIDER) var rulesProvider by configurationStore.stringToInt(Key.RULES_PROVIDER)
var enableLog by configurationStore.boolean(Key.ENABLE_LOG) var logLevel by configurationStore.stringToInt(Key.LOG_LEVEL)
var logBufSize by configurationStore.int(Key.LOG_BUF_SIZE) { 0 } var logBufSize by configurationStore.int(Key.LOG_BUF_SIZE) { 0 }
var acquireWakeLock by configurationStore.boolean(Key.ACQUIRE_WAKE_LOCK) var acquireWakeLock by configurationStore.boolean(Key.ACQUIRE_WAKE_LOCK)

View File

@ -146,6 +146,17 @@ fun buildConfig(
} }
} }
log = LogOptions().apply {
level = when (DataStore.logLevel) {
0 -> "panic"
1 -> "warn"
2 -> "info"
3 -> "debug"
4 -> "trace"
else -> "info"
}
}
dns = DNSOptions().apply { dns = DNSOptions().apply {
// TODO nb4a hosts? // TODO nb4a hosts?
// hosts = DataStore.hosts.split("\n") // hosts = DataStore.hosts.split("\n")

View File

@ -80,7 +80,7 @@ fun NaiveBean.buildNaiveConfig(port: Int): String {
if (extraHeaders.isNotBlank()) { if (extraHeaders.isNotBlank()) {
put("extra-headers", extraHeaders.split("\n").joinToString("\r\n")) put("extra-headers", extraHeaders.split("\n").joinToString("\r\n"))
} }
if (DataStore.enableLog) { if (DataStore.logLevel > 0) {
put("log", "") put("log", "")
} }
if (insecureConcurrency > 0) { if (insecureConcurrency > 0) {

View File

@ -91,7 +91,7 @@ fun TrojanGoBean.buildTrojanGoConfig(port: Int): String {
put("password", JSONArray().apply { put("password", JSONArray().apply {
put(password) put(password)
}) })
put("log_level", if (DataStore.enableLog) 0 else 2) put("log_level", if (DataStore.logLevel > 0) 0 else 2)
if (Protocols.shouldEnableMux("trojan-go")) put("mux", JSONObject().apply { if (Protocols.shouldEnableMux("trojan-go")) put("mux", JSONObject().apply {
put("enabled", true) put("enabled", true)
put("concurrency", DataStore.muxConcurrency) put("concurrency", DataStore.muxConcurrency)

View File

@ -59,6 +59,6 @@ fun TuicBean.buildTuicConfig(port: Int, cacheFile: (() -> File)?): String {
put("ip", LOCALHOST) put("ip", LOCALHOST)
put("port", port) put("port", port)
}) })
put("log_level", if (DataStore.enableLog) "debug" else "info") put("log_level", if (DataStore.logLevel > 0) "debug" else "info")
}.toStringPretty() }.toStringPretty()
} }

View File

@ -31,10 +31,7 @@ import io.nekohasekai.sagernet.bg.Executable
import io.nekohasekai.sagernet.database.DataStore import io.nekohasekai.sagernet.database.DataStore
import io.nekohasekai.sagernet.ui.MainActivity import io.nekohasekai.sagernet.ui.MainActivity
import io.nekohasekai.sagernet.ui.ThemedActivity import io.nekohasekai.sagernet.ui.ThemedActivity
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.suspendCancellableCoroutine
import moe.matsuri.nb4a.utils.NGUtil import moe.matsuri.nb4a.utils.NGUtil
import java.io.FileDescriptor import java.io.FileDescriptor
import java.net.* import java.net.*
@ -241,10 +238,13 @@ fun Fragment.needReload() {
fun Fragment.needRestart() { fun Fragment.needRestart() {
snackbar("Restart APP to apply changes.").setAction(R.string.apply) { snackbar("Restart APP to apply changes.").setAction(R.string.apply) {
Executable.killAll(true) SagerNet.stopService()
ProcessPhoenix.triggerRebirth( val ctx = requireContext()
requireContext(), Intent(requireContext(), MainActivity::class.java) runOnDefaultDispatcher {
) delay(500)
Executable.killAll(true)
ProcessPhoenix.triggerRebirth(ctx, Intent(ctx, MainActivity::class.java))
}
}.show() }.show()
} }

View File

@ -24,6 +24,7 @@ import io.nekohasekai.sagernet.utils.Theme
import io.nekohasekai.sagernet.widget.AppListPreference import io.nekohasekai.sagernet.widget.AppListPreference
import moe.matsuri.nb4a.Protocols import moe.matsuri.nb4a.Protocols
import moe.matsuri.nb4a.ui.ColorPickerPreference import moe.matsuri.nb4a.ui.ColorPickerPreference
import moe.matsuri.nb4a.ui.LongClickMenuPreference
import moe.matsuri.nb4a.ui.LongClickSwitchPreference import moe.matsuri.nb4a.ui.LongClickSwitchPreference
import moe.matsuri.nb4a.ui.MTUPreference import moe.matsuri.nb4a.ui.MTUPreference
@ -117,14 +118,14 @@ class SettingsPreferenceFragment : PreferenceFragmentCompat() {
val requireTransproxy = findPreference<SwitchPreference>(Key.REQUIRE_TRANSPROXY)!! val requireTransproxy = findPreference<SwitchPreference>(Key.REQUIRE_TRANSPROXY)!!
val transproxyPort = findPreference<EditTextPreference>(Key.TRANSPROXY_PORT)!! val transproxyPort = findPreference<EditTextPreference>(Key.TRANSPROXY_PORT)!!
val transproxyMode = findPreference<SimpleMenuPreference>(Key.TRANSPROXY_MODE)!! val transproxyMode = findPreference<SimpleMenuPreference>(Key.TRANSPROXY_MODE)!!
val enableLog = findPreference<LongClickSwitchPreference>(Key.ENABLE_LOG)!! val logLevel = findPreference<LongClickMenuPreference>(Key.LOG_LEVEL)!!
val mtu = findPreference<MTUPreference>(Key.MTU)!! val mtu = findPreference<MTUPreference>(Key.MTU)!!
enableLog.setOnPreferenceChangeListener { _, _ -> logLevel.setOnPreferenceChangeListener { _, _ ->
needRestart() needRestart()
true true
} }
enableLog.setOnLongClickListener { logLevel.setOnLongClickListener {
if (context == null) return@setOnLongClickListener true if (context == null) return@setOnLongClickListener true
val view = EditText(context).apply { val view = EditText(context).apply {

View File

@ -0,0 +1,33 @@
package moe.matsuri.nb4a.ui
import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.preference.PreferenceViewHolder
import com.takisoft.preferencex.SimpleMenuPreference
import io.nekohasekai.sagernet.R
class LongClickMenuPreference
@JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = com.takisoft.preferencex.simplemenu.R.attr.simpleMenuPreferenceStyle,
defStyleRes: Int = R.style.Preference_SimpleMenuPreference
) : SimpleMenuPreference(
context, attrs, defStyleAttr, defStyleRes
) {
private var mLongClickListener: View.OnLongClickListener? = null
override fun onBindViewHolder(holder: PreferenceViewHolder) {
super.onBindViewHolder(holder)
val itemView: View = holder.itemView
itemView.setOnLongClickListener {
mLongClickListener?.onLongClick(it) ?: true
}
}
fun setOnLongClickListener(longClickListener: View.OnLongClickListener) {
this.mLongClickListener = longClickListener
}
}

View File

@ -1,5 +1,32 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<string-array name="int_array_2">
<item>0</item>
<item>1</item>
</string-array>
<string-array name="int_array_3">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="int_array_4">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="int_array_5">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
</string-array>
<string-array name="bypass_private_route"> <string-array name="bypass_private_route">
<item>1.0.0.0/8</item> <item>1.0.0.0/8</item>
<item>2.0.0.0/7</item> <item>2.0.0.0/7</item>
@ -294,10 +321,6 @@
<item>TPROXY</item> <item>TPROXY</item>
</string-array> </string-array>
<string-array name="int_array_2">
<item>0</item>
<item>1</item>
</string-array>
<string-array name="naive_proto_entry"> <string-array name="naive_proto_entry">
<item>HTTPS</item> <item>HTTPS</item>
@ -315,13 +338,6 @@
<item>@string/disable</item> <item>@string/disable</item>
<item>@string/auto</item> <item>@string/auto</item>
</string-array> </string-array>
<string-array name="int_array_4">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="ipv6_mode"> <string-array name="ipv6_mode">
<item>@string/disable</item> <item>@string/disable</item>
<item>@string/enable</item> <item>@string/enable</item>
@ -348,11 +364,6 @@
<item>leastPing</item> <item>leastPing</item>
</string-array> </string-array>
<string-array name="int_array_3">
<item>0</item>
<item>1</item>
<item>2</item>
</string-array>
<string-array name="group_types"> <string-array name="group_types">
<item>@string/group_basic</item> <item>@string/group_basic</item>
@ -478,4 +489,12 @@
<item>v2ray-plugin</item> <item>v2ray-plugin</item>
</string-array> </string-array>
<string-array name="log_level" translatable="false">
<item>none</item>
<item>warn</item>
<item>info</item>
<item>debug</item>
<item>trace</item>
</string-array>
</resources> </resources>

View File

@ -524,5 +524,6 @@ Anyone can write advanced plugins, which can control NekoBox. please download an
<string name="enable_clash_api">Enable Clash API</string> <string name="enable_clash_api">Enable Clash API</string>
<string name="enable_clash_api_summary"><![CDATA[Provide clash api & yacd dashboard at 127.0.0.1:9090]]></string> <string name="enable_clash_api_summary"><![CDATA[Provide clash api & yacd dashboard at 127.0.0.1:9090]]></string>
<string name="xtls_flow">Flow (VLESS Sub-protocol)</string> <string name="xtls_flow">Flow (VLESS Sub-protocol)</string>
<string name="log_level">Log Level</string>
</resources> </resources>

View File

@ -71,11 +71,14 @@
app:summary="@string/show_direct_speed_sum" app:summary="@string/show_direct_speed_sum"
app:title="@string/show_direct_speed" app:title="@string/show_direct_speed"
app:useSimpleSummaryProvider="true" /> app:useSimpleSummaryProvider="true" />
<moe.matsuri.nb4a.ui.LongClickSwitchPreference <moe.matsuri.nb4a.ui.LongClickMenuPreference
app:defaultValue="0"
app:entries="@array/log_level"
app:entryValues="@array/int_array_5"
app:icon="@drawable/ic_baseline_bug_report_24" app:icon="@drawable/ic_baseline_bug_report_24"
app:key="enableLog" app:key="logLevel"
app:summary="@string/enable_log_sum" app:title="@string/log_level"
app:title="@string/enable_log" /> app:useSimpleSummaryProvider="true" />
</PreferenceCategory> </PreferenceCategory>
<PreferenceCategory app:title="@string/cag_route"> <PreferenceCategory app:title="@string/cag_route">