2016-09-05 19:47:26 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-05 19:47:26 +00:00
|
|
|
* License.....: MIT
|
|
|
|
*/
|
|
|
|
|
2016-09-07 20:29:57 +00:00
|
|
|
#if defined (__APPLE__)
|
2016-09-06 20:25:54 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
2016-09-05 19:47:26 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "logging.h"
|
|
|
|
|
|
|
|
static int last_len = 0;
|
|
|
|
|
|
|
|
static int log_final (FILE *fp, const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
if (last_len)
|
|
|
|
{
|
|
|
|
fputc ('\r', fp);
|
|
|
|
|
|
|
|
for (int i = 0; i < last_len; i++)
|
|
|
|
{
|
|
|
|
fputc (' ', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
fputc ('\r', fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
char s[4096] = { 0 };
|
|
|
|
|
2016-09-30 16:39:31 +00:00
|
|
|
const size_t max_len = sizeof (s);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
2016-09-30 16:39:31 +00:00
|
|
|
const int len = vsnprintf (s, max_len, fmt, ap);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
2016-09-30 16:39:31 +00:00
|
|
|
//if (len > max_len) len = max_len;
|
2016-09-05 19:47:26 +00:00
|
|
|
|
2016-09-30 16:39:31 +00:00
|
|
|
fwrite (s, (size_t) len, 1, fp);
|
2016-09-05 19:47:26 +00:00
|
|
|
|
|
|
|
fflush (fp);
|
|
|
|
|
|
|
|
last_len = len;
|
|
|
|
|
2016-09-30 16:39:31 +00:00
|
|
|
return last_len;
|
2016-09-05 19:47:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int log_out_nn (FILE *fp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
const int len = log_final (fp, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_info_nn (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
const int len = log_final (stdout, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_error_nn (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
const int len = log_final (stderr, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_out (FILE *fp, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
const int len = log_final (fp, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
fputc ('\n', fp);
|
|
|
|
|
|
|
|
last_len = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_info (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
const int len = log_final (stdout, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
fputc ('\n', stdout);
|
|
|
|
|
|
|
|
last_len = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
int log_error (const char *fmt, ...)
|
|
|
|
{
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
const int len = log_final (stderr, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
fputc ('\n', stderr);
|
|
|
|
|
|
|
|
last_len = 0;
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|