This source file includes following definitions.
- write_cursor
- write
- read
- log_printf
- halide_hexagon_remote_poll_log
#include "log.h"
#include <stdlib.h>
#include <stdio.h>
#include <qurt.h>
class Log {
char *buffer;
int size;
int read_cursor;
int write_cursor;
qurt_mutex_t lock;
public:
Log(int size) : buffer(NULL), size(size), read_cursor(0), write_cursor(0) {
qurt_mutex_init(&lock);
buffer = (char *)malloc(size);
}
~Log() {
free(buffer);
qurt_mutex_destroy(&lock);
}
void write(const char *in, int in_size) {
if (!buffer) return;
qurt_mutex_lock(&lock);
for (int i = 0; i < in_size; i++, write_cursor++) {
buffer[write_cursor & (size - 1)] = in[i];
}
qurt_mutex_unlock(&lock);
}
int read(char *out, int out_size, char delim = 0) {
qurt_mutex_lock(&lock);
if (out_size > write_cursor - read_cursor) {
out_size = write_cursor - read_cursor;
}
int i = 0;
while (i < out_size) {
char out_i = buffer[read_cursor++ & (size - 1)];
out[i++] = out_i;
if (out_i == delim) {
break;
}
}
qurt_mutex_unlock(&lock);
return i;
}
};
Log global_log(1024 * 64);
void log_printf(const char *fmt, ...) {
char message[1024] = { 0, };
va_list ap;
va_start(ap, fmt);
int message_size = vsnprintf(message, sizeof(message) - 1, fmt, ap);
va_end(ap);
global_log.write(message, message_size);
}
extern "C" int halide_hexagon_remote_poll_log(char *out, int size, int *read_size) {
*read_size = global_log.read(out, size - 1, '\n');
out[*read_size] = 0;
return 0;
}