Skip to content

Commit

Permalink
add logging module
Browse files Browse the repository at this point in the history
  • Loading branch information
ofabel committed Oct 4, 2024
1 parent 89f2026 commit f7e95ab
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 5 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

* Support for basic file system operations using the `io` module:
* Read and write files.
* Open in text or binary mode.
* Open in text or binary mode.
* Simple `logging` module:
* Log levels according to the Flipper Zero API: trace, debug, info, warn, error.
* Only the root logger is supported, so no `getLogger` function.
* Logs directly to the log output, so no output in the REPL.

## [1.4.0] - 2024-09-29

Expand Down
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ App(
entry_point="upython",
stack_size=4 * 1024,
fap_category="Tools",
fap_version="1.4",
fap_version="1.5",
fap_description="Compile and execute MicroPython scripts",
fap_icon="icon.png",
fap_icon_assets="images",
Expand Down
6 changes: 4 additions & 2 deletions docs/pages/features.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,17 @@ So here is a detailed list of all supported and unsupported Python language feat
The following features are enabled and supported by the interpreter:

* Garbage collector is enabled.
* Finaliser calls in the garbage collector (e.g. `__del__`).
* The `__file__` constant.
* Import of external files from the SD card.
* Read and write files from and to the SD card.
* The `time` module.
* The `random` module.
* The `logging` module.
* The `io` module.
* The `float` data type.
* The `%` string formatting operator.
* The `.format` string formatting function.
* Support for [decorator](https://docs.python.org/3/glossary.html#term-decorator) functions.
* The `setattr` function.
* The `filter` function.
Expand All @@ -27,7 +31,6 @@ The following features are enabled and supported by the interpreter:

The following features are disabled and _not_ supported by the interpreter:

* Finaliser calls in the garbage collector (e.g. `__del__`).
* The `__doc__` constants.
* Source code line numbers in exceptions.
* The `cmath` module.
Expand All @@ -41,7 +44,6 @@ The following features are disabled and _not_ supported by the interpreter:
* Support for `bytes.hex` and `bytes.fromhex`.
* Support for unicode characters.
* The string functions `.center`, `.count`, `.partition`, `.rpartition` and `.splitlines`.
* The `%` string formatting operator (use `.format` instead).
* The `bytearray` data type.
* The `memoryview` data type.
* The `slice` object.
Expand Down
15 changes: 15 additions & 0 deletions examples/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import logging

logging.setLevel(logging.TRACE)

logging.trace('trace')
logging.debug('debug %d %s',123,'message')
logging.info('info %d %s',123,'message')
logging.warn('warn %d %s',123,'message')
logging.error('error %d %s',123,'message')

logging.log(logging.TRACE, "level: %d", logging.TRACE)
logging.log(logging.DEBUG, "level: %d", logging.DEBUG)
logging.log(logging.INFO, "level: %d", logging.INFO)
logging.log(logging.WARN, "level: %d", logging.WARN)
logging.log(logging.ERROR, "level: %d", logging.ERROR)
2 changes: 1 addition & 1 deletion lib/micropython
1 change: 1 addition & 0 deletions lib/micropython-port/mp_flipper_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ typedef struct {
mp_flipper_gpio_pin_t* gpio_pins;
mp_flipper_infrared_rx_t* infrared_rx;
mp_flipper_infrared_tx_t* infrared_tx;
uint8_t log_level;
} mp_flipper_context_t;
47 changes: 47 additions & 0 deletions lib/micropython-port/mp_flipper_logging.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include <furi.h>

#include <mp_flipper_logging.h>
#include <mp_flipper_runtime.h>

#include "mp_flipper_context.h"

static inline FuriLogLevel decode_log_level(uint8_t level) {
switch(level) {
case MP_FLIPPER_LOG_LEVEL_TRACE:
return FuriLogLevelTrace;
break;
case MP_FLIPPER_LOG_LEVEL_DEBUG:
return FuriLogLevelDebug;
break;
case MP_FLIPPER_LOG_LEVEL_INFO:
return FuriLogLevelInfo;
break;
case MP_FLIPPER_LOG_LEVEL_WARN:
return FuriLogLevelWarn;
break;
case MP_FLIPPER_LOG_LEVEL_ERROR:
return FuriLogLevelError;
break;
default:
return FuriLogLevelDefault;
break;
}
}

uint8_t mp_flipper_log_get_level() {
mp_flipper_context_t* ctx = mp_flipper_context;

return ctx->log_level;
}

void mp_flipper_log_set_level(uint8_t level) {
mp_flipper_context_t* ctx = mp_flipper_context;

ctx->log_level = level;
}

void mp_flipper_log(uint8_t raw_level, const char* message) {
FuriLogLevel level = decode_log_level(raw_level);

furi_log_print_format(level, "uPython", message);
}
4 changes: 4 additions & 0 deletions lib/micropython-port/mp_flipper_runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <mp_flipper_runtime.h>
#include <mp_flipper_modflipperzero.h>
#include <mp_flipper_logging.h>

#include "mp_flipper_context.h"

Expand Down Expand Up @@ -112,6 +113,9 @@ void* mp_flipper_context_alloc() {
ctx->infrared_tx->size = 0;
ctx->infrared_tx->level = false;

// log level
ctx->log_level = MP_FLIPPER_LOG_LEVEL_INFO;

return ctx;
}

Expand Down

0 comments on commit f7e95ab

Please sign in to comment.