data/domains: Drop unused msgs.xsl
[yelp.git] / libyelp / yelp-debug.c
blob7a12ddedad907e7da5c9df0d5097df711697b779
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
2 /*
3 * Copyright (C) 2006 Brent Smith <gnome@nextreality.net>
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of the
8 * License, or (at your option) any later version.
10 * This program 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 * General Public License for more details.
15 * You should have received a copy of the GNU General Public
16 * License along with this program; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
21 #include <config.h>
22 #include <glib/gi18n.h>
23 #include <glib.h>
24 #include <glib/gprintf.h>
25 #include <unistd.h>
26 #include <time.h>
28 #include "yelp-debug.h"
31 * @file: you should pass the __FILE__ constant as this parameter
32 * @line: you should pass the __LINE__ constant as this parameter
33 * @function: you should pass the __FUNCTION__ constant as this parameter
34 * @flags: should be one of #YelpDebugEnums. Arguments can be bitwise OR'd together.
35 * @format: the printf style format
37 * This function is not meant to be called directly, instead you should use the
38 * debug_print() macro which will pass the __FILE__, __LINE__, and __FUNCTION__
39 * constants for you.
40 * The flags passed to the function identify the type of debug statement. Some action
41 * (usually a print to the console) will take place depending on if the corresponding
42 * flag is set in the YELP_DEBUG environment variable. YELP_DEBUG is a colon separated
43 * list of zero or more the following values:
44 * "log" - debug_print calls with DB_LOG, DB_INFO, DB_DEBUG, DB_WARN, and
45 * DB_ERROR will be printed to stdout
46 * "info" - debug_print calls with DB_INFO, DB_DEBUG, DB_WARN and DB_ERROR
47 * will be printed to stdout
48 * "debug" - debug_print calls with DB_DEBUG, DB_WARN, and DB_ERROR will be
49 * printed to stdout
50 * "warn" - debug_print calls with DB_WARN and DB_ERROR will be printed to
51 * stdout
52 * "error" - debug_print calls with DB_ERROR will be printed to stdout
53 * "function-calls" - debug_print (DB_FUNCTION, ...) calls will be printed to stdout
54 * along with the function name
55 * "function-args" - debug_print (DB_ARG, ...) calls will be printed to stdout
56 * "enable-profiling" - debug_print (DB_PROFILE, ...) calls will make an access() call
57 * with a "MARK:" at the beginning: this is for profiling with
58 * Federico's plot-timeline.py python script:
59 * see http://primates.ximian.com/~federico/docs/login-profile/plot-timeline.py
60 * "all" - Turns on all options.
63 void yelp_debug (const gchar *file, guint line,
64 const gchar *function, guint flags, const gchar *format, ...)
66 static gboolean first_call = TRUE;
67 static guint debug_flags = 0;
68 static gchar **debug_files = NULL;
69 va_list args;
70 const gchar *debugenv = NULL;
71 gchar *formatted = NULL;
72 gchar *str = NULL;
73 gint i;
75 const GDebugKey debug_keys[] = {
76 { "function-calls", DB_FUNCTION },
77 { "function-args", DB_ARG },
78 { "enable-profiling", DB_PROFILE },
79 { "log", DB_LOG },
80 { "info", DB_INFO },
81 { "debug", DB_DEBUG },
82 { "warn", DB_WARN },
83 { "error", DB_ERROR },
84 { "all", DB_ALL }
87 /* figure out which debug flags were set in the environment on the first
88 * call to this function */
89 if (first_call) {
90 debugenv = g_getenv ("YELP_DEBUG");
92 if (debugenv != NULL)
93 debug_flags = g_parse_debug_string (debugenv, debug_keys,
94 G_N_ELEMENTS (debug_keys));
95 else
96 debug_flags = 0;
98 /* turn on all flags if "all" option was specified */
99 if (debug_flags & DB_ALL)
100 debug_flags |= (DB_LOG | DB_INFO | DB_DEBUG | DB_WARN | DB_ERROR |
101 DB_FUNCTION | DB_ARG | DB_PROFILE);
103 /* turn on all higher severity logging levels; for instance if DB_LOG is set
104 * in debug_flags, then we turn on INFO, DEBUG, WARN and ERROR */
105 else if (debug_flags & DB_LOG)
106 debug_flags |= (DB_INFO | DB_DEBUG | DB_WARN | DB_ERROR);
107 else if (debug_flags & DB_INFO)
108 debug_flags |= (DB_DEBUG | DB_WARN | DB_ERROR);
109 else if (debug_flags & DB_DEBUG)
110 debug_flags |= (DB_WARN | DB_ERROR);
111 else if (debug_flags & DB_WARN)
112 debug_flags |= (DB_ERROR);
114 debugenv = g_getenv ("YELP_DEBUG_FILTER");
116 if (debugenv != NULL && *debugenv != '\0')
117 debug_files = g_strsplit (debugenv, ":", -1);
118 else
119 debug_files = NULL;
121 first_call = FALSE;
124 /* if none of the flags are set either by the calling function
125 * or in the YELP_DEBUG environment variable, then just return */
126 if ((flags & debug_flags) == 0)
127 return;
129 /* if the YELP_DEBUG_FILTER environment variable was specified with
130 * a colon delimited list of source files, then debug_files will not be
131 * NULL and will contain a list of files for which we print out debug
132 * statements. Check if this function was called from one of the files
133 * in the list. If not, return. */
134 if (debug_files != NULL) {
135 for (i=0; debug_files[i] != NULL; i++) {
136 if (*(debug_files[i]) == '\0')
137 continue;
138 if (g_strrstr (file, debug_files[i]) != NULL)
139 break;
142 /* if we are on the NULL element, then a match was not found so
143 * we should return now */
144 if (debug_files[i] == NULL)
145 return;
148 va_start (args, format);
150 if (flags & DB_FUNCTION) {
151 g_fprintf (stdout, "%s:%u: %s: ", file, line, function);
152 g_vfprintf (stdout, format, args);
153 } else if ((flags & DB_LOG) ||
154 (flags & DB_INFO) ||
155 (flags & DB_DEBUG) ||
156 (flags & DB_WARN) ||
157 (flags & DB_ERROR) ||
158 (flags & DB_ARG)) {
159 g_fprintf (stdout, "%s:%u: ", file, line);
160 g_vfprintf (stdout, format, args);
163 if (flags & DB_PROFILE) {
164 time_t t;
165 struct tm *tmp;
166 gchar timestamp[20];
168 t = time (NULL);
169 tmp = localtime(&t);
171 strftime (timestamp, 20, "%H:%M:%S", tmp);
172 formatted = g_strdup_vprintf (format, args);
174 g_fprintf (stdout, "PROFILE [%s]: %s\n", timestamp, formatted);
175 str = g_strdup_printf ("MARK: %s: %s", g_get_prgname(), formatted);
176 access (str, F_OK);
177 g_free (formatted);
178 g_free (str);
181 va_end (args);