summaryrefslogtreecommitdiff
path: root/src/libaudcore/logger.cc
blob: 5fa300b35d5f5b0d45ba6f83f3b7ba918ccd01e9 (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
/*
 * logger.cc
 * Copyright 2014 John Lindgren and William Pitcock
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions, and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions, and the following disclaimer in the documentation
 *    provided with the distribution.
 *
 * This software is provided "as is" and without any warranty, express or
 * implied. In no event shall the authors be liable for any damages arising from
 * the use of this software.
 */

#include "audstrings.h"
#include "index.h"
#include "runtime.h"
#include "tinylock.h"

#include <stdio.h>

namespace audlog {

struct HandlerData {
    Handler handler;
    Level level;
};

static TinyRWLock lock;
static Index<HandlerData> handlers;
static Level stderr_level = Warning;
static Level min_level = Warning;

EXPORT void set_stderr_level (Level level)
{
    tiny_lock_write (& lock);

    min_level = stderr_level = level;

    for (const HandlerData & h : handlers)
    {
        if (h.level < min_level)
            min_level = h.level;
    }

    tiny_unlock_write (& lock);
}

EXPORT void subscribe (Handler handler, Level level)
{
    tiny_lock_write (& lock);

    handlers.append (handler, level);

    if (level < min_level)
        min_level = level;

    tiny_unlock_write (& lock);
}

EXPORT void unsubscribe (Handler handler)
{
    tiny_lock_write (& lock);

    auto is_match = [=] (const HandlerData & data)
        { return data.handler == handler; };

    handlers.remove_if (is_match, true);

    min_level = stderr_level;

    for (const HandlerData & h : handlers)
    {
        if (h.level < min_level)
            min_level = h.level;
    }

    tiny_unlock_write (& lock);
}

EXPORT const char * get_level_name (Level level)
{
    switch (level)
    {
        case Debug: return "DEBUG";
        case Info: return "INFO";
        case Warning: return "WARNING";
        case Error: return "ERROR";
    };

    return nullptr;
}

EXPORT void log (Level level, const char * file, int line, const char * func,
 const char * format, ...)
{
    tiny_lock_read (& lock);

    if (level >= min_level)
    {
        va_list args;
        va_start (args, format);
        StringBuf message = str_vprintf (format, args);
        va_end (args);

        if (level >= stderr_level)
            fprintf (stderr, "%s %s:%d [%s]: %s", get_level_name (level), file,
             line, func, (const char *) message);

        for (const HandlerData & h : handlers)
        {
            if (level >= h.level)
                h.handler (level, file, line, func, message);
        }
    }

    tiny_unlock_read (& lock);
}

} // namespace audlog