[Replicant] [libsamsung-ipc] [PATCH 7/9] tools: ipc-modem: Move log functions in their own file

Denis 'GNUtoo' Carikli GNUtoo at cyberdimension.org
Mon May 23 16:56:15 UTC 2022


This has several advantages:
- This makes the code cleaner as we now have a more abstract logging
  and less code in the main c file.
- If new files are added, this will enable to use logging functions in
  the new files.

Signed-off-by: Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
---
 tools/ipc-modem/Makefile.am     |  2 +-
 tools/ipc-modem/ipc-modem-log.c | 84 +++++++++++++++++++++++++++++++++
 tools/ipc-modem/ipc-modem-log.h | 41 ++++++++++++++++
 tools/ipc-modem/ipc-modem.c     | 55 +--------------------
 tools/ipc-modem/ipc-modem.h     | 12 -----
 5 files changed, 128 insertions(+), 66 deletions(-)
 create mode 100644 tools/ipc-modem/ipc-modem-log.c
 create mode 100644 tools/ipc-modem/ipc-modem-log.h

diff --git a/tools/ipc-modem/Makefile.am b/tools/ipc-modem/Makefile.am
index 092664a..069f082 100644
--- a/tools/ipc-modem/Makefile.am
+++ b/tools/ipc-modem/Makefile.am
@@ -12,6 +12,6 @@ PY_LOG_COMPILER = $(PYTHON)
 TEST_EXTENSIONS = .py
 TESTS = tests/ipc-modem.py
 
-ipc_modem_SOURCES = ipc-modem.c
+ipc_modem_SOURCES = ipc-modem.c ipc-modem-log.c
 ipc_modem_LDADD = $(top_builddir)/samsung-ipc/libsamsung-ipc.la
 ipc_modem_LDFLAGS = -lpthread
diff --git a/tools/ipc-modem/ipc-modem-log.c b/tools/ipc-modem/ipc-modem-log.c
new file mode 100644
index 0000000..7e6b48d
--- /dev/null
+++ b/tools/ipc-modem/ipc-modem-log.c
@@ -0,0 +1,84 @@
+/*
+ * This file is part of libsamsung-ipc.
+ *
+ * Copyright (C) 2011 Simon Busch <morphis at gravedo.de>
+ * Copyright (C) 2011 Paul Kocialkowsk <contact at paulk.fr>
+ * Copyright (C) 2022 Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
+ *
+ * libsamsung-ipc 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.
+ *
+ * libsamsung-ipc 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 libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <stdarg.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
+
+#include "ipc-modem-log.h"
+
+static enum log_target log_target;
+
+void modem_log_handler(void *user_data, const char *msg)
+{
+	int i, l;
+	char *message;
+
+	message = strdup(msg);
+	l = strlen(message);
+
+	if (l > 1) {
+		for (i = l ; i > 0 ; i--) {
+			if (message[i] == '\n')
+				message[i] = 0;
+			else if (message[i] != 0)
+				break;
+		}
+
+		if (log_target == LOG_TO_STDOUT && user_data)
+			printf("[%s] %s\n", (char *)user_data, message);
+		else if (log_target == LOG_TO_STDOUT)
+			printf("[%s] %s\n", MODEM_LOG_DEBUG, message);
+		else if (log_target == LOG_TO_SYSLOG && user_data)
+			syslog(LOG_INFO,
+			       "[%s] %s\n", (char *)user_data, message);
+		else if (log_target == LOG_TO_SYSLOG)
+			syslog(LOG_INFO, "[%s] %s\n", MODEM_LOG_DEBUG, message);
+	}
+
+	free(message);
+}
+
+void modem_log_handler_quiet(__attribute__((unused)) void *user_data,
+			     __attribute__((unused)) const char *msg)
+{
+}
+
+void ipc_modem_log(__attribute__((unused)) struct ipc_client *client,
+		   char *prefix, const char *message, ...)
+{
+	va_list args;
+	char buffer[4096];
+
+	va_start(args, message);
+	vsnprintf((char *) &buffer, sizeof(buffer), message, args);
+	va_end(args);
+
+	modem_log_handler(prefix, buffer);
+}
+
+void ipc_modem_set_log_target(enum log_target target)
+{
+	log_target = target;
+}
+
diff --git a/tools/ipc-modem/ipc-modem-log.h b/tools/ipc-modem/ipc-modem-log.h
new file mode 100644
index 0000000..1786295
--- /dev/null
+++ b/tools/ipc-modem/ipc-modem-log.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of libsamsung-ipc.
+ *
+ * Copyright (C) 2022 Denis 'GNUtoo' Carikli <GNUtoo at cyberdimension.org>
+ *
+ * libsamsung-ipc 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.
+ *
+ * libsamsung-ipc 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 libsamsung-ipc.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#ifndef IPC_MODEM_LOG_H
+#define IPC_MODEM_LOG_H
+
+#include <stdarg.h>
+
+#include <samsung-ipc.h>
+
+#define MODEM_LOG_ERROR "E"
+#define MODEM_LOG_INFO  "I"
+#define MODEM_LOG_DEBUG "D"
+
+enum log_target {
+	LOG_TO_STDOUT,
+	LOG_TO_SYSLOG,
+};
+
+void ipc_modem_log(struct ipc_client *client,
+		   char *prefix, const char *message, ...);
+void modem_log_handler(void *user_data, const char *msg);
+void modem_log_handler_quiet(void *user_data, const char *msg);
+void ipc_modem_set_log_target(enum log_target target);
+
+#endif /* IPC_MODEM_LOG_H */
diff --git a/tools/ipc-modem/ipc-modem.c b/tools/ipc-modem/ipc-modem.c
index 08f9c3a..0641a8f 100644
--- a/tools/ipc-modem/ipc-modem.c
+++ b/tools/ipc-modem/ipc-modem.c
@@ -22,14 +22,12 @@
 #include <fcntl.h>
 #include <getopt.h>
 #include <pthread.h>
