Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modules & Logger #79

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft

Modules & Logger #79

wants to merge 6 commits into from

Conversation

nopeslide
Copy link
Contributor

@nopeslide nopeslide commented Nov 29, 2020

Related Issues & PRs

none

Description

  • module abstraction for static allocation
  • logging of module state
  • port other things to module structure

Context

As of our design decision to go completely static in term of memory allocation we need to have a easy way to allocate, initialize and track our used objects.
I propose an abstraction to do this, called "module".
Each object that needs static allocation is a module and therefore contains a module struct which defines

  • the name of the object
  • the parent of the object
  • the type of the object (a string set to the object type name)
  • an initializing function for the object
  • a state dumping function of the object
  • a list of further submodules

Each module should also come with a macro that expands to the default values of the object.
Initializing any object automatically initializes any submodule and calls the objects initializer afterwards.

usage demo

#include "module.h"
#include "logger_json.h"
#include "logger_yaml.h"

#include <stdio.h>

/*************** mysubmodule.h ****************/

typedef struct mysubmodule
{
    Lifesensor_module module;
    int data;
} Mysubmodule;

static
void mysubmodule_dump(Lifesensor_module *module, Lifesensor_logger *logger, Lifesensor_logger_scope *scope)
{
    Mysubmodule *inst = (void *)module;
    logger->log_int(scope, inst->data, "data");
}

static
void mysubmodule_init(Lifesensor_module *module)
{
    Mysubmodule *inst = (void *)module;
    inst->data = -123;
}

#define INIT_MYSUBMODULE(NAME)                                                                     \
    {                                                                                              \
        .module = INIT_LIFESENSOR_MODULE(Mysubmodule, NAME, &mysubmodule_init, &mysubmodule_dump), \
        .data = 0,                                                                                 \
    }

/*************** mymodule.h ****************/

typedef struct mymodule
{
    Lifesensor_module module;
    int data;
    Mysubmodule submodule1;
    Mysubmodule submodule2;
} Mymodule;

#define INIT_MYMODULE(NAME)                                                                   \
    {                                                                                         \
        .module = INIT_LIFESENSOR_MODULE(Mymodule, NAME, NULL, NULL, submodule1, submodule2), \
        .data = 0,                                                                            \
        .submodule1 = INIT_MYSUBMODULE("submodule1"),                                         \
        .submodule2 = INIT_MYSUBMODULE("submodule2")                                          \
    }


/*************** main.c ****************/

Mymodule main_module = INIT_MYMODULE("module");

int main()
{
    lifesensor_module_init(&main_module.module);

    Lifesensor_logger logger;
    Lifesensor_logger_scope root;

    printf("\nstart json dump\n");
    lifesensor_logger_json_init(&logger, &root, &printf);
    logger.enter_map(NULL, &root, NULL);
    lifesensor_module_dump(&main_module.module, &logger, &root);
    logger.exit(NULL, &root);
    printf("\nend json dump\n");

    printf("\nstart yaml dump\n");
    lifesensor_logger_yaml_init(&logger, &root, &printf);
    logger.enter_map(NULL, &root, NULL);
    lifesensor_module_dump(&main_module.module, &logger, &root);
    logger.exit(NULL, &root);
    printf("\nend yaml dump\n");
}

this demo produces a json dump in a single line

{ "address": "0x55dc2080", "parent": "0x0", "type": "Mymodule", "name": "module", "init": "0x0", "dump": "0x0", "submodule_offsets": [ +56, +112 ], "locals": {  }, "submodules": { "submodule1": { "address": "0x55dc20b8", "parent": "0x55dc2080", "type": "Mysubmodule", "name": "submodule1", "init": "0x55dbf18e", "dump": "0x55dbf149", "submodule_offsets": [  ], "locals": { "data": -123 }, "submodules": {  } }, "submodule2": { "address": "0x55dc20f0", "parent": "0x55dc2080", "type": "Mysubmodule", "name": "submodule2", "init": "0x55dbf18e", "dump": "0x55dbf149", "submodule_offsets": [  ], "locals": { "data": -123 }, "submodules": {  } } }

and a yaml dump over multiple lines

address: 0x55dc2080
parent: 0x0
type: "Mymodule"
name: "module"
init: 0x0
dump: 0x0
submodule_offsets: 
    - +56
    - +112
locals: {}
submodules: 
    submodule1: 
        address: 0x55dc20b8
        parent: 0x55dc2080
        type: "Mysubmodule"
        name: "submodule1"
        init: 0x55dbf18e
        dump: 0x55dbf149
        submodule_offsets: []
        locals: 
            data: -123
        submodules: {}
    submodule2: 
        address: 0x55dc20f0
        parent: 0x55dc2080
        type: "Mysubmodule"
        name: "submodule2"
        init: 0x55dbf18e
        dump: 0x55dbf149
        submodule_offsets: []
        locals: 
            data: -123
        submodules: {}

Testing

  • module test
  • logger test
  • yaml validation
  • json validation

Documentation

  • module documentation
  • logger documentation

@nopeslide nopeslide changed the title Modules Modules & Logger Nov 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant