Print file name before warning from stream readers, compress warnings if necessary, add file name to errors

This commit is contained in:
Matthias Koefferlein 2024-09-16 23:48:30 +02:00
parent 5aa67342e9
commit ca9b1d779d
13 changed files with 156 additions and 56 deletions

View File

@ -61,7 +61,7 @@ join_layer_names (std::string &s, const std::string &n)
// ReaderBase implementation
ReaderBase::ReaderBase ()
: m_warnings_as_errors (false), m_warn_level (1)
: m_warnings_as_errors (false), m_warn_level (1), m_warn_count_for_same_message (0), m_first_warning (true)
{
}
@ -79,6 +79,39 @@ void
ReaderBase::init (const db::LoadLayoutOptions &options)
{
m_warn_level = options.warn_level ();
m_last_warning.clear ();
m_warn_count_for_same_message = 0;
m_first_warning = true;
}
bool
ReaderBase::first_warning ()
{
bool f = m_first_warning;
m_first_warning = false;
return f;
}
int
ReaderBase::compress_warning (const std::string &msg)
{
const int max_warnings = 10;
if (! msg.empty () && msg == m_last_warning) {
if (m_warn_count_for_same_message < max_warnings) {
++m_warn_count_for_same_message;
return -1;
} else if (m_warn_count_for_same_message == max_warnings) {
++m_warn_count_for_same_message;
return 0;
} else {
return 1;
}
} else {
m_last_warning = msg;
m_warn_count_for_same_message = 0;
return -1;
}
}
// ---------------------------------------------------------------

View File

