Skip to content

Commit

Permalink
[core] Print log for errors that happen in onStart() of a pipeline
Browse files Browse the repository at this point in the history
  • Loading branch information
pajama-coder committed Aug 14, 2023
1 parent 2b0ccfa commit c53c326
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
20 changes: 1 addition & 19 deletions src/filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -299,25 +299,7 @@ void Filter::error(const char *format, ...) {

auto Filter::error_location(char *buf, size_t len) -> size_t {
Dump d; dump(d);
auto source = m_location.source;
if (!source || source->filename.empty()) {
return std::snprintf(
buf, len,
"%s() at line %d column %d",
d.name.c_str(),
m_location.line,
m_location.column
);
} else {
return std::snprintf(
buf, len,
"%s() at line %d column %d in %s",
d.name.c_str(),
m_location.line,
m_location.column,
m_location.source->filename.c_str()
);
}
return Log::format_location(buf, len, m_location, d.name.c_str());
}

} // namespace pipy
22 changes: 22 additions & 0 deletions src/log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ auto Log::format_header(Level level, char *buf, size_t len) -> size_t {
return i;
}

auto Log::format_location(char *buf, size_t len, const pjs::Context::Location &loc, const char *func_name) -> size_t {
auto source = loc.source;
if (!source || source->filename.empty()) {
return std::snprintf(
buf, len,
"%s() at line %d column %d",
func_name,
loc.line,
loc.column
);
} else {
return std::snprintf(
buf, len,
"%s() at line %d column %d in %s",
func_name,
loc.line,
loc.column,
loc.source->filename.c_str()
);
}
}

void Log::write(const Data &data) {
if (s_log_local_only) {
for (const auto &c : data.chunks()) {
Expand Down
1 change: 1 addition & 0 deletions src/log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class Log {
static auto format_elapsed_time() -> const char*;
static auto format_elapsed_time(char *buf, size_t len, bool fill = false) -> size_t;
static auto format_header(Level level, char *buf, size_t len) -> size_t;
static auto format_location(char *buf, size_t len, const pjs::Context::Location &loc, const char *func_name) -> size_t;

static void write(const Data &data);
static void write(const std::string &data);
Expand Down
31 changes: 9 additions & 22 deletions src/pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,14 @@ auto PipelineLayout::alloc(Context *ctx) -> Pipeline* {
}

void PipelineLayout::start(Pipeline *pipeline, int argc, pjs::Value *argv) {
static const std::string s_invalid_input_events(
"initial input is not or did not return events or messages. "
"Consider using void(...) if no initial input is intended"
);
if (auto *o = m_on_start.get()) {
pjs::Value starting_events;
if (o->is<pjs::Function>()) {
auto &ctx = *pipeline->context();
(*o->as<pjs::Function>())(ctx, argc, argv, starting_events);
if (!ctx.ok()) {
Log::pjs_error(ctx.error());
ctx.reset();
pipeline->input()->input(StreamEnd::make(StreamEnd::RUNTIME_ERROR));
return;
}
Expand All @@ -133,24 +131,13 @@ void PipelineLayout::start(Pipeline *pipeline, int argc, pjs::Value *argv) {
}
if (!starting_events.is_nullish()) {
if (!Message::output(starting_events, pipeline->input())) {
const auto &loc = m_on_start_location;
char buf[200];
auto len = (!loc.source || loc.source->filename.empty() ?
std::snprintf(
buf, sizeof(buf),
"onStart() at line %d column %d: %s",
loc.line,
loc.column,
s_invalid_input_events.c_str()
) :
std::snprintf(
buf, sizeof(buf),
"onStart() at line %d column %d in %s: %s",
loc.line,
loc.column,
loc.source->filename.c_str(),
s_invalid_input_events.c_str()
)
char loc[100];
Log::format_location(loc, sizeof(loc), m_on_start_location, "onStart");
char buf[300];
auto len = std::snprintf(
buf, sizeof(buf),
"%s: initial input is not or did not return events or messages. "
"Consider using void(...) if no initial input is intended", loc
);
Log::error("%s", buf);
pipeline->input()->input(StreamEnd::make(
Expand Down

0 comments on commit c53c326

Please sign in to comment.