-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.c
140 lines (130 loc) · 4.73 KB
/
logger.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include "logger.h"
#include "system.h"
#include "cpu.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int logger_init(struct logger_t *logger, int type,
const char *filename, int stat_count, struct logger_stat_t *stats)
{
logger->type = type;
logger->stat_count = stat_count;
logger->file = NULL;
logger->stats = NULL;
if (stat_count == 0)
return 0;
logger->stats = (struct logger_stat_t *)malloc(
sizeof(struct logger_stat_t) * stat_count);
if (logger->stats == NULL)
return 1;
for (int i = 0; i < stat_count; ++i)
logger->stats[i] = stats[i];
logger->file = fopen(filename, "w");
if (logger->file == NULL) {
free(logger->stats);
return 2;
}
for (int i = 0; i < logger->stat_count; ++i) {
struct logger_stat_t stat = logger->stats[i];
char value[128];
if (stat.type == LOGGER_CPU_USAGE)
sprintf(value, "CPU%d Usage", stat.data.cpu_id);
else if (stat.type == LOGGER_CPU_TEMPERATURE)
sprintf(value, "CPU%d Temperature", stat.data.cpu_id);
else if (stat.type == LOGGER_CPU_FREQUENCY)
sprintf(value, "CPU%d Frequency (KHz)", stat.data.cpu_id);
else if (stat.type == LOGGER_RAM_USED)
sprintf(value, "RAM Used");
else if (stat.type == LOGGER_RAM_BUFFERS)
sprintf(value, "RAM Buffers");
else if (stat.type == LOGGER_RAM_CACHED)
sprintf(value, "RAM Caches");
else if (stat.type == LOGGER_DISK_READ)
sprintf(value, "Disk %s Read Speed (B/s)", stat.data.disk_name);
else if (stat.type == LOGGER_DISK_WRITE)
sprintf(value, "Disk %s Write Speed (B/s)", stat.data.disk_name);
else if (stat.type == LOGGER_IFACE_READ)
sprintf(value, "Interface %s Download Speed (B/s)", stat.data.iface_name);
else if (stat.type == LOGGER_IFACE_WRITE)
sprintf(value, "Interface %s Upload Speed (B/s)", stat.data.iface_name);
else if (stat.type == LOGGER_BAT_CHARGE)
sprintf(value, "Battery %s Charge (%%)", stat.data.battery_name);
else if (stat.type == LOGGER_BAT_CURRENT)
sprintf(value, "Battery %s Current (A)", stat.data.battery_name);
else if (stat.type == LOGGER_BAT_VOLTAGE)
sprintf(value, "Battery %s Voltage (V)", stat.data.battery_name);
else
value[0] = '\0';
fprintf(logger->file, "%s", value);
fputc(i == logger->stat_count - 1 ? '\n' : ',', logger->file);
}
return 0;
}
void logger_destroy(struct logger_t *logger)
{
free(logger->stats);
if (logger->file)
fclose(logger->file);
}
void logger_log(struct logger_t *logger, struct system_t *system)
{
for (int i = 0; i < logger->stat_count; ++i) {
struct logger_stat_t stat = logger->stats[i];
char value[128];
if (stat.type == LOGGER_CPU_FREQUENCY) {
sprintf(value, "%d", system->cpus[stat.data.cpu_id].cur_freq);
} else if (stat.type == LOGGER_CPU_USAGE) {
sprintf(value, "%f", system->cpus[stat.data.cpu_id].total_usage * 100.0);
} else if (stat.type == LOGGER_CPU_TEMPERATURE) {
sprintf(value, "%f", system->cpus[stat.data.cpu_id].cur_temp / 1000.0);
} else if (stat.type == LOGGER_RAM_USED) {
sprintf(value, "%lld", system->ram_used);
} else if (stat.type == LOGGER_RAM_BUFFERS) {
sprintf(value, "%lld", system->ram_buffers);
} else if (stat.type == LOGGER_RAM_CACHED) {
sprintf(value, "%lld", system->ram_cached);
} else if (stat.type == LOGGER_DISK_READ || stat.type == LOGGER_DISK_WRITE) {
struct disk_t *disk = NULL;
for (int i = 0; i < system->disk_count; ++i) {
if (!strcmp(system->disks[i].name, stat.data.disk_name)) {
disk = &system->disks[i];
break;
}
}
int disk_stat = stat.type == LOGGER_DISK_READ ? DISK_READ_SECTORS : DISK_WRITE_SECTORS;
sprintf(value, "%d", disk ? disk->stats_delta[disk_stat] * 512 : 0);
} else if (stat.type == LOGGER_IFACE_READ || stat.type == LOGGER_IFACE_WRITE) {
struct interface_t *interface = NULL;
for (int i = 0; i < system->interface_count; ++i) {
if (!strcmp(system->interfaces[i].name, stat.data.iface_name)) {
interface = &system->interfaces[i];
break;
}
}
sprintf(value, "%llu", interface ? ( stat.type == LOGGER_IFACE_READ ?
interface->delta_rx_bytes : interface->delta_tx_bytes) : 0);
} else if (stat.type == LOGGER_BAT_CHARGE ||
stat.type == LOGGER_BAT_CURRENT ||
stat.type == LOGGER_BAT_VOLTAGE) {
struct battery_t *battery = NULL;
for (int i = 0; i < system->battery_count; ++i) {
if (!strcmp(system->batteries[i].name, stat.data.battery_name)) {
battery = &system->batteries[i];
break;
}
}
int v;
if (stat.type == LOGGER_BAT_CHARGE)
v = battery->charge;
else if (stat.type == LOGGER_BAT_CURRENT)
v = battery->current;
else if (stat.type == LOGGER_BAT_VOLTAGE)
v = battery->voltage;
sprintf(value, "%d", v);
} else {
value[0] = '\0';
}
fprintf(logger->file, "%s", value);
fputc(i == logger->stat_count - 1 ? '\n' : ',', logger->file);
}
}