aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/utils/log.cpp
blob: 64e3e8e445bf1e92cb02a7d3b399e6fa277be1ff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
//
// Copyright 2012,2014,2016 Ettus Research LLC
// Copyright 2018 Ettus Research, a National Instruments Company
// Copyright 2019 Ettus Research, a National Instruments Brand
//
// SPDX-License-Identifier: GPL-3.0-or-later
//

#include <uhd/transport/bounded_buffer.hpp>
#include <uhd/utils/log.hpp>
#include <uhd/utils/log_add.hpp>
#include <uhd/utils/paths.hpp>
#include <uhd/utils/static.hpp>
#include <uhd/version.hpp>
#include <uhdlib/utils/isatty.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <memory>
#include <atomic>
#include <cctype>
#include <fstream>
#include <memory>
#include <mutex>
#include <thread>

namespace pt                  = boost::posix_time;
constexpr double READ_TIMEOUT = 0.5; // Waiting time to read from the queue

// Don't make these static const std::string -- we need their lifetime guaranteed!
#define PURPLE "\033[0;35m" // purple
#define BLUE "\033[1;34m" // blue
#define GREEN "\033[0;32m" // green
#define YELLOW "\033[0;33m" // yellow
#define BYELLOW "\033[1;33m" // yellow
#define RED "\033[0;31m" // red
#define BRED "\033[1;31m" // bright red
#define RED_ON_YELLOW "\033[0;31;43m" // bright red
#define RESET_COLORS "\033[0;39m" // reset colors

/***********************************************************************
 * Helpers
 **********************************************************************/
namespace {
std::string verbosity_color(const uhd::log::severity_level& level)
{
    static const bool tty = uhd::is_a_tty(2); // is stderr a tty?
    if (!tty) {
        return "";
    }
    switch (level) {
        case (uhd::log::trace):
            return PURPLE;
        case (uhd::log::debug):
            return BLUE;
        case (uhd::log::info):
            return GREEN;
        case (uhd::log::warning):
            return BYELLOW;
        case (uhd::log::error):
            return BRED;
        case (uhd::log::fatal):
            return RED_ON_YELLOW;
        default:
            return RESET_COLORS;
    }
}

std::string verbosity_name(const uhd::log::severity_level& level)
{
    switch (level) {
        case (uhd::log::trace):
            return "TRACE";
        case (uhd::log::debug):
            return "DEBUG";
        case (uhd::log::info):
            return "INFO";
        case (uhd::log::warning):
            return "WARNING";
        case (uhd::log::error):
            return "ERROR";
        case (uhd::log::fatal):
            return "FATAL";
        default:
            return "-";
    }
}

//! get the relative file path from the host directory
inline std::string path_to_filename(std::string path)
{
    return path.substr(path.find_last_of("/\\") + 1);
}

} // namespace

/***********************************************************************
 * Logger backends
 **********************************************************************/
void console_log(const uhd::log::logging_info& log_info)
{
    std::clog
#ifdef UHD_LOG_CONSOLE_COLOR
        << verbosity_color(log_info.verbosity)
#endif
#ifdef UHD_LOG_CONSOLE_TIME
        << "[" << pt::to_simple_string(log_info.time) << "] "
#endif
#ifdef UHD_LOG_CONSOLE_THREAD
        << "[0x" << log_info.thread_id << "] "
#endif
#ifdef UHD_LOG_CONSOLE_SRC
        << "[" << path_to_filename(log_info.file) << ":" << log_info.line << "] "
#endif
        << "[" << verbosity_name(log_info.verbosity) << "] "
        << "[" << log_info.component << "] "
#ifdef UHD_LOG_CONSOLE_COLOR
        << verbosity_color(uhd::log::off) // This will reset colors
#endif
        << log_info.message << std::endl;
}

/*! Helper class to implement file logging
 *
 * The class holds references to the file stream object, and handles closing
 * and cleanup.
 */
class file_logger_backend
{
public:
    file_logger_backend(const std::string& file_path)
    {
        _file_stream.exceptions(std::ofstream::failbit | std::ofstream::badbit);
        if (!file_path.empty()) {
            try {
                _file_stream.open(
                    file_path.c_str(), std::fstream::out | std::fstream::app);
            } catch (const std::ofstream::failure& fail) {
                std::cerr << "Error opening log file: " << fail.what() << std::endl;
            }
        }
    }

