-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBattStats.cpp
More file actions
80 lines (68 loc) · 2.21 KB
/
BattStats.cpp
File metadata and controls
80 lines (68 loc) · 2.21 KB
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
#include "BattStats.h"
#include <limits.h> //just fot PATH_MAX
#include <libgen.h>
BattTracker bt;
void inthand(int signum){
bt.unlock();
}
void displayHelp();
int main(int argc, char** argv){
signal(SIGINT, inthand);
signal(SIGTERM, inthand);
signal(SIGKILL, inthand);
//Fix directory to executable path (otherwise app run from another directory has different behaviour)
{
char* path = new char[PATH_MAX];
//get path with executable name
if(readlink("/proc/self/exe", path, PATH_MAX) == -1){
cout<<"readlink error"<<endl;
exit(1);
}
//extract path only
path = dirname(path);
//change dir to path
if( chdir(path) == -1){
cout<<"chdir error"<<endl;
exit(1);
}
}
if(argc>1 && !strcmp(argv[1], "start")){
//launching child to detach from console
int pid = fork();
if(pid < 0){
cout << "Error launching app" << endl;
} else if (pid != 0){
//Parent process has launched child (deamon)
//Parent is no more needed
exit(0);
}
setsid();
//From now on the child has power
//setting measurement interval
int interval = atoi(argv[argc-1]); //get last argument
if(interval == 0){ //not set or set to 0
interval = 5; //set to 5 minutes
}
//second condition sets boolean for clean
bt.start(interval, (argc>2 && !strcmp(argv[2], "clean")));
} else if(argc>1 && !strcmp(argv[1], "stop")){
bt.kill();
} else if(argc>1 && !strcmp(argv[1], "help")){
displayHelp();
} else {
bt.status();
}
return 0;
}
void displayHelp(){
cout<<"Battstats is a simple utility to track your battery usage."<<endl
<<endl<<'\t'<<"./battstats start : starts the 'daemon'"<<endl
<<'\t'<<"./battstats start clean : starts and cleans the log file"<<endl
<<'\t'<<"./battstats start 2 : starts with 2 minutes (or whetever it is set) measurement interval"<<endl
<<'\t'<<"./battstats start clean 2 : same as before but it cleans log"<<endl
<<'\t'<<"./battstats stop : obvious"<<endl
<<'\t'<<"./battstats : prints information about battery percentage and time remaining"<<endl
<<'\t'<<"./battstats help : prints this help screen"<<endl
<<endl<<"battstats.log contains information about computer state (charging or discharging), time on battery and relative battery percentage."
<<endl;
}