fixed #131 (thanks to r00t!!)
This commit is contained in:
parent
acae400cb0
commit
d67c965d46
4 changed files with 62 additions and 41 deletions
|
@ -50,7 +50,7 @@ reading_t * buffer_push(buffer_t *buf, reading_t *rd);
|
|||
void buffer_free(buffer_t *buf);
|
||||
void buffer_clean(buffer_t *buf);
|
||||
void buffer_clear(buffer_t *buf);
|
||||
char * buffer_dump(buffer_t *buf, char *dump, int len);
|
||||
char * buffer_dump(buffer_t *buf, char *dump, size_t len);
|
||||
|
||||
|
||||
#endif /* _BUFFER_H_ */
|
||||
|
|
34
src/buffer.c
34
src/buffer.c
|
@ -97,26 +97,32 @@ void buffer_clean(buffer_t *buf) {
|
|||
pthread_mutex_unlock(&buf->mutex);
|
||||
}
|
||||
|
||||
char * buffer_dump(buffer_t *buf, char *dump, int len) {
|
||||
strcpy(dump, "|");
|
||||
char * buffer_dump(buffer_t *buf, char *dump, size_t len) {
|
||||
size_t pos = 0;
|
||||
dump[pos++] = '{';
|
||||
|
||||
for (reading_t *rd = buf->head; rd != NULL; rd = rd->next) {
|
||||
char tmp[16];
|
||||
sprintf(tmp, "%.2f|", rd->value);
|
||||
|
||||
if (strlen(dump)+strlen(tmp) < len) {
|
||||
if (buf->sent == rd) { /* indicate last sent reading */
|
||||
strcat(dump, "!");
|
||||
}
|
||||
|
||||
strcat(dump, tmp);
|
||||
if (pos < len) {
|
||||
pos += snprintf(dump+pos, len-pos, "%.2f", rd->value);
|
||||
}
|
||||
else {
|
||||
return NULL; /* dump buffer is full! */
|
||||
|
||||
/* indicate last sent reading */
|
||||
if (pos < len && buf->sent == rd) {
|
||||
dump[pos++] = '!';
|
||||
}
|
||||
|
||||
/* add seperator between values */
|
||||
if (pos < len && rd->next != NULL) {
|
||||
dump[pos++] = ',';
|
||||
}
|
||||
}
|
||||
|
||||
return dump;
|
||||
if (pos+1 < len) {
|
||||
dump[pos++] = '}';
|
||||
dump[pos] = '\0'; /* zero terminated string */
|
||||
}
|
||||
|
||||
return (pos < len) ? dump : NULL; /* buffer full? */
|
||||
}
|
||||
|
||||
void buffer_free(buffer_t *buf) {
|
||||
|
|
|
@ -120,9 +120,24 @@ void * reading_thread(void *arg) {
|
|||
|
||||
/* debugging */
|
||||
if (options.verbosity >= log_debug) {
|
||||
char dump[1024];
|
||||
buffer_dump(buf, dump, 1024);
|
||||
print(log_debug, "Buffer dump: %s (size=%i, keep=%i)", ch, dump, buf->size, buf->keep);
|
||||
size_t dump_len = 24;
|
||||
char *dump = malloc(dump_len);
|
||||
|
||||
if (dump == NULL) {
|
||||
print(log_error, "cannot allocate buffer", ch);
|
||||
}
|
||||
|
||||
while (dump == NULL || buffer_dump(buf, dump, dump_len) == NULL) {
|
||||
dump_len *= 1.5;
|
||||
print(log_debug, "New dump_len: %i", ch ,dump_len);
|
||||
|
||||
free(dump);
|
||||
dump = malloc(dump_len);
|
||||
}
|
||||
|
||||
print(log_debug, "Buffer dump (size=%i keep=%i): %s", ch, buf->size, buf->keep, dump);
|
||||
|
||||
free(dump);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -94,48 +94,48 @@ const char *long_options_descs[] = {
|
|||
* @param id could be NULL for general messages
|
||||
* @todo integrate into syslog
|
||||
*/
|
||||
void print(int level, const char *format, void *id, ... ) {
|
||||
va_list args;
|
||||
|
||||
void print(log_level_t level, const char *format, void *id, ... ) {
|
||||
if (level > options.verbosity) {
|
||||
return; /* skip message if its under the verbosity level */
|
||||
}
|
||||
|
||||
struct timeval now;
|
||||
struct tm * timeinfo;
|
||||
char buffer[1024];
|
||||
char *pos = buffer;
|
||||
char prefix[24];
|
||||
size_t pos = 0;
|
||||
|
||||
gettimeofday(&now, NULL);
|
||||
timeinfo = localtime(&now.tv_sec);
|
||||
|
||||
/* print timestamp to buffer */
|
||||
pos += sprintf(pos, "[");
|
||||
pos += strftime(pos, 16, "%b %d %H:%M:%S", timeinfo);
|
||||
/* format timestamp */
|
||||
pos += strftime(prefix+pos, 18, "[%b %d %H:%M:%S]", timeinfo);
|
||||
|
||||
/* print logging 'section' */
|
||||
pos += (id != NULL) ? sprintf(pos, "][%s]", (char *) id) : sprintf(pos, "]");
|
||||
|
||||
/* fill with whitespaces */
|
||||
while(pos - buffer < 24) {
|
||||
pos += sprintf(pos, " ");
|
||||
/* format section */
|
||||
if (id) {
|
||||
snprintf(prefix+pos, 8, "[%s]", (char *) id);
|
||||
}
|
||||
|
||||
/* print formatstring */
|
||||
va_list args;
|
||||
va_start(args, id);
|
||||
pos += vsprintf(pos, format, args);
|
||||
/* print to stdout/stderr */
|
||||
if (getppid() != 1) { /* running as fork in background? */
|
||||
FILE *stream = (level > 0) ? stdout : stderr;
|
||||
|
||||
fprintf(stream, "%-24s", prefix);
|
||||
vfprintf(stream, format, args);
|
||||
fprintf(stream, "\n");
|
||||
}
|
||||
va_end(args);
|
||||
|
||||
/* print to stdout/stderr */
|
||||
if (getppid() != 1) {
|
||||
fprintf((level > 0) ? stdout : stderr, "%s\n", buffer);
|
||||
}
|
||||
|
||||
va_start(args, id);
|
||||
/* append to logfile */
|
||||
if (options.logfd) {
|
||||
fprintf(options.logfd, "%s\n", buffer);
|
||||
fflush(options.logfd);
|
||||
fprintf(options.logfd, "%-24s", prefix);
|
||||
vfprintf(options.logfd, format, args);
|
||||
fprintf(options.logfd, "\n");
|
||||
fflush(options.logfd);
|
||||
}
|
||||
va_end(args);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Reference in a new issue