Speedup. Inline functions and toString uses string and not ostringstream

This commit is contained in:
KjellKod 2013-11-12 03:37:36 -07:00
parent 2ba9d6187c
commit b1ed735d39
2 changed files with 73 additions and 81 deletions

View File

@ -35,95 +35,91 @@ namespace {
namespace g2 {
std::string LogMessage::line() const {
return std::to_string(_line);
}
// std::string LogMessage::toString() const {
// std::ostringstream oss;
// oss << "\n" << timestamp() << "." << microseconds() << "\t";
// oss << level() << " [" << file();
// oss << " L: " << line() << "]\t";
//
// // Non-fatal Log Message
// if (false == wasFatal()) {
// oss << '"' << message() << '"';
// return oss.str();
// }
//
// if (internal::FATAL_SIGNAL.value == _level.value) {
// oss.str(""); // clear any previous text and formatting
// oss << "\n" << timestamp() << "." << microseconds();
// oss << "\n\n***** FATAL SIGNAL RECEIVED ******* " << std::endl;
// oss << '"' << message() << '"';
// return oss.str();
// }
//
// // Not crash scenario but LOG or CONTRACT
// auto level_value = _level.value;
// if (FATAL.value == level_value) {
// oss << "\n\t*******\tEXIT trigger caused by LOG(FATAL) entry: \n\t";
// oss << '"' << message() << '"';
// oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump();
// } else if (internal::CONTRACT.value == level_value) {
// oss << "\n\t *******\tEXIT trigger caused by broken Contract: CHECK(" << _expression << ")\n\t";
// oss << '"' << message() << '"';
// oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump() << '"';
// } else {
// oss << "\n\t*******\tUNKNOWN Log Message Type\n" << '"' << message() << '"';
// }
//
// return oss.str();
// }
std::string LogMessage::file() const {
return _file;
}
std::string LogMessage::function() const {
return _function;
}
std::string LogMessage::level() const {
return _level.text;
}
std::string LogMessage::timestamp(const std::string & time_look) const {
std::string LogMessage::timestamp(const std::string & time_look) const {
std::ostringstream oss;
oss << localtime_formatted(_timestamp, time_look);
return oss.str();
}
std::string LogMessage::microseconds() const {
return std::to_string(_microseconds);
}
std::string LogMessage::message() const {
return _stream.str();
}
std::string LogMessage::expression() const {
return _expression;
}
bool LogMessage::wasFatal() const {
return internal::wasFatal(_level);
}
std::string LogMessage::toString() const {
std::ostringstream oss;
oss << "\n" << timestamp() << "." << microseconds() << "\t";
oss << level() << " [" << file();
oss << " L: " << line() << "]\t";
std::string out;
out.append("\n").append(timestamp()).append(".")
.append(microseconds()).append("\t")
.append(level()).append(" [").append(file())
.append(" L: ").append(line()).append("]\t");
// Non-fatal Log Message
if (false == wasFatal()) {
oss << '"' << message() << '"';
return oss.str();
out.append("\"").append(message()).append("\"");
return out;
}
if (internal::FATAL_SIGNAL.value == _level.value) {
oss.str(""); // clear any previous text and formatting
oss << "\n" << timestamp() << "." << microseconds();
oss << "\n\n***** FATAL SIGNAL RECEIVED ******* " << std::endl;
oss << '"' << message() << '"';
return oss.str();
out = {"\n"};
out.append(timestamp()).append(".").append(microseconds())
.append("\n\n***** FATAL SIGNAL RECEIVED ******* \n""\"").append(message()).append("\"");
return out;
}
// Not crash scenario but LOG or CONTRACT
auto level_value = _level.value;
if (FATAL.value == level_value) {
oss << "\n\t*******\tEXIT trigger caused by LOG(FATAL) entry: \n\t";
oss << '"' << message() << '"';
oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump();
out.append("\n\t*******\tEXIT trigger caused by LOG(FATAL) entry: \n\t")
.append("\"").append(message()).append("\"")
.append("\n*******\tSTACKDUMP *******\n")
.append(internal::stackdump());
} else if (internal::CONTRACT.value == level_value) {
oss << "\n\t *******\tEXIT trigger caused by broken Contract: CHECK(" << _expression << ")\n\t";
oss << '"' << message() << '"';
oss << "\n*******\tSTACKDUMP *******\n" << internal::stackdump() << '"';
out.append("\n\t *******\tEXIT trigger caused by broken Contract: CHECK(").append(_expression)
.append(")\n\t").append("\"").append(message()).append("\"")
.append("\n*******\tSTACKDUMP *******\n").append(internal::stackdump()).append("\"");
} else {
oss << "\n\t*******\tUNKNOWN Log Message Type\n" << '"' << message() << '"';
out.append("\n\t*******\tUNKNOWN Log Message Type\n").append("\"").append(message()).append("\"");
}
return oss.str();
}
// is strictly not speaking const
void LogMessage::setExpression(const std::string expression) {
_expression = expression;
}
return out;
}
std::ostringstream& LogMessage::stream(){
return _stream;
}
LogMessage::LogMessage(const std::string &file, const int line,
LogMessage::LogMessage(const std::string &file, const int line,
const std::string& function, const LEVELS& level)
: _timestamp(g2::systemtime_now())
, _microseconds(microsecondsCounter())

View File

@ -23,31 +23,27 @@
namespace g2 {
struct LogMessage {
std::string file() const;
std::string line() const;
std::string function() const;
std::string level() const;
std::string file() const { return _file; }
std::string line() const { return std::to_string(_line); }
std::string function() const { return _function; }
std::string level() const { return _level.text; }
std::string timestamp(const std::string& time_format = {internal::date_formatted + " " + internal::time_formatted}) const;
std::string microseconds() const;
std::string message() const;
std::string expression() const;
std::string microseconds() const { return std::to_string(_microseconds); }
std::string message() const { return _stream.str(); }
std::string expression() const { return _expression; }
bool wasFatal() const { return internal::wasFatal(_level); }
bool wasFatal() const;
// convert all content to ONE string
std::string toString() const;
std::ostringstream& stream();
void setExpression(const std::string expression);
std::ostringstream& stream() { return _stream; }
void setExpression(const std::string expression) { _expression = expression; }
LogMessage(const std::string &file, const int line, const std::string& function, const LEVELS& level);
explicit LogMessage(const std::string& fatalOsSignalCrashMessage);
LogMessage(const LogMessage&);
//LogMessage(LogMessage&&);
LogMessage(const LogMessage&);
~LogMessage() = default;
protected: