error.h 21.7 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/* Spell-QTL  Software suite for the QTL analysis of modern datasets.
 * Copyright (C) 2016,2017  Damien Leroux <damien.leroux@inra.fr>, Sylvain Jasson <sylvain.jasson@inra.fr>
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

18
19
#ifndef _SPEL_ERROR_H_
#define _SPEL_ERROR_H_
20
21
22

#include <string>
#include <iostream>
23
#include <fstream>
24
#include <exception>
25
26
27
#include <set>
#include <utility>
#include <sstream>
28
#include <thread>
Damien Leroux's avatar
Damien Leroux committed
29
#include <mutex>
30
#include <condition_variable>
31
#include <vector>
32
#include <deque>
33
34
#include "stl_output.h"

35
36
extern "C" {
#include <unistd.h>
Damien Leroux's avatar
WIP.    
Damien Leroux committed
37
#include <sys/ioctl.h>
38
#include <signal.h>
39
}
40
41
42

#define _WHITE "\x1b[37;1m"
#define _RED "\x1b[31;1m"
Damien Leroux's avatar
WIP.    
Damien Leroux committed
43
#define _GREEN "\x1b[32;1m"
44
45
46
47
#define _YELLOW "\x1b[33;1m"
#define _CYAN "\x1b[36;1m"
#define _NORMAL "\x1b[0;m"

48
49
50
51
52
53
54
#define _DBG_INDENT_MARK '\x11'
#define _DBG_DEDENT_MARK '\x12'
#define _DBG_INDENT_MARK_S "\x11"
#define _DBG_DEDENT_MARK_S "\x12"
#define _DBG_SYNC_MARK '\x13'
#define _DBG_SYNC_MARK_S "\x13"

55
56
#define MSG_HANDLER_IS_SYNCED

57

58
59
60
61
62

static inline std::string __fetch_string(const std::ostream& os)
{
    return dynamic_cast<const std::stringstream*>(&os)->str();
}
63
#define MESSAGE(_expr_) __fetch_string(std::stringstream() << _expr_)
64
65
66
67




68
69
template <typename X, typename W>
using if_integral = typename std::enable_if<std::is_integral<X>::value, W>::type;
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
template <typename X, typename W>
using unless_integral = typename std::enable_if<!std::is_integral<X>::value, W>::type;



template <typename S>
struct builder {
    typedef std::basic_ostream<S> _Stream;
    std::stringstream _s;
    _Stream& out;

    static std::unique_lock<std::mutex> lock() { static std::mutex output_mutex; return std::unique_lock<std::mutex>(output_mutex); }

    builder(std::ostream& os) : _s(), out(os) {}
    builder(builder&& b) : _s(std::move(b._s)), out(b.out) {}
    ~builder() { auto synchronize_outputs = lock(); out << _s.str(); out.flush(); }

    builder<S>& operator << (const char* x) { std::move(_s) << x; return *this; }
    template <typename X> builder<S>& operator << (X&& x) { _s << x; return *this; }
//    template <typename X> builder<S>& operator << (const X& x) { std::move(_s) << x; return *this; }
//    template <typename X> unless_integral<X, builder<S>&> operator << (X&& x) { std::move(_s) << x; return *this; }
//    template <typename X> if_integral<X, builder<S>&> operator << (X x) { std::move(_s) << x; return *this; }
    builder<S>& operator << (_Stream&(*x)(_Stream&)) { _s << x; return *this; }
};



template <typename S>
struct endpoint {
    typedef std::basic_ostream<S> _Stream;
    _Stream& out;

    endpoint(_Stream& _) : out(_) {}

    builder<S> operator << (const char* x) { builder<S> b(out); b._s << x; return b; }
    template <typename X> builder<S> operator << (X&& x) { builder<S> b(out); b._s << x; return b; }
//    template <typename X> builder<S> operator << (const X& x) { builder<S> b(out); b._s << ((const X&) x); return b; }
//    template <typename X> unless_integral<X, builder<S>> operator << (const X&& x) { builder<S> b(out); b._s << x; return b; }
//    template <typename X> if_integral<X, builder<S>> operator << (X x) { builder<S> b(out); b._s << x; return b; }
    builder<S> operator << (_Stream&(*x)(_Stream&)) { builder<S> b(out); b._s << x; return b; }
};
Damien Leroux's avatar
Damien Leroux committed
112

113
114


115
116
struct DirectOutputIsForbidden : public std::runtime_error {
    DirectOutputIsForbidden()
117
            : std::runtime_error("Direct output to std::cerr, cout, or clog is forbidden. Use msg_handler_t::{cout|cerr|clog}() instead.")
118
119
120
    {}
};

121

122
123
124
125
126
127
128
129
130
131
/* inspired from http://stackoverflow.com/questions/22042414/c-stream-insert-string-after-newline */
class HeaderInserter : public std::streambuf {
    std::streambuf* dest;
    bool start_of_line;
    /*int indent;*/
    std::vector<std::string> prefix;
    bool prefix_insert_mode;
    std::stringstream prefix_ss;
protected:
    int overflow(int ch) override;
132

133
134
public:
    HeaderInserter(std::streambuf* dest)
135
136
            : dest(dest)
            , start_of_line(true)
137
138
139
140
            /*, indent(0)*/
            , prefix()
            , prefix_insert_mode(false)
            , prefix_ss()
141
    {}
142

143
144
    void rdbuf(std::streambuf* rb) { dest = rb; }
};
145

