Skip to content

Commit

Permalink
Minor logging fix.
Browse files Browse the repository at this point in the history
src/formula.cpp: Do not pass the list of known identifiers to the SDL
logging system.

src/logger.*pp: Provides warnings logging in a way not tied to SDL.
  • Loading branch information
galegosimpatico committed Apr 27, 2018
1 parent 80021e4 commit 7f37095
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
47 changes: 43 additions & 4 deletions src/formula.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@


#include "asserts.hpp"
#include "logger.hpp"
#include "formatter.hpp"
#include "formula.hpp"
#include "formula_callable.hpp"
Expand Down Expand Up @@ -1040,13 +1041,25 @@ namespace {
}

std::sort(known_v.begin(), known_v.end());
std::string known;

std::string some_known = "";
std::string all_known = "";

const uint_fast8_t some_known_limit = 3;
uint_fast8_t some_known_index = 0;

// Suggest a correction
boost::optional<std::string> candidate_match;
size_t candidate_value = std::min(static_cast<size_t>(4), id_.size());
for(const std::string& k : known_v) {
known += k + " \n";

all_known += k + '\n';

if (some_known_index < some_known_limit) {

some_known += k + '\n';
some_known_index++;
}

size_t d = edit_distance_calculator(id_, k)();
if (candidate_value > d) {
Expand All @@ -1061,11 +1074,37 @@ namespace {
if (candidate_match) {
suggested_match = "\nMaybe you meant '" + *candidate_match + "'?\n";
}

if(callable_def_->getTypeName() != nullptr) {
STRICT_ERROR("Unknown symbol '" << id_ << "' in " << *callable_def_->getTypeName() << " " << debugPinpointLocation() << suggested_match << "\nKnown symbols: (excluding built-in functions)\n" << known << "\n");
LOG_WARN_WO_SDL(
"Unknown symbol '" << id_ << "' in " <<
* callable_def_->getTypeName() << ' ' <<
debugPinpointLocation() << suggested_match <<
"\nKnown symbols: (excluding built-in functions)\n" <<
all_known << '\n');

STRICT_ERROR(
"Unknown symbol '" << id_ << "' in " <<
* callable_def_->getTypeName() << ' ' <<
debugPinpointLocation() << suggested_match <<
"\nThere are " << known_v.size() << " known symbols (excluding " <<
"built-in functions), check recent console output to find the " <<
"list of known symbols.\n");
} else {
STRICT_ERROR("Unknown identifier '" << id_ << "' " << debugPinpointLocation() << suggested_match << "\nIdentifiers that are valid in this scope:\n" << known << "\n");
LOG_WARN_WO_SDL(
"Unknown identifier '" << id_ << "' " <<
debugPinpointLocation() << suggested_match <<
"\nIdentifiers that are valid in this scope:\n" <<
all_known << '\n');

STRICT_ERROR(
"Unknown identifier '" << id_ << "' " <<
debugPinpointLocation() << suggested_match <<
"\nThere are " << known_v.size() << " known identifiers valid " <<
"in this scope, check recent console output to find the list " <<
"of valid identifiers.\n");
}

} else if(callable_def_) {
std::string type_name = "unk";
if(callable_def_->getTypeName()) {
Expand Down
12 changes: 12 additions & 0 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
distribution.
*/

#include <iostream>

#include "logger.hpp"

namespace
Expand All @@ -41,3 +43,13 @@ void log_internal(SDL_LogPriority priority, const std::string& str)
}
}

/**
* Helps the fact that a very large message can not be logged using
* `SDL_LogMessage(4)`.
*/
void log_internal_wo_SDL(
const SDL_LogPriority priority, const std::string & str)
{
// XXX Change for something more worked, such as `https://github.com/xeekworx/logger`.
std::cout << str; /* XXX */
}
16 changes: 16 additions & 0 deletions src/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@

void log_internal(SDL_LogPriority priority, const std::string& s);

/**
* Logs without resorting to SDL. This way very large messages can be
* logged. SDL truncates log messages larger than 4.096 characters.
*/
void log_internal_wo_SDL(SDL_LogPriority priority, const std::string & s);

#define LOG_VERBOSE(_a) \
do { \
std::ostringstream _s; \
Expand Down Expand Up @@ -72,6 +78,16 @@ void log_internal(SDL_LogPriority priority, const std::string& s);
log_internal(SDL_LOG_PRIORITY_WARN, _s.str()); \
} while(0)

/**
* Logs without resorting to SDL. This way very large messages can be
* logged. SDL truncates log messages larger than 4.096 characters.
*/
#define LOG_WARN_WO_SDL(_a) if (true) { \
std::ostringstream _s; \
_s << __SHORT_FORM_OF_FILE__ << ':' << __LINE__ << \
" : " << _a; \
log_internal_wo_SDL(SDL_LOG_PRIORITY_WARN, _s.str()); }

#define LOG_ERROR(_a) \
do { \
std::ostringstream _s; \
Expand Down

0 comments on commit 7f37095

Please sign in to comment.