[LyX/master] tex2lyx: Also factor out error and debug messages
Juergen Spitzmueller
spitz at lyx.org
Thu Dec 29 07:39:58 UTC 2022
commit a5115d4e4adc9b017b593f44006e1ffd0d9523bf
Author: Juergen Spitzmueller <spitz at lyx.org>
Date: Thu Dec 29 09:37:00 2022 +0100
tex2lyx: Also factor out error and debug messages
---
src/tex2lyx/Parser.cpp | 3 +-
src/tex2lyx/Preamble.cpp | 6 +--
src/tex2lyx/math.cpp | 8 ++--
src/tex2lyx/table.cpp | 5 +-
src/tex2lyx/tex2lyx.cpp | 108 ++++++++++++++++++++++++++-------------------
src/tex2lyx/tex2lyx.h | 5 ++
6 files changed, 77 insertions(+), 58 deletions(-)
diff --git a/src/tex2lyx/Parser.cpp b/src/tex2lyx/Parser.cpp
index 61619cb..d048995 100644
--- a/src/tex2lyx/Parser.cpp
+++ b/src/tex2lyx/Parser.cpp
@@ -15,6 +15,7 @@
#include "tex2lyx.h"
#include "Encoding.h"
+#include "support/convert.h"
#include "support/lstrings.h"
#include "support/textutils.h"
@@ -920,7 +921,7 @@ void Parser::dump() const
void Parser::error(string const & msg) const
{
- cerr << "Line ~" << lineno_ << ": parse error: " << msg << endl;
+ error_message("Line ~" + convert<string>(lineno_) + ": parse error: " + msg);
dump();
//exit(1);
}
diff --git a/src/tex2lyx/Preamble.cpp b/src/tex2lyx/Preamble.cpp
index 9fc6068..fd8e84a 100644
--- a/src/tex2lyx/Preamble.cpp
+++ b/src/tex2lyx/Preamble.cpp
@@ -475,7 +475,7 @@ void Preamble::setTextClass(string const & tclass, TeX2LyXDocClass & tc)
h_textclass = tclass;
tc.setName(h_textclass);
if (!LayoutFileList::get().haveClass(h_textclass) || !tc.load()) {
- cerr << "Error: Could not read layout file for textclass \"" << h_textclass << "\"." << endl;
+ error_message("Could not read layout file for textclass \"" + h_textclass + "\".");
exit(EXIT_FAILURE);
}
}
@@ -2192,10 +2192,8 @@ void Preamble::parse(Parser & p, string const & forceclass,
Token const & t = p.get_token();
-#ifdef FILEDEBUG
if (!detectEncoding)
- cerr << "t: " << t << '\n';
-#endif
+ debug_message("t: " + t.asInput());
//
// cat codes
diff --git a/src/tex2lyx/math.cpp b/src/tex2lyx/math.cpp
index e99710f..0e34798 100644
--- a/src/tex2lyx/math.cpp
+++ b/src/tex2lyx/math.cpp
@@ -15,6 +15,8 @@
#include "Preamble.h"
#include "tex2lyx.h"
+#include "support/convert.h"
+
#include <iostream>
using namespace std;
@@ -43,9 +45,7 @@ void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
while (p.good()) {
Token const & t = p.get_token();
-#ifdef FILEDEBUG
- cerr << "t: " << t << " flags: " << flags << "\n";
-#endif
+ debug_message("t: " + t.asInput() + " flags: " + convert<string>(flags));
if (flags & FLAG_ITEM) {
if (t.cat() == catSpace)
@@ -92,7 +92,7 @@ void parse_math(Parser & p, ostream & os, unsigned flags, const mode_type mode)
}
else {
- cerr << "\nmode: " << mode << endl;
+ warning_message("\nmode: " + mode);
p.error("something strange in the parser\n");
break;
}
diff --git a/src/tex2lyx/table.cpp b/src/tex2lyx/table.cpp
index 0036620..994a0cd 100644
--- a/src/tex2lyx/table.cpp
+++ b/src/tex2lyx/table.cpp
@@ -352,9 +352,8 @@ void handle_colalign(Parser & p, vector<ColInfo> & colinfo,
ColInfo next = start;
for (Token t = p.get_token(); p.good() && t.cat() != catEnd;
t = p.get_token()) {
-#ifdef FILEDEBUG
- cerr << "t: " << t << " c: '" << t.character() << "'\n";
-#endif
+
+ debug_message("t: " + t.asInput() + " c: '" + t.character() + "'");
// We cannot handle comments here
if (t.cat() == catComment) {
diff --git a/src/tex2lyx/tex2lyx.cpp b/src/tex2lyx/tex2lyx.cpp
index 8444712..2130103 100644
--- a/src/tex2lyx/tex2lyx.cpp
+++ b/src/tex2lyx/tex2lyx.cpp
@@ -43,6 +43,9 @@
#include <vector>
#include <map>
+// comment out to enable debug_messages
+//#define FILEDEBUG
+
using namespace std;
using namespace lyx::support;
using namespace lyx::support::os;
@@ -556,8 +559,8 @@ bool read_syntaxfile(FileName const & file_name)
{
ifdocstream is(file_name.toFilesystemEncoding().c_str());
if (!is.good()) {
- cerr << "Could not open syntax file \""
- << file_name << "\" for reading." << std::endl;
+ error_message("Could not open syntax file \""
+ + file_name.absFileName() + "\" for reading.");
return false;
}
// We can use our TeX parser, since the syntax of the layout file is
@@ -682,9 +685,9 @@ int parse_version(string const &, string const &)
}
-void error_message(string const & message)
+void error_with_message(string const & message)
{
- cerr << "tex2lyx: " << message << "\n\n";
+ error_message(message);
error_code = EXIT_FAILURE;
parse_help(string(), string());
}
@@ -693,7 +696,7 @@ void error_message(string const & message)
int parse_class(string const & arg, string const &)
{
if (arg.empty())
- error_message("Missing textclass string after -c switch");
+ error_with_message("Missing textclass string after -c switch");
documentclass = arg;
return 1;
}
@@ -709,7 +712,7 @@ int parse_module(string const & arg, string const &)
int parse_encoding(string const & arg, string const &)
{
if (arg.empty())
- error_message("Missing encoding string after -e switch");
+ error_with_message("Missing encoding string after -e switch");
default_encoding = arg;
return 1;
}
@@ -718,7 +721,7 @@ int parse_encoding(string const & arg, string const &)
int parse_fixed_encoding(string const & arg, string const &)
{
if (arg.empty())
- error_message("Missing encoding string after -fixedenc switch");
+ error_with_message("Missing encoding string after -fixedenc switch");
default_encoding = arg;
fixed_encoding = true;
return 1;
@@ -728,7 +731,7 @@ int parse_fixed_encoding(string const & arg, string const &)
int parse_syntaxfile(string const & arg, string const &)
{
if (arg.empty())
- error_message("Missing syntaxfile string after -s switch");
+ error_with_message("Missing syntaxfile string after -s switch");
syntaxfile = internal_path(arg);
return 1;
}
@@ -743,7 +746,7 @@ string cl_user_support;
int parse_sysdir(string const & arg, string const &)
{
if (arg.empty())
- error_message("Missing directory for -sysdir switch");
+ error_with_message("Missing directory for -sysdir switch");
cl_system_support = internal_path(arg);
return 1;
}
@@ -752,7 +755,7 @@ int parse_sysdir(string const & arg, string const &)
int parse_userdir(string const & arg, string const &)
{
if (arg.empty())
- error_message("Missing directory for -userdir switch");
+ error_with_message("Missing directory for -userdir switch");
cl_user_support = internal_path(arg);
return 1;
}
@@ -831,7 +834,7 @@ void TeX2LyXApp::easyParse()
// don't complain if not found - may be parsed later
if (it == cmdmap.end()) {
if (argv_[i][0] == '-')
- error_message(string("Unknown option `") + argv_[i] + "'.");
+ error_with_message(string("Unknown option `") + argv_[i] + "'.");
else
continue;
}
@@ -926,7 +929,7 @@ bool tex2lyx(idocstream & is, ostream & os, string const & encoding,
list<string> removed_modules;
LayoutFile const & baseClass = LayoutFileList::get()[textclass.name()];
if (!used_modules.adaptToBaseClass(&baseClass, removed_modules)) {
- cerr << "Could not load default modules for text class." << endl;
+ error_message("Could not load default modules for text class.");
return false;
}
@@ -938,8 +941,8 @@ bool tex2lyx(idocstream & is, ostream & os, string const & encoding,
// Empty module names are silently skipped.
for (auto const & module : preloaded_modules) {
if (!module.empty() && !addModule(module)) {
- cerr << "Error: Could not load module \""
- << module << "\"." << endl;
+ error_message("Error: Could not load module \""
+ + module + "\".");
return false;
}
}
@@ -971,7 +974,7 @@ bool tex2lyx(idocstream & is, ostream & os, string const & encoding,
preamble.addModule(*it);
}
if (!preamble.writeLyXHeader(os, !active_environments.empty(), outfiledir)) {
- cerr << "Could not write LyX file header." << endl;
+ error_message( "Could not write LyX file header.");
return false;
}
@@ -1032,8 +1035,8 @@ bool tex2lyx(FileName const & infilename, ostream & os, string encoding,
is.rdbuf()->pubsetbuf(0, 0);
is.open(infilename.toFilesystemEncoding().c_str());
if (!is.good()) {
- cerr << "Could not open input file \"" << infilename
- << "\" for reading." << endl;
+ error_message("Could not open input file \""
+ + infilename.absFileName() + "\" for reading.");
return false;
}
string const oldParentFilePath = parentFilePathTeX;
@@ -1056,17 +1059,18 @@ bool tex2lyx(string const & infilename, FileName const & outfilename,
if (!ifname.exists() && ifname.extension().empty()) {
ifname.changeExtension("tex");
if (!ifname.exists()) {
- cerr << "Could not open input file \"" << infilename
- << "\" for reading." << endl;
+ error_message("Could not open input file \""
+ + infilename + "\" for reading.");
return false;
}
}
if (outfilename.isReadableFile()) {
if (overwrite_files) {
- warning_message("Overwriting existing file " + outfilename.absFileName());
+ warning_message("Overwriting existing file "
+ + outfilename.absFileName());
} else {
- cerr << "Not overwriting existing file "
- << outfilename << endl;
+ error_message("Not overwriting existing file "
+ + outfilename.absFileName());
return false;
}
} else {
@@ -1074,14 +1078,14 @@ bool tex2lyx(string const & infilename, FileName const & outfilename,
}
ofstream os(outfilename.toFilesystemEncoding().c_str());
if (!os.good()) {
- cerr << "Could not open output file \"" << outfilename
- << "\" for writing." << endl;
+ error_message("Could not open output file \""
+ + outfilename.absFileName() + "\" for writing.");
return false;
}
-#ifdef FILEDEBUG
- cerr << "Input file: " << ifname << "\n";
- cerr << "Output file: " << outfilename << "\n";
-#endif
+
+ debug_message("Input file: " + ifname.absFileName());
+ debug_message("Output file: " + outfilename.absFileName());
+
return tex2lyx(ifname, os, encoding,
outfilename.onlyPath().absFileName() + '/');
}
@@ -1107,7 +1111,7 @@ bool tex2tex(string const & infilename, FileName const & outfilename,
Systemcall one;
if (one.startscript(Systemcall::Wait, command) == 0)
return true;
- cerr << "Error: Running '" << command << "' failed." << endl;
+ error_message("Running '" + command + "' failed.");
return false;
}
@@ -1119,6 +1123,21 @@ void warning_message(string const & message)
}
+void error_message(string const & message)
+{
+ cerr << "tex2lyx error: " << message << endl;
+}
+
+#ifdef FILEDEBUG
+void debug_message(string const & message)
+{
+ cerr << "tex2lyx debug info: " << message << endl;
+}
+#else
+void debug_message(string const &){}
+#endif
+
+
namespace {
int TeX2LyXApp::run()
@@ -1129,8 +1148,8 @@ int TeX2LyXApp::run()
try {
init_package(internal_path(os::utf8_argv(0)), string(), string());
} catch (ExceptionMessage const & message) {
- cerr << to_utf8(message.title_) << ":\n"
- << to_utf8(message.details_) << endl;
+ error_message(to_utf8(message.title_) + ":\n"
+ + to_utf8(message.details_));
if (message.type_ == ErrorException)
return EXIT_FAILURE;
}
@@ -1144,8 +1163,8 @@ int TeX2LyXApp::run()
init_package(internal_path(os::utf8_argv(0)),
cl_system_support, cl_user_support);
} catch (ExceptionMessage const & message) {
- cerr << to_utf8(message.title_) << ":\n"
- << to_utf8(message.details_) << endl;
+ error_message(to_utf8(message.title_) + ":\n"
+ + to_utf8(message.details_));
if (message.type_ == ErrorException)
return EXIT_FAILURE;
}
@@ -1178,16 +1197,15 @@ int TeX2LyXApp::run()
outfilename = makeAbsPath(outfilename).absFileName();
if (roundtrip) {
if (outfilename == "-") {
- cerr << "Error: Writing to standard output is "
- "not supported in roundtrip mode."
- << endl;
+ error_message("Writing to standard output is "
+ "not supported in roundtrip mode.");
return EXIT_FAILURE;
}
string texfilename = changeExtension(outfilename, ".tex");
if (equivalent(FileName(infilename), FileName(texfilename))) {
- cerr << "Error: The input file `" << infilename
- << "´ would be overwritten by the TeX file exported from `"
- << outfilename << "´ in roundtrip mode." << endl;
+ error_message("The input file `" + infilename
+ + "´ would be overwritten by the TeX file exported from `"
+ + outfilename + "´ in roundtrip mode.");
return EXIT_FAILURE;
}
}
@@ -1200,7 +1218,7 @@ int TeX2LyXApp::run()
// Read the syntax tables
FileName const system_syntaxfile = libFileSearch("", "syntax.default");
if (system_syntaxfile.empty()) {
- cerr << "Error: Could not find syntax file \"syntax.default\"." << endl;
+ error_message("Could not find syntax file \"syntax.default\".");
return EXIT_FAILURE;
}
if (!read_syntaxfile(system_syntaxfile))
@@ -1212,14 +1230,12 @@ int TeX2LyXApp::run()
// Read the encodings table.
FileName const symbols_path = libFileSearch(string(), "unicodesymbols");
if (symbols_path.empty()) {
- cerr << "Error: Could not find file \"unicodesymbols\"."
- << endl;
+ error_message("Could not find file \"unicodesymbols\".");
return EXIT_FAILURE;
}
FileName const enc_path = libFileSearch(string(), "encodings");
if (enc_path.empty()) {
- cerr << "Error: Could not find file \"encodings\"."
- << endl;
+ error_message("Could not find file \"encodings\".");
return EXIT_FAILURE;
}
encodings.read(enc_path, symbols_path);
@@ -1252,8 +1268,8 @@ int TeX2LyXApp::run()
FileName const path(masterFilePathLyX);
if (!path.isDirectory()) {
if (!path.createPath()) {
- cerr << "Warning: Could not create directory for file `"
- << masterFilePathLyX << "´." << endl;
+ error_message("Could not create directory for file `"
+ + masterFilePathLyX + "´.");
return EXIT_FAILURE;
}
}
diff --git a/src/tex2lyx/tex2lyx.h b/src/tex2lyx/tex2lyx.h
index 1e29712..d1fd33a 100644
--- a/src/tex2lyx/tex2lyx.h
+++ b/src/tex2lyx/tex2lyx.h
@@ -217,6 +217,11 @@ bool tex2lyx(std::string const & infilename,
/// A general warning message that can be silenced with -q
void warning_message(std::string const & message);
+/// A general error message
+void error_message(std::string const & message);
+/// A general debug message that outputs if
+/// FILEDEBUG is definied
+void debug_message(std::string const & message);
} // namespace lyx
More information about the lyx-cvs
mailing list