mbpfan-josie

Add logging/printing helper

This eliminates duplicate code, fixes some log levels, and removes
unnecessary trailing newlines.  Fixes #202.

abda2b8901b004557d3abb0fc0e90df6b68d3d65
Andrew Gaul <andrew@gaul.org> · 2019-10-30T10:38 · browse files at this commit

parents: 0219257

diff --git a/src/daemon.c b/src/daemon.c
index afdef6a..7ce03fd 100644
--- a/src/daemon.c
+++ b/src/daemon.c
@@ -33,6 +33,7 @@
 #include "mbpfan.h"
 #include "global.h"
 #include "daemon.h"
+#include "util.h"
 
 int write_pid(int pid)
 {
@@ -207,33 +208,21 @@ void go_daemon(void (*fan_control)())
 
     if (read_pid() == -1) {
         if (verbose) {
-            printf("Writing a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID);
-            syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
+            mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
         }
 
         if (write_pid(current_pid) == 0) {
-            syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID);
-
-            if (verbose) {
-                printf("ERROR: Can not create a .pid file at: %s. Aborting\n", PROGRAM_PID);
-            }
-
+            mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID);
             exit(EXIT_FAILURE);
 
         } else {
             if (verbose) {
-                printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID);
-                syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
+                mbp_log(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
             }
         }
 
     } else {
-        syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID);
-
-        if (verbose) {
-            printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", PROGRAM_PID);
-        }
-
+        mbp_log(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID);
         exit(EXIT_FAILURE);
     }
 
diff --git a/src/main.c b/src/main.c
index 2c4286b..df22318 100644
--- a/src/main.c
+++ b/src/main.c
@@ -31,6 +31,7 @@
 #include "global.h"
 #include "main.h"
 #include "minunit.h"
+#include "util.h"
 
 int daemonize = 1;
 int verbose = 0;
@@ -64,8 +65,7 @@ void check_requirements()
     uid_t uid=getuid(), euid=geteuid();
 
     if (uid != 0 || euid != 0) {
-        syslog(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
-        printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME);
+        mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
         exit(EXIT_FAILURE);
     }
 
@@ -75,8 +75,7 @@ void check_requirements()
     DIR* dir = opendir(CORETEMP_PATH);
 
     if (ENOENT == errno) {
-        syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
-        printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
+        mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
         exit(EXIT_FAILURE);
     }
 
@@ -86,8 +85,7 @@ void check_requirements()
     dir = opendir(APPLESMC_PATH);
 
     if (ENOENT == errno) {
-        syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
-        printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
+        mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
         exit(EXIT_FAILURE);
     }
 
diff --git a/src/mbpfan.c b/src/mbpfan.c
index 477f6e8..129c708 100644
--- a/src/mbpfan.c
+++ b/src/mbpfan.c
@@ -42,6 +42,7 @@
 #include "mbpfan.h"
 #include "global.h"
 #include "settings.h"
+#include "util.h"
 
 /* lazy min/max... */
 #define min(a,b) ((a) < (b) ? (a) : (b))
