1 /* iptc-log.c
2  *
3  * Copyright � 2005 David Moore <dcm@acm.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20 
21 #include <config.h>
22 #include <libiptcdata/iptc-log.h>
23 #include "i18n.h"
24 
25 #include <string.h>
26 
27 struct _IptcLog {
28 	unsigned int ref_count;
29 
30 	IptcLogFunc func;
31 	void *data;
32 
33 	IptcMem *mem;
34 };
35 
36 static struct {
37 	IptcLogCode code;
38 	const char *title;
39 	const char *message;
40 } codes[] = {
41 	{ IPTC_LOG_CODE_DEBUG, N_("Debugging information"),
42 	  N_("Debugging information is available.") },
43 	{ IPTC_LOG_CODE_NO_MEMORY, N_("Not enough memory"),
44 	  N_("The system cannot provide enough memory.") },
45 	{ IPTC_LOG_CODE_CORRUPT_DATA, N_("Corrupt data"),
46 	  N_("The data provided does not follow the specification.") },
47 	{ 0, NULL, NULL }
48 };
49 
50 const char *
iptc_log_code_get_title(IptcLogCode code)51 iptc_log_code_get_title (IptcLogCode code)
52 {
53 	unsigned int i;
54 
55 	for (i = 0; codes[i].title; i++) if (codes[i].code == code) break;
56 	return _(codes[i].title);
57 }
58 
59 const char *
iptc_log_code_get_message(IptcLogCode code)60 iptc_log_code_get_message (IptcLogCode code)
61 {
62 	unsigned int i;
63 
64 	for (i = 0; codes[i].message; i++) if (codes[i].code == code) break;
65 	return _(codes[i].message);
66 }
67 
68 IptcLog *
iptc_log_new_mem(IptcMem * mem)69 iptc_log_new_mem (IptcMem *mem)
70 {
71 	IptcLog *log;
72 
73 	log = iptc_mem_alloc (mem, sizeof (IptcLog));
74 	if (!log) return NULL;
75 	log->ref_count = 1;
76 
77 	log->mem = mem;
78 	iptc_mem_ref (mem);
79 
80 	return log;
81 }
82 
83 IptcLog *
iptc_log_new(void)84 iptc_log_new (void)
85 {
86 	IptcMem *mem = iptc_mem_new_default ();
87 	IptcLog *log = iptc_log_new_mem (mem);
88 
89 	iptc_mem_unref (mem);
90 
91 	return log;
92 }
93 
94 void
iptc_log_ref(IptcLog * log)95 iptc_log_ref (IptcLog *log)
96 {
97 	if (!log) return;
98 	log->ref_count++;
99 }
100 
101 void
iptc_log_unref(IptcLog * log)102 iptc_log_unref (IptcLog *log)
103 {
104 	if (!log) return;
105 	if (log->ref_count > 0) log->ref_count--;
106 	if (!log->ref_count) iptc_log_free (log);
107 }
108 
109 void
iptc_log_free(IptcLog * log)110 iptc_log_free (IptcLog *log)
111 {
112 	if (!log) return;
113 
114 	iptc_mem_free (log->mem, log);
115 }
116 
117 void
iptc_log_set_func(IptcLog * log,IptcLogFunc func,void * data)118 iptc_log_set_func (IptcLog *log, IptcLogFunc func, void *data)
119 {
120 	if (!log) return;
121 	log->func = func;
122 	log->data = data;
123 }
124 
125 void
iptc_log(IptcLog * log,IptcLogCode code,const char * domain,const char * format,...)126 iptc_log (IptcLog *log, IptcLogCode code, const char *domain,
127 	  const char *format, ...)
128 {
129 	va_list args;
130 
131 	va_start (args, format);
132 	iptc_logv (log, code, domain, format, args);
133 	va_end (args);
134 }
135 
136 void
iptc_logv(IptcLog * log,IptcLogCode code,const char * domain,const char * format,va_list args)137 iptc_logv (IptcLog *log, IptcLogCode code, const char *domain,
138 	   const char *format, va_list args)
139 {
140 	if (!log) return;
141 	if (!log->func) return;
142 	log->func (log, code, domain, format, args, log->data);
143 }
144