Imported Upstream version 2.74.0
[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   if (level < G_LOG_LEVEL_DEBUG || ENABLE_DEBUG_LOGS)
43     {
44       gchar *header = NULL;
45       gchar *message = NULL;
46       gchar *thread = NULL;
47       va_list args;
48       int ret;
49
50       va_start (args, format);
51       ret = g_vasprintf (&message, format, args);
52       va_end (args);
53
54       if (ret <= 0)
55         goto out;
56
57       if (conn && G_IS_TLS_CONNECTION (conn)) {
58         if (G_IS_TLS_CLIENT_CONNECTION (conn))
59           header = g_strdup_printf ("CLIENT[%p]: ", conn);
60         else if (G_IS_TLS_SERVER_CONNECTION (conn))
61           header = g_strdup_printf ("SERVER[%p]: ", conn);
62         else
63           g_assert_not_reached ();
64       } else {
65         header = g_strdup ("");
66       }
67
68       thread = g_strdup_printf ("%p", g_thread_self ());
69       g_log_structured (G_LOG_DOMAIN, level,
70                         "GLIB_NET_THREAD", thread,
71                         "CODE_FILE", file,
72                         "CODE_LINE", line,
73                         "CODE_FUNC", func,
74                         "MESSAGE", "%s%s", header, message);
75
76     out:
77       g_free (header);
78       g_free (message);
79       g_free (thread);
80     }
81 }