// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. #include #include #include namespace vespalib { Trace::Trace(const Trace &rhs) : _root(), _level(rhs._level) { if (!rhs.isEmpty()) { _root = std::make_unique(rhs.getRoot()); } } bool Trace::trace(uint32_t level, const string ¬e, bool addTime) { if (!shouldTrace(level)) { return false; } if (addTime) { struct timeval tv; gettimeofday(&tv, nullptr); ensureRoot().addChild(make_string("[%ld.%06ld] %s", tv.tv_sec, static_cast(tv.tv_usec), note.c_str())); } else { ensureRoot().addChild(note); } return true; } string Trace::toString(size_t limit) const { return _root ? _root->toString(limit) : ""; } string Trace::encode() const { return isEmpty() ? "" : getRoot().encode(); } void Trace::clear() { _level = 0; _root.reset(); } TraceNode & Trace::ensureRoot() { if (!_root) { _root = std::make_unique(); } return *_root; } } // namespace vespalib