2019-04-05 16:20:30 +02:00
|
|
|
/******************************************************************************
|
|
|
|
*
|
|
|
|
* Copyright (c) 2017-2019 by Löwenware Ltd
|
|
|
|
* Please, refer LICENSE file for legal information
|
|
|
|
*
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file log.c
|
|
|
|
* @author Ilja Kartašov <ik@lowenware.com>
|
|
|
|
* @brief cStuff log module source file
|
|
|
|
*
|
|
|
|
* @see https://lowenware.com/
|
|
|
|
*/
|
|
|
|
#include <time.h>
|
|
|
|
#include "log.h"
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
cstuff_log_init(cstuff_log_t self, uint8_t level)
|
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
self->target = stderr;
|
|
|
|
self->level = level;
|
2019-04-05 16:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
cstuff_log_release(cstuff_log_t self)
|
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
if (self->target != stderr)
|
|
|
|
fclose(self->target);
|
2019-04-05 16:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
cstuff_retcode_t
|
|
|
|
cstuff_log_set_file(cstuff_log_t self, const char * filename)
|
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
FILE * fp;
|
2019-04-05 16:20:30 +02:00
|
|
|
|
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
if ( !(fp = fopen(filename, "a")) )
|
|
|
|
return CSTUFF_SYSCALL_ERROR;
|
2019-04-05 16:20:30 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
self->target = fp;
|
|
|
|
return CSTUFF_SUCCESS;
|
2019-04-05 16:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
cstuff_log_printf(cstuff_log_t self, uint8_t level, const char * format, ...)
|
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
int result;
|
|
|
|
va_list vl;
|
2019-04-05 16:20:30 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
va_start(vl, format);
|
|
|
|
result = cstuff_log_vprintf(self, level, format, vl);
|
|
|
|
va_end(vl);
|
2019-04-05 16:20:30 +02:00
|
|
|
|
2019-04-16 14:26:37 +02:00
|
|
|
return result;
|
2019-04-05 16:20:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
cstuff_log_vprintf(cstuff_log_t self,
|
2019-04-16 14:26:37 +02:00
|
|
|
uint8_t level,
|
|
|
|
const char * format,
|
|
|
|
va_list vl)
|
2019-04-05 16:20:30 +02:00
|
|
|
{
|
2019-04-16 14:26:37 +02:00
|
|
|
int result;
|
|
|
|
|
|
|
|
if (self->level & level)
|
|
|
|
{
|
|
|
|
FILE * f = self->target;
|
|
|
|
char * label;
|
|
|
|
time_t rawtime;
|
|
|
|
char strtime[32+1];
|
|
|
|
struct tm timeinfo;
|
|
|
|
|
|
|
|
time (&rawtime);
|
|
|
|
localtime_r (&rawtime, &timeinfo);
|
|
|
|
strftime (strtime, 32, "%F %T", &timeinfo);
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
{
|
|
|
|
case CSTUFF_LOG_ERROR : label = "!!"; break;
|
|
|
|
case CSTUFF_LOG_ALERT : label = "~!"; break;
|
|
|
|
case CSTUFF_LOG_DEBUG : label = "**"; break;
|
|
|
|
default : label = "--";
|
|
|
|
}
|
|
|
|
|
|
|
|
result = fprintf(f, "%s %s ", strtime, label);
|
|
|
|
result += vfprintf(f, format, vl);
|
|
|
|
fputc('\n', f);
|
|
|
|
result++;
|
|
|
|
fflush(f);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
result = 0;
|
|
|
|
|
|
|
|
return result;
|
2019-04-05 16:20:30 +02:00
|
|
|
}
|