• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GStreamer
2  *
3  * Copyright (C) 2014 Samsung Electronics. All rights reserved.
4  *     @Author: Reynaldo H. Verdejo Pinochet <r.verdejo@sisa.samsung.com>
5  * Copyright (C) 2021 Igalia S.L.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 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  * Library General Public License for more
16  */
17 
18 
19 #include <glib.h>
20 #include <gst/gst.h>
21 #include "gstsouputils.h"
22 #include "gstsouploader.h"
23 
24 /*
25  * Soup logger funcs
26  */
27 
28 GST_DEBUG_CATEGORY_EXTERN (soup_utils_debug);
29 #define GST_CAT_DEFAULT soup_utils_debug
30 
31 static inline gchar
gst_soup_util_log_make_level_tag(SoupLoggerLogLevel level)32 gst_soup_util_log_make_level_tag (SoupLoggerLogLevel level)
33 {
34   gchar c;
35 
36   if (G_UNLIKELY ((gint) level > 9))
37     return '?';
38 
39   switch (level) {
40     case SOUP_LOGGER_LOG_MINIMAL:
41       c = 'M';
42       break;
43     case SOUP_LOGGER_LOG_HEADERS:
44       c = 'H';
45       break;
46     case SOUP_LOGGER_LOG_BODY:
47       c = 'B';
48       break;
49     default:
50       /* Unknown level. If this is hit libsoup likely added a new
51        * log level to SoupLoggerLogLevel and it should be added
52        * as a case */
53       c = level + '0';
54       break;
55   }
56   return c;
57 }
58 
59 static void
gst_soup_util_log_printer_cb(SoupLogger G_GNUC_UNUSED * logger,SoupLoggerLogLevel level,char direction,const char * data,gpointer user_data)60 gst_soup_util_log_printer_cb (SoupLogger G_GNUC_UNUSED * logger,
61     SoupLoggerLogLevel level, char direction, const char *data,
62     gpointer user_data)
63 {
64   gchar c;
65   c = gst_soup_util_log_make_level_tag (level);
66   GST_TRACE_OBJECT (G_OBJECT (user_data), "HTTP_SESSION(%c): %c %s", c,
67       direction, data);
68 }
69 
70 void
gst_soup_util_log_setup(SoupSession * session,SoupLoggerLogLevel level,GObject * object)71 gst_soup_util_log_setup (SoupSession * session, SoupLoggerLogLevel level,
72     GObject * object)
73 {
74   SoupLogger *logger;
75 
76   if (!level) {
77     GST_INFO_OBJECT (object, "Not attaching a logger with level 0");
78     return;
79   }
80 
81   g_assert (session && object);
82 
83   if (gst_debug_category_get_threshold (GST_CAT_DEFAULT)
84       < GST_LEVEL_TRACE) {
85     GST_INFO_OBJECT (object, "Not setting up HTTP session logger. "
86         "Need at least GST_LEVEL_TRACE");
87     return;
88   }
89 
90   /* Create a new logger and set body_size_limit to -1 (no limit) */
91   logger = _soup_logger_new (level);
92 
93   _soup_logger_set_printer (logger, gst_soup_util_log_printer_cb, object, NULL);
94 
95   /* Attach logger to session */
96   _soup_session_add_feature (session, (SoupSessionFeature *) logger);
97   g_object_unref (logger);
98 }
99