-
Notifications
You must be signed in to change notification settings - Fork 171
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Purpose Explain the purpose of the PR here, including references to any existing Notion tasks. - add logging macros to make onboarding challenge more consistent with our actual code base # New Changes - Explain new changes # Testing - Explain tests that you ran to verify code functionality. - built successfully when I defined the address macro - forced an error and ran integration test and it was correctly printed # Outstanding Changes - If there are non-critical changes (i.e. additional features) that can be made to this feature in the future, indicate them here. - Once merged, the onboarding challenge description on notion should mention these macros
- Loading branch information
Showing
8 changed files
with
160 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include "logging.h" | ||
#include "errors.h" | ||
#include "console.h" | ||
|
||
#include <stdarg.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
#define MAX_MSG_SIZE 128U | ||
#define MAX_FNAME_LINENUM_SIZE 128U | ||
// Extra 10 for the small extra pieces in "%s - %s\r\n" | ||
#define MAX_LOG_SIZE (MAX_MSG_SIZE + MAX_FNAME_LINENUM_SIZE + 10U) | ||
|
||
#define UART_MUTEX_BLOCK_TIME portMAX_DELAY | ||
|
||
static const char *LEVEL_STRINGS[] = {"TRACE", "DEBUG", "INFO", "WARN", "ERROR", "FATAL"}; | ||
|
||
static log_level_t logLevel; | ||
|
||
void initLogger(void) { | ||
logLevel = LOG_DEFAULT_LEVEL; | ||
} | ||
|
||
void logSetLevel(log_level_t newLogLevel) { logLevel = newLogLevel; } | ||
|
||
error_code_t logLog(log_level_t msgLevel, const char *file, uint32_t line, const char *s, ...) { | ||
if (msgLevel < logLevel) return ERR_CODE_LOG_MSG_SILENCED; | ||
|
||
if (file == NULL || s == NULL) return ERR_CODE_INVALID_ARG; | ||
|
||
int ret = 0; | ||
|
||
// Message | ||
char msgbuf[MAX_MSG_SIZE] = {0}; | ||
va_list args; | ||
va_start(args, s); | ||
ret = vsnprintf(msgbuf, MAX_MSG_SIZE, s, args); | ||
va_end(args); | ||
if (ret < 0) return ERR_CODE_INVALID_ARG; | ||
if ((uint32_t)ret >= MAX_MSG_SIZE) return ERR_CODE_BUFF_TOO_SMALL; | ||
|
||
// File & line number | ||
char infobuf[MAX_FNAME_LINENUM_SIZE] = {0}; | ||
ret = snprintf(infobuf, MAX_FNAME_LINENUM_SIZE, "%-5s -> %s:%u", LEVEL_STRINGS[msgLevel], file, line); | ||
if (ret < 0) return ERR_CODE_INVALID_ARG; | ||
if ((uint32_t)ret >= MAX_FNAME_LINENUM_SIZE) return ERR_CODE_BUFF_TOO_SMALL; | ||
|
||
// Prepare entire output | ||
char buf[MAX_LOG_SIZE] = {0}; | ||
ret = snprintf(buf, MAX_LOG_SIZE, "%s - %s\r\n", infobuf, msgbuf); | ||
if (ret < 0) return ERR_CODE_INVALID_ARG; | ||
if ((uint32_t)ret >= MAX_LOG_SIZE) return ERR_CODE_BUFF_TOO_SMALL; | ||
|
||
printConsole((unsigned char *)buf); | ||
|
||
return ERR_CODE_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#pragma once | ||
|
||
#include "errors.h" | ||
|
||
#include <stdint.h> | ||
#include <stdarg.h> | ||
|
||
/** | ||
* @enum log_level_t | ||
* @brief Log levels enum. | ||
* | ||
* Enum containing all log levels. | ||
*/ | ||
typedef enum { LOG_TRACE, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL, LOG_OFF } log_level_t; | ||
|
||
#ifndef LOG_DEFAULT_LEVEL | ||
#define LOG_DEFAULT_LEVEL LOG_TRACE | ||
#endif | ||
|
||
#define LOG_TRACE(...) logLog(LOG_TRACE, __FILE__, __LINE__, __VA_ARGS__) | ||
#define LOG_DEBUG(...) logLog(LOG_DEBUG, __FILE__, __LINE__, __VA_ARGS__) | ||
#define LOG_INFO(...) logLog(LOG_INFO, __FILE__, __LINE__, __VA_ARGS__) | ||
#define LOG_WARN(...) logLog(LOG_WARN, __FILE__, __LINE__, __VA_ARGS__) | ||
#define LOG_ERROR(...) logLog(LOG_ERROR, __FILE__, __LINE__, __VA_ARGS__) | ||
#define LOG_FATAL(...) logLog(LOG_FATAL, __FILE__, __LINE__, __VA_ARGS__) | ||
|
||
#define LOG_ERROR_CODE(errCode) LOG_ERROR("Error code: %lu", (uint32_t)errCode) | ||
|
||
#define RETURN_IF_ERROR_CODE(_ret) \ | ||
do { \ | ||
errCode = _ret; \ | ||
if (errCode != ERR_CODE_SUCCESS) { \ | ||
LOG_ERROR_CODE(errCode); \ | ||
return errCode; \ | ||
} \ | ||
} while (0) | ||
|
||
#define LOG_IF_ERROR_CODE(_ret) \ | ||
do { \ | ||
errCode = _ret; \ | ||
if (errCode != ERR_CODE_SUCCESS) { \ | ||
LOG_ERROR_CODE(errCode); \ | ||
} \ | ||
} while (0) | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Initialize the logger | ||
*/ | ||
void initLogger(void); | ||
|
||
/** | ||
* @brief Set the logging level | ||
* | ||
* @param newLogLevel The new logging level | ||
*/ | ||
void logSetLevel(log_level_t newLogLevel); | ||
|
||
/** | ||
* @brief Log a message | ||
* | ||
* @param msgLevel Level of the message | ||
* @param file File of message | ||
* @param line Line of message | ||
* @param s Message to log | ||
* @param ... Additional arguments for the message | ||
* @return error_code_t | ||
* | ||
*/ | ||
error_code_t logLog(log_level_t msgLevel, const char *file, uint32_t line, const char *s, ...); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#include "logging.h" | ||
#include "errors.h" | ||
|
||
#include <stdarg.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
|
||
void logSetLevel(log_level_t newLogLevel) { /* do nothing */ } | ||
|
||
error_code_t logLog(log_level_t msgLevel, const char *file, uint32_t line, const char *s, ...) { | ||
return ERR_CODE_SUCCESS; | ||
} |