1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
2  *
3  * Copyright (C) 2007-2011 David Zeuthen <zeuthen@gmail.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
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
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18  *
19  */
20 
21 #include "config.h"
22 #include <glib/gi18n-lib.h>
23 
24 #include <sys/types.h>
25 #include <sys/syscall.h>
26 
27 #include "udiskslogging.h"
28 
29 /**
30  * SECTION:udiskslogging
31  * @title: Logging
32  * @short_description: Logging Routines
33  *
34  * Logging routines.
35  */
36 
37 /**
38  * udisks_log:
39  * @level: A #UDisksLogLevel.
40  * @function: Pass #G_STRFUNC here.
41  * @location: Pass #G_STRLOC here.
42  * @format: printf()-style format.
43  * @...: Arguments for format.
44  *
45  * Low-level logging function used by udisks_debug() and other macros.
46  */
47 void
udisks_log(UDisksLogLevel level,const gchar * function,const gchar * location,const gchar * format,...)48 udisks_log (UDisksLogLevel     level,
49             const gchar       *function,
50             const gchar       *location,
51             const gchar       *format,
52             ...)
53 {
54   va_list var_args;
55   gchar *message;
56   gchar *thread_id;
57 
58   va_start (var_args, format);
59   message = g_strdup_vprintf (format, var_args);
60   va_end (var_args);
61 
62 #if GLIB_CHECK_VERSION(2, 50, 0)
63   thread_id = g_strdup_printf ("%d", (gint) syscall (SYS_gettid));
64   g_log_structured ("udisks", (GLogLevelFlags) level,
65                     "THREAD_ID", thread_id,
66                     "CODE_FUNC", function, "CODE_FILE", location,
67                     "MESSAGE", "%s", message);
68   g_free (thread_id);
69 #else
70   g_log ("udisks", level, "[%d]: %s [%s, %s()]", (gint) syscall (SYS_gettid), message, location, function);
71 #endif
72 
73   g_free (message);
74 }
75