-#include <stdarg.h>
 #include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <strings.h>
 #include <sysexits.h>
-#include <syslog.h>
 #include <termios.h>
 #include <unistd.h>
 #include <string.h>
@@ -41,8 +39,7 @@
 #include <samsung-ipc.h>
 
 #include "ipc-modem.h"
-
-enum log_target log_target;
+#include "ipc-modem-log.h"
 
 int seq_get(struct ipc_modem_data *data)
 {
@@ -468,54 +465,6 @@ int modem_dummy_read_loop(__attribute__((unused)) struct ipc_client *client)
 	return 0;
 }
 
-void modem_log_handler(void *user_data, const char *msg)
-{
-	int i, l;
-	char *message;
-
-	message = strdup(msg);
-	l = strlen(message);
-
-	if (l > 1) {
-		for (i = l ; i > 0 ; i--) {
-			if (message[i] == '\n')
-				message[i] = 0;
-			else if (message[i] != 0)
-				break;
-		}
-
-		if (log_target == LOG_TO_STDOUT && user_data)
-			printf("[%s] %s\n", (char *)user_data, message);
-		else if (log_target == LOG_TO_STDOUT)
-			printf("[%s] %s\n", MODEM_LOG_DEBUG, message);
-		else if (log_target == LOG_TO_SYSLOG && user_data)
-			syslog(LOG_INFO,
-			       "[%s] %s\n", (char *)user_data, message);
-		else if (log_target == LOG_TO_SYSLOG)
-			syslog(LOG_INFO, "[%s] %s\n", MODEM_LOG_DEBUG, message);
-	}
-
-	free(message);
-}
-
-void modem_log_handler_quiet(__attribute__((unused)) void *user_data,
-			     __attribute__((unused)) const char *msg)
-{
-}
-
-void ipc_modem_log(__attribute__((unused)) struct ipc_client *client,
-		   char *prefix, const char *message, ...)
-{
-	va_list args;
-	char buffer[4096];
-
-	va_start(args, message);
-	vsnprintf((char *) &buffer, sizeof(buffer), message, args);
-	va_end(args);
-
-	modem_log_handler(prefix, buffer);
-}
-
 int modem_start(struct ipc_client *client)
 {
 	int rc = -1;
@@ -709,7 +658,7 @@ int parse_cmdline_opts(struct ipc_modem_data *data, int argc, char *argv[])
 		} else if ((strcmp(opt_l[opt_i].name, "log-target") == 0)) {
 			if (optarg) {
 				if (!strcmp(optarg, "syslog")) {
-					log_target = LOG_TO_SYSLOG;
+					ipc_modem_set_log_target(LOG_TO_SYSLOG);
 				} else if (strcmp(optarg, "stdout")) {
 					ipc_modem_log(
 						data->client, MODEM_LOG_ERROR,
diff --git a/tools/ipc-modem/ipc-modem.h b/tools/ipc-modem/ipc-modem.h
index 3b6d13a..0ba2f8e 100644
--- a/tools/ipc-modem/ipc-modem.h
+++ b/tools/ipc-modem/ipc-modem.h
@@ -25,10 +25,6 @@
 #define MODEM_STATE_NORMAL  2
 #define MODEM_STATE_SIM_OK  4
 
-#define MODEM_LOG_ERROR "E"
-#define MODEM_LOG_INFO  "I"
-#define MODEM_LOG_DEBUG "D"
-
 enum command {
 	CMD_NONE,
 	CMD_START,
@@ -37,11 +33,6 @@ enum command {
 	CMD_POWER_OFF,
 };
 
-enum log_target {
-	LOG_TO_STDOUT,
-	LOG_TO_SYSLOG,
-};
-
 struct ipc_modem_data {
 	struct ipc_client *client;
 	char call_number[14];
@@ -57,7 +48,4 @@ struct ipc_modem_data {
 	int seq;
 };
 
-void ipc_modem_log(struct ipc_client *client,
-		   char *prefix, const char *message, ...);
-
 #endif /* IPC_MODEM */
-- 
2.36.0



More information about the Replicant mailing list