    void log(const uhd::log::logging_info& log_info)
    {
        if (_file_stream.is_open()) {
            _file_stream << pt::to_simple_string(log_info.time) << ","
                         << "0x" << log_info.thread_id << ","
                         << path_to_filename(log_info.file) << ":" << log_info.line << ","
                         << log_info.verbosity << "," << log_info.component << ","
                         << log_info.message << std::endl;
            ;
        }
    }


    ~file_logger_backend()
    {
        if (_file_stream.is_open()) {
            _file_stream.close();
        }
    }

private:
    std::ofstream _file_stream;
};

/***********************************************************************
 * Global resources for the logger
 **********************************************************************/

#define UHD_CONSOLE_LOGGER_KEY "console"
#define UHD_FILE_LOGGER_KEY "file"

class log_resource
{
public:
    uhd::log::severity_level global_level;

    log_resource(void)
        : global_level(uhd::log::off)
        , _exit(false)
        ,
#ifndef UHD_LOG_FASTPATH_DISABLE
        _fastpath_queue(10)
        ,
#endif
        _log_queue(10)
    {
        // allow override from macro definition
#ifdef UHD_LOG_MIN_LEVEL
        this->global_level =
            _get_log_level(BOOST_STRINGIZE(UHD_LOG_MIN_LEVEL), this->global_level);
#endif
        // allow override from environment variables
        const char* log_level_env = std::getenv("UHD_LOG_LEVEL");
        if (log_level_env != NULL && log_level_env[0] != '\0') {
            this->global_level = _get_log_level(log_level_env, this->global_level);
        }

        // Setup default loggers (console and file)
        _setup_console_logging();
        _setup_file_logging();

        // On boot, we print the current UHD version info:
        {
            std::ostringstream sys_info;
            sys_info << BOOST_PLATFORM << "; " << BOOST_COMPILER << "; "
                     << "Boost_" << BOOST_VERSION << "; "
                     << uhd::get_component() << "_" << uhd::get_version_string();
            _publish_log_msg(sys_info.str(), uhd::log::info, "UHD");
        }

        // Launch log message consumer
        _pop_task =
            std::make_shared<std::thread>(std::thread([this]() { this->pop_task(); }));

        // Fastpath message consumer
#ifndef UHD_LOG_FASTPATH_DISABLE
        // allow override from environment variables
        const bool enable_fastpath = []() {
            const char* disable_fastpath_env = std::getenv("UHD_LOG_FASTPATH_DISABLE");
            if (disable_fastpath_env != NULL && disable_fastpath_env[0] != '\0') {
                return false;
            }
            return true;
        }();

        if (enable_fastpath) {
            _pop_fastpath_task = std::make_shared<std::thread>(
                std::thread([this]() { this->pop_fastpath_task(); }));
        } else {
            _pop_fastpath_task = std::make_shared<std::thread>(
                std::thread([this]() { this->pop_fastpath_dummy_task(); }));
            _publish_log_msg("Fastpath logging disabled at runtime.");
        }
#else
        {
            _publish_log_msg("Fastpath logging disabled at compile time.");
        }
#endif
    }

    ~log_resource(void)
    {
        _exit = true;

#ifndef BOOST_MSVC // push a final message is required, since the pop_with_wait() function
                   // will be used.
        // We push a final message to kick the pop task out of it's wait state.
        // This wouldn't be necessary if pop_with_wait() could fail. Should
        // that ever get fixed, we can remove this.
        auto final_message    = uhd::log::logging_info(pt::microsec_clock::local_time(),
            uhd::log::trace,
            __FILE__,
            __LINE__,
            "LOGGING",
            boost::this_thread::get_id());
        final_message.message = "";
        push(final_message);
#    ifndef UHD_LOG_FASTPATH_DISABLE
        push_fastpath("");
#    endif
#endif // BOOST_MSVC

        _pop_task->join();
        {
            std::lock_guard<std::mutex> l(_logmap_mutex);
            _loggers.clear();
        }
        _pop_task.reset();
#ifndef UHD_LOG_FASTPATH_DISABLE
        _pop_fastpath_task->join();
        _pop_fastpath_task.reset();
#endif
    }

    void push(const uhd::log::logging_info& log_info)
    {
        static const double PUSH_TIMEOUT = 0.25; // seconds
        _log_queue.push_with_timed_wait(log_info, PUSH_TIMEOUT);
    }

#ifndef UHD_LOG_FASTPATH_DISABLE
    void push_fastpath(const std::string& message)
    {
        // Never wait. If the buffer is full, we just don't see the message.
        // Too bad.
        _fastpath_queue.push_with_haste(message);
    }
#endif

