fhub: create the bluez log to /run/network/log 43/289743/1
authorAyush Garg <ayush.garg@samsung.com>
Thu, 22 Sep 2022 08:56:49 +0000 (14:26 +0530)
committerWootak Jung <wootak.jung@samsung.com>
Tue, 14 Mar 2023 05:33:28 +0000 (14:33 +0900)
Change-Id: I6564eeb4b46e142de74587c350c13427b773d04c
Signed-off-by: Ayush Garg <ayush.garg@samsung.com>
Makefile.am
Makefile.tools
src/bluez-log.c [new file with mode: 0644]
src/log.h
src/main.c

index 9d77cff..9045c53 100755 (executable)
@@ -282,7 +282,7 @@ libexec_PROGRAMS += src/bluetoothd
 src_bluetoothd_SOURCES = $(builtin_sources) \
                        $(attrib_sources) $(btio_sources) \
                        src/bluetooth.ver \
-                       src/main.c src/log.h src/log.c \
+                       src/main.c src/log.h src/log.c src/bluez-log.c \
                        src/backtrace.h src/backtrace.c \
                        src/rfkill.c src/btd.h src/sdpd.h \
                        src/sdpd-server.c src/sdpd-request.c \
@@ -547,10 +547,10 @@ unit_test_hog_LDADD = src/libshared-glib.la \
 unit_tests += unit/test-gattrib
 
 unit_test_gattrib_SOURCES = unit/test-gattrib.c attrib/gattrib.c \
-                                       $(btio_sources) src/log.h src/log.c
+                                       $(btio_sources) src/log.h src/log.c src/bluez-log.c
 unit_test_gattrib_LDADD = lib/libbluetooth-internal.la \
                        src/libshared-glib.la \
-                       $(GLIB_LIBS) $(DBUS_LIBS) -ldl -lrt
+                       $(GLIB_LIBS) $(DBUS_LIBS) @LIBXML_LIBS@ -ldl -lrt
 
 if MIDI
 unit_tests += unit/test-midi
index 6146d9b..2cceb69 100755 (executable)
@@ -379,16 +379,16 @@ tools_btiotest_LDADD = lib/libbluetooth-internal.la $(GLIB_LIBS)
 
 tools_mcaptest_SOURCES = tools/mcaptest.c \
                                btio/btio.h btio/btio.c \
-                               src/log.c src/log.h \
+                               src/log.c src/bluez-log.c src/log.h \
                                profiles/health/mcap.h profiles/health/mcap.c
-tools_mcaptest_LDADD = lib/libbluetooth-internal.la $(GLIB_LIBS) \
+tools_mcaptest_LDADD = lib/libbluetooth-internal.la $(GLIB_LIBS) @LIBXML_LIBS@ \
                                src/libshared-mainloop.la -lrt
 
 tools_bneptest_SOURCES = tools/bneptest.c \
                                btio/btio.h btio/btio.c \
-                               src/log.h src/log.c \
+                               src/log.h src/log.c src/bluez-log.c \
                                profiles/network/bnep.h profiles/network/bnep.c
-tools_bneptest_LDADD = lib/libbluetooth-internal.la $(GLIB_LIBS) \
+tools_bneptest_LDADD = lib/libbluetooth-internal.la $(GLIB_LIBS) @LIBXML_LIBS@ \
                                src/libshared-mainloop.la
 
 tools_cltest_SOURCES = tools/cltest.c
@@ -431,10 +431,10 @@ noinst_PROGRAMS += attrib/gatttool tools/btmgmt \
 attrib_gatttool_SOURCES = attrib/gatttool.c attrib/att.c attrib/gatt.c \
                                attrib/gattrib.c btio/btio.c \
                                attrib/gatttool.h attrib/interactive.c \
-                               attrib/utils.c src/log.c client/display.c \
+                               attrib/utils.c src/log.c src/bluez-log.c client/display.c \
                                client/display.h
 attrib_gatttool_LDADD = lib/libbluetooth-internal.la \
-                       src/libshared-glib.la $(GLIB_LIBS) -lreadline
+                       src/libshared-glib.la $(GLIB_LIBS) -lreadline @LIBXML_LIBS@
 
 tools_obex_client_tool_SOURCES = $(gobex_sources) $(btio_sources) \
                                                tools/obex-client-tool.c
