Skip to content

Commit

Permalink
Print more than one line of errors
Browse files Browse the repository at this point in the history
As we're printing to the terminal instead of a GUI status line, we have
more space.

Also don't fail rendering on parser warnings, of which there are plenty
now. This completely supresses warnings though if there are no errors.
  • Loading branch information
lluchs committed Apr 1, 2017
1 parent ffdc932 commit df30cd6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 23 deletions.
27 changes: 15 additions & 12 deletions src/cpp-handles/log-handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@
#include "C4Include.h"
#include "lib/C4Log.h"

// This implements the Log engine function such that the first log message
// is stored and can be retrieved later by the C API.
std::string first_log;
// This implements the Log engine function such that the log messages
// are stored and can be retrieved later by the C API.
std::string logs;
unsigned int n_logs = 0;

bool Log(const char *msg)
{
if(first_log.empty())
{
assert(n_logs == 0);
first_log = msg;
}
if (!logs.empty())
logs.append("\n");
logs.append(msg);

if(*msg != '\0')
++n_logs;
Expand All @@ -54,19 +52,24 @@ extern "C" {

void c4_log_handle_clear()
{
first_log.clear();
logs.clear();
n_logs = 0;
}

const char* c4_log_handle_get_first_log_message()
const char* c4_log_handle_get_log_messages()
{
if(first_log.empty()) return nullptr;
return first_log.c_str();
if (logs.empty()) return nullptr;
return logs.c_str();
}

unsigned int c4_log_handle_get_n_log_messages()
{
return n_logs;
}

bool c4_log_handle_has_error()
{
return logs.find("ERROR:") != std::string::npos;
}

} // extern "C"
3 changes: 2 additions & 1 deletion src/cpp-handles/log-handle.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,9 @@ extern "C" {
typedef struct _C4MapgenHandle C4MapgenHandle;

void c4_log_handle_clear();
const char* c4_log_handle_get_first_log_message();
const char* c4_log_handle_get_log_messages();
unsigned int c4_log_handle_get_n_log_messages();
bool c4_log_handle_has_error();

#ifdef __cplusplus
}
Expand Down
14 changes: 7 additions & 7 deletions src/cpp-handles/mapgen-handle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,15 @@ C4MapgenHandle* c4_mapgen_handle_new_script(const char* filename, const char* so
// here:
::GameScript.LoadData("Script.c", "", nullptr);

const char* parse_error = c4_log_handle_get_first_log_message();
if(parse_error)
const char* parse_error = c4_log_handle_get_log_messages();
if(c4_log_handle_has_error() && parse_error)
throw std::runtime_error(parse_error);

// Link script engine (resolve includes/appends, generate code)
c4_log_handle_clear();
ScriptEngine.Link(&::Definitions);
if(c4_log_handle_get_n_log_messages() > 1)
throw std::runtime_error(c4_log_handle_get_first_log_message());
throw std::runtime_error(c4_log_handle_get_log_messages());

// Generate map, fail if return error occurs
c4_log_handle_clear();
Expand All @@ -230,7 +230,7 @@ C4MapgenHandle* c4_mapgen_handle_new_script(const char* filename, const char* so
&out_ptr_fg, &out_ptr_bg);

// Don't show any map if there was a script runtime error
const char* runtime_error = c4_log_handle_get_first_log_message();
const char* runtime_error = c4_log_handle_get_log_messages();
if(runtime_error)
throw std::runtime_error(runtime_error);

Expand Down Expand Up @@ -317,15 +317,15 @@ C4MapgenHandle* c4_mapgen_handle_new(const char* filename, const char* source, c
c4_log_handle_clear();
GameScript.Load(File, basename, nullptr, nullptr);

const char* parse_error = c4_log_handle_get_first_log_message();
const char* parse_error = c4_log_handle_get_log_messages();
if(parse_error)
throw std::runtime_error(parse_error);

// Link script engine (resolve includes/appends, generate code)
c4_log_handle_clear();
ScriptEngine.Link(&::Definitions);
if(c4_log_handle_get_n_log_messages() > 1)
throw std::runtime_error(c4_log_handle_get_first_log_message());
throw std::runtime_error(c4_log_handle_get_log_messages());
}

c4_log_handle_clear();
Expand All @@ -334,7 +334,7 @@ C4MapgenHandle* c4_mapgen_handle_new(const char* filename, const char* source, c
std::unique_ptr<CSurface8> out_ptr_fg(_out_ptr_fg), out_ptr_bg(_out_ptr_bg);

// Don't show any map if there was a script runtime error
const char* runtime_error = c4_log_handle_get_first_log_message();
const char* runtime_error = c4_log_handle_get_log_messages();
if(runtime_error)
{
throw std::runtime_error(runtime_error);
Expand Down
6 changes: 4 additions & 2 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,14 @@ extern "C" {
pub fn c4_log_handle_clear();
}
extern "C" {
pub fn c4_log_handle_get_first_log_message()
-> *const ::std::os::raw::c_char;
pub fn c4_log_handle_get_log_messages() -> *const ::std::os::raw::c_char;
}
extern "C" {
pub fn c4_log_handle_get_n_log_messages() -> ::std::os::raw::c_uint;
}
extern "C" {
pub fn c4_log_handle_has_error() -> bool;
}
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct _C4TextureMapHandle([u8; 0]);
Expand Down
2 changes: 1 addition & 1 deletion src/scenpar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl Scenpar {
Ok(())
} else {
bail!("couldn't load scenario parameters: {}",
CStr::from_ptr(c4_log_handle_get_first_log_message())
CStr::from_ptr(c4_log_handle_get_log_messages())
.to_string_lossy());
}
}
Expand Down

0 comments on commit df30cd6

Please sign in to comment.