1 /* exif-log.c
2  *
3  * Copyright (c) 2004 Lutz Mueller <lutz@users.sourceforge.net>
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., 51 Franklin Street, Fifth Floor,
18  * Boston, MA  02110-1301  USA.
19  */
20 
21 #include <config.h>
22 
23 #include <libexif/exif-log.h>
24 #include <libexif/i18n.h>
25 
26 #include <stdlib.h>
27 #include <string.h>
28 
29 struct _ExifLog {
30 	unsigned int ref_count;
31 
32 	ExifLogFunc func;
33 	void *data;
34 
35 	ExifMem *mem;
36 };
37 
38 static const struct {
39 	ExifLogCode code;
40 	const char *title;
41 	const char *message;
42 } codes[] = {
43 	{ EXIF_LOG_CODE_DEBUG, N_("Debugging information"),
44 	  N_("Debugging information is available.") },
45 	{ EXIF_LOG_CODE_NO_MEMORY, N_("Not enough memory"),
46 	  N_("The system cannot provide enough memory.") },
47 	{ EXIF_LOG_CODE_CORRUPT_DATA, N_("Corrupt data"),
48 	  N_("The data provided does not follow the specification.") },
49 	{ 0, NULL, NULL }
50 };
51 
52 const char *
exif_log_code_get_title(ExifLogCode code)53 exif_log_code_get_title (ExifLogCode code)
54 {
55 	unsigned int i;
56 
57 	for (i = 0; codes[i].title; i++) if (codes[i].code == code) break;
58 	return _(codes[i].title);
59 }
60 
61 const char *
exif_log_code_get_message(ExifLogCode code)62 exif_log_code_get_message (ExifLogCode code)
63 {
64 	unsigned int i;
65 
66 	for (i = 0; codes[i].message; i++) if (codes[i].code == code) break;
67 	return _(codes[i].message);
68 }
69 
70 ExifLog *
exif_log_new_mem(ExifMem * mem)71 exif_log_new_mem (ExifMem *mem)
72 {
73 	ExifLog *log;
74 
75 	log = exif_mem_alloc (mem, sizeof (ExifLog));
76 	if (!log) return NULL;
77 	log->ref_count = 1;
78 
79 	log->mem = mem;
80 	exif_mem_ref (mem);
81 
82 	return log;
83 }
84 
85 ExifLog *
exif_log_new(void)86 exif_log_new (void)
87 {
88 	ExifMem *mem = exif_mem_new_default ();
89 	ExifLog *log = exif_log_new_mem (mem);
90 
91 	exif_mem_unref (mem);
92 
93 	return log;
94 }
95 
96 void
exif_log_ref(ExifLog * log)97 exif_log_ref (ExifLog *log)
98 {
99 	if (!log) return;
100 	log->ref_count++;
101 }
102 
103 void
exif_log_unref(ExifLog * log)104 exif_log_unref (ExifLog *log)
105 {
106 	if (!log) return;
107 	if (log->ref_count > 0) log->ref_count--;
108 	if (!log->ref_count) exif_log_free (log);
109 }
110 
111 void
exif_log_free(ExifLog * log)112 exif_log_free (ExifLog *log)
113 {
114 	ExifMem *mem = log ? log->mem : NULL;
115 
116 	if (!log) return;
117 
118 	exif_mem_free (mem, log);
119 	exif_mem_unref (mem);
120 }
121 
122 void
exif_log_set_func(ExifLog * log,ExifLogFunc func,void * data)123 exif_log_set_func (ExifLog *log, ExifLogFunc func, void *data)
124 {
125 	if (!log) return;
126 	log->func = func;
127 	log->data = data;
128 }
129 
130 #ifdef NO_VERBOSE_TAG_STRINGS
131 /* exif_log forms part of the API and can't be commented away */
132 #undef exif_log
133 #endif
134 void
exif_log(ExifLog * log,ExifLogCode code,const char * domain,const char * format,...)135 exif_log (ExifLog *log, ExifLogCode code, const char *domain,
136 	  const char *format, ...)
137 {
138 	va_list args;
139 
140 	va_start (args, format);
141 	exif_logv (log, code, domain, format, args);
142 	va_end (args);
143 }
144 
145 void
exif_logv(ExifLog * log,ExifLogCode code,const char * domain,const char * format,va_list args)146 exif_logv (ExifLog *log, ExifLogCode code, const char *domain,
147 	   const char *format, va_list args)
148 {
149 	if (!log) return;
150 	if (!log->func) return;
151 	log->func (log, code, domain, format, args, log->data);
152 }
153