146
147
148
149
150
151
152
153
154
155
class ForbidOutput : public std::streambuf {
protected:
    int overflow(int) override
    {
        /* Direct output is forbidden */
        /*throw std::ios_base::failure("Direct output is forbidden");*/
        throw DirectOutputIsForbidden();
        abort();
    }
};
156

157
158
159
160
161
162
163
/* from http://wordaligned.org/articles/cpp-streambufs */
class teebuf: public std::streambuf
{
public:
    teebuf(std::streambuf * sb1, std::streambuf * sb2) : sb1(sb1), sb2(sb2) {}
private:
    virtual int overflow(int c)
164
    {
165
166
167
168
169
170
        if (c == EOF) {
            return !EOF;
        } else {
            int const r1 = sb1->sputc(c);
            int const r2 = sb2->sputc(c);
            return r1 == EOF || r2 == EOF ? EOF : c;
171
        }
172
    }
173

174
175
176
177
178
179
180
181
182
183
184
185
186
    virtual int sync() {
        int const r1 = sb1->pubsync();
        int const r2 = sb2->pubsync();
        return r1 == 0 && r2 == 0 ? 0 : -1;
    }
private:
    std::streambuf * sb1;
    std::streambuf * sb2;
};


struct ostream_manager {
    /* FIXME indent/dedent must be managed by this very class... Maybe use \x1 for indent and \x2 for dedent... */
187

188
189
190
191
192
    std::streambuf* old_cout_rdbuf;
    std::streambuf* old_clog_rdbuf;
    std::streambuf* old_cerr_rdbuf;
    HeaderInserter hi;
    ForbidOutput forbid;
193
194
    std::ostream _cerr, _cout, _clog;
    endpoint<char> cerr, cout, clog;
195

196
197
    std::vector<teebuf> tees;

198
    ostream_manager()
199
200
201
202
203
204
205
206
            : old_cout_rdbuf(std::cout.rdbuf())
            , old_clog_rdbuf(std::clog.rdbuf())
            , old_cerr_rdbuf(std::cerr.rdbuf())
            , hi(old_cout_rdbuf)
            , forbid()
            , _cerr(old_cerr_rdbuf), _cout(old_cout_rdbuf), _clog(&hi)
            , cerr(_cerr), cout(_cout), clog(_clog)
            , tees()
207
    {
208
        tees.reserve(3);
209
#ifndef SPELL_UNSAFE_OUTPUT
210
211
212
        std::clog.rdbuf(&forbid);
        std::cout.rdbuf(&forbid);
        std::cerr.rdbuf(&forbid);
213
214
#else
        std::clog.rdbuf(&hi);
215
#endif
216
217
218
219
    }

