1 #include <iostream>
2 #include "message.hpp"
3 #include "event_messaged_sdbus.h"
4 #include <string>
5 #include <unistd.h>
6 
7 const char *path_to_messages = "/var/lib/obmc/events";
8 
9 using namespace std;
10 
message_refresh_events(event_manager * em)11 void message_refresh_events(event_manager *em)
12 {
13 	em->next_log_refresh();
14 	return;
15 }
message_next_event(event_manager * em)16 uint16_t message_next_event(event_manager *em)
17 {
18 	return em->next_log();
19 }
20 
message_create_new_log_event(event_manager * em,event_record_t * rec)21 uint16_t message_create_new_log_event(event_manager *em, event_record_t *rec)
22 {
23 	return em->create(rec);
24 }
message_load_log(event_manager * em,uint16_t logid,event_record_t ** rec)25 int message_load_log(event_manager *em,uint16_t logid, event_record_t **rec)
26 {
27 	return em->open(logid, rec);
28 }
message_free_log(event_manager * em,event_record_t * rec)29 void message_free_log(event_manager *em, event_record_t *rec)
30 {
31 	return em->close(rec);
32 }
message_delete_log(event_manager * em,uint16_t logid)33 int message_delete_log(event_manager *em, uint16_t logid)
34 {
35 	return em->remove(logid);
36 }
37 
load_existing_events(event_manager * em)38 int load_existing_events(event_manager *em)
39 {
40 	uint16_t id;
41 	event_record_t *rec;
42 
43 	while ( (id = em->next_log()) != 0) {
44 
45 		em->open(id, &rec);
46 		send_log_to_dbus(em, id, rec->association);
47 		em->close(rec);
48 	}
49 
50 	return 0;
51 }
52 
53 
print_usage(void)54 void print_usage(void)
55 {
56 	cout << "[-s <x>] : Maximum bytes to use for event logger"  << endl;
57 	cout << "[-t <x>] : Limit total number of logs (will ignore newer)"  << endl;
58 	return;
59 }
60 
61 
main(int argc,char * argv[])62 int main(int argc, char *argv[])
63 {
64 	unsigned long maxsize=0, maxlogs=0;
65 	int rc, c;
66 
67 	while ((c = getopt (argc, argv, "s:t:")) != -1)
68 		switch (c) {
69 			case 's':
70 				maxsize =  strtoul(optarg, NULL, 10);
71 				break;
72 			case 't':
73 				maxlogs =  strtoul(optarg, NULL, 10);
74 				break;
75 			case 'h':
76 			case '?':
77 				print_usage();
78 				return 1;
79 		}
80 
81 
82 	cout << maxsize <<endl;
83 	event_manager em(path_to_messages, maxsize, maxlogs);
84 
85 
86 	rc = build_bus(&em);
87 	if (rc < 0) {
88 		fprintf(stderr, "Event Messager failed to connect to dbus rc=%d", rc);
89 		goto finish;
90 	}
91 
92 	rc = load_existing_events(&em);
93 	if (rc < 0) {
94 		fprintf(stderr, "Event Messager failed add previous logs to dbus rc=%d", rc);
95 		goto finish;
96 	}
97 
98 	rc = start_event_monitor();
99 
100 finish:
101 	cleanup_event_monitor();
102 
103 	return rc;
104 }
105