1
0
Fork 0

fixed logging in arangosh

This commit is contained in:
Jan Steemann 2013-10-31 18:43:00 +01:00
parent 83a2e5ca3d
commit 4ad95f8b53
2 changed files with 57 additions and 28 deletions

View File

@ -493,35 +493,60 @@ void ArangoClient::stopPager () {
#endif
////////////////////////////////////////////////////////////////////////////////
/// @brief strip binary data from string
/// this is done before sending the string to a pager or writing it to the log
////////////////////////////////////////////////////////////////////////////////
static std::string StripBinary (const char* value) {
string result;
char const* p = value;
bool inBinary = false;
while (*p) {
if (inBinary) {
if (*p == 'm') {
inBinary = false;
}
}
else {
if (*p == '\x1b') {
inBinary = true;
}
else {
result.push_back(*p);
}
}
++p;
}
return result;
}
////////////////////////////////////////////////////////////////////////////////
/// @brief print to pager
////////////////////////////////////////////////////////////////////////////////
void ArangoClient::internalPrint (const char* format, const char* str) {
if (str) {
if (*str == '\x1b') {
// terminal escape sequence
if (_pager == stdout) {
fprintf(_pager, format, str);
}
}
else {
// regular string value
fprintf(_pager, format, str);
log(format, str);
if (str == 0) {
str = format;
format = "%s";
}
if (_pager == stdout) {
fprintf(_pager, format, str);
if (_log) {
string sanitised = StripBinary(str);
log(format, sanitised.c_str());
}
}
else {
if (*format == '\x1b') {
// terminal escape sequence
if (_pager == stdout) {
fprintf(_pager, "%s", format);
}
}
else {
// regular string value
fprintf(_pager, "%s", format);
log("%s", format);
string sanitised = StripBinary(str);
if (! sanitised.empty()) {
fprintf(_pager, format, sanitised.c_str());
log(format, sanitised.c_str());
}
}
}
@ -534,7 +559,7 @@ void ArangoClient::openLog () {
if (! _logFile.empty()) {
_log = fopen(_logFile.c_str(), "w");
ostringstream s;
ostringstream s;
if (_log == 0) {
s << "Cannot open file '" << _logFile << "' for logging.";
printErrLine(s.str());
@ -592,9 +617,11 @@ void ArangoClient::printByeBye () {
void ArangoClient::log (const char* format,
const char* str) {
if (_log) {
if (*str != '\x1b') {
string sanitised = StripBinary(str);
if (! sanitised.empty()) {
// do not print terminal escape sequences into log
fprintf(_log, format, str);
fprintf(_log, format, sanitised.c_str());
}
}
}
@ -607,9 +634,11 @@ void ArangoClient::log (const char* format,
const char* prompt,
const char* str) {
if (_log) {
if (*str != '\x1b') {
string sanitised = StripBinary(str);
if (! sanitised.empty()) {
// do not print terminal escape sequences into log
fprintf(_log, format, prompt, str);
fprintf(_log, format, prompt, sanitised.c_str());
}
}
}

View File

@ -1360,7 +1360,7 @@ static void RunShell (v8::Handle<v8::Context> context, bool promptError) {
// command failed
string exception(TRI_StringifyV8Exception(&tryCatch));
BaseClient.printErrLine(exception);
BaseClient.printErrLine(exception);
BaseClient.log("%s", exception.c_str());
// this will change the prompt for the next round
@ -1368,7 +1368,7 @@ static void RunShell (v8::Handle<v8::Context> context, bool promptError) {
}
BaseClient.stopPager();
BaseClient.printLine("");
BaseClient.printLine("");
BaseClient.log("%s\n", "");
// make sure the last command result makes it into the log file