1 /* GStreamer
2  *
3  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4  *     @Author: Reynaldo H. Verdejo Pinochet <r.verdejo@sisa.samsung.com>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more
15  */
16 
17 
18 #include <glib.h>
19 #include <gst/gst.h>
20 #include <libsoup/soup.h>
21 #include "gstsouputils.h"
22 
23 /**
24  * Soup logger funcs
25  */
26 
27 GST_DEBUG_CATEGORY_EXTERN (soup_utils_debug);
28 #define GST_CAT_DEFAULT soup_utils_debug
29 
30 static inline gchar
gst_soup_util_log_make_level_tag(SoupLoggerLogLevel level)31 gst_soup_util_log_make_level_tag (SoupLoggerLogLevel level)
32 {
33   gchar c;
34 
35   if (G_UNLIKELY ((gint) level > 9))
36     return '?';
37 
38   switch (level) {
39     case SOUP_LOGGER_LOG_MINIMAL:
40       c = 'M';
41       break;
42     case SOUP_LOGGER_LOG_HEADERS:
43       c = 'H';
44       break;
45     case SOUP_LOGGER_LOG_BODY:
46       c = 'B';
47       break;
48     default:
49       /* Unknown level. If this is hit libsoup likely added a new
50        * log level to SoupLoggerLogLevel and it should be added
51        * as a case */
52       c = level + '0';
53       break;
54   }
55   return c;
56 }
57 
58 static void
gst_soup_util_log_printer_cb(SoupLogger G_GNUC_UNUSED * logger,SoupLoggerLogLevel level,char direction,const char * data,gpointer user_data)59 gst_soup_util_log_printer_cb (SoupLogger G_GNUC_UNUSED * logger,
60     SoupLoggerLogLevel level, char direction, const char *data,
61     gpointer user_data)
62 {
63   gchar c;
64   c = gst_soup_util_log_make_level_tag (level);
65   GST_TRACE_OBJECT (GST_ELEMENT (user_data), "HTTP_SESSION(%c): %c %s", c,
66       direction, data);
67 }
68 
69 void
gst_soup_util_log_setup(SoupSession * session,SoupLoggerLogLevel level,GstElement * element)70 gst_soup_util_log_setup (SoupSession * session, SoupLoggerLogLevel level,
71     GstElement * element)
72 {
73   SoupLogger *logger;
74 
75   if (!level) {
76     GST_INFO_OBJECT (element, "Not attaching a logger with level 0");
77     return;
78   }
79 
80   g_assert (session && element);
81 
82   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT)
83       < GST_LEVEL_TRACE) {
84     GST_INFO_OBJECT (element, "Not setting up HTTP session logger. "
85         "Need at least GST_LEVEL_TRACE");
86     return;
87   }
88 
89   /* Create a new logger and set body_size_limit to -1 (no limit) */
90   logger = soup_logger_new (level, -1);
91   soup_logger_set_printer (logger, gst_soup_util_log_printer_cb,
92       gst_object_ref (element), (GDestroyNotify) gst_object_unref);
93 
94   /* Attach logger to session */
95   soup_session_add_feature (session, SOUP_SESSION_FEATURE (logger));
96   g_object_unref (logger);
97 }
98