@ -126,12 +126,27 @@ public:
return m_warn_level;
}
/**
* @brief Returns true (once) if this is the first warning
*/
bool first_warning ();
/**
* @brief Returns a value indicating whether to compress the given warning
*
* The return value is either -1 (do not skip), 0 (first warning not to be shown), 1 (warning not shown(.
*/
int compress_warning (const std::string &msg);
protected:
virtual void init (const db::LoadLayoutOptions &options);
private:
bool m_warnings_as_errors;
int m_warn_level;
std::string m_last_warning;
int m_warn_count_for_same_message;
bool m_first_warning;
};
/**

View File

@ -86,7 +86,7 @@ CIFReader::read (db::Layout &layout)
void
CIFReader::error (const std::string &msg)
{
throw CIFReaderException (msg, m_stream.line_number (), m_cellname);
throw CIFReaderException (msg, m_stream.line_number (), m_cellname, m_stream.source ());
}
void
@ -96,11 +96,19 @@ CIFReader::warn (const std::string &msg, int wl)
return;
}
// TODO: compress
tl::warn << msg
<< tl::to_string (tr (" (line=")) << m_stream.line_number ()
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
if (first_warning ()) {
tl::warn << tl::sprintf (tl::to_string (tr ("In file %s:")), m_stream.source ());
}
int ws = compress_warning (msg);
if (ws < 0) {
tl::warn << msg
<< tl::to_string (tr (" (line=")) << m_stream.line_number ()
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
} else if (ws == 0) {
tl::warn << tl::to_string (tr ("... further warnings of this kind are not shown"));
}
}
/**

View File

@ -52,8 +52,8 @@ class DB_PLUGIN_PUBLIC CIFReaderException
: public ReaderException
{
public:
CIFReaderException (const std::string &msg, size_t l, const std::string &cell)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (line=%ld, cell=%s)")), msg, l, cell))
CIFReaderException (const std::string &msg, size_t l, const std::string &cell, const std::string &source)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (line=%ld, cell=%s), in file: %s")), msg, l, cell, source))
{ }
};

View File

@ -367,9 +367,9 @@ void
DXFReader::error (const std::string &msg)
{
if (m_ascii) {
throw DXFReaderException (msg, m_line_number, m_cellname);
throw DXFReaderException (msg, m_line_number, m_cellname, m_stream.source ());
} else {
throw DXFReaderException (msg, m_stream.pos (), m_cellname);
throw DXFReaderException (msg, m_stream.pos (), m_cellname, m_stream.source ());
}
}
@ -380,17 +380,25 @@ DXFReader::warn (const std::string &msg, int wl)
return;
}
// TODO: compress
if (m_ascii) {
tl::warn << msg
<< tl::to_string (tr (" (line=")) << m_line_number
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
} else {
tl::warn << msg
<< tl::to_string (tr (" (position=")) << m_stream.pos ()
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
if (first_warning ()) {
tl::warn << tl::sprintf (tl::to_string (tr ("In file %s:")), m_stream.source ());
}
int ws = compress_warning (msg);
if (ws < 0) {
if (m_ascii) {
tl::warn << msg
<< tl::to_string (tr (" (line=")) << m_line_number
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
} else {
tl::warn << msg
<< tl::to_string (tr (" (position=")) << m_stream.pos ()
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
}
} else if (ws == 0) {
tl::warn << tl::to_string (tr ("... further warnings of this kind are not shown"));
}
}

View File

@ -53,12 +53,12 @@ class DB_PLUGIN_PUBLIC DXFReaderException
: public ReaderException
{
public:
DXFReaderException (const std::string &msg, size_t p, const std::string &cell)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (position=%ld, cell=%s)")), msg.c_str (), p, cell))
DXFReaderException (const std::string &msg, size_t p, const std::string &cell, const std::string &source)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (position=%ld, cell=%s), in file: %s")), msg.c_str (), p, cell, source))
{ }
DXFReaderException (const std::string &msg, int line, const std::string &cell)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (line=%d, cell=%s)")), msg.c_str (), line, cell))
DXFReaderException (const std::string &msg, int line, const std::string &cell, const std::string &source)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (line=%d, cell=%s), in file: %s")), msg.c_str (), line, cell, source))
{ }
};

View File

@ -298,7 +298,7 @@ GDS2ReaderText::path () const
void
GDS2ReaderText::error (const std::string &msg)
{
throw GDS2ReaderTextException (msg, int(sStream.line_number()), cellname().c_str ());
throw GDS2ReaderTextException (msg, int (sStream.line_number()), cellname ().c_str (), sStream.source ());
}
void
@ -308,11 +308,19 @@ GDS2ReaderText::warn (const std::string &msg, int wl)
return;
}
// TODO: compress
tl::warn << msg
<< tl::to_string (tr (", line number=")) << sStream.line_number()
<< tl::to_string (tr (", cell=")) << cellname ().c_str ()
<< ")";
if (first_warning ()) {
tl::warn << tl::sprintf (tl::to_string (tr ("In file %s:")), sStream.source ());
}
int ws = compress_warning (msg);
if (ws < 0) {
tl::warn << msg
<< tl::to_string (tr (", line number=")) << sStream.line_number()
<< tl::to_string (tr (", cell=")) << cellname ().c_str ()
<< ")";
} else if (ws == 0) {
tl::warn << tl::to_string (tr ("... further warnings of this kind are not shown"));
}
}
void

View File

@ -39,8 +39,8 @@ class DB_PLUGIN_PUBLIC GDS2ReaderTextException
: public ReaderException
{
public:
GDS2ReaderTextException (const std::string &msg, size_t n, const std::string &cell)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (line number=%ld, cell=%s)")).c_str (), msg.c_str (), n, cell.c_str ()))
GDS2ReaderTextException (const std::string &msg, size_t n, const std::string &cell, const std::string &source)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (line number=%ld, cell=%s), in file: %s")).c_str (), msg.c_str (), n, cell.c_str (), source))
{ }
};

View File

@ -282,7 +282,7 @@ GDS2Reader::path () const
void
GDS2Reader::error (const std::string &msg)
{
throw GDS2ReaderException (msg, m_stream.pos (), m_recnum, cellname ().c_str ());
throw GDS2ReaderException (msg, m_stream.pos (), m_recnum, cellname ().c_str (), m_stream.source ());
}
void
@ -292,12 +292,20 @@ GDS2Reader::warn (const std::string &msg, int wl)
return;
}
// TODO: compress
tl::warn << msg
<< tl::to_string (tr (" (position=")) << m_stream.pos ()
<< tl::to_string (tr (", record number=")) << m_recnum
<< tl::to_string (tr (", cell=")) << cellname ().c_str ()
<< ")";
if (first_warning ()) {
tl::warn << tl::sprintf (tl::to_string (tr ("In file %s:")), m_stream.source ());
}
int ws = compress_warning (msg);
if (ws < 0) {
tl::warn << msg
<< tl::to_string (tr (" (position=")) << m_stream.pos ()
<< tl::to_string (tr (", record number=")) << m_recnum
<< tl::to_string (tr (", cell=")) << cellname ().c_str ()
<< ")";
} else if (ws == 0) {
tl::warn << tl::to_string (tr ("... further warnings of this kind are not shown"));
}
}
}

View File

@ -48,8 +48,8 @@ class DB_PLUGIN_PUBLIC GDS2ReaderException
: public ReaderException
{
public:
GDS2ReaderException (const std::string &msg, size_t p, size_t n, const std::string &cell)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (position=%ld, record number=%ld, cell=%s)")), msg, p, n, cell))
GDS2ReaderException (const std::string &msg, size_t p, size_t n, const std::string &cell, const std::string &source)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (position=%ld, record number=%ld, cell=%s), in file: %s")), msg, p, n, cell, source))
{ }
};

View File

@ -143,11 +143,19 @@ MAGReader::warn (const std::string &msg, int wl)
return;
}
// TODO: compress
tl::warn << msg
<< tl::to_string (tr (" (line=")) << mp_current_stream->line_number ()
<< tl::to_string (tr (", file=")) << mp_current_stream->source ()
<< ")";
if (first_warning ()) {
tl::warn << tl::sprintf (tl::to_string (tr ("In file %s:")), mp_current_stream->source ());
}
int ws = compress_warning (msg);
if (ws < 0) {
tl::warn << msg
<< tl::to_string (tr (" (line=")) << mp_current_stream->line_number ()
<< tl::to_string (tr (", file=")) << mp_current_stream->source ()
<< ")";
} else if (ws == 0) {
tl::warn << tl::to_string (tr ("... further warnings of this kind are not shown"));
}
}
db::cell_index_type

View File

@ -482,7 +482,7 @@ OASISReader::get_gdelta (long grid)
void
OASISReader::error (const std::string &msg)
{
throw OASISReaderException (msg, m_stream.pos (), m_cellname.c_str ());
throw OASISReaderException (msg, m_stream.pos (), m_cellname.c_str (), m_stream.source ());
}
void
@ -493,13 +493,25 @@ OASISReader::warn (const std::string &msg, int wl)
}
if (warnings_as_errors ()) {
error (msg);
} else {
// TODO: compress
tl::warn << msg
<< tl::to_string (tr (" (position=")) << m_stream.pos ()
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
if (first_warning ()) {
tl::warn << tl::sprintf (tl::to_string (tr ("In file %s:")), m_stream.source ());
}
int ws = compress_warning (msg);
if (ws < 0) {
tl::warn << msg
<< tl::to_string (tr (" (position=")) << m_stream.pos ()
<< tl::to_string (tr (", cell=")) << m_cellname
<< ")";
} else if (ws == 0) {
tl::warn << tl::to_string (tr ("... further warnings of this kind are not shown"));
}
}
}

View File

@ -54,8 +54,8 @@ class DB_PLUGIN_PUBLIC OASISReaderException
: public ReaderException
{
public:
OASISReaderException (const std::string &msg, size_t p, const std::string &cell)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (position=%ld, cell=%s)")), msg, p, cell))
OASISReaderException (const std::string &msg, size_t p, const std::string &cell, const std::string &source)
: ReaderException (tl::sprintf (tl::to_string (tr ("%s (position=%ld, cell=%s), in file: %s")), msg, p, cell, source))
{ }
};