Skip to content

Commit abda2b8

Browse files
committed
Add logging/printing helper
This eliminates duplicate code, fixes some log levels, and removes unnecessary trailing newlines. Fixes #202.
1 parent 0219257 commit abda2b8

File tree

5 files changed

+50
-91
lines changed

5 files changed

+50
-91
lines changed

src/daemon.c

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "mbpfan.h"
3434
#include "global.h"
3535
#include "daemon.h"
36+
#include "util.h"
3637

3738
int write_pid(int pid)
3839
{
@@ -207,33 +208,21 @@ void go_daemon(void (*fan_control)())
207208

208209
if (read_pid() == -1) {
209210
if (verbose) {
210-
printf("Writing a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID);
211-
syslog(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
211+
mbp_log(LOG_INFO, "Writing a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
212212
}
213213

214214
if (write_pid(current_pid) == 0) {
215-
syslog(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID);
216-
217-
if (verbose) {
218-
printf("ERROR: Can not create a .pid file at: %s. Aborting\n", PROGRAM_PID);
219-
}
220-
215+
mbp_log(LOG_ERR, "Can not create a .pid file at: %s. Aborting", PROGRAM_PID);
221216
exit(EXIT_FAILURE);
222217

223218
} else {
224219
if (verbose) {
225-
printf("Successfully written a new .pid file with value %d at: %s\n", current_pid, PROGRAM_PID);
226-
syslog(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
220+
mbp_log(LOG_INFO, "Successfully written a new .pid file with value %d at: %s", current_pid, PROGRAM_PID);
227221
}
228222
}
229223

230224
} else {
231-
syslog(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID);
232-
233-
if (verbose) {
234-
printf("ERROR: a previously created .pid file exists at: %s.\n Aborting\n", PROGRAM_PID);
235-
}
236-
225+
mbp_log(LOG_ERR, "A previously created .pid file exists at: %s. Aborting", PROGRAM_PID);
237226
exit(EXIT_FAILURE);
238227
}
239228

src/main.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "global.h"
3232
#include "main.h"
3333
#include "minunit.h"
34+
#include "util.h"
3435

3536
int daemonize = 1;
3637
int verbose = 0;
@@ -64,8 +65,7 @@ void check_requirements()
6465
uid_t uid=getuid(), euid=geteuid();
6566

6667
if (uid != 0 || euid != 0) {
67-
syslog(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
68-
printf("%s not started with root privileges. Please run %s as root. Exiting.\n", PROGRAM_NAME, PROGRAM_NAME);
68+
mbp_log(LOG_ERR, "%s needs root privileges. Please run %s as root. Exiting.", PROGRAM_NAME, PROGRAM_NAME);
6969
exit(EXIT_FAILURE);
7070
}
7171

@@ -75,8 +75,7 @@ void check_requirements()
7575
DIR* dir = opendir(CORETEMP_PATH);
7676

7777
if (ENOENT == errno) {
78-
syslog(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
79-
printf("%s needs coretemp module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
78+
mbp_log(LOG_ERR, "%s needs coretemp support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
8079
exit(EXIT_FAILURE);
8180
}
8281

@@ -86,8 +85,7 @@ void check_requirements()
8685
dir = opendir(APPLESMC_PATH);
8786

8887
if (ENOENT == errno) {
89-
syslog(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
90-
printf("%s needs applesmc module.\nPlease either load it or build it into the kernel. Exiting.\n", PROGRAM_NAME);
88+
mbp_log(LOG_ERR, "%s needs applesmc support. Please either load it or build it into the kernel. Exiting.", PROGRAM_NAME);
9189
exit(EXIT_FAILURE);
9290
}
9391

src/mbpfan.c

Lines changed: 17 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include "mbpfan.h"
4343
#include "global.h"
4444
#include "settings.h"
45+
#include "util.h"
4546

4647
/* lazy min/max... */
4748
#define min(a,b) ((a) < (b) ? (a) : (b))
@@ -100,8 +101,7 @@ bool is_modern_sensors_path()
100101
str_kernel_version = strtok(kernel.release, ".");
101102

102103
if (atoi(str_kernel_version) < 3){
103-
syslog(LOG_INFO, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
104-
printf("mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
104+
mbp_log(LOG_ERR, "mbpfan detected a pre-3.x.x linux kernel. Detected version: %s. Exiting.\n", kernel.release);
105105
exit(EXIT_FAILURE);
106106
}
107107

@@ -136,23 +136,15 @@ t_sensors *retrieve_sensors()
136136

137137
if (!is_modern_sensors_path()) {
138138
if(verbose) {
139-
printf("Using legacy sensor path for kernel < 3.15.0\n");
140-
141-
if(daemonize) {
142-
syslog(LOG_INFO, "Using legacy path for kernel < 3.15.0");
143-
}
139+
mbp_log(LOG_INFO, "Using legacy path for kernel < 3.15.0");
144140
}
145141

146142
path_begin = strdup("/sys/devices/platform/coretemp.0/temp");
147143

148144
} else {
149145

150146
if(verbose) {
151-
printf("Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0\n");
152-
153-
if(daemonize) {
154-
syslog(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 ");
155-
}
147+
mbp_log(LOG_INFO, "Using new sensor path for kernel >= 3.15.0 or some CentOS versions with kernel 3.10.0 ");
156148
}
157149

158150
// loop over up to 6 processors
@@ -176,12 +168,7 @@ t_sensors *retrieve_sensors()
176168
path_begin = smprintf("%s/temp", hwmon_path);
177169

178170
if(verbose) {
179-
printf("Found hwmon path at %s\n", path_begin);
180-
181-
if(daemonize) {
182-
syslog(LOG_INFO, "Found hwmon path at %s\n", path_begin);
183-
}
184-
171+
mbp_log(LOG_INFO, "Found hwmon path at %s", path_begin);
185172
}
186173

187174
free(hwmon_path);
@@ -228,16 +215,11 @@ t_sensors *retrieve_sensors()
228215
}
229216

230217
if(verbose) {
231-
printf("Found %d sensors\n", sensors_found);
232-
233-
if(daemonize) {
234-
syslog(LOG_INFO, "Found %d sensors", sensors_found);
235-
}
218+
mbp_log(LOG_INFO, "Found %d sensors", sensors_found);
236219
}
237220

238221
if (sensors_found == 0){
239-
syslog(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.\n");
240-
printf("mbpfan could not detect any temp sensor. Please contact the developer.\n");
222+
mbp_log(LOG_CRIT, "mbpfan could not detect any temp sensor. Please contact the developer.");
241223
exit(EXIT_FAILURE);
242224
}
243225

@@ -363,16 +345,11 @@ t_fans *retrieve_fans()
363345
}
364346

365347
if(verbose) {
366-
printf("Found %d fans\n", fans_found);
367-
368-
if(daemonize) {
369-
syslog(LOG_INFO, "Found %d fans", fans_found);
370-
}
348+
mbp_log(LOG_INFO, "Found %d fans", fans_found);
371349
}
372350

373351
if (fans_found == 0){
374-
syslog(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.\n");
375-
printf("mbpfan could not detect any fan. Please contact the developer.\n");
352+
mbp_log(LOG_CRIT, "mbpfan could not detect any fan. Please contact the developer.");
376353
exit(EXIT_FAILURE);
377354
}
378355

@@ -481,11 +458,7 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
481458
if (f == NULL) {
482459
/* Could not open configfile */
483460
if(verbose) {
484-
printf("Couldn't open configfile, using defaults\n");
485-
486-
if(daemonize) {
487-
syslog(LOG_INFO, "Couldn't open configfile, using defaults");
488-
}
461+
mbp_log(LOG_INFO, "Couldn't open configfile, using defaults");
489462
}
490463

491464
} else {
@@ -495,11 +468,7 @@ void retrieve_settings(const char* settings_path, t_fans* fans)
495468
if (settings == NULL) {
496469
/* Could not read configfile */
497470
if(verbose) {
498-
printf("Couldn't read configfile\n");
499-
500-
if(daemonize) {
501-
syslog(LOG_WARNING, "Couldn't read configfile");
502-
}
471+
mbp_log(LOG_WARNING, "Couldn't read configfile");
503472
}
504473

505474
} else {
@@ -570,16 +539,14 @@ void mbpfan()
570539
while(fan != NULL) {
571540

572541
if (fan->fan_min_speed > fan->fan_max_speed) {
573-
syslog(LOG_INFO, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed);
574-
printf("Invalid fan speeds: %d %d\n", fan->fan_min_speed, fan->fan_max_speed);
542+
mbp_log(LOG_ERR, "Invalid fan speeds: %d %d", fan->fan_min_speed, fan->fan_max_speed);
575543
exit(EXIT_FAILURE);
576544
}
577545
fan = fan->next;
578546
}
579547

580548
if (low_temp > high_temp || high_temp > max_temp) {
581-
syslog(LOG_INFO, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp);
582-
printf("Invalid temperatures: %d %d %d\n", low_temp, high_temp, max_temp);
549+
mbp_log(LOG_ERR, "Invalid temperatures: %d %d %d", low_temp, high_temp, max_temp);
583550
exit(EXIT_FAILURE);
584551
}
585552

@@ -601,11 +568,7 @@ void mbpfan()
601568

602569
recalibrate:
603570
if(verbose) {
604-
printf("Sleeping for 2 seconds to get first temp delta\n");
605-
606-
if(daemonize) {
607-
syslog(LOG_INFO, "Sleeping for 2 seconds to get first temp delta");
608-
}
571+
mbp_log(LOG_INFO, "Sleeping for 2 seconds to get first temp delta");
609572
}
610573
sleep(2);
611574

@@ -639,24 +602,15 @@ void mbpfan()
639602
}
640603

641604
if(verbose) {
642-
printf("Old Temp: %d New Temp: %d Fan: %s Speed: %d\n", old_temp, new_temp, fan->label, fan_speed);
643-
644-
if(daemonize) {
645-
syslog(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed);
646-
}
605+
mbp_log(LOG_INFO, "Old Temp: %d New Temp: %d Fan: %s Speed: %d", old_temp, new_temp, fan->label, fan_speed);
647606
}
648607

649608
set_fan_speed(fan, fan_speed);
650609
fan = fan->next;
651610
}
652611

653612
if(verbose) {
654-
printf("Sleeping for %d seconds\n", polling_interval);
655-
fflush(stdout);
656-
657-
if(daemonize) {
658-
syslog(LOG_INFO, "Sleeping for %d seconds", polling_interval);
659-
}
613+
mbp_log(LOG_INFO, "Sleeping for %d seconds", polling_interval);
660614
}
661615

662616
time_t before_sleep = time(NULL);
@@ -670,13 +624,7 @@ void mbpfan()
670624

671625
time_t after_sleep = time(NULL);
672626
if(after_sleep - before_sleep > 2 * polling_interval) {
673-
printf("Clock skew detected - slept for %ld seconds but expected %d\n", after_sleep - before_sleep, polling_interval);
674-
fflush(stdout);
675-
676-
if(daemonize) {
677-
syslog(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval);
678-
}
679-
627+
mbp_log(LOG_INFO, "Clock skew detected - slept for %ld seconds but expected %d", after_sleep - before_sleep, polling_interval);
680628
set_fans_man(fans);
681629
goto recalibrate;
682630
}

src/util.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include <stdarg.h>
2+
#include <stdio.h>
3+
#define __USE_MISC
4+
#include <syslog.h>
5+
6+
#include "global.h"
7+
8+
void mbp_log(int level, const char *fmt, ...)
9+
{
10+
va_list args;
11+
va_start(args, fmt);
12+
if (daemonize) {
13+
vsyslog(level, fmt, args);
14+
}
15+
vprintf(fmt, args);
16+
puts(""); // trailing newline
17+
va_end(args);
18+
}

src/util.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#ifndef _UTIL_H_
2+
#define _UTIL_H_
3+
4+
void mbp_log(int level, const char *fmt, ...);
5+
6+
#endif

0 commit comments

Comments
 (0)