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.
This commit is contained in:
Jean-Marc Lasgouttes 2021-03-26 17:49:38 +01:00
parent 0429b580e6
commit 9ff27f8ce0
4 changed files with 66 additions and 22 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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();
}

View File

@ -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);