1 /* GSequencer - Advanced GTK Sequencer
2  * Copyright (C) 2005-2020 Joël Krähemann
3  *
4  * This file is part of GSequencer.
5  *
6  * GSequencer is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * GSequencer is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with GSequencer.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <ags/lib/ags_log.h>
21 
22 #include <stdlib.h>
23 
24 void ags_log_class_init(AgsLogClass *log);
25 void ags_log_init (AgsLog *log);
26 void ags_log_finalize(GObject *gobject);
27 
28 /**
29  * SECTION:ags_log
30  * @short_description: Log
31  * @title: AgsLog
32  * @section_id:
33  * @include: ags/lib/ags_log.h
34  *
35  * The #AgsLog logging class.
36  */
37 
38 static gpointer ags_log_parent_class = NULL;
39 AgsLog *ags_log = NULL;
40 
41 GType
ags_log_get_type(void)42 ags_log_get_type(void)
43 {
44   static volatile gsize g_define_type_id__volatile = 0;
45 
46   if(g_once_init_enter (&g_define_type_id__volatile)){
47     GType ags_type_log = 0;
48 
49     static const GTypeInfo ags_log_info = {
50       sizeof (AgsLogClass),
51       NULL, /* base_init */
52       NULL, /* base_finalize */
53       (GClassInitFunc) ags_log_class_init,
54       NULL, /* class_finalize */
55       NULL, /* class_data */
56       sizeof (AgsLog),
57       0,    /* n_preallocs */
58       (GInstanceInitFunc) ags_log_init,
59     };
60 
61     ags_type_log = g_type_register_static(G_TYPE_OBJECT,
62 					  "AgsLog",
63 					  &ags_log_info,
64 					  0);
65 
66     g_once_init_leave(&g_define_type_id__volatile, ags_type_log);
67   }
68 
69   return g_define_type_id__volatile;
70 }
71 
72 void
ags_log_class_init(AgsLogClass * log)73 ags_log_class_init(AgsLogClass *log)
74 {
75   GObjectClass *gobject;
76 
77   ags_log_parent_class = g_type_class_peek_parent(log);
78 
79   /* GObjectClass */
80   gobject = (GObjectClass *) log;
81 
82   gobject->finalize = ags_log_finalize;
83 }
84 
85 void
ags_log_init(AgsLog * log)86 ags_log_init(AgsLog *log)
87 {
88   log->flags = 0;
89 
90   /* create mutex */
91   g_rec_mutex_init(&(log->obj_mutex));
92 
93   log->messages = NULL;
94 }
95 
96 void
ags_log_finalize(GObject * gobject)97 ags_log_finalize(GObject *gobject)
98 {
99   AgsLog *log;
100 
101   log = AGS_LOG(gobject);
102 
103   /* free messages and list */
104   g_list_free_full(g_atomic_pointer_get(&(log->messages)),
105 		   g_free);
106 
107   if(log == ags_log){
108     ags_log = NULL;
109   }
110 
111   /* call parent */
112   G_OBJECT_CLASS(ags_log_parent_class)->finalize(gobject);
113 }
114 
115 /**
116  * ags_log_get_instance:
117  *
118  * Get your logging instance.
119  *
120  * Returns: (transfer none): the #AgsLog instance
121  *
122  * Since: 3.0.0
123  */
124 AgsLog*
ags_log_get_instance()125 ags_log_get_instance()
126 {
127   static GMutex mutex;
128 
129   g_mutex_lock(&mutex);
130 
131   if(ags_log == NULL){
132     ags_log = ags_log_new();
133   }
134 
135   g_mutex_unlock(&mutex);
136 
137   return(ags_log);
138 }
139 
140 /**
141  * ags_log_add_message:
142  * @log: the #AgsLog
143  * @str: (transfer full): the message
144  *
145  * Add a message to @log.
146  *
147  * Since: 3.0.0
148  */
149 void
ags_log_add_message(AgsLog * log,gchar * str)150 ags_log_add_message(AgsLog *log,
151 		    gchar *str)
152 {
153   g_rec_mutex_lock(&(log->obj_mutex));
154 
155   g_atomic_pointer_set(&(log->messages),
156 		       g_list_prepend(g_atomic_pointer_get(&(log->messages)),
157 				      g_strdup(str)));
158 
159   g_rec_mutex_unlock(&(log->obj_mutex));
160 }
161 
162 /**
163  * ags_log_get_messages:
164  * @log: the #AgsLog
165  *
166  * Get log messages as #GList-struct containing strings.
167  *
168  * Returns: (element-type utf8) (transfer none): the #GList-struct containing log messages
169  *
170  * Since: 3.0.0
171  */
172 GList*
ags_log_get_messages(AgsLog * log)173 ags_log_get_messages(AgsLog *log)
174 {
175   return(g_atomic_pointer_get(&(log->messages)));
176 }
177 
178 /**
179  * ags_log_new:
180  *
181  * Instantiate a new #AgsLog.
182  *
183  * Returns: the new instance
184  *
185  * Since: 3.0.0
186  */
187 AgsLog*
ags_log_new()188 ags_log_new()
189 {
190   AgsLog *log;
191 
192   log = g_object_new(AGS_TYPE_LOG,
193 		     NULL);
194 
195   return(log);
196 }
197