Skip to content

Commit

Permalink
Print file name before warning from stream readers, compress warnings…
Browse files Browse the repository at this point in the history
… if necessary, add file name to errors
  • Loading branch information
Matthias Koefferlein committed Sep 16, 2024
1 parent 5aa6734 commit ca9b1d7
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 56 deletions.
35 changes: 34 additions & 1 deletion src/db/db/dbReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
}

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

// ---------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions src/db/db/dbReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,27 @@ class DB_PUBLIC ReaderBase
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;
};

/**
Expand Down
20 changes: 14 additions & 6 deletions src/plugins/streamers/cif/db_plugin/dbCIFReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
}
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/streamers/cif/db_plugin/dbCIFReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{ }
};

Expand Down
34 changes: 21 additions & 13 deletions src/plugins/streamers/dxf/db_plugin/dbDXFReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
}
}

Expand All @@ -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"));
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/plugins/streamers/dxf/db_plugin/dbDXFReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{ }
};

Expand Down
20 changes: 14 additions & 6 deletions src/plugins/streamers/gds2/db_plugin/contrib/dbGDS2TextReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{ }
};

Expand Down
22 changes: 15 additions & 7 deletions src/plugins/streamers/gds2/db_plugin/dbGDS2Reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
}
}

}
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/streamers/gds2/db_plugin/dbGDS2Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{ }
};

Expand Down
18 changes: 13 additions & 5 deletions src/plugins/streamers/magic/db_plugin/dbMAGReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/plugins/streamers/oasis/db_plugin/dbOASISReader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"));
}

}
}

Expand Down
4 changes: 2 additions & 2 deletions src/plugins/streamers/oasis/db_plugin/dbOASISReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
{ }
};

Expand Down

0 comments on commit ca9b1d7

Please sign in to comment.