cf3ab05444f4ee6e69badfe578ec9d4a8680afe2
[platform/upstream/glib-networking.git] / tls / base / gtlslog.c
1 /* -*- Mode: C; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /*
3  * GIO - GLib Input, Output and Streaming Library
4  *
5  * Copyright 2009 Red Hat, Inc
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General
18  * Public License along with this library; if not, see
19  * <http://www.gnu.org/licenses/>.
20  *
21  * In addition, when the library is used with OpenSSL, a special
22  * exception applies. Refer to the LICENSE_EXCEPTION file for details.
23  */
24
25 #include "config.h"
26
27 #include <gio/gio.h>
28 #include <glib.h>
29 #include <glib/gprintf.h>
30 #include <stdarg.h>
31
32 #include "gtlslog.h"
33
34 void g_tls_log (GLogLevelFlags  level,
35                 gpointer        conn,
36                 const gchar    *file,
37                 const gchar    *line,
38                 const gchar    *func,
39                 const gchar    *format,
40                 ...)
41 {
42   gchar *header = NULL;
43   gchar *message = NULL;
44   gchar *thread = NULL;
45   va_list args;
46   int ret;
47
48   va_start (args, format);
49   ret = g_vasprintf (&message, format, args);
50   va_end (args);
51
52   if (ret <= 0)
53     goto out;
54
55   if (conn && G_IS_TLS_CONNECTION (conn)) {
56     if (G_IS_TLS_CLIENT_CONNECTION (conn))
57       header = g_strdup_printf ("CLIENT[%p]: ", conn);
58     else if (G_IS_TLS_SERVER_CONNECTION (conn))
59       header = g_strdup_printf ("SERVER[%p]: ", conn);
60     else
61       g_assert_not_reached ();
62   } else {
63     header = g_strdup ("");
64   }
65
66   thread = g_strdup_printf ("%p", g_thread_self ());
67   g_log_structured (G_LOG_DOMAIN, level,
68                     "GLIB_NET_THREAD", thread,
69                     "CODE_FILE", file,
70                     "CODE_LINE", line,
71                     "CODE_FUNC", func,
72                     "MESSAGE", "%s%s", header, message);
73
74 out:
75   g_free (header);
76   g_free (message);
77   g_free (thread);
78 }