[LyX/master] Produce an error on bad debug level values

Jean-Marc Lasgouttes lasgouttes at lyx.org
Fri Mar 26 16:50:08 UTC 2021


commit 9ff27f8ce09a79e05bc1fe1c1d2e4d8c6ccbfc7e
Author: Jean-Marc Lasgouttes <lasgouttes at lyx.org>
Date:   Fri Mar 26 17:49:38 2021 +0100

    Produce an error on bad debug level values
    
    Add new funciton Debug::badValue, that returns the first bad value in a debug
    setting string.
    
    Use it to parse the -dbg comand line option and to control the
    execution of the debug-level-set lfun.
    
    Use range-based loops in a few places.
---
 src/LyX.cpp                         |   13 +++++--
 src/frontends/qt/GuiApplication.cpp |    9 ++++-
 src/support/debug.cpp               |   62 +++++++++++++++++++++++++---------
 src/support/debug.h                 |    4 ++
 4 files changed, 66 insertions(+), 22 deletions(-)

diff --git a/src/LyX.cpp b/src/LyX.cpp
index a0035fb..fc2f871 100644
--- a/src/LyX.cpp
+++ b/src/LyX.cpp
@@ -1180,10 +1180,15 @@ int parse_dbg(string const & arg, string const &, string &)
 		Debug::showTags(cout);
 		exit(0);
 	}
-	lyxerr << to_utf8(bformat(_("Setting debug level to %1$s"), from_utf8(arg))) << endl;
-
-	lyxerr.setLevel(Debug::value(arg));
-	Debug::showLevel(lyxerr, lyxerr.level());
+	string bad = Debug::badValue(arg);
+	if (bad.empty()) {
+		lyxerr.setLevel(Debug::value(arg));
+		Debug::showLevel(lyxerr, lyxerr.level());
+	} else {
+		cout << to_utf8(bformat(_("Bad debug value `%1$s'. Exiting."),
+		                        from_utf8(bad))) << endl;
+		exit(1);
+	}
 	return 1;
 }
 
diff --git a/src/frontends/qt/GuiApplication.cpp b/src/frontends/qt/GuiApplication.cpp
index aaff335..7d9bb94 100644
--- a/src/frontends/qt/GuiApplication.cpp
+++ b/src/frontends/qt/GuiApplication.cpp
@@ -1399,10 +1399,17 @@ bool GuiApplication::getStatus(FuncRequest const & cmd, FuncStatus & flag) const
 	case LFUN_REPEAT:
 	case LFUN_PREFERENCES_SAVE:
 	case LFUN_BUFFER_SAVE_AS_DEFAULT:
-	case LFUN_DEBUG_LEVEL_SET:
 		// these are handled in our dispatch()
 		break;
 
+	case LFUN_DEBUG_LEVEL_SET: {
+		string bad = Debug::badValue(to_utf8(cmd.argument()));
+		enable = bad.empty();
+		if (!bad.empty())
+			flag.message(bformat(_("Bad debug value `%1$s'."), from_utf8(bad)));
+		break;
+	}
+
 	case LFUN_WINDOW_CLOSE:
 		enable = !d->views_.empty();
 		break;
diff --git a/src/support/debug.cpp b/src/support/debug.cpp
index c86e1c8..ca14220 100644
--- a/src/support/debug.cpp
+++ b/src/support/debug.cpp
@@ -129,31 +129,59 @@ Debug::Type Debug::value(string const & val)
 		if (isStrInt(tmp))
 			l |= static_cast<Type>(convert<int>(tmp));
 		else
-		// Search for an explicit name
-		for (int i = 0 ; i < numErrorTags ; ++i)
-			if (tmp == errorTags[i].name) {
-				l |= errorTags[i].level;
-				break;
-			}
+			// Search for an explicit name
+			for (DebugErrorItem const & item : errorTags)
+				if (tmp == item.name) {
+					l |= item.level;
+					break;
+				}
 		if (st == string::npos)
-    		break;
+			break;
 		v.erase(0, st + 1);
 	}
 	return l;
 }
 
 
+string Debug::badValue(string const & val)
+{
+	string v = val;
+	while (!v.empty()) {
+		size_t const st = v.find(',');
+		string const tmp = ascii_lowercase(v.substr(0, st));
+		if (tmp.empty())
+			break;
+		// Is it a number?
+		if (!tmp.empty() && !isStrInt(tmp)) {
+			// Search for an explicit name
+			bool found = false;
+			for (DebugErrorItem const & item : errorTags)
+				if (tmp == item.name) {
+					found = true;
+					break;
+				}
+			if (!found)
+				return tmp;
+		}
+		if (st == string::npos)
+			break;
+		v.erase(0, st + 1);
+	}
+	return empty_string();
+}
+
+
 void Debug::showLevel(ostream & os, Debug::Type level)
 {
 	// Show what features are traced
-	for (int i = 0; i < numErrorTags; ++i) {
-		if (errorTags[i].level != Debug::ANY
-		      && errorTags[i].level != Debug::NONE
-		      && errorTags[i].level & level) {
+	for (DebugErrorItem const & item : errorTags) {
+		if (item.level != Debug::ANY
+		      && item.level != Debug::NONE
+		      && item.level & level) {
 			// avoid to_utf8(_(...)) re-entrance problem
-			docstring const s = _(errorTags[i].desc);
+			docstring const s = _(item.desc);
 			os << to_utf8(bformat(_("Debugging `%1$s' (%2$s)"),
-					from_utf8(errorTags[i].name), s))
+					from_utf8(item.name), s))
 			   << '\n';
 		}
 	}
@@ -163,10 +191,10 @@ void Debug::showLevel(ostream & os, Debug::Type level)
 
 void Debug::showTags(ostream & os)
 {
-	for (int i = 0; i != numErrorTags ; ++i)
-		os << setw(10) << static_cast<unsigned int>(errorTags[i].level)
-		   << setw(13) << errorTags[i].name
-		   << "  " << to_utf8(_(errorTags[i].desc)) << '\n';
+	for (DebugErrorItem const & item : errorTags)
+		os << setw(10) << static_cast<unsigned int>(item.level)
+		   << setw(13) << item.name
+		   << "  " << to_utf8(_(item.desc)) << '\n';
 	os.flush();
 }
 
diff --git a/src/support/debug.h b/src/support/debug.h
index da10cd1..f12a842 100644
--- a/src/support/debug.h
+++ b/src/support/debug.h
@@ -114,6 +114,10 @@ namespace Debug {
 	/// A function to convert debug level string names numerical values
 	Type value(std::string const & val);
 
+	/// Check the validity of debug level names
+	/// \return the first bad level name
+	std::string badValue(std::string const & val);
+
 	/// A function to convert index of level to their numerical value
 	Type value(int val);
 


More information about the lyx-cvs mailing list