From ffa48d2a646d1deb92eaf4e13ed17d5da9bfda65 Mon Sep 17 00:00:00 2001
From: guowenxue <guowenxue@gmail.com>
Date: Wed, 26 Jun 2019 00:59:31 +0800
Subject: [PATCH] update mqttd hal code, instead printf by logger system

---
 mqttd/hal/ds18b20.c |   12 +++--
 mqttd/hal/beep.c    |    3 +
 mqttd/hal/sht20.c   |   47 ++++++++++++-----------
 3 files changed, 33 insertions(+), 29 deletions(-)

diff --git a/mqttd/hal/beep.c b/mqttd/hal/beep.c
index 6f4816d..0be202d 100644
--- a/mqttd/hal/beep.c
+++ b/mqttd/hal/beep.c
@@ -27,6 +27,7 @@
 #include <errno.h>
 
 #include <wiringPi.h>
+#include "lylib/logger.h"
 #include "beep.h"
 
 /*+-----------------------------+
@@ -47,7 +48,7 @@
 
 		if(freq<2000 || freq>5000)
         {
-            printf("Beep set invalid PWM frequency[%d]!\n", freq);
+            log_err("Beep set invalid PWM frequency[%d]!\n", freq);
             return -1;
         }
 
diff --git a/mqttd/hal/ds18b20.c b/mqttd/hal/ds18b20.c
index 2aa9116..cf9daf8 100644
--- a/mqttd/hal/ds18b20.c
+++ b/mqttd/hal/ds18b20.c
@@ -20,6 +20,8 @@
 #include <time.h>
 #include <errno.h>
 
+#include "lylib/logger.h"
+
 /* File Content:
    pi@raspberrypi:~/guowenxue $ cat /sys/bus/w1/devices/28-041731f7c0ff/w1_slave 
    3a 01 4b 46 7f ff 0c 10 a5 : crc=a5 YES
@@ -48,7 +50,7 @@
      *+-------------------------------------------------------------------+*/
     if((dirp = opendir(w1_path)) == NULL)
     {
-        printf("opendir error: %s\n", strerror(errno));
+        log_err("opendir '%s' error: %s\n", w1_path, strerror(errno));
         return -2;
     }
 
@@ -66,7 +68,7 @@
 
     if( !found )
     {
-            printf("Can not find ds18b20 in %s\n", w1_path);
+            log_fatal("Can not find ds18b20 in %s\n", w1_path);
             return -3;
     }
 
@@ -77,20 +79,20 @@
     /* open file /sys/bus/w1/devices/28-xxxx/w1_slave to get temperature */
     if( (fd=open(w1_path, O_RDONLY)) < 0 ) 
     { 
-            printf("open %s error: %s\n", w1_path, strerror(errno)); 
+            log_err("open %s error: %s\n", w1_path, strerror(errno)); 
             return -4;
     } 
     
     if(read(fd, buf, sizeof(buf)) < 0)
     {
-            printf("read %s error: %s\n", w1_path, strerror(errno)); 
+            log_err("read %s error: %s\n", w1_path, strerror(errno)); 
             return -5;
     } 
     
     ptr = strstr(buf, "t=");
     if( !ptr )
     {
-            printf("ERROR: Can not get temperature\n");
+            log_err("ERROR: Can not get temperature\n");
             return -6;
     }
 
diff --git a/mqttd/hal/sht20.c b/mqttd/hal/sht20.c
index 66af731..b9b4e6b 100644
--- a/mqttd/hal/sht20.c
+++ b/mqttd/hal/sht20.c
@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include "sht20.h"
+#include "lylib/logger.h"
 
 static int s_sht2x_fd = -1;
 
@@ -36,7 +37,7 @@
 {
     close(s_sht2x_fd);
     s_sht2x_fd = -1;
-    printf("Terminate SHT2X\n");
+    log_warn("Terminate SHT2X\n");
 }
 
 #ifdef I2C_API_RDWR  /* Use I2C userspace driver read/write API */
