Skip to content
Snippets Groups Projects
Select Git revision
  • windows_ci
  • master default
  • cert_pk_id
  • proxy_push_result
  • cnode_put_id
  • update-windows-build
  • proxy
  • resubscribe_on_token_change
  • actions
  • client_mode
  • llhttp
  • search_node_add
  • crypto_aes_gcm_argon2
  • ios_notifications
  • log_fmt
  • v2asio
  • fix-msvc
  • message_split
  • meson
  • build_unify
  • v3.4.0
  • v3.3.1
  • v3.3.1rc1
  • v3.3.1rc2
  • v3.3.0
  • v3.2.0
  • v3.1.11
  • v3.1.10
  • v3.1.9
  • v3.1.8.2
  • v3.1.8.1
  • v3.1.8
  • v3.1.7
  • v3.1.6
  • v3.1.5
  • v3.1.4
  • v3.1.3
  • v3.1.2
  • v3.1
  • v3.0.1
40 results

log.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    log.cpp 3.47 KiB
    /*
     *  Copyright (C) 2014-2019 Savoir-faire Linux Inc.
     *
     *  Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com>
     *
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  This program is distributed in the hope that it will be useful,
     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program. If not, see <https://www.gnu.org/licenses/>.
     */
    
    #include "log.h"
    
    #ifndef _WIN32
    #include <syslog.h>
    #endif
    
    #include <fstream>
    #include <chrono>
    
    namespace dht {
    namespace log {
    
    /**
     * Print va_list to std::ostream (used for logging).
     */
    void
    printLog(std::ostream& s, char const *m, va_list args) {
        // print log to buffer
        std::array<char, 8192> buffer;
        int ret = vsnprintf(buffer.data(), buffer.size(), m, args);
        if (ret < 0)
            return;
    
        // write timestamp
        using namespace std::chrono;
        using log_precision = microseconds;
        constexpr auto den = log_precision::period::den;
        auto num = duration_cast<log_precision>(steady_clock::now().time_since_epoch()).count();
        s << "[" << std::setfill('0') << std::setw(6) << num / den << "."
                 << std::setfill('0') << std::setw(6) << num % den << "]" << " ";
    
        // write log
        s.write(buffer.data(), std::min((size_t) ret, buffer.size()));
        if ((size_t) ret >= buffer.size())
            s << "[[TRUNCATED]]";
        s << std::endl;
    }
    
    void
    enableLogging(dht::DhtRunner &dht) {
        dht.setLoggers(
            [](char const *m, va_list args) {
                std::cerr << red;
                printLog(std::cerr, m, args);
                std::cerr << def;
            },
            [](char const *m, va_list args) {
                std::cout << yellow;
                printLog(std::cout, m, args);
                std::cout << def;
            },
            [](char const *m, va_list args) { printLog(std::cout, m, args); }
        );
    }
    
    void
    enableFileLogging(dht::DhtRunner &dht, const std::string &path) {
        auto logfile = std::make_shared<std::fstream>();
        logfile->open(path, std::ios::out);
    
        dht.setLoggers(
            [=](char const *m, va_list args) { printLog(*logfile, m, args); },
            [=](char const *m, va_list args) { printLog(*logfile, m, args); },
            [=](char const *m, va_list args) { printLog(*logfile, m, args); }
        );
    }
    
    OPENDHT_PUBLIC void
    enableSyslog(dht::DhtRunner &dht, const char* name) {
    #ifndef _WIN32
        struct Syslog {
            Syslog(const char* n) {
                openlog(n, LOG_NDELAY, LOG_USER);
            }
            ~Syslog() {
                closelog();
            }
        };
        // syslog is global. Existing instance must be reused.
        static std::weak_ptr<Syslog> opened_logfile;
        auto logfile = opened_logfile.lock();
        if (not logfile) {
            logfile = std::make_shared<Syslog>(name);
            opened_logfile = logfile;
        }
        dht.setLoggers(
            [logfile](char const *m, va_list args) { vsyslog(LOG_ERR, m, args); },
            [logfile](char const *m, va_list args) { vsyslog(LOG_WARNING, m, args); },
            [logfile](char const *m, va_list args) { vsyslog(LOG_INFO, m, args); }
        );
    #endif
    }
    
    void
    disableLogging(dht::DhtRunner &dht) {
        dht.setLoggers(dht::NOLOG, dht::NOLOG, dht::NOLOG);
    }
    
    }
    }