    void _handle_log_info(const uhd::log::logging_info& log_info)
    {
        if (log_info.message.empty()) {
            return;
        }
        std::lock_guard<std::mutex> l(_logmap_mutex);
        for (const auto& logger_pair : _loggers) {
            const auto& logger = logger_pair.second;
            if (log_info.verbosity < logger.first) {
                continue;
            }
            logger.second(log_info);
        }
    }

    void pop_task()
    {
        uhd::log::logging_info log_info;
        log_info.message = "";

        // For the lifetime of this thread, we run the following loop:
        while (!_exit) {
#ifdef BOOST_MSVC
            // Some versions of MSVC will hang if threads are being joined after main has
            // completed, so we need to guarantee a timeout here
            if (_log_queue.pop_with_timed_wait(log_info, READ_TIMEOUT)) {
                _handle_log_info(log_info);
            }
#else
            _log_queue.pop_with_wait(log_info); // Blocking call
            _handle_log_info(log_info);
#endif // BOOST_MSVC
        }

        // Exit procedure: Clear the queue
        while (_log_queue.pop_with_haste(log_info)) {
            _handle_log_info(log_info);
        }

        // Terminate this thread.
    }

    void pop_fastpath_task()
    {
#ifndef UHD_LOG_FASTPATH_DISABLE
        std::string msg;
        while (!_exit) {
#    ifdef BOOST_MSVC
            // Some versions of MSVC will hang if threads are being joined after main has
            // completed, so we need to guarantee a timeout here
            if (_fastpath_queue.pop_with_timed_wait(msg, READ_TIMEOUT)) {
                std::cerr << msg << std::flush;
            }
#    else
            _fastpath_queue.pop_with_wait(msg);
            std::cerr << msg << std::flush;
#    endif // BOOST_MSVC
        }

        // Exit procedure: Clear the queue
        while (_fastpath_queue.pop_with_haste(msg)) {
            std::cerr << msg << std::flush;
        }
#endif
    }

    void pop_fastpath_dummy_task()
    {
#ifndef UHD_LOG_FASTPATH_DISABLE
        std::string msg;
        while (!_exit) {
#    ifdef BOOST_MSVC
            // Some versions of MSVC will hang if threads are being joined after main has
            // completed, so we need to guarantee a timeout here
            _fastpath_queue.pop_with_timed_wait(msg, READ_TIMEOUT);
#    else
            _fastpath_queue.pop_with_wait(msg);
#    endif // BOOST_MSVC
        }

        // Exit procedure: Clear the queue
        while (_fastpath_queue.pop_with_haste(msg))
            ;
#endif
    }

    void add_logger(const std::string& key, uhd::log::log_fn_t logger_fn)
    {
        std::lock_guard<std::mutex> l(_logmap_mutex);
        _loggers[key] = level_logfn_pair{global_level, logger_fn};
    }

    void set_log_level(const std::string& key, const uhd::log::severity_level level)
    {
        std::lock_guard<std::mutex> l(_logmap_mutex);
        _loggers[key].first = level;
    }

private:
    std::shared_ptr<std::thread> _pop_task;
#ifndef UHD_LOG_FASTPATH_DISABLE
    std::shared_ptr<std::thread> _pop_fastpath_task;
#endif
    uhd::log::severity_level _get_log_level(
        const std::string& log_level_str, const uhd::log::severity_level& previous_level)
    {
        if (std::isdigit(log_level_str[0])) {
            const uhd::log::severity_level log_level_num =
                uhd::log::severity_level(std::stoi(log_level_str));
            if (log_level_num >= uhd::log::trace and log_level_num <= uhd::log::fatal) {
                return log_level_num;
            } else {
                UHD_LOGGER_ERROR("LOG")
                    << "Failed to set log level to: " << log_level_str;
                return previous_level;
            }
        }

#define if_loglevel_equal(name) else if (log_level_str == #name) return uhd::log::name
        if_loglevel_equal(trace);
        if_loglevel_equal(debug);
        if_loglevel_equal(info);
        if_loglevel_equal(warning);
        if_loglevel_equal(error);
        if_loglevel_equal(fatal);
        if_loglevel_equal(off);
        return previous_level;
    }

