Skip to content

Commit 9688df6

Browse files
authored
Merge pull request #74 from CarletonURocketry/common-logging
Implemented logging for packager using common logging utility.
2 parents 21d263c + 3162279 commit 9688df6

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

common.mk

+4
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ WARNINGS += -Wdisabled-optimization -Wsuggest-attribute=const
3333
### UPDATE CFLAGS ###
3434
CCFLAGS += -std=$(CSTD) $(WARNINGS)
3535

36+
# Program name for logging
37+
CCFLAGS += -DPROGNAME=$(NAME)
38+
3639
#### PROJECT SPECIFIC ####
3740

3841
### SOURCE FILES ###
3942
EXTRA_SRCVPATH += $(PROJECT_ROOT)/src
43+
EXTRA_SRCVPATH += $(PROJECT_ROOT)/../logging-utils
4044

4145
include $(MKFILES_ROOT)/qtargets.mk
4246

src/main.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include "../logging-utils/logging.h"
12
#include "intypes.h"
23
#include "packet_types.h"
34
#include <errno.h>
@@ -80,7 +81,7 @@ int main(int argc, char **argv) {
8081
if (infile != NULL) {
8182
input = fopen(infile, "r");
8283
if (input == NULL) {
83-
fprintf(stderr, "File '%s' could not be opened for reading.\n", infile);
84+
log_print(stderr, LOG_ERROR, "File '%s' could not be opened for reading.\n", infile);
8485
exit(EXIT_FAILURE);
8586
}
8687
}
@@ -92,7 +93,8 @@ int main(int argc, char **argv) {
9293
/* Open input message queue. */
9394
mqd_t in_q = mq_open(INPUT_QUEUE, O_RDONLY, &in_q_attr);
9495
if (in_q == -1) {
95-
fprintf(stderr, "Could not open input message queue %s with error %s\n", INPUT_QUEUE, strerror(errno));
96+
log_print(stderr, LOG_ERROR, "Could not open input message queue %s with error %s\n", INPUT_QUEUE,
97+
strerror(errno));
9698
exit(EXIT_FAILURE);
9799
}
98100

@@ -104,7 +106,7 @@ int main(int argc, char **argv) {
104106
};
105107
mqd_t out_q = mq_open(OUTPUT_QUEUE, O_CREAT | O_WRONLY, S_IWOTH, &q_attributes);
106108
if (out_q == -1) {
107-
fprintf(stderr, "Could not open output queue %s with error %s\n", OUTPUT_QUEUE, strerror(errno));
109+
log_print(stderr, LOG_ERROR, "Could not open output queue %s with error %s\n", OUTPUT_QUEUE, strerror(errno));
108110
exit(EXIT_FAILURE);
109111
}
110112

@@ -123,7 +125,8 @@ int main(int argc, char **argv) {
123125

124126
/* Read input data. */
125127
if (mq_receive(in_q, (char *)&recv_msg, sizeof(recv_msg), &last_priority) == -1) {
126-
fprintf(stderr, "Could not read message from queue %s with error %s\n", INPUT_QUEUE, strerror(errno));
128+
log_print(stderr, LOG_ERROR, "Could not read message from queue %s with error %s\n", INPUT_QUEUE,
129+
strerror(errno));
127130
continue;
128131
}
129132

@@ -196,7 +199,7 @@ int main(int argc, char **argv) {
196199
// Ignore this data for now, currently useful as debug output
197200
break;
198201
default:
199-
fprintf(stderr, "Unknown input data type: %u\n", recv_msg.type);
202+
log_print(stderr, LOG_ERROR, "Unknown input data type: %u\n", recv_msg.type);
200203
continue; // Skip to next iteration without storing block
201204
}
202205

@@ -209,7 +212,8 @@ int main(int argc, char **argv) {
209212

210213
// Send packet with the priority matching the highest priority data received
211214
if (mq_send(out_q, (char *)packet, packet_header_get_length((PacketHeader *)packet), highest_priority) == -1) {
212-
fprintf(stderr, "Failed to output encoded packet #%u with error: %s\n", pkt_count - 1, strerror(errno));
215+
log_print(stderr, LOG_ERROR, "Failed to output encoded packet #%u with error: %s\n", pkt_count - 1,
216+
strerror(errno));
213217
}
214218

215219
if (print_output) packet_print_hex(stdout, packet);

0 commit comments

Comments
 (0)