@@ -100,8 +101,7 @@ bool is_modern_sensors_path()
     str_kernel_version = strtok(kernel.release, ".");
 
     if (atoi(str_kernel_version) < 3){
-        syslog(LOG_INFO, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
-        printf("mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
+        mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
         exit(EXIT_FAILURE);
     }
 
@@ -136,11 +136,7 @@ t_sensors *retrieve_sensors()
 
     if (!is_modern_sensors_path()) {
         if(verbose) {
-            printf("Using legacy sensor path for kernel < 3.15.0\n");
-
-            if(daemonize) {
-                syslog(LOG_INFO, "Using legacy path for kernel < 3.15.0");
-            }
+            mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0");
         }
 
         path_begin = strdup("/sys/devices/platform/coretemp.0/temp");
@@ -148,11 +144,7 @@ t_sensors *retrieve_sensors()
     } else {
 
         if(verbose) {
-            printf("Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0\n");
-
-            if(daemonize) {
-                syslog(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 ");
-            }
+            mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 ");
         }
 
 	// loop over up to 6 processors
@@ -176,12 +168,7 @@ t_sensors *retrieve_sensors()
 		    path_begin = smprintf("%s/temp", hwmon_path);
 
 		    if(verbose) {
-			printf("Found hwmon path at %s\n", path_begin);
-
-			if(daemonize) {
-			    syslog(LOG_INFO, "Found hwmon path at %s\n", path_begin);
-			}
-
+                        mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin);
 		    }
 
                     free(hwmon_path);
@@ -228,16 +215,11 @@ t_sensors *retrieve_sensors()
     }
 
     if(verbose) {
-        printf("Found %d sensors\n", sensors_found);
-
-        if(daemonize) {
-            syslog(LOG_INFO, "Found %d sensors", sensors_found);
-        }
+        mbp_log(LOG_INFO, "Found %d sensors", sensors_found);
     }
 
     if (sensors_found == 0){
-        syslog(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.\n");
-        printf("mbpfan could not detect any temp sensor. Please contact the developer.\n");
+        mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.");
         exit(EXIT_FAILURE);
     }
 
@@ -363,16 +345,11 @@ t_fans *retrieve_fans()
     }
 
     if(verbose) {
-        printf("Found %d fans\n", fans_found);
-
-        if(daemonize) {
-            syslog(LOG_INFO, "Found %d fans", fans_found);
-        }
+        mbp_log(LOG_INFO, "Found %d fans", fans_found);
     }
 
     if (fans_found == 0){
-        syslog(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.\n");
-        printf("mbpfan could not detect any fan. Please contact the developer.\n");
+        mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.");
         exit(EXIT_FAILURE);
     }
 
@@ -481,11 +458,7 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
     if (f == NULL) {
         /* Could not open configfile */
         if(verbose) {
-            printf("Couldn't open configfile, using defaults\n");
-
-            if(daemonize) {
-                syslog(LOG_INFO, "Couldn't open configfile, using defaults");
-            }
+            mbp_log(LOG_INFO, "Couldn't open configfile, using defaults");
         }
 
     } else {
@@ -495,11 +468,7 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
         if (settings == NULL) {
             /* Could not read configfile */
             if(verbose) {
-                printf("Couldn't read configfile\n");
-
-                if(daemonize) {
-                    syslog(LOG_WARNING, "Couldn't read configfile");
-                }
+                mbp_log(LOG_WARNING, "Couldn't read configfile");
             }
 
         } else {
@@ -570,16 +539,14 @@ void mbpfan()
     while(fan != NULL) {
 	
         if (fan->fan_min_speed > fan->fan_max_speed) {
-            syslog(LOG_INFO, "Invalid fan speeds: %d %d", fan->fan_min_speed,  fan->fan_max_speed);
-            printf("Invalid fan speeds: %d %d\n", fan->fan_min_speed, fan->fan_max_speed);
+            mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed,  fan->fan_max_speed);
             exit(EXIT_FAILURE);
         }
 	fan = fan->next;
     }
 
     if (low_temp > high_temp || high_temp > max_temp) {
-        syslog(LOG_INFO, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp);
-        printf("Invalid temperatures: %d %d %d\n", low_temp, high_temp, max_temp);
+        mbp_log(LOG_ERR, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp);
         exit(EXIT_FAILURE);
     }
 
@@ -601,11 +568,7 @@ void mbpfan()
 
 recalibrate:
     if(verbose) {
-        printf("Sleeping for 2 seconds to get first temp delta\n");
-
-        if(daemonize) {
-            syslog(LOG_INFO, "Sleeping for 2 seconds to get first temp delta");
-        }
+        mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta");
     }
     sleep(2);
 
@@ -639,11 +602,7 @@ recalibrate:
             }
 
             if(verbose) {
-                printf("Old Temp: %d New Temp: %d Fan: %s Speed: %d\n", old_temp, new_temp, fan->label, fan_speed);
-	    
-                if(daemonize) {
-                   syslog(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed);
-                }
+                mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed);
 	    }
 
 	    set_fan_speed(fan, fan_speed);
@@ -651,12 +610,7 @@ recalibrate:
 	} 
 
         if(verbose) {
-            printf("Sleeping for %d seconds\n", polling_interval);
-            fflush(stdout);
-
-            if(daemonize) {
-                syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval);
-            }
+            mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval);
         }
    
         time_t before_sleep = time(NULL);
@@ -670,13 +624,7 @@ recalibrate:
 
         time_t after_sleep = time(NULL);
         if(after_sleep - before_sleep > 2 * polling_interval) {
-            printf("Clock skew detected - slept for %ld seconds but expected %d\n", after_sleep - before_sleep, polling_interval);
-            fflush(stdout);
-
-            if(daemonize) {
-                syslog(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval);
-            }
-
+            mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval);
             set_fans_man(fans);
             goto recalibrate;
         }
diff --git a/src/util.c b/src/util.c
new file mode 100644
index 0000000..73600c4
--- /dev/null
+++ b/src/util.c
@@ -0,0 +1,18 @@
+#include <stdarg.h>
+#include <stdio.h>
+#define __USE_MISC
+#include <syslog.h>
+
+#include "global.h"
+
+void mbp_log(int level, const char *fmt, ...)
+{
+    va_list args;
+    va_start(args, fmt);
+    if (daemonize) {
+        vsyslog(level, fmt, args);
+    }
+    vprintf(fmt, args);
+    puts("");  // trailing newline
+    va_end(args);
+}
diff --git a/src/util.h b/src/util.h
new file mode 100644
index 0000000..1f3819e
--- /dev/null
+++ b/src/util.h
@@ -0,0 +1,6 @@
+#ifndef _UTIL_H_
+#define _UTIL_H_
+
+void mbp_log(int level, const char *fmt, ...);
+
+#endif