    void _setup_console_logging()
    {
#ifndef UHD_LOG_CONSOLE_DISABLE
        uhd::log::severity_level console_level = uhd::log::trace;
#    ifdef UHD_LOG_CONSOLE_LEVEL
        console_level =
            _get_log_level(BOOST_STRINGIZE(UHD_LOG_CONSOLE_LEVEL), console_level);
#    endif
        const char* log_console_level_env = std::getenv("UHD_LOG_CONSOLE_LEVEL");
        if (log_console_level_env != NULL && log_console_level_env[0] != '\0') {
            console_level = _get_log_level(log_console_level_env, console_level);
        }
        _loggers[UHD_CONSOLE_LOGGER_KEY] = level_logfn_pair{console_level, &console_log};
#endif
    }

    void _setup_file_logging()
    {
        uhd::log::severity_level file_level = uhd::log::trace;
        std::string log_file_target;
#if defined(UHD_LOG_FILE_LEVEL)
        file_level = _get_log_level(BOOST_STRINGIZE(UHD_LOG_FILE_LEVEL), file_level);
#endif
#if defined(UHD_LOG_FILE)
        log_file_target = BOOST_STRINGIZE(UHD_LOG_FILE);
#endif
        const char* log_file_level_env = std::getenv("UHD_LOG_FILE_LEVEL");
        if (log_file_level_env != NULL && log_file_level_env[0] != '\0') {
            file_level = _get_log_level(log_file_level_env, file_level);
        }
        const char* log_file_env = std::getenv("UHD_LOG_FILE");
        if ((log_file_env != NULL) && (log_file_env[0] != '\0')) {
            log_file_target = std::string(log_file_env);
        }
        if (!log_file_target.empty()) {
            auto F = std::make_shared<file_logger_backend>(log_file_target);
            _loggers[UHD_FILE_LOGGER_KEY] = level_logfn_pair{file_level,
                [F](const uhd::log::logging_info& log_info) { F->log(log_info); }};
        }
    }

    void _publish_log_msg(const std::string& msg,
        const uhd::log::severity_level level = uhd::log::info,
        const std::string& component         = "LOGGING")
    {
        if (level < global_level) {
            return;
        }
        auto log_msg    = uhd::log::logging_info(pt::microsec_clock::local_time(),
            level,
            __FILE__,
            __LINE__,
            component,
            boost::this_thread::get_id());
        log_msg.message = msg;
        _log_queue.push_with_timed_wait(log_msg, 0.25);
    }

    std::mutex _logmap_mutex;
    std::atomic<bool> _exit;
    using level_logfn_pair = std::pair<uhd::log::severity_level, uhd::log::log_fn_t>;
    std::map<std::string, level_logfn_pair> _loggers;
#ifndef UHD_LOG_FASTPATH_DISABLE
    uhd::transport::bounded_buffer<std::string> _fastpath_queue;
#endif
    uhd::transport::bounded_buffer<uhd::log::logging_info> _log_queue;
};

UHD_SINGLETON_FCN(log_resource, log_rs);

/***********************************************************************
 * The logger object implementation
 **********************************************************************/
uhd::_log::log::log(const uhd::log::severity_level verbosity,
    const std::string& file,
    const unsigned int line,
    const std::string& component,
    const boost::thread::id thread_id)
    : _log_it(verbosity >= log_rs().global_level)
{
    if (_log_it) {
        this->_log_info = uhd::log::logging_info(pt::microsec_clock::local_time(),
            verbosity,
            file,
            line,
            component,
            thread_id);
    }
}

uhd::_log::log::~log(void)
{
    if (_log_it) {
        this->_log_info.message = _ss.str();
        try {
            log_rs().push(this->_log_info);
        } catch (...) {
        }
    }
}

#ifndef UHD_LOG_FASTPATH_DISABLE
void uhd::_log::log_fastpath(const std::string& msg)
{
    log_rs().push_fastpath(msg);
}
#else
void uhd::_log::log_fastpath(const std::string&)
{
    // nop
}
#endif

/***********************************************************************
 * Public API calls
 **********************************************************************/
void uhd::log::add_logger(const std::string& key, log_fn_t logger_fn)
{
    log_rs().add_logger(key, logger_fn);
}

void uhd::log::set_log_level(uhd::log::severity_level level)
{
    log_rs().global_level = level;
}

void uhd::log::set_logger_level(const std::string& key, uhd::log::severity_level level)
{
    log_rs().set_log_level(key, level);
}

void uhd::log::set_console_level(uhd::log::severity_level level)
{
    set_logger_level(UHD_CONSOLE_LOGGER_KEY, level);
}

void uhd::log::set_file_level(uhd::log::severity_level level)
{
    set_logger_level(UHD_FILE_LOGGER_KEY, level);
}