// // logger.cpp // libsclogging // // Created by Bob Polis at 2020-04-29 // Copyright (c) 2020 SwiftCoder. All rights reserved. // #include "logger.hpp" #include #include #include #include #include using namespace sc; logger::logger(const std::string& name, loglevel level) : _level(level), _name(name) {} void logger::msg(loglevel level, const char* file, int line, const char* func) { if (level >= _level) { if (_topcall) { write_prefix(level, file, line, func); } else { _topcall = true; } std::cerr << '\n'; } } void logger::write_prefix(loglevel level, const char* file, int line, const char* func) { // retrieve accurate time for milliseconds display struct timeval secs; (void)::gettimeofday(&secs, nullptr); int ms = static_cast(std::roundf(secs.tv_usec / 1000.0)) % 1000; // create a date/time stamp char dtstamp[20]; std::strftime(dtstamp, 20, "%F %T", std::localtime(&secs.tv_sec)); std::cerr << dtstamp << "." << std::setfill('0') << std::setw(3) << ms << " "; std::cerr << _name << "[" << ::getpid() << "] "; std::cerr << file << ":" << line << " " << func << "() "; std::cerr << '[' << loglevel_desc(level) << "] "; }