os-core/kernel/leos/log.c

83 lines
1.3 KiB
C
Raw Normal View History

2019-12-01 22:57:02 +01: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
*
* @see https://lowenware.com/
*/
2020-01-17 10:13:01 +01:00
#include <drivers/uart/uart.h>
2019-12-01 22:57:02 +01:00
#include "log.h"
void
2020-01-15 10:56:04 +01:00
Log_init(void)
2019-12-01 22:57:02 +01:00
{
2020-01-15 10:56:04 +01:00
UART_init();
2019-12-01 22:57:02 +01:00
}
2020-01-15 10:56:04 +01:00
int
Log_putS(const char *string)
2019-12-01 22:57:02 +01:00
{
2020-01-15 10:56:04 +01:00
char c;
const char *p = string;
2019-12-01 22:57:02 +01:00
while((c = *p) != 0) {
2020-01-17 10:13:01 +01:00
if (UART_put(c))
2019-12-01 22:57:02 +01:00
return -1;
p++;
}
2020-01-15 10:56:04 +01:00
return (int)(p - string);
2019-12-01 22:57:02 +01:00
}
2020-01-15 10:56:04 +01:00
int
Log_putI(int64_t value, uint8_t base)
2019-12-01 22:57:02 +01:00
{
2020-01-15 10:56:04 +01:00
int result;
2019-12-01 22:57:02 +01:00
if (value < 0) {
2020-01-17 10:13:01 +01:00
if (UART_put('-'))
2019-12-01 22:57:02 +01:00
return -1;
value = -value;
result = 1;
} else {
result = 0;
}
2020-01-17 10:13:01 +01:00
result += Log_putU(value, base);
2019-12-01 22:57:02 +01:00
return (result) ? result : -1;
}
2020-01-15 10:56:04 +01:00
int
Log_putU(uint64_t value, uint8_t base)
2019-12-01 22:57:02 +01:00
{
2020-01-15 10:56:04 +01:00
int i = 0, result;
char buffer[64];
2019-12-01 22:57:02 +01:00
if (!value) {
2020-01-17 10:13:01 +01:00
return (UART_put('0')) ? -1 : 1;
2019-12-01 22:57:02 +01:00
}
do {
2020-01-15 10:56:04 +01:00
uint64_t r;
2019-12-01 22:57:02 +01:00
if (!(i < (sizeof(buffer) / sizeof(buffer[0]))))
return -1;
2020-01-15 10:56:04 +01:00
r = value % base;
2019-12-01 22:57:02 +01:00
buffer[i++] = (r < 0x0A) ? '0' + r : 'A' + r - 0x0A;
value /= base;
} while (value);
result = i;
while (i--) {
2020-01-17 10:13:01 +01:00
if (UART_put(buffer[i]))
2019-12-01 22:57:02 +01:00
return -1;
}
return result;
}