aboutsummaryrefslogtreecommitdiff
path: root/src/mm-log-object.c
blob: ed41355f9541e4eb3843b182a52a4fdb8df44141 (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
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * 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 2 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:
 *
 * Copyright (C) 2020 Aleksander Morgado <aleksander@aleksander.es>
 */

#include "mm-log-object.h"

G_DEFINE_INTERFACE (MMLogObject, mm_log_object, G_TYPE_OBJECT)

/*****************************************************************************/
/* Private data context */

#define PRIVATE_TAG "log-object"
static GQuark private_quark;

typedef struct {
  gchar *owner_id;
  gchar *id;
} Private;

static void
private_free (Private *priv)
{
    g_free (priv->owner_id);
    g_free (priv->id);
    g_slice_free (Private, priv);
}

static Private *
get_private (MMLogObject *self)
{
    Private *priv;

    if (G_UNLIKELY (!private_quark))
        private_quark = g_quark_from_static_string (PRIVATE_TAG);

    priv = g_object_get_qdata (G_OBJECT (self), private_quark);
    if (!priv) {
        priv = g_slice_new0 (Private);
        g_object_set_qdata_full (G_OBJECT (self), private_quark, priv, (GDestroyNotify)private_free);
    }

    return priv;
}

const gchar *
mm_log_object_get_id (MMLogObject *self)
{
    Private *priv;

    priv = get_private (self);
    if (!priv->id) {
        gchar *self_id;

        self_id = MM_LOG_OBJECT_GET_IFACE (self)->build_id (self);
        if (priv->owner_id) {
            priv->id = g_strdup_printf ("%s/%s", priv->owner_id, self_id);
            g_free (self_id);
        } else
            priv->id = self_id;
    }
    return priv->id;
}

void
mm_log_object_set_owner_id (MMLogObject *self,
                            const gchar *owner_id)
{
    Private *priv;

    priv = get_private (self);
    g_free (priv->owner_id);
    priv->owner_id = g_strdup (owner_id);
}

static void
mm_log_object_default_init (MMLogObjectInterface *iface)
{
}