165 lines
5.1 KiB
C
165 lines
5.1 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 "CmsisRtosThreadUtils.h"
|
|
#include CMSIS_device_header
|
|
|
|
|
|
static void LoggerToSerialPort_TimeToString(time_t timestamp, char *strbuf) {
|
|
struct tm timestampTM;
|
|
localtime_r(×tamp, ×tampTM);
|
|
asctime_r(×tampTM, strbuf);
|
|
}
|
|
|
|
static void LoggerToSerialPort_PrintLegend(
|
|
tLoggerToSerialPort *env,
|
|
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, ×tamp);
|
|
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_VIRT, (uint8_t *) str, strLen, env->timeout);
|
|
|
|
env->open = true;
|
|
}
|
|
|
|
static void LoggerToSerialPort_Logging(
|
|
tLoggerToSerialPort *env,
|
|
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_VIRT, (uint8_t *) msg, msgLen, env->timeout);
|
|
|
|
if (complete) {
|
|
//переводим строку
|
|
SerialPortTransmit(env->serialPortIo_VIRT, (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_VIRT,
|
|
tSerialPortIO *serialPortIo_PHYSIC,
|
|
tRtcIO *rtc,
|
|
uint16_t flags,
|
|
uint32_t timeoutTransmittedLog
|
|
) {
|
|
env->serialPortIo_VIRT = serialPortIo_VIRT;
|
|
env->serialPortIo_PHYSIC = serialPortIo_PHYSIC;
|
|
env->open = false;
|
|
env->timeout = 0;// не ждем на каждой операции так как если порт исправен то все должно отрабатывать моментально
|
|
env->logger.env = env;
|
|
env->greenwichOffset = greenwichOffset;
|
|
env->logger.logging = (LoggerGenericMethod) LoggerToSerialPort_Logging;
|
|
env->rtc = rtc;
|
|
env->flags = flags;
|
|
env->timeoutTransmittedLog = timeoutTransmittedLog;
|
|
// env->showDate = showDate;
|
|
// env->showTime = showTime;
|
|
// env->showLoglevel = showLoglevel;
|
|
// env->showAuthor = showAuthor;
|
|
|
|
InitThreadAtrStatic(&env->thread.attr, "LogTransmitter", env->thread.controlBlock, env->thread.stack,
|
|
osPriorityNormal);
|
|
|
|
LogTransmitter_StartThread(env);
|
|
}
|
|
|
|
extern uint8_t buf_USART2_TRANSMITTED[256];
|
|
|
|
static _Noreturn void LogTransmitter_Thread(tLoggerToSerialPort *env) {
|
|
for (;;) {
|
|
uint16_t len = env->serialPortIo_VIRT->receive(env->serialPortIo_VIRT->env, buf_USART2_TRANSMITTED,
|
|
sizeof(buf_USART2_TRANSMITTED), env->timeoutTransmittedLog);
|
|
|
|
if (len > 0) {
|
|
len = env->serialPortIo_PHYSIC->transmit(env->serialPortIo_PHYSIC->env, buf_USART2_TRANSMITTED,
|
|
len, env->timeoutTransmittedLog);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
void LogTransmitter_StartThread(tLoggerToSerialPort *env) {
|
|
if (!env->thread.id) {
|
|
env->thread.id = osThreadNew((osThreadFunc_t) (LogTransmitter_Thread), (void *) (env), &env->thread.attr);
|
|
}
|
|
} |