@@ -47,7 +48,7 @@
 
 	if( fd<0 )
 	{
-		printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
+		log_err("Invalid input arguments\n");
 		return -1;
 	}
 
@@ -67,7 +68,7 @@
 	
 	if( (s_sht2x_fd=open("/dev/i2c-1", O_RDWR)) < 0)
 	{
-		printf("i2c device open failed: %s\n", strerror(errno));
+		log_fatal("i2c device open failed: %s\n", strerror(errno));
         return -1;
 	}
 	
@@ -77,7 +78,7 @@
 
 	if( sht2x_softreset(s_sht2x_fd) < 0 )
 	{
-		printf("SHT2x softreset failure\n");
+		log_err("SHT2x softreset failure\n");
         sht2x_term();
 		return -2;
 	}
@@ -91,7 +92,7 @@
 
 	if( !temp || !rh )
 	{
-		printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
+		log_err("Invalid input arguments\n");
 		return -1;
 	}
 
@@ -99,7 +100,7 @@
     {
         if( sht2x_init() < 0)
         {
-            printf("SHT2x initialise failure\n");
+            log_err("SHT2x initialise failure\n");
             return -2;
         }
     }
@@ -137,7 +138,7 @@
 
 	if( !serialnumber || size!=8 )
 	{
-		printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
+		log_err("Invalid input arguments\n");
 		return -1;
 	}
 
@@ -145,7 +146,7 @@
     {
         if( sht2x_init() < 0)
         {
-            printf("SHT2x initialise failure\n");
+            log_err("SHT2x initialise failure\n");
             return -2;
         }
     }
@@ -193,7 +194,7 @@
 
 	if( fd<0 )
 	{
-		printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
+		log_err("Invalid input arguments\n");
 		return -1;
 	}
 
@@ -208,7 +209,7 @@
 
 	if( ioctl(fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}
@@ -223,13 +224,13 @@
 {
 	if( (s_sht2x_fd=open("/dev/i2c-1", O_RDWR)) < 0)
 	{
-		printf("i2c device open failed: %s\n", strerror(errno));
-	       	return -1;
+        log_err("i2c device open failed: %s\n", strerror(errno));
+        return -1;
 	}
 
 	if( sht2x_softreset(s_sht2x_fd) < 0 )
 	{
-		printf("SHT2x softreset failure\n");
+		log_err("SHT2x softreset failure\n");
         sht2x_term();
 		return -2;
 	}
@@ -246,7 +247,7 @@
 
 	if( !serialnumber || size!=8 )
 	{
-		printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
+		log_err("Invalid input arguments\n");
 		return -1;
 	}
 
@@ -254,7 +255,7 @@
     {
         if( sht2x_init() < 0)
         {
-            printf("SHT2x initialise failure\n");
+            log_err("SHT2x initialise failure\n");
             return -2;
         }
     }
@@ -280,7 +281,7 @@
 
 	if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}
@@ -312,7 +313,7 @@
 
 	if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}
@@ -336,7 +337,7 @@
 
 	if( !temp || !rh )
 	{
-		printf("%s line [%d] %s() get invalid input arguments\n", __FILE__, __LINE__, __func__ );
+		log_err("Invalid input arguments\n");
 		return -1;
 	}
 
@@ -344,7 +345,7 @@
     {
         if( sht2x_init() < 0)
         {
-            printf("SHT2x initialise failure\n");
+            log_err("SHT2x initialise failure\n");
             return -2;
         }
     }
@@ -364,7 +365,7 @@
 
 	if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}
@@ -382,7 +383,7 @@
 
 	if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}
@@ -406,7 +407,7 @@
 
 	if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}
@@ -424,7 +425,7 @@
 
 	if( ioctl(s_sht2x_fd, I2C_RDWR, &sht2x_data) < 0 )
 	{
-		printf("%s() ioctl failure: %s\n", __func__, strerror(errno));
+		log_err("sht2x I2C_RDWR ioctl failure: %s\n", strerror(errno));
         sht2x_term();
 		return -2;
 	}

--
Gitblit v1.9.1