2016-09-06 11:16:38 +00:00
|
|
|
/**
|
2016-09-11 20:20:15 +00:00
|
|
|
* Author......: See docs/credits.txt
|
2016-09-06 11:16:38 +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-06 11:16:38 +00:00
|
|
|
#include "common.h"
|
2016-09-06 17:44:27 +00:00
|
|
|
#include "types.h"
|
2016-09-06 11:16:38 +00:00
|
|
|
#include "memory.h"
|
|
|
|
#include "logfile.h"
|
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
static int logfile_generate_id ()
|
|
|
|
{
|
|
|
|
const int n = rand ();
|
|
|
|
|
|
|
|
time_t t;
|
|
|
|
|
|
|
|
time (&t);
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
return t + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
void logfile_generate_topid (logfile_ctx_t *logfile_ctx)
|
2016-09-06 11:16:38 +00:00
|
|
|
{
|
2016-09-23 19:41:05 +00:00
|
|
|
if (logfile_ctx->enabled == false) return;
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
const int id = logfile_generate_id ();
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
snprintf (logfile_ctx->topid, 1 + 16, "TOP%08x", id);
|
2016-09-06 11:16:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
void logfile_generate_subid (logfile_ctx_t *logfile_ctx)
|
2016-09-06 11:16:38 +00:00
|
|
|
{
|
2016-09-23 19:41:05 +00:00
|
|
|
if (logfile_ctx->enabled == false) return;
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
const int id = logfile_generate_id ();
|
|
|
|
|
|
|
|
snprintf (logfile_ctx->subid, 1 + 16, "SUB%08x", id);
|
2016-09-06 11:16:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
void logfile_append (const logfile_ctx_t *logfile_ctx, const char *fmt, ...)
|
2016-09-06 11:16:38 +00:00
|
|
|
{
|
2016-09-23 19:41:05 +00:00
|
|
|
if (logfile_ctx->enabled == false) return;
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
FILE *fp = fopen (logfile_ctx->logfile, "ab");
|
2016-09-06 11:16:38 +00:00
|
|
|
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start (ap, fmt);
|
|
|
|
|
|
|
|
vfprintf (fp, fmt, ap);
|
|
|
|
|
|
|
|
va_end (ap);
|
|
|
|
|
|
|
|
fputc ('\n', fp);
|
|
|
|
|
|
|
|
fflush (fp);
|
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
fclose (fp);
|
2016-09-06 11:16:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
void logfile_init (logfile_ctx_t *logfile_ctx, const user_options_t *user_options, const folder_config_t *folder_config)
|
2016-09-06 11:16:38 +00:00
|
|
|
{
|
2016-09-23 19:41:05 +00:00
|
|
|
if (user_options->logfile_disable == true) return;
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
logfile_ctx->logfile = (char *) mymalloc (HCBUFSIZ_TINY);
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
snprintf (logfile_ctx->logfile, HCBUFSIZ_TINY - 1, "%s/%s.log", folder_config->session_dir, user_options->session);
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
logfile_ctx->subid = (char *) mymalloc (HCBUFSIZ_TINY);
|
|
|
|
logfile_ctx->topid = (char *) mymalloc (HCBUFSIZ_TINY);
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
logfile_ctx->enabled = true;
|
2016-09-06 11:16:38 +00:00
|
|
|
}
|
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
void logfile_destroy (logfile_ctx_t *logfile_ctx)
|
2016-09-06 11:16:38 +00:00
|
|
|
{
|
2016-10-01 22:00:21 +00:00
|
|
|
if (logfile_ctx->enabled == false) return;
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-09-23 19:41:05 +00:00
|
|
|
myfree (logfile_ctx->logfile);
|
|
|
|
myfree (logfile_ctx->topid);
|
|
|
|
myfree (logfile_ctx->subid);
|
2016-09-06 11:16:38 +00:00
|
|
|
|
2016-10-01 22:00:21 +00:00
|
|
|
memset (logfile_ctx, 0, sizeof (logfile_ctx_t));
|
2016-09-06 11:16:38 +00:00
|
|
|
}
|