diff --git a/src/bluez-log.c b/src/bluez-log.c
new file mode 100644 (file)
index 0000000..134ad52
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ */
+
+/**
+ * This file implements bluez log functions
+ *
+ * @file       bluez-log.c
+ * @author     Sohyeon Choi (shss.choi@samsung.com)
+ * @version    0.1
+ */
+
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <glib.h>
+
+#include "log.h"
+#define LOG_FILE_PATH "/run/network/log/bt-bluez.log"
+#define MAX_LOG_SIZE 1 * 512 * 1024
+#define MAX_LOG_COUNT 1
+
+static FILE *log_file = NULL;
+
+static inline void __bt_bluez_log_update_file_revision(int rev)
+{
+       int next_log_rev = 0;
+       char *log_file = NULL;
+       char *next_log_file = NULL;
+
+       next_log_rev = rev + 1;
+
+       log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
+       next_log_file = g_strdup_printf("%s.%d", LOG_FILE_PATH, next_log_rev);
+
+       if (next_log_rev >= MAX_LOG_COUNT)
+               remove(next_log_file);
+
+       if (access(next_log_file, F_OK) == 0)
+               __bt_bluez_log_update_file_revision(next_log_rev);
+
+       if (rename(log_file, next_log_file) != 0)
+               remove(log_file);
+
+       g_free(log_file);
+       g_free(next_log_file);
+}
+
+static inline void __bt_bluez_log_make_backup(void)
+{
+       const int rev = 0;
+       char *backup = NULL;
+
+       backup = g_strdup_printf("%s.%d", LOG_FILE_PATH, rev);
+
+       if (access(backup, F_OK) == 0)
+               __bt_bluez_log_update_file_revision(rev);
+
+       if (rename(LOG_FILE_PATH, backup) != 0)
+               remove(LOG_FILE_PATH);
+
+       g_free(backup);
+}
+
+static inline void __bt_bluez_log_get_local_time(char *strtime, const int size)
+{
+       time_t buf;
+       struct tm *local_ptm;
+       struct tm result = {0, };
+
+       time(&buf);
+       buf = time(NULL);
+       local_ptm = localtime_r(&buf, &result);
+
+       if (local_ptm)
+               strftime(strtime, size, "%m/%d %H:%M:%S", local_ptm);
+}
+
+EXPORT_SYM void bt_bluez_log(const char *format, ...)
+{
+       va_list ap;
+       int log_size = 0;
+       struct stat buf;
+       char str[256];
+       char strtime[40];
+
+       if (log_file == NULL)
+               log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
+
+       if (log_file == NULL)
+               return;
+
+       va_start(ap, format);
+
+       if (fstat(fileno(log_file), &buf) == 0)
+               log_size = buf.st_size;
+
+       if (log_size >= MAX_LOG_SIZE) {
+               fclose(log_file);
+               log_file = NULL;
+
+               __bt_bluez_log_make_backup();
+
+               log_file = (FILE *)fopen(LOG_FILE_PATH, "a+");
+
+               if (log_file == NULL) {
+                       va_end(ap);
+                       return;
+               }
+       }
+
+       __bt_bluez_log_get_local_time(strtime, sizeof(strtime));
+
+       if (vsnprintf(str, sizeof(str), format, ap) > 0)
+               fprintf(log_file, "%s %s", strtime, str);
+
+       va_end(ap);
+}
+
+EXPORT_SYM void log_cleanup(void)
+{
+       if (log_file == NULL)
+               return;
+
+       fclose(log_file);
+       log_file = NULL;
+}
+#endif
index e2a99a6..058b9a8 100755 (executable)
--- a/src/log.h
+++ b/src/log.h
 
 #include <stdint.h>
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
+#include "../profile.h"
+#endif
+
 void info(const char *format, ...) __attribute__((format(printf, 1, 2)));
 
 void btd_log(uint16_t index, int priority, const char *format, ...)
@@ -58,8 +62,43 @@ void __btd_enable_debug(struct btd_debug_desc *start,
                btd_debug(idx, "%s:%s() " fmt, __FILE__, __func__ , ## arg); \
 } while (0)
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
+#ifndef EXPORT_SYM
+#define EXPORT_SYM __attribute__((__visibility__("default")))
+#endif
+
+void bt_bluez_log(const char *format, ...);
+void log_cleanup(void);
+
+#define BT_BLUEZ_DEBUG_FILE (1)
+
+#define DBG(fmt, arg...) \
+       do { \
+               if (TIZEN_FEATURE_BLUEZ_FHUB && BT_BLUEZ_DEBUG_FILE) { \
+                       bt_bluez_log("[%s:%d] "fmt"\n", __func__, __LINE__, ## arg); \
+               } \
+               DBG_IDX(0xffff, fmt, ## arg); \
+       } while(0)
+
+#define error(fmt, arg...) \
+       do { \
+               if (TIZEN_FEATURE_BLUEZ_FHUB && BT_BLUEZ_DEBUG_FILE) { \
+                       bt_bluez_log("[%s:%d] "fmt"\n", __func__, __LINE__, ## arg); \
+               } \
+               btd_error(0xffff, "%s:%s() " fmt, __FILE__, __func__, ## arg); \
+       } while(0)
+
+#define warn(fmt, arg...) \
+       do { \
+               if (TIZEN_FEATURE_BLUEZ_FHUB && BT_BLUEZ_DEBUG_FILE) { \
+                       bt_bluez_log("[%s:%d] "fmt"\n", __func__, __LINE__, ## arg); \
+               } \
+               btd_warn(0xffff, "%s:%s() " fmt, __FILE__, __func__, ## arg); \
+       } while(0)
+#else
 #define DBG(fmt, arg...) DBG_IDX(0xffff, fmt, ## arg)
 #define error(fmt, arg...) \
        btd_error(0xffff, "%s:%s() " fmt, __FILE__, __func__, ## arg)
 #define warn(fmt, arg...) \
        btd_warn(0xffff, "%s:%s() " fmt, __FILE__, __func__, ## arg)
+#endif
index 0596d4b..57af6db 100755 (executable)
@@ -1308,5 +1308,10 @@ int main(int argc, char *argv[])
 
        __btd_log_cleanup();
 
+#ifdef TIZEN_FEATURE_BLUEZ_MODIFY
+       if (TIZEN_FEATURE_BLUEZ_FHUB)
+               log_cleanup();
+#endif
+
        return 0;
 }