SingingCat 0
application
logging.c
1#include "main-header.h"
2// #include "bare-metal.h"
3
8static long lognodeid;
9static byte enabled = 0;
15static char buf[400];
16static char zbuf[512];
21 enabled = 0;
22 buf[0] = 0;
23}
27void logging_set_node(long nodeid) {
28 lognodeid = nodeid;
29}
35 printf("Logging disabled.\r\n");
36 enabled = 0;
37}
42void logging_on() {
43 printf("Logging enabled.\r\n");
44 enabled = 1;
45}
46static void printbuf() {
47 printf("************ LOGBUF ********\r\n");
48 printf("%s\r\n", buf);
49 printf("********** END LOGBUF ******\r\n");
50}
55 struct command *logcommand;
56
57 if (!enabled) {
58 return;
59 }
60 if (strlen(buf) == 0) {
61 return;
62 }
63 logcommand = alloc_command();
64 if (logcommand == NULL) {
65 printf("No command left to allocate to log.\r\n");
66 return;
67 }
68 // send log commands until buffer is completely sent
69 command_init(logcommand);
70 logcommand->com = 2; // log
71 logcommand->target = lognodeid;
72 command_add_arg(logcommand, "%s", buf);
73 send_command_quietly(logcommand);
74 free_command(logcommand);
75 buf[0] = 0;
76}
77
78
79static void overflow() {
80 printf("LOGGING OVERFLOW (%i).\r\n", strlen(buf));
81 printbuf();
82 buf[0] = 0;
83}
87void iplog(const char *format, ...) {
88 va_list args;
89
90 if (!enabled) {
91 va_start(args, format);
92 vsnprintf((char *)&zbuf, 260, format, args);
93 va_end(args);
94 print((const char *)&zbuf);
95 return;
96 }
97 int i;
98 int bsize = sizeof(buf);
99
100 va_start(args, format);
101 i = strlen(buf);
102 if (i > (bsize - 5)) {
103 overflow();
104 return;
105 }
106 // note we check if strlen == 4. we can only ever
107 // hit this once because we're adding to strlen ;)
108 if (i >= (bsize - 10)) {
109 if (i > (bsize - 10)) {
110 i = bsize - 10;
111 }
112 buf[i++] = '.';
113 buf[i++] = '.';
114 buf[i++] = '.';
115 buf[i++] = 0;
116 return;
117 }
118 /*
119 * if (i != 0) {
120 * buf[i++] = '\n';
121 * buf[i] = 0;
122 * }
123 */
124 vsnprintf(buf + i, (bsize - i), format, args);
125 va_end(args);
126}
void logging_set_node(long nodeid)
log to a given node. Usually this would be an app or a server
Definition: logging.c:27
void logging_init()
initialise logging. logging is disabled after calling this function
Definition: logging.c:20
void logging_off()
disable logging and logging processing. guarantees that no buffers are used for logging
Definition: logging.c:34
void free_command(struct command *com)
free a command
Definition: queue.c:200
void command_add_arg(struct command *com, const char *format,...)
adds an arg to a partially initialised command structure
void logging_event_loop()
sends current buffer and empties it
Definition: logging.c:54
void command_init(struct command *com)
initialize a command structure with default values
void logging_on()
enable logging and logging processing. buffers are used for logging
Definition: logging.c:42
struct command * alloc_command()
allocate a free command
Definition: queue.c:173
void iplog(const char *format,...)
log something to a remote node
Definition: logging.c:87
int com
Definition: command.h:22
long target
Definition: command.h:16