-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
115 lines (100 loc) · 2.83 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
* Copyright 2014,2022 Dennis Vesterlund
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#include "main.h"
#include <fcntl.h>
#include <getopt.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
int running = 1;
int main(int argc, char *argv[]) {
int opt;
char logfile[] = LOGFILE;
int sleeptime = SLEEPTIME;
while((opt = getopt(argc, argv, "hS:")) != -1) {
switch(opt) {
case 'h':
printHelp(argv[0]);
exit(0);
case 'S':
sleeptime = atoi(optarg);
if(sleeptime < 1)
sleeptime = SLEEPTIME;
break;
}
}
signal(SIGINT, sighandler);
signal(SIGTERM, sighandler);
signal(SIGCONT, sighandler);
logLoop(logfile, sleeptime * 1000L);
return 0;
}
void printHelp(char *name) {
printf("%s [hdS]\n\
Log the battery level to a file.\n\n\
Options:\n\
-h : Print this help.\n\
-S <seconds>: Time to sleep in seconds?\n", name);
}
void logLoop(char *logfile, long sleeptime) {
batteries *batteries = get_batteries();
while(running) {
for (int i = 0; i < batteries->num_batteries; i++) {
get_battery(batteries->batteries[i]);
}
saveToFile(logfile, batteries);
usleep(sleeptime/10);
}
free_batteries(batteries);
}
void sighandler(int signo) {
switch(signo) {
case SIGTERM:
case SIGINT:
running = 0;
break;
case SIGCONT:
break;
}
}
char saveToFile(char *filename, batteries *batteries) {
FILE *f = fopen(filename, "a+");
if(f == NULL)
return -1;
time_t rawtime;
struct tm *ti;
time(&rawtime);
ti = localtime(&rawtime);
int ret = fprintf(f, "[%i-%0.2i-%0.2i %0.2i:%0.2i:%0.2i] ",
ti->tm_year+1900,
ti->tm_mon,
ti->tm_mday,
ti->tm_hour,
ti->tm_min,
ti->tm_sec);
for (int i = 0; i < batteries->num_batteries; i++) {
battery *bat = batteries->batteries[i];
ret = fprintf(f, "%s %s %d %ld %s ", bat->name, bat->type, bat->capacity, bat->energy_full, bat->status);
}
fprintf(f, "\n");
fclose(f);
return ret;
}