    ~ostream_manager()
    {
220
#ifndef SPELL_UNSAFE_OUTPUT
221
222
223
        std::clog.rdbuf(old_clog_rdbuf);
        std::cout.rdbuf(old_cout_rdbuf);
        std::cerr.rdbuf(old_cerr_rdbuf);
224
#endif
225
    }
226
227

    void
228
229
230
231
232
233
    redirect(std::ostream& os)
    {
        hi.rdbuf(os.rdbuf());
        _cerr.rdbuf(os.rdbuf());
        _cout.rdbuf(os.rdbuf());
    }
234
235

    void
236
237
238
239
240
241
    restore()
    {
        hi.rdbuf(old_clog_rdbuf);
        _cerr.rdbuf(old_cerr_rdbuf);
        _cout.rdbuf(old_cout_rdbuf);
    }
242
243
244
245
246
247
248

    void
    tee(std::ostream& os)
    {
        restore();
        tees.clear();
        tees.emplace_back(old_cout_rdbuf, os.rdbuf());
249
        _cout.rdbuf(&tees.back());
250
        tees.emplace_back(old_cerr_rdbuf, os.rdbuf());
251
        _cerr.rdbuf(&tees.back());
252
253
254
        tees.emplace_back(old_clog_rdbuf, os.rdbuf());
        hi.rdbuf(&tees.back());
    }
255
256
};

257
258


259
struct msg_handler_t {
260
#ifdef MSG_HANDLER_IS_SYNCED
261
262
    typedef std::recursive_mutex lock_type;
    typedef std::unique_lock<lock_type> scoped_lock_type;
263
264
265
266
267
268
269
270
271
#else
    typedef struct {
        void lock() {}
        void unlock() {}
    } lock_type;
    typedef struct _slt {
        _slt(lock_type&) {}
    } scoped_lock_type;
#endif
272

273
274
275
276
    struct state_t {
        bool color;
        std::set<std::string> workarounds;
        int count;
277
        int debug_indent;
278
        bool debug_enabled;
279
        bool quiet;
280
        std::vector<std::function<void()>> hooks;
281
        std::ostream raw_cout;
282
        ostream_manager manager;
283
        std::unique_ptr<std::ofstream> log_file;
284
285
286
287
288
289

        const char* error() { ++count; return color ? _RED : ""; }
        const char* warning() { return color ? _YELLOW : ""; }
        const char* info() { return color ? _CYAN : ""; }
        const char* normal() { return color ? _NORMAL : ""; }

290
        state_t()
291
292
                : color(!!isatty(fileno(stdout))), workarounds(), count(0), debug_indent(0), debug_enabled(true), quiet(false), hooks()
                , raw_cout(std::cout.rdbuf()), manager(), log_file(nullptr)
293
        {
294
            /*std::cout << "Message handler instance created." << std::endl;*/
295
        }
296
        ~state_t() { close_log_file(); }
297
298
        void check(bool fatal);
        void reset();
299
        void run_hooks() { for (auto& f: hooks) { f(); } }
300
301
302
        void close_log_file()
        {
            if (log_file) {
303
                manager.restore();
304
305
306
307
308
309
310
311
312
                log_file->close();
                log_file.reset(nullptr);
            }

        }
        void set_log_file(const std::string& path)
        {
            close_log_file();
            log_file.reset(new std::ofstream(path, std::ios_base::out|std::ios_base::app));
313
            manager.tee(*log_file);
314
        }
315
316
317
318
319
320
321
322
323
324
325
326
327
328
    };

    static state_t& instance() { static state_t _; return _; }

    static void set_color(bool _) { instance().color = _; }
    static bool color() { return instance().color; }

    static const char* e() { return instance().error(); }
    static const char* w() { return instance().warning(); }
    static const char* i() { return instance().info(); }
    static const char* n() { return instance().normal(); }

    static void check(bool fatal) { instance().check(fatal); }
    static void reset() { instance().reset(); }
329

330
331
332
    static void hook(std::function<void()>&& f) { instance().hooks.push_back(f); }
    static void run_hooks() { instance().run_hooks(); }

333
    static void indent() { instance().debug_indent += 3; }
334
335
336
337
338
339
340
341
342
343
344
345
346

    static endpoint<char>& cout() { return instance().manager.cout; }
    static endpoint<char>& cerr() { return instance().manager.cerr; }
    static endpoint<char>& clog() { return instance().manager.clog; }
//    static endpoint<std::ostream>& channel(msg_channel c)
//    {
//        switch (c) {
//            case msg_channel::Err: return cerr();
//            case msg_channel::Out: return cout();
//            case msg_channel::Log: return clog();
//        };
//    }

347
348
//    static void dedent() { instance().debug_indent -= 3; }
//    static int get_indent() { return instance().debug_indent; }
349

350
351
//    static void enqueue(const message_handle& mh) { instance().manager.enqueue(mh); }
//    static void wait_for_flush() { instance().manager.wait_for_flush(); }
352

Damien Leroux's avatar
WIP.    
Damien Leroux committed
353
354
355
356
357
358
359
    static int termcols()
    {
        struct winsize w;
        ioctl(0, TIOCGWINSZ, &w);
        return w.ws_col;
    }

360
    static bool& debug_enabled() { return instance().debug_enabled; }
361
    static bool& quiet() { return instance().quiet; }
362

363
    static lock_type mutex;
364

365
366
    static void redirect(std::ostream& os) { instance().manager.redirect(os); }
    static void restore() { instance().manager.restore(); }
367
    static void set_log_file(const std::string& path) { instance().set_log_file(path); }
368
369
};

370
371


372
inline
373
int HeaderInserter::overflow(int ch)
374
375
{
    int retval = 0;
376
    if (ch == _DBG_INDENT_MARK) {
377
378
379
380
381
382
383
384
        /*indent += 3;*/
        prefix_insert_mode = !prefix_insert_mode;
        if (prefix_insert_mode) {
            prefix_ss.clear();
            prefix_ss.str(std::string());
        } else {
            prefix.push_back(prefix_ss.str());
        }
385
    } else if (ch == _DBG_DEDENT_MARK) {
386
        if (prefix_insert_mode) {
387
            overflow(_DBG_INDENT_MARK);
388
389
390
391
392
393
394
        }
        if (prefix.size()) {
            prefix.pop_back();
        }
        /*indent -= 3 * (indent > 0);*/
    } else if (prefix_insert_mode) {
        prefix_ss << (char) ch;
395
    } else if (ch != traits_type::eof()) {
396
        if (start_of_line) {
397
398
            for (const std::string& s: prefix) {
                for (const char c: s) {
399
                    dest->sputc(c);
400
//                    dest->overflow(c);
401
                }
402
            }
403
            /*for (int i = 0; i < indent; ++i) {*/
404
            /*dest->sputc(' ');*/
405
            /*}*/
406
        }
407
        retval = dest->sputc(ch);
408
//        retval = dest->overflow(ch);
409
410
411
412
413
        start_of_line = ch == '\n';
    }
    return retval;
}

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
















#if 0

enum msg_channel { Out, Err, Log };

struct message_struc {
    msg_channel channel;
    std::string message;
    message_struc(msg_channel c, std::string&& msg) : channel(c), message(std::move(msg)) {}
};

typedef std::shared_ptr<message_struc> message_handle;

/*#define MAKE_MESSAGE(_dest_, _expr_) do { std::stringstream __s; __s << _expr_; _dest_ = __s.str(); } while (0)*/




/*#define CREATE_MESSAGE(_var_, _channel_, _expr_) message_handle _var_(new message_struc {_channel_, MESSAGE(_expr_)});*/

#ifndef SPELL_UNSAFE_OUTPUT
//#define CREATE_MESSAGE(_channel_, _what_) if (!msg_handler_t::quiet()) msg_handler_t::enqueue(message_handle{new message_struc {_channel_, _what_}});
#define CREATE_MESSAGE(_channel_, _what_) do { msg_handler_t::channel(_channel) << _what_; } while (0)
#else
#define CREATE_MESSAGE(_channel_, _what_) if (!msg_handler_t::quiet()) switch(_channel_) { \
        case msg_channel::Out: \
            std::cout << _what_; \
            break; \
        case msg_channel::Err: \
            std::cerr << _what_; \
            break; \
        case msg_channel::Log: \
            std::clog << _what_; \
            break; \
    };
#endif




struct message_queue : public ostream_manager {
    typedef std::mutex mutex_type;
    typedef std::unique_lock<mutex_type> scoped_lock_type;

    std::deque<message_handle> m_queue;
    mutex_type m_mutex;
    mutex_type m_stream_mutex;
    std::condition_variable m_condition;
    std::condition_variable m_flush_condition;

    static void catch_SIGSEG(int signo);
    static sighandler_t& old_sighandler() { static sighandler_t _; return _; }

    bool m_stop;

    std::thread m_thread;

    message_queue()
        : ostream_manager()
        , m_queue()
        , m_mutex()
        , m_condition()
        , m_stop(false)
        , m_thread([this] () { run(); })
    {
        /*old_sighandler() = signal(SIGSEGV, catch_SIGSEG);*/
    }

    ~message_queue()
    {
        {
            scoped_lock_type lock(m_mutex);
            m_stop = true;
        }
        m_condition.notify_one();
        m_thread.join();
    }

    void enqueue(const message_handle& mh)
    {
        scoped_lock_type slt(m_mutex);
        m_queue.push_back(mh);
        m_condition.notify_one();
    }

    void lock_stream() { m_stream_mutex.lock(); }
    void unlock_stream() { m_stream_mutex.unlock(); }

    void run();

    void wait_for_flush()
    {
        scoped_lock_type lock(m_mutex);
        while (!m_queue.empty()) {
            m_flush_condition.wait(lock);
        }
    }
};





532
533
534
inline
void message_queue::run()
{
Damien Leroux's avatar
Damien Leroux committed
535
    message_handle  next;
536
537
    while (true)
    {
Damien Leroux's avatar
Damien Leroux committed
538
539
        {
            scoped_lock_type lock(m_mutex);
540

Damien Leroux's avatar
Damien Leroux committed
541
            m_condition.wait(lock, [this]() { return m_stop || !m_queue.empty(); });
542

Damien Leroux's avatar
Damien Leroux committed
543
544
545
            /*while (!m_stop && m_queue.empty()) {*/
                /*m_condition.wait(lock);*/
            /*}*/
546

Damien Leroux's avatar
Damien Leroux committed
547
548
549
            if (m_stop && m_queue.empty()) {
                return;
            }
550

Damien Leroux's avatar
Damien Leroux committed
551
552
            next = m_queue.front();
            m_queue.pop_front();
553

Damien Leroux's avatar
Damien Leroux committed
554
        }
555
        if (next->message == _DBG_SYNC_MARK_S) {
Damien Leroux's avatar
Damien Leroux committed
556
557
558
            m_flush_condition.notify_one();
            continue;
        } else if (next->message.size() == 0) {
559
560
            continue;
        }
Damien Leroux's avatar
Damien Leroux committed
561
562
563
        std::ostream& channel = next->channel == msg_channel::Out ? cout
                              : next->channel == msg_channel::Log ? clog
                              : cerr;
564
565
566
567
568
569
570
        lock_stream();
        channel << next->message << std::flush;
        unlock_stream();
        msg_handler_t::run_hooks();
    }
}

571
572
573
574
#endif



575
576
577
578
579
580
581
582
#ifdef SPELL_UNSAFE_OUTPUT

#define MSG_ERROR(_msg_expr_, _workaround_expr_) do { std::cerr << _msg_expr_ << std::endl; } while (0)
#define MSG_WARNING(_msg_expr_)  do { std::cout << _msg_expr_ << std::endl; } while (0)
#define MSG_INFO(_msg_expr_) do { std::cout << _msg_expr_ << std::endl; } while (0)
#define MSG_QUEUE_FLUSH()
#define MSG_DEBUG(_msg_expr_) do { std::clog << _msg_expr_ << std::endl; } while (0)

583
#elif 0
584

585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
#define MSG_ERROR(_msg_expr_, _workaround_expr_) \
    do {\
        CREATE_MESSAGE(msg_channel::Err, MESSAGE(msg_handler_t::e() << "[ERR] " << _msg_expr_ << msg_handler_t::n() << std::endl));\
        std::stringstream s; s << _workaround_expr_;\
        if (s.str().size()) { msg_handler_t::instance().workarounds.insert(s.str()); }\
} while (0)

#define MSG_WARNING(_msg_expr_) \
    do {\
        CREATE_MESSAGE(msg_channel::Out, MESSAGE(msg_handler_t::w() << "[WRN] " << _msg_expr_ << msg_handler_t::n() << std::endl));\
} while (0)

#define MSG_INFO(_msg_expr_) \
    do {\
        CREATE_MESSAGE(msg_channel::Out, MESSAGE(msg_handler_t::i() << "[MSG] " << _msg_expr_ << msg_handler_t::n() << std::endl));\
} while (0)

602
603
604
605
606
#define MSG_QUEUE_FLUSH() do { \
    CREATE_MESSAGE(msg_channel::Log, _DBG_SYNC_MARK_S); \
    msg_handler_t::wait_for_flush(); \
} while (0)

607
608
#define MSG_DEBUG(_msg_expr_) \
    do {\
609
610
611
612
        if (msg_handler_t::debug_enabled()) {\
            CREATE_MESSAGE(msg_channel::Log, MESSAGE(_msg_expr_ << std::endl));\
        }\
    } while (0);
613

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
#define MSG_DEBUG_INDENT CREATE_MESSAGE(msg_channel::Log, _DBG_INDENT_MARK_S "   " _DBG_INDENT_MARK_S)
#define MSG_DEBUG_INDENT_EXPR(_str_) CREATE_MESSAGE(msg_channel::Log, _DBG_INDENT_MARK << _str_ << _DBG_INDENT_MARK)
#define MSG_DEBUG_DEDENT CREATE_MESSAGE(msg_channel::Log, _DBG_DEDENT_MARK_S)

#else

#define MSG_ERROR(_msg_expr_, _workaround_expr_) \
    do {\
        msg_handler_t::cerr() << msg_handler_t::e() << "[ERR] " << _msg_expr_ << msg_handler_t::n() << std::endl;\
        std::stringstream s; s << _workaround_expr_;\
        if (s.str().size()) { msg_handler_t::instance().workarounds.insert(s.str()); }\
} while (0)

#define MSG_WARNING(_msg_expr_) \
    do {\
        msg_handler_t::cout() << msg_handler_t::w() << "[WRN] " << _msg_expr_ << msg_handler_t::n() << std::endl;\
} while (0)

#define MSG_INFO(_msg_expr_) \
    do {\
        msg_handler_t::cout() << msg_handler_t::i() << "[MSG] " << _msg_expr_ << msg_handler_t::n() << std::endl;\
} while (0)

#define MSG_QUEUE_FLUSH()

#define MSG_DEBUG(_msg_expr_) \
    do {\
        if (msg_handler_t::debug_enabled()) {\
            msg_handler_t::clog() << _msg_expr_ << std::endl;\
        }\
    } while (0);

#define MSG_DEBUG_INDENT do { msg_handler_t::clog() << _DBG_INDENT_MARK_S "   " _DBG_INDENT_MARK_S; } while (0)
#define MSG_DEBUG_INDENT_EXPR(_str_) do { msg_handler_t::clog() << _DBG_INDENT_MARK << _str_ << _DBG_INDENT_MARK; } while (0)
#define MSG_DEBUG_DEDENT do { msg_handler_t::clog() << _DBG_DEDENT_MARK_S; } while (0)

650
#endif
Damien Leroux's avatar
Damien Leroux committed
651

652
#if 0
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#define MSG_ERROR(_msg_expr_, _workaround_expr_) \
    do {\
        {msg_handler_t::scoped_lock_type _(msg_handler_t::mutex);\
        std::cerr << msg_handler_t::e() << "[ERR] " << _msg_expr_ << msg_handler_t::n() << std::endl;}\
        std::stringstream s; s << _workaround_expr_;\
        if (s.str().size()) { msg_handler_t::instance().workarounds.insert(s.str()); }\
        msg_handler_t::run_hooks();\
} while (0)

#define MSG_WARNING(_msg_expr_) \
    do {\
        msg_handler_t::scoped_lock_type _(msg_handler_t::mutex);\
        std::cerr << msg_handler_t::w() << "[WRN] " << _msg_expr_ << msg_handler_t::n() << std::endl;\
        msg_handler_t::run_hooks();\
    } while(0)

#define MSG_INFO(_msg_expr_) \
    do {\
        msg_handler_t::scoped_lock_type _(msg_handler_t::mutex);\
        std::cout << msg_handler_t::i() << "[MSG] " << _msg_expr_ << msg_handler_t::n() << std::endl;\
        msg_handler_t::run_hooks();\
    } while(0)

#define MSG_DEBUG(_msg_expr_) \
    do {\
        msg_handler_t::scoped_lock_type _(msg_handler_t::mutex);\
679
        std::clog << _msg_expr_ << std::endl;\
680
681
        msg_handler_t::run_hooks();\
    } while(0)
682
#endif
683

684

685
686
/*#ifndef _SPELL_ERROR_H_MSGQ_CATCH_SIGSEG_*/
/*#define _SPELL_ERROR_H_MSGQ_CATCH_SIGSEG_*/
687
688
689
690
691
692
693
694
//inline
//void message_queue::catch_SIGSEG(int)
//{
//    MSG_DEBUG("**SEGFAULT DETECTED**");
//    MSG_QUEUE_FLUSH();
//    signal(SIGSEGV, old_sighandler());
//    raise(SIGSEGV);
//}
695

696
/*#endif*/
697

698
699
700
701
702
703
struct scoped_indent {
    scoped_indent() { MSG_DEBUG_INDENT; }
    scoped_indent(const std::string& str) { MSG_DEBUG_INDENT_EXPR(str); }
    ~scoped_indent() { MSG_DEBUG_DEDENT; }
};

704

705
706
inline void msg_handler_t::state_t::check(bool fatal)
{
707
    msg_handler_t::scoped_lock_type _(msg_handler_t::mutex);
708
    if (count > 0) {
709
710
711
        cerr() << info() << "[MSG] " << count << " error"
               << (count > 1 ? "s were" : " was")
               << " reported. Suggestions to fix this:" << std::endl;
712
        for (auto& w: workarounds) {
713
            cerr() << info() << "      - " << w << normal() << std::endl;
714
715
        }
        if (fatal) {
716
            cout() << normal() <<"At least one fatal error encountered. Aborting process." << std::endl;
717
718
719
720
721
722
723
724
725
726
            exit(-count);
        } else {
            reset();
        }
    }
}

inline void msg_handler_t::state_t::reset()
{
    if (workarounds.size()) {
727
        MSG_WARNING(workarounds.size() << " workarounds silently discarded");
728
729
730
731
732
733
734
    }
    count = 0;
    workarounds.clear();
}

#define WHITE (msg_handler_t::instance().color ? _WHITE : "")
#define YELLOW (msg_handler_t::instance().color ? _YELLOW : "")
735
#define GREEN (msg_handler_t::instance().color ? _GREEN : "")
736
737
738
739
#define RED (msg_handler_t::instance().color ? _RED : "")
#define CYAN (msg_handler_t::instance().color ? _CYAN : "")
#define NORMAL (msg_handler_t::instance().color ? _NORMAL : "")

740
741
742
743
744
745
#ifdef NDEBUG
#define DUMP_FILE_LINE()
#else
#define DUMP_FILE_LINE() MSG_DEBUG(__FILE__ << ':' << __LINE__)
#endif

746
747
#endif