Prechádzať zdrojové kódy

implement logging to syslog

Helmut Pozimski 4 rokov pred
rodič
commit
98a58f53f9
1 zmenil súbory, kde vykonal 8 pridanie a 5 odobranie
  1. 8 5
      src/main.c

+ 8 - 5
src/main.c

@@ -13,6 +13,7 @@
 #include <string.h>
 #include <netinet/in.h>
 #include <time.h>
+#include <syslog.h>
 
 #include "tcpserver.h"
 #include "wavfile.h"
@@ -43,7 +44,7 @@ int sock;
  * signal: the number of the signal sent
  */
 static void signal_handler(int signal) {
-	printf("signal %d received, shutting down\n", signal);
+	syslog(LOG_INFO, "signal %d received, shutting down\n", signal);
 	if (!file_closed) {
 		wavfile_close(file);
 	}
@@ -117,13 +118,14 @@ int main(int argc, char **argv) {
 	action.sa_handler = signal_handler;
 	sigaction(SIGTERM, &action,NULL);
 	sigaction(SIGINT, &action,NULL);
+	openlog("tpr", LOG_CONS | LOG_PID, LOG_DAEMON);
 	if ((sock = tcpserver_start(ip_address, port)) < 0 ) {
-		printf("Failed to start TCP server\n");
+		syslog(LOG_ERR, "Failed to start TCP server\n");
 		return EXIT_FAILURE;
 	}
 	filepath = malloc(strlen(directory) + 1 + 29);
 	if (filepath == NULL) {
-		printf("Error allocating memory for file path\n");
+		syslog(LOG_ERR, "Error allocating memory for file path\n");
 		return EXIT_FAILURE;
 	}
 	namelen = sizeof(client);
@@ -134,11 +136,11 @@ int main(int argc, char **argv) {
 		}
 		recv(conn, &sync, sizeof(sync), 0);
 		if (sync.sample_rate < 8000 || sync.sample_rate > 48000) {
-			printf("Invalid sample rate received\n");
+			syslog(LOG_ERR, "Invalid sample rate received\n");
 			continue;
 		}
 		construct_file_path(directory, filepath);
-		printf("Current file path: %s\n", filepath);
+		syslog(LOG_INFO, "opening file at: %s\n", filepath);
 		file = wavfile_create(filepath, 1, sync.sample_rate, sync.bits);
 		file_closed = 0;
 		strncpy(ack_packet.ack,"ACK", 4);
@@ -152,6 +154,7 @@ int main(int argc, char **argv) {
 		while ((bytes_received = recv(conn, buffer, BUF_SIZE, 0)) != 0) {
 			wavfile_append_data(file, buffer, bytes_received);		
 		}
+		syslog(LOG_INFO, "closing file: %s\n", filepath);
 		wavfile_close(file);
 		file_closed = 1;
 	}