diff smpp-trx-sa/log.c @ 222:9d6e8d99d2b1

smpp-trx-sa: new program
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 03 Aug 2023 21:13:41 -0800
parents
children a375639e4190
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/smpp-trx-sa/log.c	Thu Aug 03 21:13:41 2023 -0800
@@ -0,0 +1,88 @@
+/*
+ * This module implements smpp-trx-sa log file output.
+ */
+
+#include <sys/types.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <strings.h>
+#include <time.h>
+
+extern FILE *logF;
+extern time_t curtime;
+extern int init_done;
+
+static char fmt_time[32];
+
+static void
+format_time()
+{
+	struct tm *tm;
+
+	tm = gmtime(&curtime);
+	sprintf(fmt_time, "%d-%02d-%02dT%02d:%02d:%02dZ",
+		tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
+		tm->tm_hour, tm->tm_min, tm->tm_sec);
+}
+
+void
+log_fatal_error(cause)
+	char *cause;
+{
+	if (!init_done)
+		return;
+	format_time();
+	fprintf(logF, "\n%s %s\n", fmt_time, cause);
+}
+
+static void
+pdu_hexdump(pdu, pdulen)
+	u_char *pdu;
+	unsigned pdulen;
+{
+	unsigned off, chunk;
+	int i, c;
+
+	for (off = 0; off < pdulen; off += chunk) {
+		fprintf(logF, "%04X:  ", off);
+		chunk = pdulen - off;
+		if (chunk > 16)
+			chunk = 16;
+		for (i = 0; i < 16; i++) {
+			if (i < chunk)
+				fprintf(logF, "%02X ", pdu[off + i]);
+			else
+				fputs("   ", logF);
+			if (i == 7 || i == 15)
+				putc(' ', logF);
+		}
+		for (i = 0; i < chunk; i++) {
+			c = pdu[off + i];
+			if (c < ' ' || c > '~')
+				c = '.';
+			putc(c, logF);
+		}
+		putc('\n', logF);
+	}
+}
+
+void
+log_rx_pdu(pdu, pdulen)
+	u_char *pdu;
+	unsigned pdulen;
+{
+	format_time();
+	fprintf(logF, "\n%s Received PDU:\n", fmt_time);
+	pdu_hexdump(pdu, pdulen);
+}
+
+void
+log_sent_pdu(pdu, pdulen)
+	u_char *pdu;
+	unsigned pdulen;
+{
+	format_time();
+	fprintf(logF, "\n%s Sent PDU:\n", fmt_time);
+	pdu_hexdump(pdu, pdulen);
+}