OpenSTA/util/Report.cc

410 lines
8.6 KiB
C++
Raw Normal View History

2018-09-28 17:54:21 +02:00
// OpenSTA, Static Timing Analyzer
// Copyright (c) 2025, Parallax Software, Inc.
2018-09-28 17:54:21 +02:00
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2018-09-28 17:54:21 +02:00
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// The origin of this software must not be misrepresented; you must not
// claim that you wrote the original software.
//
// Altered source versions must be plainly marked as such, and must not be
// misrepresented as being the original software.
//
// This notice may not be removed or altered from any source distribution.
2018-09-28 17:54:21 +02:00
2020-04-05 23:53:44 +02:00
#include "Report.hh"
2020-04-05 20:35:51 +02:00
2018-09-28 17:54:21 +02:00
#include <algorithm> // min
2020-12-26 17:29:43 +01:00
#include <cstdlib> // exit
2020-12-27 01:53:27 +01:00
#include <cstring> // strlen
2020-04-05 20:35:51 +02:00
2021-02-14 17:44:35 +01:00
#include "Machine.hh"
2020-04-05 23:53:44 +02:00
#include "Error.hh"
2018-09-28 17:54:21 +02:00
namespace sta {
using std::min;
2020-12-14 02:21:35 +01:00
Report *Report::default_ = nullptr;
2018-09-28 17:54:21 +02:00
Report::Report() :
2019-03-13 01:25:53 +01:00
log_stream_(nullptr),
redirect_stream_(nullptr),
2018-09-28 17:54:21 +02:00
redirect_to_string_(false),
buffer_size_(1000),
buffer_(new char[buffer_size_]),
buffer_length_(0)
{
2020-12-14 02:21:35 +01:00
default_ = this;
2018-09-28 17:54:21 +02:00
}
Report::~Report()
{
delete [] buffer_;
}
2021-01-04 00:35:14 +01:00
size_t
Report::printConsole(const char *buffer,
size_t length)
{
printf("%s", buffer);
return length;
}
void
Report::printLine(const char *line,
size_t length)
{
printString(line, length);
printString("\n", 1);
}
2018-09-28 17:54:21 +02:00
size_t
2020-12-20 01:27:29 +01:00
Report::printString(const char *buffer,
size_t length)
2018-09-28 17:54:21 +02:00
{
size_t ret = length;
if (redirect_to_string_)
redirectStringPrint(buffer, length);
else {
if (redirect_stream_)
ret = min(ret, fwrite(buffer, sizeof(char), length, redirect_stream_));
else
ret = min(ret, printConsole(buffer, length));
if (log_stream_)
ret = min(ret, fwrite(buffer, sizeof(char), length, log_stream_));
}
return ret;
}
void
2020-12-28 18:04:57 +01:00
Report::reportLine(const char *fmt, ...)
2018-09-28 17:54:21 +02:00
{
va_list args;
va_start(args, fmt);
2020-12-29 20:49:30 +01:00
std::unique_lock<std::mutex> lock(buffer_lock_);
printToBuffer(fmt, args);
printBufferLine();
2018-09-28 17:54:21 +02:00
va_end(args);
}
2021-01-05 03:14:04 +01:00
void
Report::reportBlankLine()
{
printLine("", 0);
}
2020-12-26 01:55:46 +01:00
void
2020-12-28 18:04:57 +01:00
Report::reportLineString(const char *line)
2020-12-26 01:55:46 +01:00
{
printLine(line, strlen(line));
2020-12-26 01:55:46 +01:00
}
2020-12-29 03:04:49 +01:00
void
Report::reportLineString(const std::string &line)
2020-12-29 03:04:49 +01:00
{
printLine(line.c_str(), line.length());
2020-12-29 03:04:49 +01:00
}
2020-12-14 02:21:35 +01:00
////////////////////////////////////////////////////////////////
2020-12-28 18:04:57 +01:00
void
Report::printToBuffer(const char *fmt,
...)
{
va_list args;
va_start(args, fmt);
printToBuffer(fmt, args);
va_end(args);
}
void
Report::printToBuffer(const char *fmt,
va_list args)
{
buffer_length_ = 0;
printToBufferAppend(fmt, args);
}
void
Report::printToBufferAppend(const char *fmt,
...)
{
va_list args;
va_start(args, fmt);
printToBufferAppend(fmt, args);
va_end(args);
}
void
Report::printToBufferAppend(const char *fmt,
va_list args)
{
// Copy args in case we need to grow the buffer.
va_list args_copy;
va_copy(args_copy, args);
size_t length = vsnprint(buffer_ + buffer_length_, buffer_size_- buffer_length_,
fmt, args);
if (length >= buffer_size_) {
buffer_size_ = buffer_length_ + length * 2;
char *new_buffer = new char[buffer_size_];
strncpy(new_buffer, buffer_, buffer_length_);
2020-12-28 18:04:57 +01:00
delete [] buffer_;
buffer_ = new_buffer;
length = vsnprint(buffer_ + buffer_length_, buffer_size_ - buffer_length_,
fmt, args_copy);
2020-12-28 18:04:57 +01:00
}
buffer_length_ += length;
va_end(args_copy);
}
void
Report::printBufferLine()
2020-12-28 18:04:57 +01:00
{
printLine(buffer_, buffer_length_);
2020-12-28 18:04:57 +01:00
}
////////////////////////////////////////////////////////////////
2018-09-28 17:54:21 +02:00
void
Report::warn(int id,
2020-12-14 02:21:35 +01:00
const char *fmt,
...)
2018-09-28 17:54:21 +02:00
{
// Skip suppressed messages.
if (!isSuppressed(id)) {
va_list args;
va_start(args, fmt);
printToBuffer("Warning: ");
printToBufferAppend(fmt, args);
printBufferLine();
va_end(args);
}
2018-09-28 17:54:21 +02:00
}
2020-12-25 23:00:11 +01:00
void
Report::vwarn(int id,
2020-12-25 23:00:11 +01:00
const char *fmt,
va_list args)
{
// Skip suppressed messages.
if (!isSuppressed(id)) {
printToBuffer("Warning: ");
printToBufferAppend(fmt, args);
printBufferLine();
}
2020-12-25 23:00:11 +01:00
}
2018-09-28 17:54:21 +02:00
void
Report::fileWarn(int id,
2020-12-14 02:21:35 +01:00
const char *filename,
int line,
const char *fmt,
...)
2018-09-28 17:54:21 +02:00
{
// Skip suppressed messages.
if (!isSuppressed(id)) {
va_list args;
va_start(args, fmt);
printToBuffer("Warning: %s line %d, ", filename, line);
printToBufferAppend(fmt, args);
printBufferLine();
va_end(args);
}
2018-09-28 17:54:21 +02:00
}
void
Report::vfileWarn(int id,
2020-12-14 02:21:35 +01:00
const char *filename,
int line,
const char *fmt,
2020-12-25 23:00:11 +01:00
va_list args)
2018-09-28 17:54:21 +02:00
{
// Skip suppressed messages.
if (!isSuppressed(id)) {
printToBuffer("Warning: %s line %d, ", filename, line);
printToBufferAppend(fmt, args);
printBufferLine();
}
2018-09-28 17:54:21 +02:00
}
2020-12-14 02:21:35 +01:00
////////////////////////////////////////////////////////////////
2018-09-28 17:54:21 +02:00
void
Report::error(int id,
2020-12-14 02:21:35 +01:00
const char *fmt, ...)
2018-09-28 17:54:21 +02:00
{
2020-12-14 02:21:35 +01:00
va_list args;
va_start(args, fmt);
// No prefix msg, no \n.
printToBuffer(fmt, args);
2020-12-14 02:21:35 +01:00
va_end(args);
throw ExceptionMsg(buffer_, isSuppressed(id));
2018-09-28 17:54:21 +02:00
}
2020-12-25 23:00:11 +01:00
void
Report::verror(int id,
2020-12-25 23:00:11 +01:00
const char *fmt,
va_list args)
{
// No prefix msg, no \n.
printToBuffer(fmt, args);
throw ExceptionMsg(buffer_, isSuppressed(id));
2020-12-25 23:00:11 +01:00
}
2018-09-28 17:54:21 +02:00
void
Report::fileError(int id,
2020-12-14 02:21:35 +01:00
const char *filename,
int line,
const char *fmt,
...)
2018-09-28 17:54:21 +02:00
{
va_list args;
va_start(args, fmt);
// No prefix msg, no \n.
2020-12-25 23:00:11 +01:00
printToBuffer("%s line %d, ", filename, line);
printToBufferAppend(fmt, args);
2018-09-28 17:54:21 +02:00
va_end(args);
throw ExceptionMsg(buffer_, isSuppressed(id));
2018-09-28 17:54:21 +02:00
}
void
Report::vfileError(int id,
2020-12-14 02:21:35 +01:00
const char *filename,
int line,
const char *fmt,
va_list args)
2018-09-28 17:54:21 +02:00
{
// No prefix msg, no \n.
2020-12-25 23:00:11 +01:00
printToBuffer("%s line %d, ", filename, line);
printToBufferAppend(fmt, args);
throw ExceptionMsg(buffer_, isSuppressed(id));
2020-12-14 02:21:35 +01:00
}
////////////////////////////////////////////////////////////////
2018-09-28 17:54:21 +02:00
void
2020-12-14 02:21:35 +01:00
Report::critical(int /* id */,
const char *fmt,
...)
2018-09-28 17:54:21 +02:00
{
va_list args;
va_start(args, fmt);
printToBuffer("Critical: ");
printToBufferAppend(fmt, args);
printBufferLine();
2018-09-28 17:54:21 +02:00
va_end(args);
exit(1);
2018-09-28 17:54:21 +02:00
}
void
2020-12-14 02:21:35 +01:00
Report::fileCritical(int /* id */,
const char *filename,
int line,
const char *fmt,
...)
2018-09-28 17:54:21 +02:00
{
2020-12-14 02:21:35 +01:00
va_list args;
va_start(args, fmt);
2021-01-04 00:35:14 +01:00
printToBuffer("Critical: %s line %d, ", filename, line);
printToBufferAppend(fmt, args);
printBufferLine();
2020-12-14 02:21:35 +01:00
va_end(args);
exit(1);
2018-09-28 17:54:21 +02:00
}
2020-12-14 02:21:35 +01:00
////////////////////////////////////////////////////////////////
void
Report::suppressMsgId(int id)
{
suppressed_msg_ids_.insert(id);
}
void
Report::unsuppressMsgId(int id)
{
suppressed_msg_ids_.erase(id);
}
bool
Report::isSuppressed(int id)
{
return suppressed_msg_ids_.find(id) != suppressed_msg_ids_.end();
}
////////////////////////////////////////////////////////////////
2018-09-28 17:54:21 +02:00
void
Report::logBegin(const char *filename)
{
log_stream_ = fopen(filename, "w");
2019-03-13 01:25:53 +01:00
if (log_stream_ == nullptr)
2018-09-28 17:54:21 +02:00
throw FileNotWritable(filename);
}
void
Report::logEnd()
{
if (log_stream_)
fclose(log_stream_);
2019-03-13 01:25:53 +01:00
log_stream_ = nullptr;
2018-09-28 17:54:21 +02:00
}
void
Report::redirectFileBegin(const char *filename)
{
redirect_stream_ = fopen(filename, "w");
2019-03-13 01:25:53 +01:00
if (redirect_stream_ == nullptr)
2018-09-28 17:54:21 +02:00
throw FileNotWritable(filename);
}
void
Report::redirectFileAppendBegin(const char *filename)
{
redirect_stream_ = fopen(filename, "a");
2019-03-13 01:25:53 +01:00
if (redirect_stream_ == nullptr)
2018-09-28 17:54:21 +02:00
throw FileNotWritable(filename);
}
void
Report::redirectFileEnd()
{
if (redirect_stream_)
fclose(redirect_stream_);
2019-03-13 01:25:53 +01:00
redirect_stream_ = nullptr;
2018-09-28 17:54:21 +02:00
}
void
Report::redirectStringBegin()
{
redirect_to_string_ = true;
redirect_string_.clear();
}
const char *
Report::redirectStringEnd()
{
redirect_to_string_ = false;
return redirect_string_.c_str();
}
void
2020-12-20 01:27:29 +01:00
Report::redirectStringPrint(const char *buffer,
size_t length)
2018-09-28 17:54:21 +02:00
{
redirect_string_.append(buffer, length);
}
} // namespace