summaryrefslogtreecommitdiffstats
path: root/logd/src/logd/service.h
blob: 3ff2e7fab929903e0bbf370d496e21ed202dc34c (plain) (blame)
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include <logd/config-logd.h>
#include <vespa/vespalib/util/hashmap.h>
#include <vespa/log/log.h>
#include <cassert>

namespace logdemon {

typedef ns_log::Logger::LogLevel LogLevel;

class Component
{
    unsigned long _isforwarding;
    double        _lastseen;
    int           _lastpid;
    const char   *_myservice;
    char         *_myname;
    char         *_logctlname;

    Component(const Component& other);
    Component& operator= (const Component& other);

    static unsigned long defFwd;
public:
    static void defaultDoForward(LogLevel level)   { defFwd |=  (1 << level); }
    static void defaultDontForward(LogLevel level) { defFwd &= ~(1 << level); }

    void doForward(LogLevel level)   { _isforwarding |=  (1 << level); }
    void dontForward(LogLevel level) { _isforwarding &= ~(1 << level); }
    bool shouldForward(LogLevel level) {
        return ((_isforwarding & (1 << level)) != 0);
    }
    void doLogAtAll(LogLevel level);
    void dontLogAtAll(LogLevel level);
    bool shouldLogAtAll(LogLevel level);
    Component(const char *servicename, const char *name)
        : _isforwarding(defFwd), _lastseen(0.0), _lastpid(0),
          _myservice(servicename), _myname(strdup(name)),
          _logctlname(strdup(name))
        {
            assert(ns_log::Logger::NUM_LOGLEVELS < 32);
            const char *withoutprefix = strchr(name, '.');
            if (withoutprefix != NULL) {
                strcpy(_logctlname, withoutprefix);
            } else {
                strcpy(_logctlname, "");
            }
        }
    ~Component() { free(_myname); free(_logctlname); }
    void remember(double t, int p) { _lastseen = t; _lastpid = p; }
    double lastSeen() const { return _lastseen; }
    double lastPid() const  { return _lastpid; }
};

typedef vespalib::HashMap<Component *> CompMap;
typedef vespalib::HashMap<Component *>::Iterator CompIter;

class Service
{
private:
    char *_myname;
    Service(const Service& other);
    Service& operator= (const Service& other);
public:
    CompMap _components;
    Component *getComponent(const char *comp) {
        if (! _components.isSet(comp)) {
            _components.set(comp, new Component(_myname, comp));
        }
        return _components[comp];
    }
    Service(const char *name)
        : _myname(strdup(name)), _components(NULL) {}
    ~Service();
};

typedef vespalib::HashMap<Service *> ServMap;
typedef vespalib::HashMap<Service *>::Iterator ServIter;

class Services
{
public:
    ServMap _services;
    Service *getService(const char *serv) {
        if (! _services.isSet(serv)) {
            _services.set(serv, new Service(serv));
        }
        return _services[serv];
    }
    Services() : _services(NULL) {}
    ~Services();
    void dumpState(int fildesc);
};

} // namespace