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