Oliver Fabel 1 год назад
Родитель
Сommit
f7e95ab3a1

+ 5 - 1
CHANGELOG.md

@@ -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:
 * Support for basic file system operations using the `io` module:
   * Read and write files.
   * 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
 ## [1.4.0] - 2024-09-29
 
 

+ 1 - 1
application.fam

@@ -5,7 +5,7 @@ App(
     entry_point="upython",
     entry_point="upython",
     stack_size=4 * 1024,
     stack_size=4 * 1024,
     fap_category="Tools",
     fap_category="Tools",
-    fap_version="1.4",
+    fap_version="1.5",
     fap_description="Compile and execute MicroPython scripts",
     fap_description="Compile and execute MicroPython scripts",
     fap_icon="icon.png",
     fap_icon="icon.png",
     fap_icon_assets="images",
     fap_icon_assets="images",

+ 4 - 2
docs/pages/features.md

@@ -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:
 The following features are enabled and supported by the interpreter:
 
 
 * Garbage collector is enabled.
 * Garbage collector is enabled.
+* Finaliser calls in the garbage collector (e.g. `__del__`).
 * The `__file__` constant.
 * The `__file__` constant.
 * Import of external files from the SD card.
 * Import of external files from the SD card.
 * Read and write files from and to the SD card.
 * Read and write files from and to the SD card.
 * The `time` module.
 * The `time` module.
 * The `random` module.
 * The `random` module.
+* The `logging` module.
 * The `io` module.
 * The `io` module.
 * The `float` data type.
 * 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.
 * Support for [decorator](https://docs.python.org/3/glossary.html#term-decorator) functions.
 * The `setattr` function.
 * The `setattr` function.
 * The `filter` function.
 * The `filter` function.
@@ -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:
 The following features are disabled and _not_ supported by the interpreter:
 
 
-* Finaliser calls in the garbage collector (e.g. `__del__`).
 * The `__doc__` constants.
 * The `__doc__` constants.
 * Source code line numbers in exceptions.
 * Source code line numbers in exceptions.
 * The `cmath` module.
 * The `cmath` module.
@@ -41,7 +44,6 @@ The following features are disabled and _not_ supported by the interpreter:
 * Support for `bytes.hex` and `bytes.fromhex`.
 * Support for `bytes.hex` and `bytes.fromhex`.
 * Support for unicode characters.
 * Support for unicode characters.
 * The string functions `.center`, `.count`, `.partition`, `.rpartition` and `.splitlines`.
 * The string functions `.center`, `.count`, `.partition`, `.rpartition` and `.splitlines`.
-* The `%` string formatting operator (use `.format` instead).
 * The `bytearray` data type.
 * The `bytearray` data type.
 * The `memoryview` data type.
 * The `memoryview` data type.
 * The `slice` object.
 * The `slice` object.

+ 15 - 0
examples/logger.py

@@ -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)

+ 1 - 1
lib/micropython

@@ -1 +1 @@
-Subproject commit 4aff18dad530e1afbe235fc28b981c3e6de296e4
+Subproject commit b6ee63fe1f5bbb7f008061e67583da1047bce646

+ 1 - 0
lib/micropython-port/mp_flipper_context.h

@@ -46,4 +46,5 @@ typedef struct {
     mp_flipper_gpio_pin_t* gpio_pins;
     mp_flipper_gpio_pin_t* gpio_pins;
     mp_flipper_infrared_rx_t* infrared_rx;
     mp_flipper_infrared_rx_t* infrared_rx;
     mp_flipper_infrared_tx_t* infrared_tx;
     mp_flipper_infrared_tx_t* infrared_tx;
+    uint8_t log_level;
 } mp_flipper_context_t;
 } mp_flipper_context_t;

+ 47 - 0
lib/micropython-port/mp_flipper_logging.c

@@ -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 - 0
lib/micropython-port/mp_flipper_runtime.c

@@ -3,6 +3,7 @@
 
 
 #include <mp_flipper_runtime.h>
 #include <mp_flipper_runtime.h>
 #include <mp_flipper_modflipperzero.h>
 #include <mp_flipper_modflipperzero.h>
+#include <mp_flipper_logging.h>
 
 
 #include "mp_flipper_context.h"
 #include "mp_flipper_context.h"
 
 
@@ -112,6 +113,9 @@ void* mp_flipper_context_alloc() {
     ctx->infrared_tx->size = 0;
     ctx->infrared_tx->size = 0;
     ctx->infrared_tx->level = false;
     ctx->infrared_tx->level = false;
 
 
+    // log level
+    ctx->log_level = MP_FLIPPER_LOG_LEVEL_INFO;
+
     return ctx;
     return ctx;
 }
 }