summaryrefslogtreecommitdiff
path: root/includes/Printer.h
blob: 2717a8409a04b3fc9221e1a0ade91fb7edc7f074 (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
#pragma once

#include <stdarg.h>
#include <BString.h>
#include <Threads.h>

namespace Balau {

enum {
    M_DEBUG = 1,
    M_INFO = 2,
    M_STATUS = 4,
    M_WARNING = 8,
    M_ERROR = 16,
    M_ALERT = 32,

    M_ALL = M_DEBUG | M_INFO | M_STATUS | M_WARNING | M_ERROR | M_ALERT,

    M_ENGINE_DEBUG = 64,
    M_MAX = M_ENGINE_DEBUG,
};

#undef E_STRING
#undef E_TASK
#undef E_EVENT
#undef E_HANDLE
#undef E_INPUT
#undef E_SOCKET
#undef E_THREAD
#undef E_SELECT

enum {
    E_STRING = 1,
    E_TASK = 2,
    E_EVENT = 4,
    E_HANDLE = 8,
    E_INPUT = 16,
    E_SOCKET = 32,
    E_THREAD = 64,
    E_OUTPUT = 128,
    E_HTTPSERVER = 256,
    E_ASYNC = 512,
    E_SELECT = 1024,
};

class Printer {
  protected:
    virtual void _print(const char * fmt, va_list ap);

  private:
    void _print(const char * fmt, ...);
    void _log(uint32_t level, const char * fmt, va_list ap);

    Lock m_lock;
  public:
      Printer();

    void setLocal();

    static Printer * getPrinter();
    static void log(uint32_t level, const String & fmt, ...) { va_list ap; va_start(ap, fmt); vlog(level, fmt.to_charp(), ap); va_end(ap); }
    static void log(uint32_t level, const char * fmt, ...) printfwarning(2, 3) { va_list ap; va_start(ap, fmt); vlog(level, fmt, ap); va_end(ap); }
    static void vlog(uint32_t level, const char * fmt, va_list ap) printfwarning(2, 0) { getPrinter()->_log(level, fmt, ap); }
    static void print(const String & fmt, ...) { va_list ap; va_start(ap, fmt); vprint(fmt.to_charp(), ap); va_end(ap); }
    static void print(const char * fmt, ...) printfwarning(1, 2) { va_list ap; va_start(ap, fmt); vprint(fmt, ap); va_end(ap); }
    static void vprint(const char * fmt, va_list ap) printfwarning(1, 0) { getPrinter()->_print(fmt, ap); }

#ifdef FULLDEBUG
    static void elog(uint32_t engine, const char * fmt, ...) printfwarning(2, 3) { va_list ap; va_start(ap, fmt); getPrinter()->_log(M_ENGINE_DEBUG, fmt, ap); }
#else
    static void elog(uint32_t engine, const char * fmt, ...) { }
#endif

    static void enable(uint32_t levels = M_ALL) { getPrinter()->m_verbosity |= levels; }
    static void disable(uint32_t levels = M_ALL) { getPrinter()->m_verbosity &= ~levels; }

    static void setDetailled(bool enable) { getPrinter()->m_detailledLogs = enable; }

  private:
    uint32_t m_verbosity = M_STATUS | M_WARNING | M_ERROR | M_ENGINE_DEBUG;
    bool m_detailledLogs = false;

      Printer(const Printer &) = delete;
    Printer & operator=(const Printer &) = delete;
};

};