add function: logMessage(const char*, ...)
This commit is contained in:
parent
8d776e56b5
commit
a91307202c
34
main.c
34
main.c
@ -5,9 +5,12 @@
|
|||||||
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <string.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <pigpio.h>
|
#include <pigpio.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
|
#define TEMP_PATH "/sys/class/thermal/thermal_zone0/temp"
|
||||||
#define FAN_PIN 17
|
#define FAN_PIN 17
|
||||||
@ -15,6 +18,7 @@
|
|||||||
#define HIGH 1
|
#define HIGH 1
|
||||||
#define LOW 0
|
#define LOW 0
|
||||||
|
|
||||||
|
int logMessage(const char *, ...);
|
||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
int fd;
|
int fd;
|
||||||
char buffer[6];
|
char buffer[6];
|
||||||
@ -77,3 +81,33 @@ int main(int argc, char** argv) {
|
|||||||
gpioTerminate();
|
gpioTerminate();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int logMessage(const char *format, ...) {
|
||||||
|
if (!LOG) {
|
||||||
|
#pragma clang diagnostic push
|
||||||
|
#pragma ide diagnostic ignored "UnreachableCode"
|
||||||
|
return 0;
|
||||||
|
#pragma clang diagnostic pop
|
||||||
|
}
|
||||||
|
|
||||||
|
FILE *logFile = fopen(LOG_FILE, "a");
|
||||||
|
if (!logFile) {
|
||||||
|
printf("Error opening log file");
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
time_t currentTime;
|
||||||
|
time(¤tTime);
|
||||||
|
struct tm *timeInfo = localtime(¤tTime);
|
||||||
|
|
||||||
|
fprintf(logFile, "[%04d-%02d-%02d %02d:%02d:%02d] ", timeInfo->tm_year + 1900, timeInfo->tm_mon + 1,
|
||||||
|
timeInfo->tm_mday, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec);
|
||||||
|
|
||||||
|
va_list args;
|
||||||
|
va_start(args, format);
|
||||||
|
vfprintf(logFile, format, args);
|
||||||
|
va_end(args);
|
||||||
|
|
||||||
|
fclose(logFile);
|
||||||
|
return 0;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user