LoggerToSerialPort/Src/LoggerToSerialPort.c

135 lines
3.9 KiB
C

//
// Created by CFIF on 13.11.22.
//
#include <stdbool.h>
#include "LoggerToSerialPort.h"
#include "SystemDelayInterface.h"
#include "AsciiStringAssmeblingUtils.h"
#include "Rtc.h"
#include <time.h>
#include CMSIS_device_header
static void LoggerToSerialPort_TimeToString(time_t timestamp, char *strbuf) {
struct tm timestampTM;
localtime_r(&timestamp, &timestampTM);
asctime_r(&timestampTM, strbuf);
}
static void LoggerToSerialPort_PrintLegend(
tLoggerToSerialPort *env,
const char *authorStatic,
const uint8_t authorLen,
eLoggerLevel loglevel
) {
char str[128];
size_t strLen = 0;
time_t timestamp;
char timeString[24];
if (env->flags & (SERIAL_LOGGER_SHOW_DATE | SERIAL_LOGGER_SHOW_TIME)) {
if (env->rtc) {
RtcGet(env->rtc, &timestamp);
timestamp += env->greenwichOffset * 3600;
} else {
uint32_t msFromDeviceStart = SystemGetMs() / 1000;
timestamp = 1672531200 + msFromDeviceStart;
}
LoggerToSerialPort_TimeToString(timestamp, timeString);
if (env->flags & SERIAL_LOGGER_SHOW_DATE) {
vAsciiStringAdd(str, &strLen, &timeString[20], 4);
vAsciiStringAdd(str, &strLen, &timeString[3], 7);
vAsciiStringAdd(str, &strLen, " ", 1);
}
if (env->flags & SERIAL_LOGGER_SHOW_TIME) {
vAsciiStringAdd(str, &strLen, &timeString[11], 8);
vAsciiStringAdd(str, &strLen, " ", 1);
}
}
if (env->flags & SERIAL_LOGGER_SHOW_AUTHOR) {
//выводим автора сообщения
vAsciiStringAdd(str, &strLen, authorStatic, authorLen);
}
if (env->flags & SERIAL_LOGGER_SHOW_LOG_LEVEL) {
tStringStatic levelName = LOGGER_LEVEL_NAMES[loglevel];
//выводим уровень сообщения
vAsciiStringAddChar(str, &strLen, '[');
vAsciiStringAdd(str, &strLen, levelName.data, levelName.length);
vAsciiStringAddChar(str, &strLen, ']');
}
//разделитель
if (env->flags)
vAsciiStringAdd(str, &strLen, ": ", 2);
SerialPortTransmit(env->serialPortIo, (uint8_t *) str, strLen, env->timeout);
env->open = true;
}
static void LoggerToSerialPort_Logging(
tLoggerToSerialPort *env,
const char *authorStatic,
const uint8_t authorLen,
eLoggerLevel loglevel,
char *msg,
uint16_t msgLen,
bool complete
) {
uint32_t adr = *(uint32_t *)(env->serialPortIo->env);
uint32_t timeoutLocal = env->timeout;
// Если это UART (виртуальный)
// if (adr < PERIPH_BASE) {
// env->timeout = 0;
// }
if (!env->open) {
LoggerToSerialPort_PrintLegend(env, authorStatic, authorLen, loglevel);
}
//выводим сообщение
SerialPortTransmit(env->serialPortIo, (uint8_t *) msg, msgLen, env->timeout);
if (complete) {
//переводим строку
SerialPortTransmit(env->serialPortIo, (uint8_t *) "\r\n", 2, env->timeout);
env->open = false;
} else {
env->open = true;
}
env->timeout = timeoutLocal;
}
void LoggerToSerialPort_Init(
tLoggerToSerialPort *env,
int32_t greenwichOffset,
tSerialPortIO *serialPortIo,
tRtcIO *rtc,
uint16_t flags
) {
env->serialPortIo = serialPortIo;
env->open = false;
env->timeout = 200;// не ждем на каждой операции так как если порт исправен то все должно отрабатывать моментально
env->logger.env = env;
env->greenwichOffset = greenwichOffset;
env->logger.logging = (LoggerGenericMethod) LoggerToSerialPort_Logging;
env->rtc = rtc;
env->flags = flags;
// env->showDate = showDate;
// env->showTime = showTime;
// env->showLoglevel = showLoglevel;
// env->showAuthor = showAuthor;
}